Skip to content

Commit 479d276

Browse files
authored
Merge pull request #1948 from JamesBelanger/blackrock-nsx21-last-sample
BlackrockRawIO: read the last sample of NSx 2.1 files
2 parents 50a3e59 + cbbda7a commit 479d276

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

‎neo/rawio/blackrockrawio.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ def _parse_nsx_data_v21(self, nsx_nb):
13511351
self._nsx_basic_header[nsx_nb].dtype.itemsize + self._nsx_ext_header[nsx_nb].dtype.itemsize * channels
13521352
)
13531353
filesize = self._get_file_size(filename)
1354-
num_samples = int((filesize - bytes_in_headers) / (2 * channels) - 1)
1354+
num_samples = (filesize - bytes_in_headers) // (2 * channels)
13551355

13561356
return [
13571357
{

‎neo/test/iotest/test_blackrockio.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ def test_load_muliple_nsx(self):
179179
reader = BlackrockIO(filename=filename, nsx_to_load=5)
180180
seg = reader.read_segment()
181181
self.assertEqual(len(seg.analogsignals), 1)
182-
self.assertEqual(seg.analogsignals[0].shape, (109224, 96))
182+
self.assertEqual(seg.analogsignals[0].shape, (109225, 96))
183183

184184
# load only ns2
185185
reader = BlackrockIO(filename=filename, nsx_to_load=2)
186186
seg = reader.read_segment()
187187
self.assertEqual(len(seg.analogsignals), 1)
188-
self.assertEqual(seg.analogsignals[0].shape, (3640, 6))
188+
self.assertEqual(seg.analogsignals[0].shape, (3641, 6))
189189

190190
# load only ns2
191191
reader = BlackrockIO(filename=filename, nsx_to_load=[2])
@@ -196,14 +196,14 @@ def test_load_muliple_nsx(self):
196196
reader = BlackrockIO(filename=filename, nsx_to_load=[2, 5])
197197
seg = reader.read_segment()
198198
self.assertEqual(len(seg.analogsignals), 2)
199-
self.assertEqual(seg.analogsignals[0].shape, (3640, 6))
200-
self.assertEqual(seg.analogsignals[1].shape, (109224, 96))
199+
self.assertEqual(seg.analogsignals[0].shape, (3641, 6))
200+
self.assertEqual(seg.analogsignals[1].shape, (109225, 96))
201201

202202
# load only ns5
203203
reader = BlackrockIO(filename=filename, nsx_to_load="max")
204204
seg = reader.read_segment()
205205
self.assertEqual(len(seg.analogsignals), 1)
206-
self.assertEqual(seg.analogsignals[0].shape, (109224, 96))
206+
self.assertEqual(seg.analogsignals[0].shape, (109225, 96))
207207

208208
@unittest.skipUnless(HAVE_SCIPY, "requires scipy")
209209
def test_compare_blackrockio_with_matlabloader_v21(self):

‎neo/test/rawiotest/test_blackrockrawio.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ def test_compare_blackrockio_with_matlabloader_v21(self):
151151
for c in range(0, param[2]):
152152
raw_sigs = reader.get_analogsignal_chunk(channel_indexes=[c])
153153
raw_sigs = raw_sigs.flatten()
154-
assert_equal(raw_sigs[:], lfp_ml[c, :])
154+
# The Matlab export stops one sample before the end of the file
155+
# (see test_nsx21_reads_the_last_sample)
156+
self.assertEqual(raw_sigs.size, lfp_ml.shape[1] + 1)
157+
assert_equal(raw_sigs[: lfp_ml.shape[1]], lfp_ml[c, :])
155158

156159
# Check if spikes in channels are equal
157160
nb_unit = reader.spike_channels_count()
@@ -187,6 +190,21 @@ def test_compare_blackrockio_with_matlabloader_v21(self):
187190
matlab_digievents = mts_ml[mid_ml == int(label)]
188191
assert_equal(python_digievents, matlab_digievents)
189192

193+
def test_nsx21_reads_the_last_sample(self):
194+
filename = self.get_local_path("blackrock/version_2_1/spikes/offline_sorted_units/online_sorted_units")
195+
196+
reader = BlackrockRawIO(filename=filename, nsx_to_load=2)
197+
reader.parse_header()
198+
number_of_samples = reader.get_signal_size(block_index=0, seg_index=0, stream_index=0)
199+
expected_number_of_samples = 300 # 0.3 s at 1 kHz
200+
self.assertEqual(number_of_samples, expected_number_of_samples)
201+
202+
reader = BlackrockRawIO(filename=filename, nsx_to_load=5)
203+
reader.parse_header()
204+
number_of_samples = reader.get_signal_size(block_index=0, seg_index=0, stream_index=0)
205+
expected_number_of_samples = 9000 # 0.3 s at 30 kHz
206+
self.assertEqual(number_of_samples, expected_number_of_samples)
207+
190208
def test_blackrockrawio_ptp_timestamps(self):
191209
dirname = self.get_local_path("blackrock/blackrock_3_0_ptp/20231027-125608-001")
192210
reader = BlackrockRawIO(filename=dirname)

0 commit comments

Comments
 (0)