Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/spikeinterface/core/binaryrecordingextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ def get_traces(
end_frame: int | None = None,
channel_indices: list | None = None,
) -> np.ndarray:
if self.time_axis == 1:
return self._get_traces_channel_major(start_frame, end_frame, channel_indices)

# Calculate byte offsets for start and end frames
start_byte = self.file_offset + start_frame * self.bytes_per_sample
Expand All @@ -251,10 +253,7 @@ def get_traces(

# Create a numpy array using the mmap object as the buffer
# Note that the shape must be recalculated based on the new data chunk
if self.time_axis == 0:
shape = ((end_frame - start_frame), self.num_channels)
else:
shape = (self.num_channels, (end_frame - start_frame))
shape = ((end_frame - start_frame), self.num_channels)

# Now the entire array should correspond to the data between start_frame and end_frame, so we can use it directly
traces = np.ndarray(
Expand All @@ -264,14 +263,31 @@ def get_traces(
offset=start_offset,
)

if self.time_axis == 1:
traces = traces.T

if channel_indices is not None:
traces = traces[:, channel_indices]

return traces

def _get_traces_channel_major(self, start_frame, end_frame, channel_indices):
# With time_axis=1 each channel is stored contiguously, so a time window is one block per channel
# and not a single contiguous block: map each channel's block separately
num_frames = end_frame - start_frame
channel_indices = np.arange(self.num_channels)[slice(None) if channel_indices is None else channel_indices]
traces = np.empty((num_frames, channel_indices.size), dtype=self.dtype)
if num_frames == 0:
return traces

itemsize = self.dtype.itemsize
for i, channel_index in enumerate(channel_indices):
start_byte = self.file_offset + (channel_index * self.num_samples + start_frame) * itemsize
memmap_offset, start_offset = divmod(start_byte, mmap.ALLOCATIONGRANULARITY)
memmap_offset *= mmap.ALLOCATIONGRANULARITY
length = start_offset + num_frames * itemsize
memmap_obj = mmap.mmap(self.file.fileno(), length=length, access=mmap.ACCESS_READ, offset=memmap_offset)
traces[:, i] = np.frombuffer(memmap_obj, dtype=self.dtype, count=num_frames, offset=start_offset)

return traces

def __del__(self):
# Ensure that the file handle is closed when the segment is garbage-collected
try:
Expand Down
24 changes: 24 additions & 0 deletions src/spikeinterface/core/tests/test_binaryrecordingextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,30 @@ def test_sequential_reading_of_small_traces(folder_with_binary_files):
assert np.allclose(small_traces, expected_traces)


def test_partial_reading_with_time_axis_1(tmp_path):
# With time_axis=1 the file is channel-major, so a time window is not contiguous on disk
num_samples, num_channels = 100, 3
traces = np.arange(num_samples * num_channels, dtype="int16").reshape(num_samples, num_channels)
file_path = tmp_path / "channel_major.raw"
file_offset = 8
file_path.write_bytes(bytes(file_offset) + np.ascontiguousarray(traces.T).tobytes())

recording = BinaryRecordingExtractor(
file_paths=file_path,
sampling_frequency=10_000.0,
num_channels=num_channels,
dtype="int16",
time_axis=1,
file_offset=file_offset,
)

assert np.array_equal(recording.get_traces(start_frame=20, end_frame=40), traces[20:40])
channel_ids = recording.channel_ids[[2, 0]]
assert np.array_equal(
recording.get_traces(start_frame=20, end_frame=40, channel_ids=channel_ids), traces[20:40, [2, 0]]
)


def test_num_chan_backward_compatibility_within_same_version(tmp_path):
# A dict written by the same major.minor can still carry a kwarg that the current
# constructor no longer accepts, so the repair hook must run whatever version it records.
Expand Down
Loading