Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve order of recording extractor custom properties in add_electrodes() #793

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upcoming

### Improvements

* In `add_electrodes()` custom properties are added to the electrodes table in the order they are set in the recording extractor. [PR #793](https://github.com/catalystneuro/neuroconv/issues/793)

# v0.4.8 (March 20, 2024)

### Bug fixes
Expand Down
5 changes: 4 additions & 1 deletion src/neuroconv/tools/spikeinterface/spikeinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def add_electrodes(recording: BaseRecording, nwbfile: pynwb.NWBFile, metadata: d
schema_properties = required_schema_properties | optional_schema_properties

electrode_table_previous_properties = set(nwbfile.electrodes.colnames) if nwbfile.electrodes else set()
order_of_properties = list(data_to_add.keys())
extracted_properties = set(data_to_add)
properties_to_add_by_rows = required_schema_properties | electrode_table_previous_properties
properties_to_add_by_columns = extracted_properties - properties_to_add_by_rows
Expand Down Expand Up @@ -387,7 +388,9 @@ def add_electrodes(recording: BaseRecording, nwbfile: pynwb.NWBFile, metadata: d
indexes_for_default_values = electrodes_df.index.difference(indexes_for_new_data).values

# Add properties as columns
for property in properties_to_add_by_columns - {"channel_name"}:
unordered_properties_to_add_by_columns = properties_to_add_by_columns - {"channel_name"}
ordered_properties_to_add_by_columns = sorted(unordered_properties_to_add_by_columns, key=order_of_properties.index)
for property in ordered_properties_to_add_by_columns:
cols_args = data_to_add[property]
data = cols_args["data"]
if np.issubdtype(data.dtype, np.integer):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_ecephys/test_tools_spikeinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,32 @@ def test_property_metadata_mismatch(self):
expected_property_2_values = ["", "", "value_1", "value_2", "value_3", "value_4"]
self.assertListEqual(actual_property_2_values, expected_property_2_values)

def test_custom_property_order(self):
"""
Test that custom properties are be added to the electrodes table in the order they are set.
"""
self.recording_1.set_property(key="custom_property_x", values=[0.1] * self.num_channels)
self.recording_1.set_property(key="custom_property_y", values=[0.2] * self.num_channels)
self.recording_1.set_property(key="custom_property_z", values=[0.3] * self.num_channels)
self.recording_1.set_property(key="custom_property_1", values=["value_1"] * self.num_channels)

add_electrodes(recording=self.recording_1, nwbfile=self.nwbfile)

expected_columns_in_order = [
"location",
"group",
"group_name",
"channel_name",
"custom_property_x",
"custom_property_y",
"custom_property_z",
"custom_property_1",
"rel_x",
"rel_y",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are rel_x and rel_y after the custum ones though? I would have expected them before since they are pre-defined in NWB schema

]
electrodes_columns = list(self.nwbfile.electrodes.colnames)
self.assertListEqual(expected_columns_in_order, electrodes_columns)


class TestAddUnitsTable(TestCase):
@classmethod
Expand Down
Loading