Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved file-open operation in BinaryRecordingSegment out of constructor #3296

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 22 additions & 22 deletions src/spikeinterface/core/binaryrecordingextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def __init__(self, file_path, sampling_frequency, t_start, num_channels, dtype,
self.file_offset = file_offset
self.time_axis = time_axis
self.file_path = file_path
self.file = open(self.file_path, "rb")
self.bytes_per_sample = self.num_channels * self.dtype.itemsize
self.data_size_in_bytes = Path(file_path).stat().st_size - file_offset
self.num_samples = self.data_size_in_bytes // self.bytes_per_sample
Expand Down Expand Up @@ -205,31 +204,32 @@ def get_traces(
# the memmap offset to a multiple of ALLOCATIONGRANULARITY
length += start_offset

# Create the mmap object
memmap_obj = mmap.mmap(self.file.fileno(), length=length, access=mmap.ACCESS_READ, offset=memmap_offset)
with open(self.file_path, "rb") as file:
# Create the mmap object
memmap_obj = mmap.mmap(file.fileno(), length=length, access=mmap.ACCESS_READ, offset=memmap_offset)

# 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))

# Now the entire array should correspond to the data between start_frame and end_frame, so we can use it directly
traces = np.ndarray(
shape=shape,
dtype=self.dtype,
buffer=memmap_obj,
offset=start_offset,
)
# 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))

# Now the entire array should correspond to the data between start_frame and end_frame, so we can use it directly
traces = np.ndarray(
shape=shape,
dtype=self.dtype,
buffer=memmap_obj,
offset=start_offset,
)

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

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

return traces
return traces


# For backward compatibility (old good time)
Expand Down