Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Make suggestion #1087

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion mne_bids/read.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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