From 71523c799052117f6262a190beab4980b5217ffa Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 29 May 2023 16:30:48 +0200 Subject: [PATCH 1/8] refactor electrode information extraction --- .../neuroscope/neuroscopedatainterface.py | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index 5af83c4b0..65340a5c7 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -1,3 +1,4 @@ +import re from pathlib import Path from typing import Optional @@ -34,26 +35,26 @@ def add_recording_extractor_properties(recording_extractor, xml_file_path: str, if gain: recording_extractor.set_channel_gains(gain) - channel_groups = get_channel_groups(xml_file_path=xml_file_path) - - channel_map = { - channel_id: idx - for idx, channel_id in enumerate([channel_id for group in channel_groups for channel_id in group]) - } - group_electrode_numbers = [x for channels in channel_groups for x, _ in enumerate(channels)] - group_nums = [n + 1 for n, channels in enumerate(channel_groups) for _ in channels] - group_names = [f"Group{n}" for n in group_nums] - - channel_groups_mapped = [group_nums[channel_map[channel_id]] for channel_id in channel_map.keys()] - group_names_mapped = [group_names[channel_map[channel_id]] for channel_id in channel_map.keys()] - shank_electrode_number = [group_electrode_numbers[channel_map[channel_id]] for channel_id in channel_map.keys()] - - channel_ids_mapped = recording_extractor.get_channel_ids() - recording_extractor.set_property(key="group", ids=channel_ids_mapped, values=channel_groups_mapped) - recording_extractor.set_property(key="group_name", ids=channel_ids_mapped, values=group_names_mapped) - recording_extractor.set_property( - key="shank_electrode_number", ids=channel_ids_mapped, values=shank_electrode_number - ) + channel_ids = recording_extractor.get_channel_ids() + channel_names = recording_extractor.get_property(key="channel_name") + channel_groups = [int(name.split("grp")[1]) for name in channel_names] + + channel_group_names = [f"Group{group_index}" for group_index in channel_groups] + recording_extractor.set_property(key="group", ids=channel_ids, values=channel_groups) + recording_extractor.set_property(key="group_name", ids=channel_ids, values=channel_group_names) + + unique_groups = set(channel_groups) + channel_id_to_shank_electrode_number = dict() + for group_index in unique_groups: + channel_in_groups = [ + channel_id for channel_id, group in zip(channel_ids, channel_groups) if group == group_index + ] + channel_id_to_shank_electrode_number.update( + {channel_id: electrode_number for electrode_number, channel_id in enumerate(channel_in_groups)} + ) + + group_electrode_numbers = [channel_id_to_shank_electrode_number[channel_id] for channel_id in channel_ids] + recording_extractor.set_property(key="shank_electrode_number", ids=channel_ids, values=group_electrode_numbers) class NeuroScopeRecordingInterface(BaseRecordingExtractorInterface): From b9ae16d3bb410e9ea027d69e5ee701e12684960d Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 30 May 2023 14:38:32 +0200 Subject: [PATCH 2/8] correct group numbering --- .../ecephys/neuroscope/neuroscopedatainterface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index 65340a5c7..7cbf604d9 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -39,7 +39,7 @@ def add_recording_extractor_properties(recording_extractor, xml_file_path: str, channel_names = recording_extractor.get_property(key="channel_name") channel_groups = [int(name.split("grp")[1]) for name in channel_names] - channel_group_names = [f"Group{group_index}" for group_index in channel_groups] + channel_group_names = [f"Group{group_index + 1}" for group_index in channel_groups] recording_extractor.set_property(key="group", ids=channel_ids, values=channel_groups) recording_extractor.set_property(key="group_name", ids=channel_ids, values=channel_group_names) From e5c13d2364532d85298cffb5f2c03ae51ff619a4 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 7 Jun 2023 18:43:41 +0200 Subject: [PATCH 3/8] remove xml_path --- .../ecephys/neuroscope/neuroscopedatainterface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index d57da96d8..9f44f754b 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -31,7 +31,7 @@ def subset_shank_channels(recording_extractor, xml_file_path: str): return sub_recording -def add_recording_extractor_properties(recording_extractor, xml_file_path: str, gain: Optional[float] = None): +def add_recording_extractor_properties(recording_extractor, gain: Optional[float] = None): """Automatically add properties to RecordingExtractor object.""" if gain: From 7f3bbe89f938ee34ffe19bdfea789429904d58b9 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 7 Jun 2023 18:44:31 +0200 Subject: [PATCH 4/8] remove unused module --- .../datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index 9f44f754b..a0db43d17 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -1,4 +1,3 @@ -import re from pathlib import Path from typing import Optional From 404d302df0684363d25795418785f2036323698b Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 7 Jun 2023 19:02:56 +0200 Subject: [PATCH 5/8] small refactor --- .../neuroscope/neuroscopedatainterface.py | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index a0db43d17..854551113 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -2,6 +2,7 @@ from typing import Optional import numpy as np +from spikeinterface.core import BaseRecording from .neuroscope_utils import ( get_channel_groups, @@ -30,7 +31,7 @@ def subset_shank_channels(recording_extractor, xml_file_path: str): return sub_recording -def add_recording_extractor_properties(recording_extractor, gain: Optional[float] = None): +def add_recording_extractor_properties(recording_extractor: BaseRecording, gain: Optional[float] = None): """Automatically add properties to RecordingExtractor object.""" if gain: @@ -46,16 +47,15 @@ def add_recording_extractor_properties(recording_extractor, gain: Optional[float unique_groups = set(channel_groups) channel_id_to_shank_electrode_number = dict() + for group_index in unique_groups: - channel_in_groups = [ - channel_id for channel_id, group in zip(channel_ids, channel_groups) if group == group_index - ] - channel_id_to_shank_electrode_number.update( - {channel_id: electrode_number for electrode_number, channel_id in enumerate(channel_in_groups)} - ) + group_channels = [channel_id for channel_id, group in zip(channel_ids, channel_groups) if group == group_index] + group_mapping = {channel_id: electrode_number for electrode_number, channel_id in enumerate(group_channels)} + channel_id_to_shank_electrode_number.update(group_mapping) group_electrode_numbers = [channel_id_to_shank_electrode_number[channel_id] for channel_id in channel_ids] recording_extractor.set_property(key="shank_electrode_number", ids=channel_ids, values=group_electrode_numbers) + recording_extractor.set_channel_property() class NeuroScopeRecordingInterface(BaseRecordingExtractorInterface): @@ -111,9 +111,7 @@ def __init__( super().__init__(file_path=file_path, verbose=verbose, es_key=es_key) self.source_data["xml_file_path"] = xml_file_path - add_recording_extractor_properties( - recording_extractor=self.recording_extractor, xml_file_path=xml_file_path, gain=gain - ) + add_recording_extractor_properties(recording_extractor=self.recording_extractor, gain=gain) self.recording_extractor = subset_shank_channels( recording_extractor=self.recording_extractor, xml_file_path=xml_file_path @@ -177,9 +175,7 @@ def __init__( super().__init__(file_path=file_path) self.source_data["xml_file_path"] = xml_file_path - add_recording_extractor_properties( - recording_extractor=self.recording_extractor, xml_file_path=xml_file_path, gain=gain - ) + add_recording_extractor_properties(recording_extractor=self.recording_extractor, gain=gain) self.recording_extractor = subset_shank_channels( recording_extractor=self.recording_extractor, xml_file_path=xml_file_path From a212354ccb433112d5271fb3a25cbdf7c72e2033 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 7 Jun 2023 19:05:04 +0200 Subject: [PATCH 6/8] error --- .../datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index 854551113..390af13ef 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -55,7 +55,6 @@ def add_recording_extractor_properties(recording_extractor: BaseRecording, gain: group_electrode_numbers = [channel_id_to_shank_electrode_number[channel_id] for channel_id in channel_ids] recording_extractor.set_property(key="shank_electrode_number", ids=channel_ids, values=group_electrode_numbers) - recording_extractor.set_channel_property() class NeuroScopeRecordingInterface(BaseRecordingExtractorInterface): From 7722068c5d73480f40086913a3de337b2e5f36a0 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 7 Jun 2023 19:11:40 +0200 Subject: [PATCH 7/8] can not use annotations at run time for complicated reasons --- .../ecephys/neuroscope/neuroscopedatainterface.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index 390af13ef..eb178e793 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -2,7 +2,6 @@ from typing import Optional import numpy as np -from spikeinterface.core import BaseRecording from .neuroscope_utils import ( get_channel_groups, @@ -31,7 +30,7 @@ def subset_shank_channels(recording_extractor, xml_file_path: str): return sub_recording -def add_recording_extractor_properties(recording_extractor: BaseRecording, gain: Optional[float] = None): +def add_recording_extractor_properties(recording_extractor, gain: Optional[float] = None): """Automatically add properties to RecordingExtractor object.""" if gain: From ad986b0e3bc7d349bb9f0c9be06eba088f16e616 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 7 Jun 2023 19:24:07 +0200 Subject: [PATCH 8/8] maybe this? --- .../datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py index eb178e793..5513a6021 100644 --- a/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py +++ b/src/neuroconv/datainterfaces/ecephys/neuroscope/neuroscopedatainterface.py @@ -47,6 +47,7 @@ def add_recording_extractor_properties(recording_extractor, gain: Optional[float unique_groups = set(channel_groups) channel_id_to_shank_electrode_number = dict() + # For each group, get the corresponding channels and enumerate them to have the shank electrode number for group_index in unique_groups: group_channels = [channel_id for channel_id, group in zip(channel_ids, channel_groups) if group == group_index] group_mapping = {channel_id: electrode_number for electrode_number, channel_id in enumerate(group_channels)}