From 1414e6264d532e6e2d263b853c10a441f48ee16a Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 8 Aug 2024 16:39:04 -0500 Subject: [PATCH 1/2] Move file open from BinaryRecordingSegment constructor to get_traces() method, and uses with statment so that the file will close after it's no longer needed --- .../core/binaryrecordingextractor.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/spikeinterface/core/binaryrecordingextractor.py b/src/spikeinterface/core/binaryrecordingextractor.py index a0e349728e..c35971d11f 100644 --- a/src/spikeinterface/core/binaryrecordingextractor.py +++ b/src/spikeinterface/core/binaryrecordingextractor.py @@ -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 @@ -205,29 +204,30 @@ 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 From 18f6dc0e383ad4b8d5948bf1f85da96afe22ba6d Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 8 Aug 2024 19:06:06 -0500 Subject: [PATCH 2/2] Move return within open statement block --- src/spikeinterface/core/binaryrecordingextractor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spikeinterface/core/binaryrecordingextractor.py b/src/spikeinterface/core/binaryrecordingextractor.py index c35971d11f..27fd169c0e 100644 --- a/src/spikeinterface/core/binaryrecordingextractor.py +++ b/src/spikeinterface/core/binaryrecordingextractor.py @@ -229,7 +229,7 @@ def get_traces( if channel_indices is not None: traces = traces[:, channel_indices] - return traces + return traces # For backward compatibility (old good time)