Skip to content

Commit 38340b3

Browse files
committed
delay memory access till event access on spikeglx reader
1 parent 01f5577 commit 38340b3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

neo/rawio/spikeglxrawio.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,14 @@ def _parse_header(self):
246246
chan_name = local_chan
247247
chan_id = f"{stream_name}#{chan_name}"
248248
event_channels.append((chan_name, chan_id, "event"))
249-
# add events_memmap
250-
data = np.memmap(info["bin_file"], dtype="int16", mode="r", offset=0, order="C")
251-
data = data.reshape(-1, info["num_chan"])
252-
# The digital word is usually the last channel, after all the individual analog channels
253-
extracted_word = data[:, len(info["analog_channels"])]
254-
self._events_memmap = np.unpackbits(extracted_word.astype(np.uint8)[:, None], axis=1)
249+
# Create memmap for digital word but defer unpacking until needed
250+
# The digital word is stored as the last channel, after all the individual analog channels
251+
# For example: if there are 8 analog channels (indices 0-7), the digital word is at index 8
252+
num_samples = info["sample_length"]
253+
num_chan = info["num_chan"]
254+
data = np.memmap(info["bin_file"], dtype="int16", mode="r", shape=(num_samples, num_chan), order="C")
255+
digital_word_channel_index = len(info["analog_channels"])
256+
self._events_memmap_digital_word = data[:, digital_word_channel_index]
255257
event_channels = np.array(event_channels, dtype=_event_channel_dtype)
256258

257259
# No spikes
@@ -342,7 +344,9 @@ def _get_event_timestamps(self, block_index, seg_index, event_channel_index, t_s
342344
info = self.signals_info_dict[0, "nidq"] # There are no events that are not in the nidq stream
343345
dig_ch = info["digital_channels"]
344346
if len(dig_ch) > 0:
345-
event_data = self._events_memmap
347+
# Unpack bits on-demand - this is when memory allocation happens
348+
event_data = np.unpackbits(self._events_memmap_digital_word.astype(np.uint8)[:, None], axis=1)
349+
346350
channel = dig_ch[event_channel_index]
347351
ch_idx = 7 - int(channel[2:]) # They are in the reverse order
348352
this_stream = event_data[:, ch_idx]

0 commit comments

Comments
 (0)