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

Raise helpful error message if montage is missing electrode positions #181

Merged
merged 3 commits into from
Apr 27, 2024
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
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
Loading