|
4 | 4 |
|
5 | 5 | compare with read_files_neo_io.py |
6 | 6 | """ |
| 7 | +########################################################### |
| 8 | +# First we import a RawIO from neo.rawio |
| 9 | +# For this example we will use PlexonRawIO |
7 | 10 |
|
8 | 11 | import urllib |
9 | 12 | from neo.rawio import PlexonRawIO |
10 | 13 |
|
11 | 14 | url_repo = "https://web.gin.g-node.org/NeuralEnsemble/ephy_testing_data/raw/master/" |
12 | 15 |
|
| 16 | +############################################################## |
13 | 17 | # Get Plexon files |
| 18 | +# We will be pulling these files down, but if you have local file |
| 19 | +# then all you need to do is specify the file location on your |
| 20 | +# computer |
| 21 | + |
14 | 22 | distantfile = url_repo + "plexon/File_plexon_3.plx" |
15 | 23 | localfile = "File_plexon_3.plx" |
16 | 24 | urllib.request.urlretrieve(distantfile, localfile) |
17 | 25 |
|
18 | | -# create a reader |
| 26 | +############################################################### |
| 27 | +# Create a reader |
| 28 | +# All it takes to create a reader is giving the filename |
| 29 | +# Then we need to do the slow step of parsing the header with the |
| 30 | +# `parse_header` function. This collects metadata as well as |
| 31 | +# make all future steps much faster for us |
| 32 | + |
19 | 33 | reader = PlexonRawIO(filename="File_plexon_3.plx") |
20 | 34 | reader.parse_header() |
21 | 35 | print(reader) |
| 36 | +# we can view metadata in the header |
22 | 37 | print(reader.header) |
23 | 38 |
|
| 39 | +############################################################### |
24 | 40 | # Read signal chunks |
| 41 | +# This is how we read raw data. We choose indices that we want or |
| 42 | +# we can use None to mean look at all channels. We also need to |
| 43 | +# specify the block of data (block_index) as well as the segment |
| 44 | +# (seg_index). Then we give the index start and stop. Since we |
| 45 | +# often thing in time to go from time to index would just require |
| 46 | +# the sample rate (so index = time / sampling_rate) |
25 | 47 | channel_indexes = None # could be channel_indexes = [0] |
26 | 48 | raw_sigs = reader.get_analogsignal_chunk( |
27 | 49 | block_index=0, seg_index=0, i_start=1024, i_stop=2048, channel_indexes=channel_indexes |
28 | 50 | ) |
| 51 | + |
| 52 | +# raw_sigs are not voltages so to convert to voltages we do the follwing |
29 | 53 | float_sigs = reader.rescale_signal_raw_to_float(raw_sigs, dtype="float64") |
| 54 | +# each rawio gives you access to lots of information about your data |
30 | 55 | sampling_rate = reader.get_signal_sampling_rate() |
31 | 56 | t_start = reader.get_signal_t_start(block_index=0, seg_index=0) |
32 | 57 | units = reader.header["signal_channels"][0]["units"] |
| 58 | +# and we can display all of this information |
33 | 59 | print(raw_sigs.shape, raw_sigs.dtype) |
34 | 60 | print(float_sigs.shape, float_sigs.dtype) |
35 | | -print(sampling_rate, t_start, units) |
| 61 | +print(f"{sampling_rate=}, {t_start=}, {units=}") |
| 62 | + |
| 63 | + |
| 64 | +#################################################################### |
| 65 | +# Some rawio's and file formats all provide information about spikes |
| 66 | +# If an rawio can't read this data it will raise an error, but lucky |
| 67 | +# for us PlexonRawIO does have spikes data!! |
36 | 68 |
|
37 | 69 | # Count units and spikes per unit |
38 | 70 | nb_unit = reader.spike_channels_count() |
39 | 71 | print("nb_unit", nb_unit) |
40 | 72 | for spike_channel_index in range(nb_unit): |
41 | 73 | nb_spike = reader.spike_count(block_index=0, seg_index=0, spike_channel_index=spike_channel_index) |
42 | | - print("spike_channel_index", spike_channel_index, "nb_spike", nb_spike) |
| 74 | + print(f"{spike_channel_index=}\n{nb_spike}\n") |
43 | 75 |
|
44 | | -# Read spike times |
| 76 | +# Read spike times and rescale (just like analogsignal above) |
45 | 77 | spike_timestamps = reader.get_spike_timestamps( |
46 | 78 | block_index=0, seg_index=0, spike_channel_index=0, t_start=0.0, t_stop=10.0 |
47 | 79 | ) |
48 | | -print(spike_timestamps.shape, spike_timestamps.dtype, spike_timestamps[:5]) |
| 80 | +print(f"{spike_timestamps.shape=}\n{spike_timestamps.dtype=}\n{spike_timestamps[:5]=}") |
49 | 81 | spike_times = reader.rescale_spike_timestamp(spike_timestamps, dtype="float64") |
50 | | -print(spike_times.shape, spike_times.dtype, spike_times[:5]) |
| 82 | +print(f"{spike_times.shape=}\n{spike_times.dtype=}\n{spike_times[:5]}") |
| 83 | + |
| 84 | +####################################################################### |
| 85 | +# Some file formats can also give waveform information. We are lucky |
| 86 | +# again. Our file has waveform data!! |
51 | 87 |
|
52 | 88 | # Read spike waveforms |
53 | 89 | raw_waveforms = reader.get_spike_raw_waveforms( |
54 | 90 | block_index=0, seg_index=0, spike_channel_index=0, t_start=0.0, t_stop=10.0 |
55 | 91 | ) |
56 | | -print(raw_waveforms.shape, raw_waveforms.dtype, raw_waveforms[0, 0, :4]) |
| 92 | +print(f"{raw_waveforms.shape=}\n{raw_waveforms.dtype=}\n{raw_waveforms[0, 0, :4]=}") |
57 | 93 | float_waveforms = reader.rescale_waveforms_to_float(raw_waveforms, dtype="float32", spike_channel_index=0) |
58 | | -print(float_waveforms.shape, float_waveforms.dtype, float_waveforms[0, 0, :4]) |
| 94 | +print(f"{float_waveforms.shape=}\n{float_waveforms.dtype=}{float_waveforms[0, 0, :4]=}") |
| 95 | + |
| 96 | +######################################################################### |
| 97 | +# RawIOs can also read event timestamps. But looks like our luck ran out |
| 98 | +# let's grab a new file to see this feature |
59 | 99 |
|
60 | 100 | # Read event timestamps and times (take another file) |
61 | 101 | distantfile = url_repo + "plexon/File_plexon_2.plx" |
|
66 | 106 | reader = PlexonRawIO(filename="File_plexon_2.plx") |
67 | 107 | reader.parse_header() |
68 | 108 | nb_event_channel = reader.event_channels_count() |
69 | | -print("nb_event_channel", nb_event_channel) |
| 109 | +print("nb_event_channel:", nb_event_channel) |
70 | 110 | for chan_index in range(nb_event_channel): |
71 | 111 | nb_event = reader.event_count(block_index=0, seg_index=0, event_channel_index=chan_index) |
72 | 112 | print("chan_index", chan_index, "nb_event", nb_event) |
73 | 113 |
|
74 | 114 | ev_timestamps, ev_durations, ev_labels = reader.get_event_timestamps( |
75 | 115 | block_index=0, seg_index=0, event_channel_index=0, t_start=None, t_stop=None |
76 | 116 | ) |
77 | | -print(ev_timestamps, ev_durations, ev_labels) |
| 117 | +print(f"{ev_timestamps=}\n{ev_durations=}\n{ev_labels=}") |
78 | 118 | ev_times = reader.rescale_event_timestamp(ev_timestamps, dtype="float64") |
79 | | -print(ev_times) |
| 119 | +print(f"{ev_times=}") |
0 commit comments