Skip to content

Commit

Permalink
fix tests for the re-ordering on read
Browse files Browse the repository at this point in the history
  • Loading branch information
oliche committed Oct 23, 2024
1 parent 73e3cdd commit c5f2159
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/spikeglx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -633,16 +634,21 @@ 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
:return: dictionary with keys 'row', 'col', 'ind', 'shank', 'adc', 'x', 'y', 'sample_shift'
"""
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:
Expand All @@ -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
Expand All @@ -686,6 +685,7 @@ def _geometry_from_meta(meta_data):
else:
return th


def read_geometry(meta_file):
"""
Reads the geometry
Expand Down
8 changes: 4 additions & 4 deletions src/tests/unit/cpu/test_spikeglx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c5f2159

Please sign in to comment.