Skip to content

Commit

Permalink
Raise helpful error message if montage is missing electrode positions (
Browse files Browse the repository at this point in the history
…#181)

* raise helpful error message on missing channel dig

* add changelog entry

* add test and error message if montage is entirely absent
  • Loading branch information
mscheltienne authored Apr 27, 2024
1 parent a7b4c84 commit 36356b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

Version 0.7
===========

- Raise helpful error message when montage is incomplete (:pr:`181` by `Mathieu Scheltienne`_)
14 changes: 13 additions & 1 deletion mne_icalabel/iclabel/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,21 @@ def _cart2sph(_x, _y, _z):
return azimuth, elevation, r

# get the channel position dictionary
montage = raw.copy().pick_channels(picks, ordered=True).get_montage()
montage = raw.copy().pick(picks).get_montage()
if montage is None:
raise ValueError(
"Montage is not set. Please set the montage to provide the electrode "
"positions."
)
positions = montage.get_positions()
ch_pos = positions["ch_pos"]
# check that we do have a coordinate for every points
empty = [key for key in ch_pos if np.all(np.isnan(ch_pos[key]))]
if len(empty) != 0:
raise ValueError(
f"Channel position for {empty} is missing. Please check the montage set, "
"the channel names, and ensure that every channel has a position."
)

# get locations as a 2D array
locs = np.vstack(list(ch_pos.values()))
Expand Down
15 changes: 15 additions & 0 deletions mne_icalabel/iclabel/tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ def test_eeg_topoplot(file, eeglab_result_file):
assert np.allclose(topo, topo_eeglab, equal_nan=True)


def test_eeg_topoplot_invalid_montage():
"""Test that we raise an error if the montage is badly set."""
raw = read_raw(raw_eeglab_path, preload=True)
ica = read_ica_eeglab(raw_eeglab_path)
# get icawinv
icawinv, _ = _retrieve_eeglab_icawinv(ica)
# compute feature
raw.info["chs"][0]["loc"] = np.array([np.nan] * 12)
with pytest.raises(ValueError, match="Channel position for .* is missing"):
_eeg_topoplot(raw, icawinv, ica.ch_names)
raw.set_montage(None)
with pytest.raises(ValueError, match="Montage is not set"):
_eeg_topoplot(raw, icawinv, ica.ch_names)


# ----------------------------------------------------------------------------
@pytest.mark.filterwarnings("ignore:Estimated head radius.*:RuntimeWarning")
@pytest.mark.parametrize(
Expand Down

0 comments on commit 36356b6

Please sign in to comment.