From c5f2159b14311081dfc4eeef82a52053e9f9c20c Mon Sep 17 00:00:00 2001 From: olivier Date: Wed, 23 Oct 2024 17:03:59 +0100 Subject: [PATCH] fix tests for the re-ordering on read --- src/spikeglx.py | 22 +++++++++++----------- src/tests/unit/cpu/test_spikeglx.py | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/spikeglx.py b/src/spikeglx.py index adfc185..195a56b 100644 --- a/src/spikeglx.py +++ b/src/spikeglx.py @@ -125,8 +125,9 @@ def __init__( self.channel_conversion_sample2v = _conversion_sample2v_from_meta(self.meta) self._raw = None self.geometry, order = _geometry_from_meta(self.meta, return_index=True) - self._raw_channel_order = np.arange(self.nc) - self._raw_channel_order[:order.size] = order + self.raw_channel_order = np.arange(self.nc) + if self.geometry is not None: # nidq files won't return any geometry here + self.raw_channel_order[:order.size] = order if open and self.file_bin: self.open() @@ -633,7 +634,7 @@ def _split_geometry_into_shanks(th, meta_data): return th -def _geometry_from_meta(meta_data): +def _geometry_from_meta(meta_data, return_index=False, nc=384): """ Gets the geometry, ie. the full trace header for the recording :param meta_data: meta_data dictionary as read by ibllib.io.spikeglx.read_meta_data @@ -641,8 +642,13 @@ def _geometry_from_meta(meta_data): """ cm = _map_channels_from_meta(meta_data) major_version = _get_neuropixel_major_version_from_meta(meta_data) - if cm is None: + if cm is None or all(map(lambda x: x is None, cm.values())): _logger.warning("Meta data doesn't have geometry (snsShankMap/snsGeomMap field), returning defaults") + if major_version is None: + if return_index: + return None, None + else: + return None th = neuropixel.trace_header(version=major_version) th["flag"] = th["x"] * 0 + 1.0 if return_index: @@ -669,13 +675,6 @@ def _geometry_from_meta(meta_data): ) th = _split_geometry_into_shanks(th, meta_data) th["ind"] = np.arange(th["col"].size) - if return_index: - cols = ["shank", "row", "col"] - inds = np.lexsort([th[c] for c in cols][::-1]) - return th, inds - else: - return th - if return_index: # here we sort the channels by shank, row and -col, this preserves the original NP1 # order while still allowing to deal with creative imro tables in NP2 @@ -686,6 +685,7 @@ def _geometry_from_meta(meta_data): else: return th + def read_geometry(meta_file): """ Reads the geometry diff --git a/src/tests/unit/cpu/test_spikeglx.py b/src/tests/unit/cpu/test_spikeglx.py index a7b060a..188514f 100644 --- a/src/tests/unit/cpu/test_spikeglx.py +++ b/src/tests/unit/cpu/test_spikeglx.py @@ -212,7 +212,7 @@ def test_read_slices(self): self.assertTrue(np.all(np.isclose(sr._raw[5:500, :-1] * s2mv, sr[5:500, :-1]))) self.assertTrue(np.all(np.isclose(sr._raw[5:500, 5] * s2mv, sr[5:500, 5]))) self.assertTrue(np.all(np.isclose(sr._raw[5, :-1] * s2mv, sr[5, :-1]))) - self.assertTrue(sr._raw[55, 5] * s2mv == sr[55, 5]) + np.testing.assert_almost_equal(sr._raw[55, 5] * s2mv, sr[55, 5]) self.assertTrue(np.all(np.isclose(sr._raw[55] * s2mv, sr[55]))) self.assertTrue(np.all(np.isclose(sr._raw[5:500] * s2mv, sr[5:500])[:, :-1])) @@ -494,11 +494,11 @@ def assert_read_glx(self, tglx): # test the channel geometries but skip when meta data doesn't correspond to NP if sr.major_version is not None: th = sr.geometry - h = neuropixel.trace_header( + h_expected = neuropixel.trace_header( sr.major_version, nshank=np.unique(th["shank"]).size ) - for k in h.keys(): - assert np.all(th[k] == h[k]), print(k) + for k in h_expected.keys(): + np.testing.assert_equal(h_expected[k][sr.raw_channel_order[:-sr.nsync]], th[k]) def testGetSerialNumber(self): self.meta_files.sort()