Skip to content

Commit

Permalink
ENH: Make suggestion (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Oct 17, 2022
1 parent 9231836 commit 861ccd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion mne_bids/read.py
Expand Up @@ -12,6 +12,7 @@
import re
from datetime import datetime, timezone
from difflib import get_close_matches
import os

import numpy as np
import mne
Expand Down Expand Up @@ -720,7 +721,17 @@ def read_raw_bids(bids_path, extra_params=None, verbose=None):
break

if not raw_path.exists():
raise FileNotFoundError(f'File does not exist: {raw_path}')
options = os.listdir(bids_path.directory)
matches = get_close_matches(bids_path.basename, options)
msg = f'File does not exist:\n{raw_path}'
if matches:
msg += (
'\nDid you mean one of:\n' +
'\n'.join(matches) +
'\ninstead of:\n' +
bids_path.basename
)
raise FileNotFoundError(msg)
if config_path is not None and not config_path.exists():
raise FileNotFoundError(f'config directory not found: {config_path}')

Expand Down
10 changes: 10 additions & 0 deletions mne_bids/tests/test_read.py
Expand Up @@ -1297,6 +1297,7 @@ def test_channels_tsv_raw_mismatch(tmp_path):
read_raw_bids(bids_path)


@testing.requires_testing_data
def test_file_not_found(tmp_path):
"""Check behavior if the requested file cannot be found."""
# First a path with a filename extension.
Expand All @@ -1313,6 +1314,15 @@ def test_file_not_found(tmp_path):
with pytest.raises(FileNotFoundError, match='File does not exist'):
read_raw_bids(bids_path=bp)

bp.update(extension='.fif')
_read_raw_fif(raw_fname, verbose=False).save(bp.fpath)
with pytest.warns(RuntimeWarning, match=r'channels\.tsv'):
read_raw_bids(bp) # smoke test

bp.update(task=None)
with pytest.raises(FileNotFoundError, match='Did you mean'):
read_raw_bids(bp)


@requires_version('mne', '1.2')
@pytest.mark.filterwarnings(warning_str['channel_unit_changed'])
Expand Down

0 comments on commit 861ccd1

Please sign in to comment.