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

MAINT: Tolerate new MNE behavior #1076

Merged
merged 3 commits into from
Oct 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mne_bids/dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pathlib import Path
import re
import warnings
from importlib_resources import files

import mne
import numpy as np
Expand All @@ -29,6 +28,8 @@
verbose)
from mne_bids.path import BIDSPath

data_dir = Path(__file__).parent / 'data'


def _handle_electrodes_reading(electrodes_fname, coord_frame,
coord_unit):
Expand Down Expand Up @@ -633,7 +634,6 @@ def template_to_head(info, space, coord_frame='auto', unit='auto',
if coord_frame == 'auto':
coord_frame = 'mri_voxel' if locs.min() >= 0 else 'ras'
# transform montage to head
data_dir = files('mne_bids.data')
# set to the right coordinate frame as specified by the user
for d in montage.dig: # ensure same coordinate frame
d['coord_frame'] = MNE_STR_TO_FRAME[coord_frame]
Expand Down
47 changes: 33 additions & 14 deletions mne_bids/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Authors: Stefan Appelhoff <[email protected]>
#
# License: BSD-3-Clause
from contextlib import nullcontext
import json
import os
import os.path as op
Expand All @@ -20,7 +21,7 @@
from mne.datasets import testing
from mne.io.constants import FIFF
from mne.utils import requires_nibabel, object_diff, requires_version
from mne.utils import assert_dig_allclose
from mne.utils import assert_dig_allclose, check_version

from mne_bids import BIDSPath
from mne_bids.config import (MNE_STR_TO_FRAME, BIDS_SHARED_COORDINATE_FRAMES,
Expand All @@ -39,6 +40,7 @@
run = '01'
acq = '01'
task = 'testing'
fids_added = check_version('mne', '1.2')

_bids_path = BIDSPath(
subject=subject_id, session=session_id, run=run, acquisition=acq,
Expand Down Expand Up @@ -286,7 +288,11 @@ def test_get_head_mri_trans(tmp_path):
raw_test.set_montage(montage)
raw_test.save(bids_path.fpath, overwrite=True)

with pytest.raises(RuntimeError, match='Could not extract fiducial'):
if fids_added:
ctx = nullcontext()
else:
ctx = pytest.raises(RuntimeError, match='Could not extract fiducial')
with ctx:
get_head_mri_trans(bids_path=bids_path, fs_subject='sample',
fs_subjects_dir=subjects_dir)

Expand Down Expand Up @@ -766,8 +772,12 @@ def test_handle_eeg_coords_reading(tmp_path):
montage = mne.channels.make_dig_montage(ch_pos=ch_pos,
coord_frame="unknown")
raw.set_montage(montage)
with pytest.raises(RuntimeError, match="'head' coordinate frame "
"must contain"):
if fids_added:
ctx = nullcontext()
else:
ctx = pytest.raises(
RuntimeError, match="'head' coordinate frame must contain")
with ctx:
write_raw_bids(raw, bids_path, overwrite=True)

bids_path.update(root=tmp_path)
Expand All @@ -779,24 +789,33 @@ def test_handle_eeg_coords_reading(tmp_path):
suffix='electrodes',
extension='.tsv',
on_error='warn')
assert coordsystem_fname is None
assert electrodes_fname is None
if fids_added:
assert coordsystem_fname is not None
assert electrodes_fname is not None
else:
assert coordsystem_fname is None
assert electrodes_fname is None

# create montage in head frame and set should result in
# an error if landmarks are not set
montage = mne.channels.make_dig_montage(ch_pos=ch_pos,
coord_frame="head")
raw.set_montage(montage)
with pytest.raises(RuntimeError, match="'head' coordinate frame "
"must contain"):
if fids_added:
ctx = nullcontext()
else:
ctx = pytest.raises(
RuntimeError, match="'head' coordinate frame must contain")
with ctx:
write_raw_bids(raw, bids_path, overwrite=True)

montage = mne.channels.make_dig_montage(ch_pos=ch_pos,
coord_frame="head",
nasion=[1, 0, 0],
lpa=[0, 1, 0],
rpa=[0, 0, 1])
raw.set_montage(montage)
if not fids_added: # only need to rerun if the last one failed
montage = mne.channels.make_dig_montage(ch_pos=ch_pos,
coord_frame="head",
nasion=[1, 0, 0],
lpa=[0, 1, 0],
rpa=[0, 0, 1])
raw.set_montage(montage)
write_raw_bids(raw, bids_path, overwrite=True)

# obtain the sensor positions and assert ch_coords are same
Expand Down