11from typing import List , Union
22import mmap
3-
4- import shutil
3+ import warnings
54from pathlib import Path
65
76import numpy as np
87
98from .baserecording import BaseRecording , BaseRecordingSegment
10- from .core_tools import read_binary_recording , write_binary_recording , define_function_from_class
9+ from .core_tools import write_binary_recording , define_function_from_class
1110from .job_tools import _shared_job_kwargs_doc
1211
1312
@@ -21,7 +20,9 @@ class BinaryRecordingExtractor(BaseRecording):
2120 Path to the binary file
2221 sampling_frequency: float
2322 The sampling frequency
24- num_chan: int
23+ num_channels: int
24+ Number of channels
25+ num_chan: int [deprecated, use num_channels instead, will be removed as early as v0.100.0]
2526 Number of channels
2627 dtype: str or dtype
2728 The dtype of the binary file
@@ -40,6 +41,10 @@ class BinaryRecordingExtractor(BaseRecording):
4041 is_filtered: bool or None
4142 If True, the recording is assumed to be filtered. If None, is_filtered is not set.
4243
44+ Notes
45+ -----
46+ When both num_channels and num_chan are provided, `num_channels` is used and `num_chan` is ignored.
47+
4348 Returns
4449 -------
4550 recording: BinaryRecordingExtractor
@@ -55,43 +60,50 @@ def __init__(
5560 self ,
5661 file_paths ,
5762 sampling_frequency ,
58- num_chan ,
5963 dtype ,
64+ num_channels = None ,
6065 t_starts = None ,
6166 channel_ids = None ,
6267 time_axis = 0 ,
6368 file_offset = 0 ,
6469 gain_to_uV = None ,
6570 offset_to_uV = None ,
6671 is_filtered = None ,
72+ num_chan = None ,
6773 ):
74+ # This assigns num_channels if num_channels is not None, otherwise num_chan is assigned
75+ num_channels = num_channels or num_chan
76+ assert num_channels is not None , "You must provide num_channels or num_chan"
77+ if num_chan is not None :
78+ warnings .warn ("`num_chan` is to be deprecated in version 0.100, please use `num_channels` instead" )
79+
6880 if channel_ids is None :
69- channel_ids = list (range (num_chan ))
81+ channel_ids = list (range (num_channels ))
7082 else :
71- assert len (channel_ids ) == num_chan , "Provided recording channels have the wrong length"
83+ assert len (channel_ids ) == num_channels , "Provided recording channels have the wrong length"
7284
7385 BaseRecording .__init__ (self , sampling_frequency , channel_ids , dtype )
7486
7587 if isinstance (file_paths , list ):
7688 # several segment
77- datfiles = [Path (p ) for p in file_paths ]
89+ file_path_list = [Path (p ) for p in file_paths ]
7890 else :
7991 # one segment
80- datfiles = [Path (file_paths )]
92+ file_path_list = [Path (file_paths )]
8193
8294 if t_starts is not None :
83- assert len (t_starts ) == len (datfiles ), "t_starts must be a list of same size than file_paths"
95+ assert len (t_starts ) == len (file_path_list ), "t_starts must be a list of same size than file_paths"
8496 t_starts = [float (t_start ) for t_start in t_starts ]
8597
8698 dtype = np .dtype (dtype )
8799
88- for i , datfile in enumerate (datfiles ):
100+ for i , file_path in enumerate (file_path_list ):
89101 if t_starts is None :
90102 t_start = None
91103 else :
92104 t_start = t_starts [i ]
93105 rec_segment = BinaryRecordingSegment (
94- datfile , sampling_frequency , t_start , num_chan , dtype , time_axis , file_offset
106+ file_path , sampling_frequency , t_start , num_channels , dtype , time_axis , file_offset
95107 )
96108 self .add_recording_segment (rec_segment )
97109
@@ -105,10 +117,11 @@ def __init__(
105117 self .set_channel_offsets (offset_to_uV )
106118
107119 self ._kwargs = {
108- "file_paths" : [str (e .absolute ()) for e in datfiles ],
120+ "file_paths" : [str (e .absolute ()) for e in file_path_list ],
109121 "sampling_frequency" : sampling_frequency ,
110122 "t_starts" : t_starts ,
111- "num_chan" : num_chan ,
123+ "num_channels" : num_channels ,
124+ "num_chan" : num_channels , # TODO: This should be here at least till version 0.100.0
112125 "dtype" : dtype .str ,
113126 "channel_ids" : channel_ids ,
114127 "time_axis" : time_axis ,
@@ -142,7 +155,7 @@ def get_binary_description(self):
142155 d = dict (
143156 file_paths = self ._kwargs ["file_paths" ],
144157 dtype = np .dtype (self ._kwargs ["dtype" ]),
145- num_channels = self ._kwargs ["num_chan " ],
158+ num_channels = self ._kwargs ["num_channels " ],
146159 time_axis = self ._kwargs ["time_axis" ],
147160 file_offset = self ._kwargs ["file_offset" ],
148161 )
@@ -155,23 +168,23 @@ def get_binary_description(self):
155168
156169
157170class BinaryRecordingSegment (BaseRecordingSegment ):
158- def __init__ (self , datfile , sampling_frequency , t_start , num_chan , dtype , time_axis , file_offset ):
171+ def __init__ (self , datfile , sampling_frequency , t_start , num_channels , dtype , time_axis , file_offset ):
159172 BaseRecordingSegment .__init__ (self , sampling_frequency = sampling_frequency , t_start = t_start )
160- self .num_chan = num_chan
173+ self .num_channels = num_channels
161174 self .dtype = np .dtype (dtype )
162175 self .file_offset = file_offset
163176 self .time_axis = time_axis
164177 self .datfile = datfile
165178 self .file = open (self .datfile , "r" )
166- self .num_samples = (Path (datfile ).stat ().st_size - file_offset ) // (num_chan * np .dtype (dtype ).itemsize )
179+ self .num_samples = (Path (datfile ).stat ().st_size - file_offset ) // (num_channels * np .dtype (dtype ).itemsize )
167180 if self .time_axis == 0 :
168- self .shape = (self .num_samples , self .num_chan )
181+ self .shape = (self .num_samples , self .num_channels )
169182 else :
170- self .shape = (self .num_chan , self .num_samples )
183+ self .shape = (self .num_channels , self .num_samples )
171184
172185 byte_offset = self .file_offset
173186 dtype_size_bytes = self .dtype .itemsize
174- data_size_bytes = dtype_size_bytes * self .num_samples * self .num_chan
187+ data_size_bytes = dtype_size_bytes * self .num_samples * self .num_channels
175188 self .memmap_offset , self .array_offset = divmod (byte_offset , mmap .ALLOCATIONGRANULARITY )
176189 self .memmap_length = data_size_bytes + self .array_offset
177190
0 commit comments