From b88ee13c12b6c223a0ee91f8c2c72d013200ef3c Mon Sep 17 00:00:00 2001 From: owinter Date: Thu, 2 May 2024 20:04:49 +0100 Subject: [PATCH] UUID strings for cbin, ch and meta files in spikeglx reader --- src/spikeglx.py | 19 ++++++++++++------- src/tests/unit/cpu/test_spikeglx.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/spikeglx.py b/src/spikeglx.py index c2e2ccc..f851a0c 100644 --- a/src/spikeglx.py +++ b/src/spikeglx.py @@ -17,6 +17,16 @@ _logger = logging.getLogger("ibllib") +def _get_companion_file(sglx_file, pattern='.meta'): + # on SDSC there is a possibility that there is an UUID string in the filename + sglx_file = Path(sglx_file) + companion_file = sglx_file.with_suffix(pattern) + if not companion_file.exists(): + search_pattern = f"{one.alf.files.remove_uuid_string(sglx_file).stem}*{pattern}" + companion_file = next(sglx_file.parent.glob(search_pattern), companion_file) + return companion_file + + class Reader: """ Class for SpikeGLX reading purposes @@ -60,7 +70,7 @@ def __init__( """ self.ignore_warnings = ignore_warnings sglx_file = Path(sglx_file) - meta_file = meta_file or sglx_file.with_suffix(".meta") + meta_file = meta_file or _get_companion_file(sglx_file, '.meta') # only used if MTSCOMP compressed self.ch_file = ch_file @@ -81,11 +91,6 @@ def __init__( self.nbytes = self.file_bin.stat().st_size if self.file_bin else None self.dtype = np.dtype(dtype) - # on SDSC there is a possibility that there is an UUID string in the filename - if not meta_file.exists(): - meta_file = next(sglx_file.parent.glob( - f"{one.alf.files.remove_uuid_string(sglx_file).stem}.*.meta"), meta_file) - if not meta_file.exists(): # if no meta-data file is provided, try to get critical info from the binary file # by seeing if filesize checks out with neuropixel 384 channels @@ -126,7 +131,7 @@ def open(self): sglx_file = str(self.file_bin) if self.is_mtscomp: self._raw = mtscomp.Reader() - ch_file = self.ch_file or self.file_bin.with_suffix(".ch") + ch_file = self.ch_file or _get_companion_file(sglx_file, '.ch') self._raw.open(self.file_bin, ch_file) if self._raw.shape != (self.ns, self.nc): ftsec = self._raw.shape[0] / self.fs diff --git a/src/tests/unit/cpu/test_spikeglx.py b/src/tests/unit/cpu/test_spikeglx.py index e1fb504..75d35a8 100644 --- a/src/tests/unit/cpu/test_spikeglx.py +++ b/src/tests/unit/cpu/test_spikeglx.py @@ -620,6 +620,23 @@ class TestsBasicReader(unittest.TestCase): Tests the basic usage where there is a flat binary and no metadata associated """ + def test_get_companion_file(self): + import uuid + with tempfile.TemporaryDirectory() as td: + sglx_file = Path(td) / f"sample3A_g0_t0.imec.ap.{str(uuid.uuid4())}.bin" + meta_file = Path(td) / f"sample3A_g0_t0.imec.ap.{str(uuid.uuid4())}.meta" + meta_file.touch() + self.assertEqual(meta_file, spikeglx._get_companion_file(sglx_file, '.meta')) + + with tempfile.TemporaryDirectory() as td: + sglx_file = Path(td) / f"sample3A_g0_t0.imec.ap.bin" + meta_file_ok = Path(td) / f"sample3A_g0_t0.imec.ap.meta" + meta_file = Path(td) / f"sample3A_g0_t0.imec.ap.{str(uuid.uuid4())}.meta" + meta_file.touch() + meta_file_ok.touch() + self.assertEqual(meta_file_ok, spikeglx._get_companion_file(sglx_file, '.meta')) + + def test_read_flat_binary_float32(self): # here we expect no scaling to V applied and no sync trace as the format is float32 kwargs = dict(ns=60000, nc=384, fs=30000, dtype=np.float32)