|
1 | 1 | from typing import List, Union |
| 2 | +import mmap |
2 | 3 |
|
3 | 4 | import shutil |
4 | 5 | from pathlib import Path |
@@ -156,25 +157,58 @@ def get_binary_description(self): |
156 | 157 | class BinaryRecordingSegment(BaseRecordingSegment): |
157 | 158 | def __init__(self, datfile, sampling_frequency, t_start, num_chan, dtype, time_axis, file_offset): |
158 | 159 | BaseRecordingSegment.__init__(self, sampling_frequency=sampling_frequency, t_start=t_start) |
159 | | - self._timeseries = read_binary_recording(datfile, num_chan, dtype, time_axis, file_offset) |
| 160 | + self.num_chan = num_chan |
| 161 | + self.dtype = np.dtype(dtype) |
| 162 | + self.file_offset = file_offset |
| 163 | + self.time_axis = time_axis |
| 164 | + self.datfile = datfile |
| 165 | + self.file = open(self.datfile, "r") |
| 166 | + self.num_samples = (Path(datfile).stat().st_size - file_offset) // (num_chan * np.dtype(dtype).itemsize) |
| 167 | + if self.time_axis == 0: |
| 168 | + self.shape = (self.num_samples, self.num_chan) |
| 169 | + else: |
| 170 | + self.shape = (self.num_chan, self.num_samples) |
| 171 | + |
| 172 | + byte_offset = self.file_offset |
| 173 | + dtype_size_bytes = self.dtype.itemsize |
| 174 | + data_size_bytes = dtype_size_bytes * self.num_samples * self.num_chan |
| 175 | + self.memmap_offset, self.array_offset = divmod(byte_offset, mmap.ALLOCATIONGRANULARITY) |
| 176 | + self.memmap_length = data_size_bytes + self.array_offset |
160 | 177 |
|
161 | 178 | def get_num_samples(self) -> int: |
162 | 179 | """Returns the number of samples in this signal block |
163 | 180 |
|
164 | 181 | Returns: |
165 | 182 | SampleIndex: Number of samples in the signal block |
166 | 183 | """ |
167 | | - return self._timeseries.shape[0] |
| 184 | + return self.num_samples |
168 | 185 |
|
169 | 186 | def get_traces( |
170 | 187 | self, |
171 | 188 | start_frame: Union[int, None] = None, |
172 | 189 | end_frame: Union[int, None] = None, |
173 | 190 | channel_indices: Union[List, None] = None, |
174 | 191 | ) -> np.ndarray: |
175 | | - traces = self._timeseries[start_frame:end_frame] |
| 192 | + length = self.memmap_length |
| 193 | + memmap_offset = self.memmap_offset |
| 194 | + memmap_obj = mmap.mmap(self.file.fileno(), length=length, access=mmap.ACCESS_READ, offset=memmap_offset) |
| 195 | + |
| 196 | + array = np.ndarray.__new__( |
| 197 | + np.ndarray, |
| 198 | + shape=self.shape, |
| 199 | + dtype=self.dtype, |
| 200 | + buffer=memmap_obj, |
| 201 | + order="C", |
| 202 | + offset=self.array_offset, |
| 203 | + ) |
| 204 | + |
| 205 | + if self.time_axis == 1: |
| 206 | + array = array.T |
| 207 | + |
| 208 | + traces = array[start_frame:end_frame] |
176 | 209 | if channel_indices is not None: |
177 | 210 | traces = traces[:, channel_indices] |
| 211 | + |
178 | 212 | return traces |
179 | 213 |
|
180 | 214 |
|
|
0 commit comments