Skip to content

Commit 696c994

Browse files
authored
Merge branch 'master' into neuronexus
2 parents 1b72c73 + b0d171f commit 696c994

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed

neo/rawio/blackrockrawio.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,17 @@ def _parse_header(self):
597597
for c in range(spike_channels.size):
598598
st_ann = seg_ann["spikes"][c]
599599
channel_id, unit_id = self.internal_unit_ids[c]
600-
unit_tag = {0: "unclassified", 255: "noise"}.get(unit_id, str(unit_id))
601600
st_ann["channel_id"] = channel_id
602601
st_ann["unit_id"] = unit_id
603-
st_ann["unit_tag"] = unit_tag
602+
if unit_id == 0:
603+
st_ann["unit_classification"] = "unclassified"
604+
elif 1 <= unit_id <= 16:
605+
st_ann["unit_classification"] = "sorted"
606+
elif unit_id == 255:
607+
st_ann["unit_classification"] = "noise"
608+
else: # 17-254 are reserved
609+
st_ann["unit_classification"] = "reserved"
610+
st_ann["unit_tag"] = st_ann["unit_classification"]
604611
st_ann["description"] = f"SpikeTrain channel_id: {channel_id}, unit_id: {unit_id}"
605612
st_ann["file_origin"] = self._filenames["nev"] + ".nev"
606613

@@ -1258,9 +1265,7 @@ def __read_nev_data(self, nev_data_masks, nev_data_types):
12581265
# based on blackrock's own code this is okay so applying an int to round down is necessary to obtain the
12591266
# memory map of full packets and toss the partial packet.
12601267
# See reference: https://github.com/BlackrockNeurotech/Python-Utilities/blob/fa75aa671680306788e10d3d8dd625f9da4ea4f6/brpylib/brpylib.py#L580-L587
1261-
n_packets = int(
1262-
(self.__get_file_size(filename) - header_size) / data_size
1263-
)
1268+
n_packets = int((self.__get_file_size(filename) - header_size) / data_size)
12641269

12651270
raw_data = np.memmap(
12661271
filename,

neo/rawio/spikeglxrawio.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from pathlib import Path
5454
import os
5555
import re
56+
from warnings import warn
5657

5758
import numpy as np
5859

@@ -76,7 +77,9 @@ class SpikeGLXRawIO(BaseRawWithBufferApiIO):
7677
dirname: str, default: ''
7778
The spikeglx folder containing meta/bin files
7879
load_sync_channel: bool, default: False
79-
The last channel (SY0) of each stream is a fake channel used for synchronisation
80+
Can be used to load the synch stream as the last channel of the neural data.
81+
This option is deprecated and will be removed in version 0.15.
82+
From versions higher than 0.14.1 the sync channel is always loaded as a separate stream.
8083
load_channel_location: bool, default: False
8184
If True probeinterface is used to load the channel locations from the directory
8285
@@ -109,6 +112,13 @@ def __init__(self, dirname="", load_sync_channel=False, load_channel_location=Fa
109112
BaseRawWithBufferApiIO.__init__(self)
110113
self.dirname = dirname
111114
self.load_sync_channel = load_sync_channel
115+
if load_sync_channel:
116+
warn(
117+
"The load_sync_channel=True option is deprecated and will be removed in version 0.15 \n"
118+
"The sync channel is now loaded as a separate stream by default and should be accessed as such. ",
119+
DeprecationWarning,
120+
stacklevel=2,
121+
)
112122
self.load_channel_location = load_channel_location
113123

114124
def _source_name(self):
@@ -152,6 +162,8 @@ def _parse_header(self):
152162
signal_buffers = []
153163
signal_streams = []
154164
signal_channels = []
165+
sync_stream_id_to_buffer_id = {}
166+
155167
for stream_name in stream_names:
156168
# take first segment
157169
info = self.signals_info_dict[0, stream_name]
@@ -168,6 +180,21 @@ def _parse_header(self):
168180
for local_chan in range(info["num_chan"]):
169181
chan_name = info["channel_names"][local_chan]
170182
chan_id = f"{stream_name}#{chan_name}"
183+
184+
# Sync channel
185+
if (
186+
"nidq" not in stream_name
187+
and "SY0" in chan_name
188+
and not self.load_sync_channel
189+
and local_chan == info["num_chan"] - 1
190+
):
191+
# This is a sync channel and should be added as its own stream
192+
sync_stream_id = f"{stream_name}-SYNC"
193+
sync_stream_id_to_buffer_id[sync_stream_id] = buffer_id
194+
stream_id_for_chan = sync_stream_id
195+
else:
196+
stream_id_for_chan = stream_id
197+
171198
signal_channels.append(
172199
(
173200
chan_name,
@@ -177,25 +204,33 @@ def _parse_header(self):
177204
info["units"],
178205
info["channel_gains"][local_chan],
179206
info["channel_offsets"][local_chan],
180-
stream_id,
207+
stream_id_for_chan,
181208
buffer_id,
182209
)
183210
)
184211

185-
# all channel by dafult unless load_sync_channel=False
212+
# all channel by default unless load_sync_channel=False
186213
self._stream_buffer_slice[stream_id] = None
214+
187215
# check sync channel validity
188216
if "nidq" not in stream_name:
189217
if not self.load_sync_channel and info["has_sync_trace"]:
190-
# the last channel is remove from the stream but not from the buffer
191-
last_chan = signal_channels[-1]
192-
last_chan = last_chan[:-2] + ("", buffer_id)
193-
signal_channels = signal_channels[:-1] + [last_chan]
218+
# the last channel is removed from the stream but not from the buffer
194219
self._stream_buffer_slice[stream_id] = slice(0, -1)
220+
221+
# Add a buffer slice for the sync channel
222+
sync_stream_id = f"{stream_name}-SYNC"
223+
self._stream_buffer_slice[sync_stream_id] = slice(-1, None)
224+
195225
if self.load_sync_channel and not info["has_sync_trace"]:
196226
raise ValueError("SYNC channel is not present in the recording. " "Set load_sync_channel to False")
197227

198228
signal_buffers = np.array(signal_buffers, dtype=_signal_buffer_dtype)
229+
230+
# Add sync channels as their own streams
231+
for sync_stream_id, buffer_id in sync_stream_id_to_buffer_id.items():
232+
signal_streams.append((sync_stream_id, sync_stream_id, buffer_id))
233+
199234
signal_streams = np.array(signal_streams, dtype=_signal_stream_dtype)
200235
signal_channels = np.array(signal_channels, dtype=_signal_channel_dtype)
201236

@@ -237,6 +272,14 @@ def _parse_header(self):
237272
t_start = frame_start / sampling_frequency
238273

239274
self._t_starts[stream_name][seg_index] = t_start
275+
276+
# This need special logic because sync not present in stream_names
277+
if f"{stream_name}-SYNC" in signal_streams["name"]:
278+
sync_stream_name = f"{stream_name}-SYNC"
279+
if sync_stream_name not in self._t_starts:
280+
self._t_starts[sync_stream_name] = {}
281+
self._t_starts[sync_stream_name][seg_index] = t_start
282+
240283
t_stop = info["sample_length"] / info["sampling_rate"]
241284
self._t_stops[seg_index] = max(self._t_stops[seg_index], t_stop)
242285

@@ -266,6 +309,10 @@ def _parse_header(self):
266309
# need probeinterface to be installed
267310
import probeinterface
268311

312+
# Skip for sync streams
313+
if "SYNC" in stream_name:
314+
continue
315+
269316
info = self.signals_info_dict[seg_index, stream_name]
270317
if "imroTbl" in info["meta"] and info["stream_kind"] == "ap":
271318
# only for ap channel
@@ -529,7 +576,7 @@ def extract_stream_info(meta_file, meta):
529576
if (
530577
"imDatPrb_type" not in meta
531578
or meta["imDatPrb_type"] == "0"
532-
or meta["imDatPrb_type"] in ("1015", "1016", "1022", "1030", "1031", "1032", "1100", "1121", "1300")
579+
or meta["imDatPrb_type"] in ("1015", "1016", "1022", "1030", "1031", "1032", "1100", "1121", "1123","1300")
533580
):
534581
# This work with NP 1.0 case with different metadata versions
535582
# https://github.com/billkarsh/SpikeGLX/blob/15ec8898e17829f9f08c226bf04f46281f106e5f/Markdown/Metadata_30.md

neo/test/rawiotest/test_spikeglxrawio.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_loading_only_one_probe_in_multi_probe_scenario(self):
5555
rawio = SpikeGLXRawIO(probe_folder_path)
5656
rawio.parse_header()
5757

58-
expected_stream_names = ["imec1.ap", "imec1.lf"]
58+
expected_stream_names = ["imec1.ap", "imec1.lf", "imec1.ap-SYNC", "imec1.lf-SYNC"]
5959
actual_stream_names = rawio.header["signal_streams"]["name"].tolist()
6060
assert (
6161
actual_stream_names == expected_stream_names
@@ -130,6 +130,34 @@ def test_nidq_digital_channel(self):
130130
atol = 0.001
131131
assert np.allclose(on_diff, 1, atol=atol)
132132

133+
def test_sync_channel_as_separate_stream(self):
134+
"""Test that sync channel is added as its own stream when load_sync_channel=False."""
135+
import warnings
136+
137+
# Test with load_sync_channel=False (default)
138+
rawio_no_sync = SpikeGLXRawIO(self.get_local_path("spikeglx/NP2_with_sync"), load_sync_channel=False)
139+
rawio_no_sync.parse_header()
140+
141+
# Get stream names
142+
stream_names = rawio_no_sync.header["signal_streams"]["name"].tolist()
143+
144+
# Check if there's a sync channel stream (should contain "SY0" or "SYNC" in the name)
145+
sync_streams = [name for name in stream_names if "SY0" in name or "SYNC" in name]
146+
assert len(sync_streams) > 0, "No sync channel stream found when load_sync_channel=False"
147+
148+
# Test deprecation warning when load_sync_channel=True
149+
with warnings.catch_warnings(record=True) as w:
150+
warnings.simplefilter("always")
151+
rawio_with_sync = SpikeGLXRawIO(self.get_local_path("spikeglx/NP2_with_sync"), load_sync_channel=True)
152+
153+
# Check if deprecation warning was raised
154+
assert any(
155+
issubclass(warning.category, DeprecationWarning) for warning in w
156+
), "No deprecation warning raised"
157+
assert any(
158+
"will be removed in version 0.15" in str(warning.message) for warning in w
159+
), "Deprecation warning message is incorrect"
160+
133161
def test_t_start_reading(self):
134162
"""Test that t_start values are correctly read for all streams and segments."""
135163

0 commit comments

Comments
 (0)