From 46f592dd6924228ca3051d0ba07e30a4c7a4d2b0 Mon Sep 17 00:00:00 2001 From: Jeremy Magland Date: Tue, 28 Nov 2023 09:10:57 -0500 Subject: [PATCH] adjust set_probe tests --- .../baserecordingextractorinterface.py | 2 +- .../tools/testing/data_interface_mixins.py | 37 ++----------------- src/neuroconv/tools/testing/mock_probes.py | 28 ++++++++++++++ .../test_on_data/test_recording_interfaces.py | 2 +- 4 files changed, 34 insertions(+), 35 deletions(-) create mode 100644 src/neuroconv/tools/testing/mock_probes.py diff --git a/src/neuroconv/datainterfaces/ecephys/baserecordingextractorinterface.py b/src/neuroconv/datainterfaces/ecephys/baserecordingextractorinterface.py index 423f7c8b3..bfb4e9991 100644 --- a/src/neuroconv/datainterfaces/ecephys/baserecordingextractorinterface.py +++ b/src/neuroconv/datainterfaces/ecephys/baserecordingextractorinterface.py @@ -225,7 +225,7 @@ def set_aligned_segment_starting_times(self, aligned_segment_starting_times: Lis ] self.set_aligned_segment_timestamps(aligned_segment_timestamps=aligned_segment_timestamps) - def set_probe(self, probe, *, group_mode: Literal["by_shank", "by_probe"]): + def set_probe(self, probe, group_mode: Literal["by_shank", "by_probe"]): """ Set the probe information via a ProbeInterface object. diff --git a/src/neuroconv/tools/testing/data_interface_mixins.py b/src/neuroconv/tools/testing/data_interface_mixins.py index 6be342e2a..1fb4f1970 100644 --- a/src/neuroconv/tools/testing/data_interface_mixins.py +++ b/src/neuroconv/tools/testing/data_interface_mixins.py @@ -19,9 +19,6 @@ from neuroconv.datainterfaces.ecephys.basesortingextractorinterface import ( BaseSortingExtractorInterface, ) -from neuroconv.datainterfaces.ecephys.intan.intandatainterface import ( - IntanRecordingInterface, -) from neuroconv.datainterfaces.ophys.baseimagingextractorinterface import ( BaseImagingExtractorInterface, ) @@ -30,7 +27,7 @@ ) from neuroconv.utils import NWBMetaDataEncoder -interfaces_for_testing_probe = [IntanRecordingInterface] +from .mock_probes import generate_mock_probe class DataInterfaceTestMixin: @@ -114,10 +111,11 @@ def test_conversion_as_lone_interface(self): self.case = num self.test_kwargs = kwargs self.interface = self.data_interface_cls(**self.test_kwargs) - do_set_probe = self.data_interface_cls in interfaces_for_testing_probe + do_set_probe = isinstance(self.interface, BaseRecordingExtractorInterface) if do_set_probe: + assert isinstance(self.interface, BaseRecordingExtractorInterface) self.interface.set_probe( - _create_mock_probe(num_channels=self.interface.recording_extractor.get_num_channels()), + generate_mock_probe(num_channels=self.interface.recording_extractor.get_num_channels()), group_mode="by_shank", ) self.check_metadata_schema_valid() @@ -131,33 +129,6 @@ def test_conversion_as_lone_interface(self): self.run_custom_checks() -def _create_mock_probe(*, num_channels: int): - import probeinterface as pi - - # The shank ids will be 0, 0, 0, ..., 1, 1, 1, ..., 2, 2, 2, ... - shank_ids: List[int] = [] - positions = np.zeros((num_channels, 2)) - num_shanks = 3 - # ceil division - channels_per_shank = (num_channels + num_shanks - 1) // num_shanks - for i in range(num_shanks): - # x0, y0 is the position of the first electrode in the shank - x0 = 0 - y0 = i * 200 - for j in range(channels_per_shank): - if len(shank_ids) == num_channels: - break - shank_ids.append(i) - x = x0 + j * 10 - y = y0 + (j % 2) * 10 - positions[len(shank_ids) - 1] = x, y - probe = pi.Probe(ndim=2, si_units="um") - probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5}) - probe.set_device_channel_indices(np.arange(num_channels)) - probe.set_shank_ids(shank_ids) - return probe - - class TemporalAlignmentMixin: """ Generic class for testing temporal alignment methods. diff --git a/src/neuroconv/tools/testing/mock_probes.py b/src/neuroconv/tools/testing/mock_probes.py new file mode 100644 index 000000000..0624d2c7d --- /dev/null +++ b/src/neuroconv/tools/testing/mock_probes.py @@ -0,0 +1,28 @@ +from typing import List +import numpy as np + + +def generate_mock_probe(num_channels: int, num_shanks: int = 3): + import probeinterface as pi + + # The shank ids will be 0, 0, 0, ..., 1, 1, 1, ..., 2, 2, 2, ... + shank_ids: List[int] = [] + positions = np.zeros((num_channels, 2)) + # ceil division + channels_per_shank = (num_channels + num_shanks - 1) // num_shanks + for i in range(num_shanks): + # x0, y0 is the position of the first electrode in the shank + x0 = 0 + y0 = i * 200 + for j in range(channels_per_shank): + if len(shank_ids) == num_channels: + break + shank_ids.append(i) + x = x0 + j * 10 + y = y0 + (j % 2) * 10 + positions[len(shank_ids) - 1] = x, y + probe = pi.Probe(ndim=2, si_units="um") + probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5}) + probe.set_device_channel_indices(np.arange(num_channels)) + probe.set_shank_ids(shank_ids) + return probe diff --git a/tests/test_on_data/test_recording_interfaces.py b/tests/test_on_data/test_recording_interfaces.py index 325a1e984..b383d3f58 100644 --- a/tests/test_on_data/test_recording_interfaces.py +++ b/tests/test_on_data/test_recording_interfaces.py @@ -211,7 +211,7 @@ def check_extracted_metadata(self, metadata: dict): assert len(metadata["Ecephys"]["Device"]) == 1 assert metadata["Ecephys"]["Device"][0]["name"] == "Neuronexus-32" assert metadata["Ecephys"]["Device"][0]["description"] == "The ecephys device for the MEArec recording." - assert len(metadata["Ecephys"]["ElectrodeGroup"]) == 1 + # assert len(metadata["Ecephys"]["ElectrodeGroup"]) == 1 # do not test this condition because in the test we are setting a mock probe assert metadata["Ecephys"]["ElectrodeGroup"][0]["device"] == "Neuronexus-32" assert metadata["Ecephys"]["ElectricalSeries"]["description"] == ( '{"angle_tol": 15, "bursting": false, "chunk_duration": 0, "color_noise_floor": 1, '