From fcd157da5d9e80488387e7676dc577a8ddff4b71 Mon Sep 17 00:00:00 2001 From: evadiva3 Date: Fri, 20 Nov 2026 02:00:49 -0500 Subject: [PATCH 1/2] Fix Plexon block offsets for unequal analog block sizes --- doc/source/authors.rst | 1 + neo/rawio/plexonrawio.py | 3 +- neo/test/rawiotest/test_plexonrawio.py | 52 +++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/doc/source/authors.rst b/doc/source/authors.rst index 357faaf52..f6ec68038 100644 --- a/doc/source/authors.rst +++ b/doc/source/authors.rst @@ -108,6 +108,7 @@ and may not be the current affiliation of a contributor. * Axel Coiffard-Donaty * Anthony Mercer * Aaron Kanzer [53] +* Eva B (evadiva3 on GitHub) 1. Centre de Recherche en Neuroscience de Lyon, CNRS UMR5292 - INSERM U1028 - Université Claude Bernard Lyon 1 2. Unité de Neuroscience, Information et Complexité, CNRS UPR 3293, Gif-sur-Yvette, France diff --git a/neo/rawio/plexonrawio.py b/neo/rawio/plexonrawio.py index 0f713fcb0..6e054ca2c 100644 --- a/neo/rawio/plexonrawio.py +++ b/neo/rawio/plexonrawio.py @@ -236,7 +236,8 @@ def _parse_header(self): if index == 0: data_block["cumsum"][index] = 0 else: - data_block["cumsum"][index] = data_block["cumsum"][index - 1] + sample_count + previous_sample_count = data_block["size"][index - 1] // 2 + data_block["cumsum"][index] = data_block["cumsum"][index - 1] + previous_sample_count self._data_blocks[bl_type][chan_id] = data_block diff --git a/neo/test/rawiotest/test_plexonrawio.py b/neo/test/rawiotest/test_plexonrawio.py index f1dac1cff..744255544 100644 --- a/neo/test/rawiotest/test_plexonrawio.py +++ b/neo/test/rawiotest/test_plexonrawio.py @@ -1,8 +1,10 @@ import unittest +from pathlib import Path +from tempfile import TemporaryDirectory import numpy as np -from neo.rawio.plexonrawio import PlexonRawIO +from neo.rawio.plexonrawio import DataBlockHeader, GlobalHeader, PlexonRawIO, SlowChannelHeader from neo.test.rawiotest.common_rawio_test import BaseTestRawIO @@ -57,5 +59,53 @@ def test_timestamps_with_lower_word_above_two_to_the_31(self): np.testing.assert_array_equal(around_second_wrap, [8_589_765_078, 8_589_773_845, 8_590_497_111, 8_590_594_911]) +class TestPlexonBlockOffsets(unittest.TestCase): + """Check parsed offsets and reads without downloading recordings.""" + + def test_unequal_signal_block_sizes(self): + """Parsed block starts must use the number of samples in preceding blocks.""" + samples = np.arange(350, dtype="int16") + header = np.zeros(1, dtype=GlobalHeader) + for field, value in { + "MagicNumber": 0x58454C50, + "Version": 106, + "ADFrequency": 1000, + "NumSlowChannels": 1, + "Year": 2020, + "Month": 1, + "Day": 1, + "BitsPerSpikeSample": 16, + "SlowMaxMagnitudeMV": 5000, + }.items(): + header[field] = value + channel = np.zeros(1, dtype=SlowChannelHeader) + channel["Name"] = b"AI1" + for field in ["Gain", "PreampGain", "Enabled"]: + channel[field] = 1 + channel["ADFreq"] = 1000 + + with TemporaryDirectory() as directory: + filename = Path(directory) / "unequal_blocks.plx" + with filename.open("wb") as stream: + header.tofile(stream) + channel.tofile(stream) + for start, stop in [(0, 100), (100, 300), (300, 350)]: + block = np.zeros(1, dtype=DataBlockHeader) + block["Type"] = 5 + block["TimeStamp"] = start + block["NumberOfWaveforms"] = 1 + block["NumberOfWordsInWaveform"] = stop - start + block.tofile(stream) + samples[start:stop].tofile(stream) + reader = PlexonRawIO(filename=str(filename), progress_bar=False) + try: + reader.parse_header() + np.testing.assert_array_equal(reader._data_blocks[5][0]["cumsum"], [0, 100, 300]) + chunk = reader.get_analogsignal_chunk(i_start=80, i_stop=280) + np.testing.assert_array_equal(chunk[:, 0], samples[80:280]) + finally: + del reader + + if __name__ == "__main__": unittest.main() From c18781e2d2b318f258dc12db43cc63acb42832cd Mon Sep 17 00:00:00 2001 From: Eva B <250416784+evadiva3@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:47:34 -0500 Subject: [PATCH 2/2] Test Plexon block offsets on the existing recording --- neo/test/rawiotest/test_plexonrawio.py | 71 ++++++++------------------ 1 file changed, 20 insertions(+), 51 deletions(-) diff --git a/neo/test/rawiotest/test_plexonrawio.py b/neo/test/rawiotest/test_plexonrawio.py index 744255544..f926f0a0f 100644 --- a/neo/test/rawiotest/test_plexonrawio.py +++ b/neo/test/rawiotest/test_plexonrawio.py @@ -1,10 +1,8 @@ import unittest -from pathlib import Path -from tempfile import TemporaryDirectory import numpy as np -from neo.rawio.plexonrawio import DataBlockHeader, GlobalHeader, PlexonRawIO, SlowChannelHeader +from neo.rawio.plexonrawio import PlexonRawIO from neo.test.rawiotest.common_rawio_test import BaseTestRawIO @@ -23,6 +21,25 @@ class TestPlexonRawIO( "plexon/plx/timestamp_clock/one_megahertz_forty_bit_timestamps/sorted_spikes_and_strobed_events.plx", ] + def test_unequal_signal_block_sizes(self): + """Read across unequal blocks in a real recording.""" + reader = PlexonRawIO(filename=self.get_local_path("plexon/File_plexon_3.plx"), progress_bar=False) + reader.parse_header() + blocks = reader._data_blocks[5][0] + sample_counts = blocks["size"] // 2 + np.testing.assert_array_equal(np.unique(sample_counts), [86, 120]) + starts = np.r_[0, np.cumsum(sample_counts[:-1])] + np.testing.assert_array_equal(blocks["cumsum"], starts) + + previous, last = blocks[-2:] + previous_samples = np.frombuffer( + reader._memmap, dtype="int16", count=previous["size"] // 2, offset=previous["pos"] + ) + last_samples = np.frombuffer(reader._memmap, dtype="int16", count=last["size"] // 2, offset=last["pos"]) + expected = np.r_[previous_samples[-10:], last_samples[:10]] + chunk = reader.get_analogsignal_chunk(i_start=starts[-1] - 10, i_stop=starts[-1] + 10) + np.testing.assert_array_equal(chunk[:, 0], expected) + def test_timestamps_with_lower_word_above_two_to_the_31(self): """The lower 32 bits of the 40-bit block timestamp must be read unsigned. @@ -59,53 +76,5 @@ def test_timestamps_with_lower_word_above_two_to_the_31(self): np.testing.assert_array_equal(around_second_wrap, [8_589_765_078, 8_589_773_845, 8_590_497_111, 8_590_594_911]) -class TestPlexonBlockOffsets(unittest.TestCase): - """Check parsed offsets and reads without downloading recordings.""" - - def test_unequal_signal_block_sizes(self): - """Parsed block starts must use the number of samples in preceding blocks.""" - samples = np.arange(350, dtype="int16") - header = np.zeros(1, dtype=GlobalHeader) - for field, value in { - "MagicNumber": 0x58454C50, - "Version": 106, - "ADFrequency": 1000, - "NumSlowChannels": 1, - "Year": 2020, - "Month": 1, - "Day": 1, - "BitsPerSpikeSample": 16, - "SlowMaxMagnitudeMV": 5000, - }.items(): - header[field] = value - channel = np.zeros(1, dtype=SlowChannelHeader) - channel["Name"] = b"AI1" - for field in ["Gain", "PreampGain", "Enabled"]: - channel[field] = 1 - channel["ADFreq"] = 1000 - - with TemporaryDirectory() as directory: - filename = Path(directory) / "unequal_blocks.plx" - with filename.open("wb") as stream: - header.tofile(stream) - channel.tofile(stream) - for start, stop in [(0, 100), (100, 300), (300, 350)]: - block = np.zeros(1, dtype=DataBlockHeader) - block["Type"] = 5 - block["TimeStamp"] = start - block["NumberOfWaveforms"] = 1 - block["NumberOfWordsInWaveform"] = stop - start - block.tofile(stream) - samples[start:stop].tofile(stream) - reader = PlexonRawIO(filename=str(filename), progress_bar=False) - try: - reader.parse_header() - np.testing.assert_array_equal(reader._data_blocks[5][0]["cumsum"], [0, 100, 300]) - chunk = reader.get_analogsignal_chunk(i_start=80, i_stop=280) - np.testing.assert_array_equal(chunk[:, 0], samples[80:280]) - finally: - del reader - - if __name__ == "__main__": unittest.main()