Skip to content

Commit da05655

Browse files
Merge pull request #937 from samuelgarcia/reorder_openephys_chans
Good channel order for old openephys format.
2 parents 9eb2487 + 1bbbff4 commit da05655

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

neo/rawio/openephysrawio.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -473,27 +473,31 @@ def explore_folder(dirname):
473473
info['nb_segment'] += 1
474474

475475
# order continuous file by channel number within segment
476+
# order "CH before "ADC"
476477
for seg_index, continuous_filenames in info['continuous'].items():
477-
chan_ids = {}
478+
chan_ids_by_type = {}
479+
filenames_by_type = {}
478480
for continuous_filename in continuous_filenames:
479481
s = continuous_filename.replace('.continuous', '').split('_')
480482
processor_id, ch_name = s[0], s[1]
481-
chan_str = re.split(r'(\d+)', s[1])[0]
482-
chan_id = int(ch_name.replace(chan_str, ''))
483-
if chan_str in chan_ids.keys():
484-
chan_ids[chan_str].append(chan_id)
483+
chan_type = re.split(r'(\d+)', s[1])[0]
484+
chan_id = int(ch_name.replace(chan_type, ''))
485+
if chan_type in chan_ids_by_type.keys():
486+
chan_ids_by_type[chan_type].append(chan_id)
487+
filenames_by_type[chan_type].append(continuous_filename)
485488
else:
486-
chan_ids[chan_str] = [chan_id]
487-
order = []
488-
for type in chan_ids.keys():
489-
order.append(np.argsort(chan_ids[type]))
490-
order = [list.tolist() for list in order]
491-
for i, sublist in enumerate(order):
492-
if i > 0:
493-
order[i] = [x + max(order[i - 1]) + 1 for x in order[i]]
494-
order = [item for sublist in order for item in sublist]
495-
continuous_filenames = [continuous_filenames[i] for i in order]
496-
info['continuous'][seg_index] = continuous_filenames
489+
chan_ids_by_type[chan_type] = [chan_id]
490+
filenames_by_type[chan_type] = [continuous_filename]
491+
chan_types = list(chan_ids_by_type.keys())
492+
if chan_types[0] == 'ADC':
493+
# put ADC at last position
494+
chan_types = chan_types[1:] + chan_types[0:1]
495+
ordered_continuous_filenames = []
496+
for chan_type in chan_types:
497+
local_order = np.argsort(chan_ids_by_type[chan_type])
498+
local_filenames = np.array(filenames_by_type[chan_type])[local_order]
499+
ordered_continuous_filenames.extend(local_filenames)
500+
info['continuous'][seg_index] = ordered_continuous_filenames
497501

498502
# order spike files within segment
499503
for seg_index, spike_filenames in info['spikes'].items():

neo/test/rawiotest/test_openephysrawio.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ def test_raise_error_if_strange_timestamps(self):
7272
with self.assertRaises(Exception):
7373
reader.parse_header()
7474

75+
def test_channel_order(self):
76+
reader = OpenEphysRawIO(dirname=self.get_filename_path(
77+
'OpenEphys_SampleData_1'))
78+
reader.parse_header()
79+
reader.header['signal_channels']['name'][0].startswith('CH')
80+
7581

7682
if __name__ == "__main__":
7783
unittest.main()

0 commit comments

Comments
 (0)