Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions neo/rawio/tdtrawio.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def __init__(self, dirname="", sortname=""):
Parameters
----------
dirname: str | Path, default: None
Tank-directory of a dataset to be read as multiple segments OR single file of dataset.
In the latter case only the corresponding segment will be considered.
Tank-directory of a dataset to be read as multiple segments OR single file of dataset
OR a block directory holding the files of one block (read as a single segment).
For a single file only the corresponding segment will be considered.
sortname: str, default: ''
'sortname' is used to specify the external sortcode generated by offline spike sorting.
if sortname=='PLX', there should be a ./sort/PLX/*.SortResult file in the tdt block,
Expand All @@ -76,8 +77,17 @@ def __init__(self, dirname="", sortname=""):
BaseRawIO.__init__(self)
dirname = Path(dirname)
if dirname.is_dir():
self.dirname = Path(dirname)
self.tdt_block_mode = "multi"
subdirs = [p for p in dirname.iterdir() if p.is_dir()]
if is_tdtblock(dirname) and not any(is_tdtblock(p) for p in subdirs):
# the directory is itself a single block, not a tank of blocks
tsq_files = [f for f in dirname.iterdir() if f.suffix.lower() == ".tsq"]
if len(tsq_files) != 1:
raise ValueError(f"{dirname} holds {len(tsq_files)} .tsq files, pass one file path instead")
self.dirname = tsq_files[0].with_suffix("")
self.tdt_block_mode = "single"
else:
self.dirname = dirname
self.tdt_block_mode = "multi"
elif dirname.is_file():
# in single tdt block mode the dirname also contains the block prefix
self.dirname = dirname.with_suffix("")
Expand Down
38 changes: 38 additions & 0 deletions neo/test/rawiotest/test_tdtrawio.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tempfile
import unittest
from pathlib import Path
from numpy.testing import assert_array_equal, assert_
Expand Down Expand Up @@ -62,5 +63,42 @@ def test_compare_load_multi_single_block(self):
)


class TestTdtRawIOBlockDirectory(unittest.TestCase):
def test_block_directory_is_single_block(self):
# a directory holding the files of one block (no sub-block folders), see issue 1970
with tempfile.TemporaryDirectory() as tmp:
block = Path(tmp) / "recording_name"
block.mkdir()
for ext in ("Tbk", "Tdx", "tev", "tsq"):
(block / f"recording_name.{ext}").touch()

rawio = TdtRawIO(block)

self.assertEqual(rawio.tdt_block_mode, "single")
self.assertEqual(rawio.dirname, block / "recording_name")

def test_block_directory_with_several_tsq_files_raises(self):
with tempfile.TemporaryDirectory() as tmp:
block = Path(tmp)
for name in ("a", "b"):
for ext in ("Tbk", "Tdx", "tev", "tsq"):
(block / f"{name}.{ext}").touch()

with self.assertRaises(ValueError):
TdtRawIO(block)

def test_tank_with_block_subfolders_stays_multi(self):
with tempfile.TemporaryDirectory() as tmp:
tank = Path(tmp)
for ext in ("Tbk", "Tdx", "tev", "tsq"):
(tank / f"loose.{ext}").touch()
sub = tank / "Block-1"
sub.mkdir()
for ext in ("Tbk", "Tdx", "tev", "tsq"):
(sub / f"tank_Block-1.{ext}").touch()

self.assertEqual(TdtRawIO(tank).tdt_block_mode, "multi")


if __name__ == "__main__":
unittest.main()