Repository navigation
Description
Describe the bug
For NSx 2.1 files (NEURALSG), _parse_nsx_data_v21 computes
num_samples = int((filesize - bytes_in_headers) / (2 * channels) - 1)The - 1 drops the final sample every time, even when the data section holds a whole number of samples.
To Reproduce
With blackrock/blackrock_2_1/ from ephy_testing_data:
import os
from neo.rawio import BlackrockRawIO
stem = "blackrock/blackrock_2_1/l101210-001"
io = BlackrockRawIO(filename=stem)
io.parse_header()
for i, stream in enumerate(io.header["signal_streams"]):
nb = int(stream["name"][-1])
n_ch = io.signal_channels_count(i)
# NSx 2.1: 32-byte header + 4 bytes per channel ID, then int16 samples
in_file = (os.path.getsize(f"{stem}.ns{nb}") - (32 + 4 * n_ch)) / (2 * n_ch)
print(f"{stream['name']}: neo reads {io.get_signal_size(0, 0, i)} samples, file holds {in_file}")nsx2: neo reads 3640 samples, file holds 3641.0
nsx5: neo reads 109224 samples, file holds 109225.0
The 0.3 s stub version_2_1/spikes/offline_sorted_units/online_sorted_units shows the same thing: Neo reads 8,999 (ns5) and 299 (ns2) samples, where the test-data README lists 9,000 and 300.
Expected behaviour
Neo reads every complete sample: 109,225 and 3,641 here. The dropped sample looks like ordinary signal (no zeros, and its step from the previous sample is in line with the rest of the file). Blackrock's NPMK reads it too: openNSx.m computes DataPoints = double(f.EOF-f.EOexH)/(channelCount*2), with no - 1.
Suggested fix:
num_samples = (filesize - bytes_in_headers) // (2 * channels)One thing to watch: the MATLAB reference exports in blackrock_2_1/ also stop one sample short (l101210-001_nev-02_ns5.mat is 96 x 109,224, and the ns2 array in l101210-001.mat has 3,640 samples). A test that compares full lengths against them would need to compare the first N samples instead.
Environment:
- OS: Windows 11
- Python 3.12.13
- Neo 0.14.6 (the line is unchanged on master)
- NumPy 2.5.4
Additional context
I found this while converting this recording to NWB with NeuroConv and checking the output sample for sample against a small reader written from Blackrock's file spec: https://github.com/JamesBelanger/blackrock-nwb-roundtrip. I can open a PR with the fix if that's useful.
Activity
BlackrockRawIO: read the last sample of NSx 2.1 files