Skip to content

Commit

Permalink
[FIX] naming parsing for bids files (mne-tools#5570)
Browse files Browse the repository at this point in the history
this fixes the order of arguments in the filename construction for BIDS files. essentially, it looks to see if the filename string has the specific modality, it pops it off to infix the part argument then adds the modality as a suffix to the filename.
  • Loading branch information
teonbrooks authored and massich committed Oct 4, 2018
1 parent 30d6749 commit f38ab2e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/manual/c_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ anatomy only, not on the MEG/EEG data to be analyzed.
R_{13} & R_{13} & R_{13} & z_0 \\
0 & 0 & 0 & 1
\end{bmatrix}

defined so that if the augmented location vectors in MRI
head and MRI coordinate systems are denoted by :math:`r_{head}[x_{head}\ y_{head}\ z_{head}\ 1]` and :math:`r_{MRI}[x_{MRI}\ y_{MRI}\ z_{MRI}\ 1]`,
respectively,
Expand Down
6 changes: 3 additions & 3 deletions mne/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np

from .constants import FIFF
from .utils import _construct_bids_filename
from .pick import pick_types, channel_type, pick_channels, pick_info
from .pick import _pick_data_channels, _pick_data_or_ica
from .meas_info import write_meas_info
Expand Down Expand Up @@ -2258,9 +2259,8 @@ def _write_raw(fname, raw, info, picks, fmt, data_type, reset_range, start,
if split_naming == 'neuromag':
# insert index in filename
use_fname = '%s-%d%s' % (base, part_idx, ext)
else:
# insert index in filename
use_fname = '%s_part-%02d_meg%s' % (base, part_idx, ext)
elif split_naming == 'bids':
use_fname = _construct_bids_filename(base, ext, part_idx)
# check for file existence
_check_fname(use_fname, overwrite)

Expand Down
16 changes: 9 additions & 7 deletions mne/io/fiff/tests/test_raw_fiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,24 +398,26 @@ def test_split_files():
raw_crop = raw_1.copy().crop(0, 1.)

assert_allclose(raw_1.buffer_size_sec, 10., atol=1e-2) # samp rate
split_fname = op.join(tempdir, 'split_raw.fif')
split_fname = op.join(tempdir, 'split_raw_meg.fif')
# intended filenames
split_fname_elekta_part2 = op.join(tempdir, 'split_raw-1.fif')
split_fname_elekta_part2 = op.join(tempdir, 'split_raw_meg-1.fif')
split_fname_bids_part1 = op.join(tempdir, 'split_raw_part-01_meg.fif')
split_fname_bids_part2 = op.join(tempdir, 'split_raw_part-02_meg.fif')
raw_1.set_annotations(Annotations([2.], [5.5], 'test'))
raw_1.save(split_fname, buffer_size_sec=1.0, split_size='10MB')
with pytest.warns(RuntimeWarning, match='does not conform to MNE'):
raw_1.save(split_fname, buffer_size_sec=1.0, split_size='10MB')

# check that the filenames match the intended pattern
assert op.exists(split_fname_elekta_part2)
# check that filenames are being formatted correctly for BIDS
raw_1.save(split_fname,
buffer_size_sec=1.0, split_size='10MB',
split_naming='bids',
overwrite=True)
with pytest.warns(RuntimeWarning, match='does not conform to MNE'):
raw_1.save(split_fname, buffer_size_sec=1.0, split_size='10MB',
split_naming='bids', overwrite=True)
assert op.exists(split_fname_bids_part1)
assert op.exists(split_fname_bids_part2)

split_fname = op.join(tempdir, 'split_raw.fif')
raw_1.save(split_fname, buffer_size_sec=1.0, split_size='10MB')
raw_2 = read_raw_fif(split_fname)
assert_allclose(raw_2.buffer_size_sec, 1., atol=1e-2) # samp rate
assert_array_almost_equal(raw_1.annotations.onset, raw_2.annotations.onset)
Expand Down
15 changes: 15 additions & 0 deletions mne/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,18 @@ def _synthesize_stim_channel(events, n_samples):
for onset, duration, trigger in events:
stim_channel[onset:onset + duration] = trigger
return stim_channel


def _construct_bids_filename(base, ext, part_idx):
"""Construct a BIDS compatible filename for split files."""
# insert index in filename
deconstructed_base = base.split('_')
bids_supported = ['meg', 'eeg', 'ieeg']
for mod in bids_supported:
if mod in deconstructed_base:
idx = deconstructed_base.index(mod)
modality = deconstructed_base.pop(idx)
base = '_'.join(deconstructed_base)
use_fname = '%s_part-%02d_%s%s' % (base, part_idx, modality, ext)

return use_fname

0 comments on commit f38ab2e

Please sign in to comment.