Skip to content

Commit 3a6d64d

Browse files
committed
Minimal fix of SGRawIO channel id
1 parent 1a7f60e commit 3a6d64d

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

neo/rawio/spikegadgetsrawio.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ def __init__(self, filename="", selected_streams=None):
7979
def _source_name(self):
8080
return self.filename
8181

82+
def _produce_ephys_channel_ids(self, n_total_channels, n_channels_per_chip):
83+
"""Compute the channel ID labels
84+
The ephys channels in the .rec file are stored in the following order:
85+
hwChan ID of channel 0 of first chip, hwChan ID of channel 0 of second chip, ..., hwChan ID of channel 0 of Nth chip,
86+
hwChan ID of channel 1 of first chip, hwChan ID of channel 1 of second chip, ..., hwChan ID of channel 1 of Nth chip,
87+
...
88+
So if there are 32 channels per chip and 128 channels (4 chips), then the channel IDs are:
89+
0, 32, 64, 96, 1, 33, 65, 97, ..., 128
90+
See also: https://github.com/NeuralEnsemble/python-neo/issues/1215
91+
"""
92+
x = []
93+
for k in range(n_channels_per_chip):
94+
x.append(
95+
[
96+
k + i * n_channels_per_chip
97+
for i in range(int(n_total_channels / n_channels_per_chip))
98+
]
99+
)
100+
return [item for sublist in x for item in sublist]
101+
82102
def _parse_header(self):
83103
# parse file until "</Configuration>"
84104
header_size = None
@@ -103,7 +123,21 @@ def _parse_header(self):
103123

104124
self._sampling_rate = float(hconf.attrib["samplingRate"])
105125
num_ephy_channels = int(hconf.attrib["numChannels"])
106-
126+
127+
# check for agreement with number of channels in xml
128+
sconf_channels = np.sum([len(x) for x in sconf])
129+
if sconf_channels < num_ephy_channels:
130+
num_ephy_channels = sconf_channels
131+
if sconf_channels > num_ephy_channels:
132+
raise ValueError(
133+
"SpikeGadgets: the number of channels in the spike configuration is larger than the number of channels in the hardware configuration"
134+
)
135+
136+
try:
137+
num_chan_per_chip = int(sconf.attrib["chanPerChip"])
138+
except KeyError:
139+
num_chan_per_chip = 32 # default value for Intan chips
140+
107141
# explore sub stream and count packet size
108142
# first bytes is 0x55
109143
packet_size = 1
@@ -174,6 +208,9 @@ def _parse_header(self):
174208
signal_streams.append((stream_name, stream_id))
175209
self._mask_channels_bytes[stream_id] = []
176210

211+
channel_ids = self._produce_ephys_channel_ids(
212+
num_ephy_channels, num_chan_per_chip
213+
)
177214
chan_ind = 0
178215
self.is_scaleable = "spikeScalingToUv" in sconf[0].attrib
179216
if not self.is_scaleable:
@@ -190,8 +227,8 @@ def _parse_header(self):
190227
units = ""
191228

192229
for schan in trode:
193-
name = "trode" + trode.attrib["id"] + "chan" + schan.attrib["hwChan"]
194-
chan_id = schan.attrib["hwChan"]
230+
chan_id = str(channel_ids[chan_ind])
231+
name = "hwChan" + chan_id
195232

196233
offset = 0.0
197234
signal_channels.append(

0 commit comments

Comments
 (0)