Skip to content

Commit

Permalink
MRG: Fix writing preloaded data of a non-BIDS format with automated f…
Browse files Browse the repository at this point in the history
…ormat conversion (#1102)

* Fix writing preloaded data of a non-BIDS format with automated format conversion

Fixes #1101

* Simpler

* Better error message

* Add test

* Better error message

* Add changelog entry

* Fix error message

* Add requires_testing_data decorator
  • Loading branch information
hoechenberger committed Nov 20, 2022
1 parent 1b04da8 commit b97ce0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Detailed list of changes

- When writing data containing :class:`mne.Annotations` **and** passing events to :func:`~mne_bids.write_raw_bids`, previously, annotations whose description did not appear in ``event_id`` were silently dropped. We now raise an exception and request users to specify mappings between descriptions and event codes in this case. It is still possible to omit ``event_id`` if no ``events`` are passed, by `Richard Höchenberger`_ (:gh:`1084`)
- When working with NIRS data, raise the correct error message when a faulty ``format`` argument is passed to :func:`~mne_bids.write_raw_bids`, by `Stefan Appelhoff`_ (:gh:`1092`)
- Fixed writing data preloaded from a format that's not supported by BIDS, by `Richard Höchenberger`_ (:gh:`1101`)

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

Expand Down
29 changes: 23 additions & 6 deletions mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,6 @@ def test_fif(_bids_validate, tmp_path):
for FILE in files:
assert 'split' in FILE

# test unknown extension
raw = _read_raw_fif(raw_fname)
raw._filenames = (raw.filenames[0].replace('.fif', '.foo'),)
with pytest.raises(ValueError, match='Unrecognized file format'):
write_raw_bids(raw, bids_path)

# test whether extra points in raw.info['dig'] are correctly used
# to set DigitizedHeadShape in the JSON sidecar
# unchanged sample data includes extra points
Expand Down Expand Up @@ -3948,3 +3942,26 @@ def test_events_data_deprecation(tmp_path):
raw=raw, bids_path=bids_path, events=events, events_data=events,
event_id=event_id
)


@testing.requires_testing_data
def test_unknown_extension(_bids_validate, tmp_path):
"""Write data with unknown extension to BIDS."""
bids_root = tmp_path / 'bids'
bids_path = _bids_path.copy().update(root=bids_root, datatype='meg')
raw_fname = data_path / 'MEG' / 'sample' / 'sample_audvis_trunc_raw.fif'

raw = _read_raw_fif(raw_fname)
raw._filenames = (raw.filenames[0].replace('.fif', '.foo'),)

# When data is not preloaded, we should raise an exception.
with pytest.raises(ValueError, match='file format not supported by BIDS'):
write_raw_bids(raw, bids_path)

# With preloaded data, writing should work.
raw._filenames = (raw.filenames[0].replace('.foo', '.fif'),)
raw.load_data()
raw._filenames = (raw.filenames[0].replace('.fif', '.foo'),)

write_raw_bids(raw, bids_path, allow_preload=True, format='FIF')
_bids_validate(bids_root)
16 changes: 11 additions & 5 deletions mne_bids/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def _channels_tsv(raw, fname, overwrite=False):

# get the manufacturer from the file in the Raw object
_, ext = _parse_ext(raw.filenames[0])
manufacturer = MANUFACTURERS[ext]

manufacturer = MANUFACTURERS.get(ext, '')
ignored_channels = IGNORED_CHANNELS.get(manufacturer, list())

status, ch_type, description = list(), list(), list()
Expand Down Expand Up @@ -1542,8 +1541,10 @@ def write_raw_bids(
'got %s' % type(raw))

if raw.preload is not False and not allow_preload:
raise ValueError('The data is already loaded from disk and may be '
'altered. See warning for "allow_preload".')
raise ValueError(
'The data has already been loaded from disk. To write it to BIDS, '
'pass "allow_preload=True" and the "format" parameter.'
)

if not isinstance(bids_path, BIDSPath):
raise RuntimeError('"bids_path" must be a BIDSPath object. Please '
Expand Down Expand Up @@ -1609,7 +1610,12 @@ def write_raw_bids(
ext = '.bdf'

if ext not in ALLOWED_INPUT_EXTENSIONS:
raise ValueError(f'Unrecognized file format {ext}')
raise ValueError(
f'The input data is in a file format not supported by '
f'BIDS: "{ext}". You can try to preload the data and call '
f'write_raw_bids() with the "allow_preload=True" and the '
f'"format" parameters.'
)

if symlink and ext != '.fif':
raise NotImplementedError('Symlinks are currently only supported '
Expand Down

0 comments on commit b97ce0b

Please sign in to comment.