Skip to content

Commit 6ceafb3

Browse files
committed
fix handling of broken file references in brainvisionrawio
1 parent 72ec76a commit 6ceafb3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

neo/rawio/brainvisionrawio.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def _parse_header(self):
5555
marker_filename = self.filename.replace(bname, vhdr_header["Common Infos"]["MarkerFile"])
5656
binary_filename = self.filename.replace(bname, vhdr_header["Common Infos"]["DataFile"])
5757

58+
binary_filename = self._ensure_filename(binary_filename, "binary")
59+
marker_filename = self._ensure_filename(marker_filename, "marker")
60+
5861
if vhdr_header["Common Infos"]["DataFormat"] != "BINARY":
5962
raise NeoReadWriteError(
6063
f"Only `BINARY` format has been implemented. Current Data Format is {vhdr_header['Common Infos']['DataFormat']}"
@@ -236,6 +239,25 @@ def _rescale_event_timestamp(self, event_timestamps, dtype, event_channel_index)
236239
def _get_analogsignal_buffer_description(self, block_index, seg_index, buffer_id):
237240
return self._buffer_descriptions[block_index][seg_index][buffer_id]
238241

242+
def _ensure_filename(self, filename, kind):
243+
if not os.path.exists(filename):
244+
# file not found, subsequent import stage would fail
245+
ext = os.path.splitext(filename)[1]
246+
# Check if we can fall back to a file with the same prefix as the .vhdr.
247+
# This can happen when users rename their files but forget to edit the
248+
# .vhdr file to fix the path reference to the binary and marker files,
249+
# in which case import will fail. These files come in triples, like:
250+
# myfile.vhdr, myfile.eeg and myfile.vmrk; this code will thus pick
251+
# the next best alternative.
252+
alt_name = self.filename.replace(".vhdr", ext)
253+
if os.path.exists(alt_name):
254+
self.logger.warning(
255+
f"The {kind} file {filename} was not found, but found a file whose "
256+
f"prefix matched the .vhdr ({os.path.basename(alt_name)}). Using "
257+
f"this file instead.")
258+
filename = alt_name
259+
return filename
260+
239261

240262
def read_brainvsion_soup(filename):
241263
with open(filename, "r", encoding="utf8") as f:

0 commit comments

Comments
 (0)