From 784907cc0e7e03725689978166079ef344417975 Mon Sep 17 00:00:00 2001 From: ptlnextdoor <67443596+ptlnextdoor@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:54:12 +0000 Subject: [PATCH 1/3] Fix TdtIO IndexError when given a single block directory A directory that holds the files of one TDT block (Tbk, Tdx, tev, tsq) was read as a tank of blocks. No sub-block folders were found, so no segment was created and _parse_header raised IndexError. Treat such a directory as a single block. --- neo/rawio/tdtrawio.py | 7 ++++++- neo/test/rawiotest/test_tdtrawio.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/neo/rawio/tdtrawio.py b/neo/rawio/tdtrawio.py index 280b95106..c7cb0c8e1 100644 --- a/neo/rawio/tdtrawio.py +++ b/neo/rawio/tdtrawio.py @@ -75,7 +75,12 @@ def __init__(self, dirname="", sortname=""): """ BaseRawIO.__init__(self) dirname = Path(dirname) - if dirname.is_dir(): + tsq_files = [f for f in dirname.iterdir() if f.suffix.lower() == ".tsq"] if dirname.is_dir() else [] + if dirname.is_dir() and is_tdtblock(dirname) and len(tsq_files) == 1: + # the directory is itself a single block, not a tank of blocks + self.dirname = tsq_files[0].with_suffix("") + self.tdt_block_mode = "single" + elif dirname.is_dir(): self.dirname = Path(dirname) self.tdt_block_mode = "multi" elif dirname.is_file(): diff --git a/neo/test/rawiotest/test_tdtrawio.py b/neo/test/rawiotest/test_tdtrawio.py index 1c3afe3c6..56db73dc9 100644 --- a/neo/test/rawiotest/test_tdtrawio.py +++ b/neo/test/rawiotest/test_tdtrawio.py @@ -1,3 +1,4 @@ +import tempfile import unittest from pathlib import Path from numpy.testing import assert_array_equal, assert_ @@ -64,3 +65,18 @@ def test_compare_load_multi_single_block(self): if __name__ == "__main__": unittest.main() + + +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") From c7c9f16d9795488d78ea858a5a0ed449344ce4bf Mon Sep 17 00:00:00 2001 From: ptlnextdoor <67443596+ptlnextdoor@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:36:08 +0000 Subject: [PATCH 2/3] Move TdtRawIO block directory test above the main guard --- neo/test/rawiotest/test_tdtrawio.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/neo/test/rawiotest/test_tdtrawio.py b/neo/test/rawiotest/test_tdtrawio.py index 56db73dc9..4c8f92508 100644 --- a/neo/test/rawiotest/test_tdtrawio.py +++ b/neo/test/rawiotest/test_tdtrawio.py @@ -63,10 +63,6 @@ def test_compare_load_multi_single_block(self): ) -if __name__ == "__main__": - unittest.main() - - 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 @@ -80,3 +76,7 @@ def test_block_directory_is_single_block(self): self.assertEqual(rawio.tdt_block_mode, "single") self.assertEqual(rawio.dirname, block / "recording_name") + + +if __name__ == "__main__": + unittest.main() From 7d852c01823df01c7a8651af611bd1c494065e34 Mon Sep 17 00:00:00 2001 From: ptlnextdoor <67443596+ptlnextdoor@users.noreply.github.com> Date: Mon, 30 Nov 2026 07:43:48 +0000 Subject: [PATCH 3/3] Reject ambiguous TDT block directories and document block directory input --- neo/rawio/tdtrawio.py | 25 +++++++++++++++---------- neo/test/rawiotest/test_tdtrawio.py | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/neo/rawio/tdtrawio.py b/neo/rawio/tdtrawio.py index c7cb0c8e1..3a323d0c1 100644 --- a/neo/rawio/tdtrawio.py +++ b/neo/rawio/tdtrawio.py @@ -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, @@ -75,14 +76,18 @@ def __init__(self, dirname="", sortname=""): """ BaseRawIO.__init__(self) dirname = Path(dirname) - tsq_files = [f for f in dirname.iterdir() if f.suffix.lower() == ".tsq"] if dirname.is_dir() else [] - if dirname.is_dir() and is_tdtblock(dirname) and len(tsq_files) == 1: - # the directory is itself a single block, not a tank of blocks - self.dirname = tsq_files[0].with_suffix("") - self.tdt_block_mode = "single" - elif dirname.is_dir(): - self.dirname = Path(dirname) - self.tdt_block_mode = "multi" + if dirname.is_dir(): + 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("") diff --git a/neo/test/rawiotest/test_tdtrawio.py b/neo/test/rawiotest/test_tdtrawio.py index 4c8f92508..6ffd163e0 100644 --- a/neo/test/rawiotest/test_tdtrawio.py +++ b/neo/test/rawiotest/test_tdtrawio.py @@ -77,6 +77,28 @@ def test_block_directory_is_single_block(self): 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()