Skip to content

Commit ea655ae

Browse files
authored
Merge pull request #1377 from alejoe91/fer/master
Support for just .spikes in folder for OpenEphys
2 parents 4ef2801 + 3c388e8 commit ea655ae

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

neo/rawio/openephysrawio.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,31 @@ def _parse_header(self):
171171
self._sig_length[seg_index] = all_sigs_length[0]
172172
self._sig_timestamp0[seg_index] = all_first_timestamps[0]
173173

174-
signal_channels = np.array(signal_channels, dtype=_signal_channel_dtype)
175-
self._sig_sampling_rate = signal_channels['sampling_rate'][0] # unique for channel
176-
177-
# split channels in stream depending the name CHxxx ADCxxx
178-
chan_stream_ids = [name[:2] if name.startswith('CH') else name[:3]
179-
for name in signal_channels['name']]
180-
signal_channels['stream_id'] = chan_stream_ids
181-
182-
# and create streams channels (keep natural order 'CH' first)
183-
stream_ids, order = np.unique(chan_stream_ids, return_index=True)
184-
stream_ids = stream_ids[np.argsort(order)]
185-
signal_streams = [(f'Signals {stream_id}', f'{stream_id}') for stream_id in stream_ids]
186-
signal_streams = np.array(signal_streams, dtype=_signal_stream_dtype)
187-
174+
if len(signal_channels) > 0:
175+
signal_channels = np.array(signal_channels, dtype=_signal_channel_dtype)
176+
self._sig_sampling_rate = signal_channels['sampling_rate'][0] # unique for channel
177+
178+
# split channels in stream depending the name CHxxx ADCxxx
179+
chan_stream_ids = [name[:2] if name.startswith('CH') else name[:3]
180+
for name in signal_channels['name']]
181+
signal_channels['stream_id'] = chan_stream_ids
182+
183+
# and create streams channels (keep natural order 'CH' first)
184+
stream_ids, order = np.unique(chan_stream_ids, return_index=True)
185+
stream_ids = stream_ids[np.argsort(order)]
186+
signal_streams = [(f'Signals {stream_id}', f'{stream_id}') for stream_id in stream_ids]
187+
signal_streams = np.array(signal_streams, dtype=_signal_stream_dtype)
188+
else:
189+
signal_streams = np.array([])
188190
# scan for spikes files
189191
spike_channels = []
190192

191193
if len(info['spikes']) > 0:
192-
194+
self._first_spk_timestamps = []
195+
self._last_spk_timestamps = []
193196
self._spikes_memmap = {}
194-
for seg_index, oe_index in enumerate(oe_indices):
197+
oe_indices_spk = sorted(list(info['spikes'].keys()))
198+
for seg_index, oe_index in enumerate(oe_indices_spk):
195199
self._spikes_memmap[seg_index] = {}
196200
for spike_filename in info['spikes'][oe_index]:
197201
fullname = os.path.join(self.dirname, spike_filename)
@@ -207,6 +211,9 @@ def _parse_header(self):
207211
dtype=spikes_dtype)
208212
self._spikes_memmap[seg_index][name] = data_spike
209213

214+
self._first_spk_timestamps.append(data_spike[0]['timestamp'])
215+
self._last_spk_timestamps.append(data_spike[-1]['timestamp'])
216+
210217
# In each file 'sorted_id' indicate the number of cluster so number of units
211218
# so need to scan file for all segment to get units
212219
self._spike_sampling_rate = None
@@ -335,9 +342,9 @@ def _get_spike_slice(self, seg_index, unit_index, t_start, t_stop):
335342
data_spike = self._spikes_memmap[seg_index][name]
336343

337344
if t_start is None:
338-
t_start = self._segment_t_start(0, seg_index)
345+
t_start = self._first_spk_timestamps[seg_index]
339346
if t_stop is None:
340-
t_stop = self._segment_t_stop(0, seg_index)
347+
t_stop = self._last_spk_timestamps[seg_index]
341348
ts0 = int(t_start * self._spike_sampling_rate)
342349
ts1 = int(t_stop * self._spike_sampling_rate)
343350

@@ -489,11 +496,11 @@ def explore_folder(dirname):
489496
if (seg_index + 1) > info['nb_segment']:
490497
info['nb_segment'] += 1
491498
elif filename.endswith('.spikes'):
492-
s = filename.replace('.spikes', '').split('_')
493-
if len(s) == 1:
494-
seg_index = 0
499+
s = re.findall(r"(_\d+)$", filename.replace('.spikes', ''))
500+
if s:
501+
seg_index = int(s[0][1:]) - 1
495502
else:
496-
seg_index = int(s[1]) - 1
503+
seg_index = 0
497504
if seg_index not in info['spikes'].keys():
498505
info['spikes'][seg_index] = []
499506
info['spikes'][seg_index].append(filename)

0 commit comments

Comments
 (0)