Skip to content

Commit

Permalink
Add error message when conversion of EEG locs to head space fails (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
wmvanvliet authored Aug 26, 2022
1 parent 89fbad3 commit bdc435d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ Bugs
- Fix bug in :class:`mne.viz.Brain` constructor where the first argument was named ``subject_id`` instead of ``subject`` (:gh:`11049` by `Eric Larson`_)
- Fix bug in :ref:`mne coreg` where the MEG helmet position was not updated during ICP fitting (:gh:`11084` by `Eric Larson`_)
- Document ``height`` and ``weight`` keys of ``subject_info`` entry in :class:`mne.Info` (:gh:`11019` by :newcontrib:`Sena Er`)
- Fixed bug in :func:`mne.viz.plot_filter` when plotting filters created using ``output='ba'`` mode with ``compensation`` turned on. (by `Marian Dovgialo`_)
- Fix bug in :func:`mne.viz.plot_filter` when plotting filters created using ``output='ba'`` mode with ``compensation`` turned on. (:gh:`11040` by `Marian Dovgialo`_)
- Fix bug in :func:`mne.viz.plot_topomap` when providing ``sphere="eeglab"`` (:gh:`11081` by `Mathieu Scheltienne`_)
- Applying a montage where EEG locations are not in head space (or unknown space) without fiducials will now raise an error message. (:gh:`11080` by `Marijn van Vliet`_)

API changes
~~~~~~~~~~~
Expand Down
8 changes: 8 additions & 0 deletions mne/channels/montage.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,19 @@ def transform_to_head(montage):
# Get fiducial points and their coord_frame
native_head_t = compute_native_head_t(montage)
montage = montage.copy() # to avoid inplace modification

if native_head_t['from'] != FIFF.FIFFV_COORD_HEAD:
for d in montage.dig:
if d['coord_frame'] == native_head_t['from']:
d['r'] = apply_trans(native_head_t, d['r'])
d['coord_frame'] = FIFF.FIFFV_COORD_HEAD
elif d['kind'] == FIFF.FIFFV_POINT_EEG:
raise RuntimeError(
f'Could not transform EEG channel {d["ident"]} position '
f'from {_verbose_frames[d["coord_frame"]]} to head '
'coordinates. Fiducial points are either missing or '
'specified in a different coordinate frame than the EEG '
'channel locations.')
return montage


Expand Down
11 changes: 11 additions & 0 deletions mne/channels/tests/test_montage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,17 @@ def test_transform_to_head_and_compute_dev_head_t():
montage_polhemus
))

# Test errors when transforming without fiducials explicitly where points
# are tagged to be not in head or unknown coord space.
montage_without_fids = make_dig_montage(
ch_pos={"ch_1": np.array([1, 2, 3]),
"ch_2": np.array([4, 5, 6]),
"ch_3": np.array([7, 8, 9])},
coord_frame="mri") # MRI coordinate space
with pytest.raises(RuntimeError, match='Could not transform EEG channel'):
with pytest.warns(RuntimeWarning, match='Fiducial point .* not found'):
transform_to_head(montage_without_fids)


def test_set_montage_with_mismatching_ch_names():
"""Test setting a DigMontage with mismatching ch_names."""
Expand Down
2 changes: 1 addition & 1 deletion mne/io/_digitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def _get_fid_coords(dig, raise_error=True):
if len(fid_coord_frames) > 0 and raise_error:
if set(fid_coord_frames.keys()) != set(['nasion', 'lpa', 'rpa']):
raise ValueError("Some fiducial points are missing (got %s)." %
fid_coords.keys())
fid_coord_frames.keys())

if len(set(fid_coord_frames.values())) > 1:
raise ValueError(
Expand Down

0 comments on commit bdc435d

Please sign in to comment.