Skip to content

Commit

Permalink
UUID strings for cbin, ch and meta files in spikeglx reader
Browse files Browse the repository at this point in the history
  • Loading branch information
oliche committed May 2, 2024
1 parent 7313f61 commit b88ee13
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/spikeglx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/tests/unit/cpu/test_spikeglx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b88ee13

Please sign in to comment.