Skip to content

Commit 98cb9f5

Browse files
authored
ENH: clearer error messages in copyfile_brainvision (#1444)
1 parent a9b6b08 commit 98cb9f5

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

doc/whats_new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ Detailed list of changes
5252
⚕️ Code health
5353
^^^^^^^^^^^^^^
5454

55-
- None yet
55+
- Made :func:`mne_bids.copyfiles.copyfile_brainvision` output more meaningful error messages when encountering problematic files, by `Stefan Appelhoff`_ (:gh:`1444`)
5656

5757
:doc:`Find out what was new in previous releases <whats_new_previous_releases>`

mne_bids/copyfiles.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,24 @@ def _get_brainvision_paths(vhdr_path):
108108
vmrk_file = vmrk_file_match.groups()[0]
109109

110110
# Make sure we are dealing with file names as is customary, not paths
111-
# Paths are problematic when copying the files to another system. Instead,
112-
# always use the file name and keep the file triplet in the same directory
113-
assert os.sep not in eeg_file
114-
assert os.sep not in vmrk_file
111+
for fi in [eeg_file, vmrk_file]:
112+
if os.sep in fi:
113+
raise RuntimeError(
114+
f"Detected a path separator in a file link: {fi}.\n\n"
115+
"Paths are problematic when copying the files to another system. "
116+
"Instead, always use the file name and keep the "
117+
"BrainVision file triplet (eeg/dat, vhdr, vmrk) in the same directory."
118+
)
115119

116120
# Assert the paths exist
117121
head, tail = op.split(vhdr_path)
118122
eeg_file_path = op.join(head, eeg_file)
119123
vmrk_file_path = op.join(head, vmrk_file)
120-
assert op.exists(eeg_file_path)
121-
assert op.exists(vmrk_file_path)
124+
for fpath in [eeg_file_path, vmrk_file_path]:
125+
if not Path(fpath).exists():
126+
raise FileNotFoundError(
127+
f"{fpath} referenced in {vhdr_path} but it does not exist."
128+
)
122129

123130
# Return the paths
124131
return (eeg_file_path, vmrk_file_path)
@@ -355,14 +362,24 @@ def copyfile_brainvision(vhdr_src, vhdr_dest, anonymize=None, verbose=None):
355362
# Write new header and marker files, fixing the file pointer links
356363
# For that, we need to replace an old "basename" with a new one
357364
# assuming that all .eeg/.dat, .vhdr, .vmrk share one basename
358-
__, basename_src = op.split(fname_src)
359-
assert op.split(eeg_file_path)[-1] in [basename_src + ".eeg", basename_src + ".dat"]
360-
assert basename_src + ".vmrk" == op.split(vmrk_file_path)[-1]
361-
__, basename_dest = op.split(fname_dest)
365+
basename_src = Path(fname_src).name
366+
eeg_expected = [f"{basename_src}.eeg", f"{basename_src}.dat"]
367+
vmrk_expected = [f"{basename_src}.vmrk"]
368+
if Path(eeg_file_path).name not in eeg_expected:
369+
raise RuntimeError(
370+
f"Unexpected path to data file in {vhdr_src}:\n "
371+
f"-->{Path(eeg_file_path).name}\nExpected one of {eeg_expected}."
372+
)
373+
if Path(vmrk_file_path).name not in vmrk_expected:
374+
raise RuntimeError(
375+
f"Unexpected path to marker file in {vhdr_src}:\n "
376+
f"-->{Path(vmrk_file_path).name}\nExpected one of {vmrk_expected}."
377+
)
378+
basename_dest = Path(fname_dest).name
362379
search_lines = [
363-
"DataFile=" + basename_src + ".eeg",
364-
"DataFile=" + basename_src + ".dat",
365-
"MarkerFile=" + basename_src + ".vmrk",
380+
f"DataFile={basename_src}.eeg",
381+
f"DataFile={basename_src}.dat",
382+
f"MarkerFile={basename_src}.vmrk",
366383
]
367384

368385
with open(vhdr_src, encoding=enc) as fin:

0 commit comments

Comments
 (0)