|
56 | 56 | import numpy as np |
57 | 57 | import os |
58 | 58 | import pathlib |
| 59 | +from pathlib import Path |
59 | 60 | import copy |
60 | 61 | import warnings |
61 | 62 | from collections import namedtuple, OrderedDict |
@@ -151,15 +152,15 @@ def __init__( |
151 | 152 |
|
152 | 153 | if filename is not None: |
153 | 154 | include_filenames = [filename] |
154 | | - warnings.warn("`filename` is deprecated and will be removed. Please use `include_filenames` instead") |
| 155 | + warnings.warn("`filename` is deprecated and will be removed in v1.0. Please use `include_filenames` instead") |
155 | 156 |
|
156 | 157 | if exclude_filename is not None: |
157 | 158 | if isinstance(exclude_filename, str): |
158 | 159 | exclude_filenames = [exclude_filename] |
159 | 160 | else: |
160 | 161 | exclude_filenames = exclude_filename |
161 | 162 | warnings.warn( |
162 | | - "`exclude_filename` is deprecated and will be removed. Please use `exclude_filenames` instead" |
| 163 | + "`exclude_filename` is deprecated and will be removed in v1.0. Please use `exclude_filenames` instead" |
163 | 164 | ) |
164 | 165 |
|
165 | 166 | if include_filenames is None: |
@@ -214,30 +215,42 @@ def _parse_header(self): |
214 | 215 | unit_annotations = [] |
215 | 216 | event_annotations = [] |
216 | 217 |
|
217 | | - if self.rawmode == "one-dir": |
218 | | - filenames = sorted(os.listdir(self.dirname)) |
219 | | - else: |
220 | | - filenames = self.include_filenames |
| 218 | + # 1) Get file paths based on mode and validate existence for multiple-files mode |
| 219 | + if self.rawmode == "multiple-files": |
| 220 | + # For multiple-files mode, validate that all explicitly provided files exist |
| 221 | + file_paths = [] |
| 222 | + for filename in self.include_filenames: |
| 223 | + full_path = Path(self.dirname) / filename |
| 224 | + if not full_path.is_file(): |
| 225 | + raise ValueError( |
| 226 | + f"Provided Filename is not a file: " |
| 227 | + f"{full_path}. If you want to provide a " |
| 228 | + f"directory use the `dirname` keyword" |
| 229 | + ) |
| 230 | + file_paths.append(full_path) |
| 231 | + else: # one-dir mode |
| 232 | + # For one-dir mode, get all files from directory |
| 233 | + dir_path = Path(self.dirname) |
| 234 | + file_paths = [item for item in sorted(dir_path.iterdir()) if item.is_file()] |
221 | 235 |
|
222 | | - filenames = [f for f in filenames if f not in self.exclude_filenames] |
223 | | - full_filenames = [os.path.join(self.dirname, f) for f in filenames] |
| 236 | + # 2) Filter by exclude filenames |
| 237 | + file_paths = [fp for fp in file_paths if fp.name not in self.exclude_filenames] |
224 | 238 |
|
225 | | - for filename in full_filenames: |
226 | | - if not os.path.isfile(filename): |
227 | | - raise ValueError( |
228 | | - f"Provided Filename is not a file: " |
229 | | - f"{filename}. If you want to provide a " |
230 | | - f"directory use the `dirname` keyword" |
231 | | - ) |
| 239 | + # 3) Filter to keep only files with correct extensions |
| 240 | + # Note: suffix[1:] removes the leading dot from file extension (e.g., ".ncs" -> "ncs") |
| 241 | + valid_file_paths = [ |
| 242 | + fp for fp in file_paths |
| 243 | + if fp.suffix[1:].lower() in self.extensions |
| 244 | + ] |
| 245 | + |
| 246 | + # Convert back to strings for backwards compatibility with existing code |
| 247 | + full_filenames = [str(fp) for fp in valid_file_paths] |
232 | 248 |
|
233 | 249 | stream_props = {} # {(sampling_rate, n_samples, t_start): {stream_id: [filenames]} |
234 | 250 |
|
235 | 251 | for filename in full_filenames: |
236 | 252 | _, ext = os.path.splitext(filename) |
237 | | - ext = ext[1:] # remove dot |
238 | | - ext = ext.lower() # make lower case for comparisons |
239 | | - if ext not in self.extensions: |
240 | | - continue |
| 253 | + ext = ext[1:].lower() # remove dot and make lower case |
241 | 254 |
|
242 | 255 | # Skip Ncs files with only header. Other empty file types |
243 | 256 | # will have an empty dataset constructed later. |
|
0 commit comments