@@ -28,16 +28,33 @@ class NeuroScopeRawIO(BaseRawIO):
2828 extensions = ['xml' , 'dat' , 'lfp' , 'eeg' ]
2929 rawmode = 'one-file'
3030
31- def __init__ (self , filename = '' , binary_file = None ):
31+ def __init__ (self , filename , binary_file = None ):
3232 """raw reader for Neuroscope
3333
3434 Parameters
3535 ----------
36- filename : str, optional
37- Usually the file_path of an xml file
38- binary_file : _type_, optional
39- The binary data file corresponding to the xml file:
36+ filename : str, Path
37+ Usually the path of an xml file
38+ binary_file : str or Path optional
39+ The binary data file
4040 Supported formats: ['.dat', '.lfp', '.eeg']
41+
42+ Neuroscope format is composed of two files: a xml file with metadata and a binary file
43+ in either .dat, .lfp or .eeg format.
44+
45+ For backwards compatibility, we offer three ways of initializing the reader.
46+
47+ Cases:
48+ filename provided with .xml extension:
49+ - If binary_file is provided, it is used as the data file.
50+ - If binary_file is not provided, it tries to find a binary file with the same name and the
51+ supported extensions (.dat, .lfp, .eeg) in that order.
52+ filename provided with empty extension:
53+ - If binary_file is provided, it is used as the data file.
54+ - If binary_file is not provided, it tries to find a binary file with the same name and the
55+ supported extensions (.dat, .lfp, .eeg) in that order.
56+ filename provided with a supported data extension (.dat, .lfp, .eeg):
57+ - It assumes that the XML file has the same name and a .xml extension.
4158 """
4259 BaseRawIO .__init__ (self )
4360 self .filename = filename
@@ -139,41 +156,40 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop,
139156 return raw_signals
140157
141158 def _resolve_xml_and_data_paths (self ):
142- file_path = Path (self .filename )
143- supported_data_extensions = ['.dat' , '.lfp' , '.eeg' ]
144- suffix = file_path .suffix
145- if suffix == '.xml' :
146- xml_file_path = file_path
147- data_file_path = self .binary_file
148- # If no binary file provided iterate over the formats
149- if data_file_path is None :
150- for extension in supported_data_extensions :
151- data_file_path = file_path .with_suffix (extension )
152- if data_file_path .is_file ():
153- break
154- assert data_file_path .is_file (), "data binary not found for file " \
155- f"{ data_file_path } with supported extensions: { supported_data_extensions } "
156- elif suffix == '' :
157- xml_file_path = file_path .with_suffix (".xml" )
159+ """
160+ Resolves XML and data paths from the provided filename and binary_file attributes.
161+
162+ See the __init__ of the class for more a description of the conditions.
163+
164+ Using these conditions these function updates the self.xml_file_path and self.data_file_path attributes.
165+
166+ """
167+
168+ supported_extensions = ['.dat' , '.lfp' , '.eeg' ]
169+ self .filename = Path (self .filename )
170+ self .binary_file = Path (self .binary_file ) if self .binary_file is not None else None
171+
172+ if self .filename .suffix == '.xml' :
173+ xml_file_path = self .filename
174+ data_file_path = self .binary_file
175+ elif self .filename .suffix == '' :
176+ xml_file_path = self .filename .with_suffix (".xml" )
158177 data_file_path = self .binary_file
159- # Empty suffix to keep testing behavior with non-existing
160- # file passing for backwards compatibility
161- if data_file_path is None :
162- data_file_path = file_path .with_suffix (".dat" )
163- elif suffix in supported_data_extensions :
164- xml_file_path = file_path .with_suffix (".xml" )
165- data_file_path = file_path
178+ elif self .filename .suffix in supported_extensions :
179+ xml_file_path = self .filename .with_suffix (".xml" )
180+ data_file_path = self .filename
166181 else :
167- error_string = (
168- f"Format { suffix } not supported "
169- f"filename format should be { supported_data_extensions } or .xml"
170- )
171- raise KeyError (error_string )
182+ raise KeyError (f"Format { self .filename .suffix } not supported, filename format should be { supported_extensions } or .xml" )
183+
184+ if data_file_path is None :
185+ possible_file_paths = (xml_file_path .with_suffix (extension ) for extension in supported_extensions )
186+ data_file_path = next ((path for path in possible_file_paths if path .is_file ()), None )
187+ if data_file_path is None :
188+ raise FileNotFoundError (f"data binary not found for file { xml_file_path .stem } with supported extensions: { supported_extensions } " )
172189
173- msg = f"xml file not found at the expected location { xml_file_path } "
174- assert xml_file_path .is_file (), msg
175- msg = f"binary file not found at the expected location { data_file_path } "
176- assert data_file_path .is_file (), msg
190+
191+ assert xml_file_path .is_file (), f"xml file not found at the expected location { xml_file_path } "
192+ assert data_file_path .is_file (), f"binary file not found at the expected location { data_file_path } "
177193
178194 self .xml_file_path = xml_file_path
179- self .data_file_path = data_file_path
195+ self .data_file_path = data_file_path
0 commit comments