Skip to content

Commit 92a5086

Browse files
committed
add description of loading cases to docstring in Neuroscope raw io
1 parent 727207e commit 92a5086

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

neo/rawio/neuroscoperawio.py

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,37 @@ 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)
43-
self.filename = filename
44-
self.binary_file = binary_file
60+
self.filename = Path(filename)
61+
self.binary_file = Path(binary_file) if binary_file is not None else None
4562

4663
def _source_name(self):
4764
return Path(self.filename).stem
@@ -139,41 +156,38 @@ 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+
170+
if self.filename.suffix == '.xml':
171+
xml_file_path = self.filename
172+
data_file_path = self.binary_file
173+
elif self.filename.suffix == '':
174+
xml_file_path = self.filename.with_suffix(".xml")
158175
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
176+
elif self.filename.suffix in supported_extensions:
177+
xml_file_path = self.filename.with_suffix(".xml")
178+
data_file_path = self.filename
166179
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)
180+
raise KeyError(f"Format {self.filename.suffix} not supported, filename format should be {supported_extensions} or .xml")
181+
182+
if data_file_path is None:
183+
possible_file_paths = (xml_file_path.with_suffix(extension) for extension in supported_extensions)
184+
data_file_path = next((path for path in possible_file_paths if path.is_file()), None)
185+
if data_file_path is None:
186+
raise FileNotFoundError(f"data binary not found for file {xml_file_path.stem} with supported extensions: {supported_extensions}")
172187

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
188+
189+
assert xml_file_path.is_file(), f"xml file not found at the expected location {xml_file_path}"
190+
assert data_file_path.is_file(), f"binary file not found at the expected location {data_file_path}"
177191

178192
self.xml_file_path = xml_file_path
179-
self.data_file_path = data_file_path
193+
self.data_file_path = data_file_path

0 commit comments

Comments
 (0)