Skip to content

Commit 7e62d83

Browse files
[BUG] IndexError in set_montage() for MEG+EEG recordings when digitization is skipped (#13700)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6c94c0a commit 7e62d83

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

doc/changes/dev/13700.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where :func:`mne.channels.DigMontage.set_montage` raised an :exc:`IndexError` on MEG+EEG recordings when EEG reference positions were set to the placeholder value ``[1, 0, 0]`` (i.e., digitization was not performed), by `Famous077`_.

mne/channels/montage.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,12 @@ def _backcompat_value(pos, ref_pos):
12561256
# keep reference location from EEG-like channels if they
12571257
# already exist and are all the same.
12581258
# Note: ref position is an empty list for fieldtrip data
1259-
if len(ref_pos) and ref_pos[0].any() and (ref_pos[0] == ref_pos).all():
1259+
if (
1260+
len(ref_pos)
1261+
and ref_pos[0].any()
1262+
and (ref_pos[0] == ref_pos).all()
1263+
and not np.array_equal(ref_pos[0], [1.0, 0.0, 0.0])
1264+
):
12601265
eeg_ref_pos = ref_pos[0]
12611266
# since we have an EEG reference position, we have
12621267
# to add it into the info['dig'] as EEG000

mne/channels/tests/test_montage.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import mne.channels.montage
2323
from mne import (
24-
__file__ as _mne_file,
25-
)
26-
from mne import (
24+
EpochsArray,
2725
create_info,
2826
pick_types,
2927
read_evokeds,
3028
)
29+
from mne import (
30+
__file__ as _mne_file,
31+
)
3132
from mne._fiff._digitization import (
3233
_count_points_by_type,
3334
_format_dig_points,
@@ -2145,3 +2146,28 @@ def test_fnirs_montage():
21452146
raw.set_channel_types({ch_name: "eeg" for ch_name in raw.ch_names[-2:]})
21462147
with pytest.raises(ValueError, match="mix of fNIRS"):
21472148
raw.get_montage()
2149+
2150+
2151+
def test_set_montage_meg_eeg_no_digitization():
2152+
"""Regression test for GH-12011.
2153+
2154+
set_montage() must not crash when MEG+EEG info has EEG reference
2155+
positions set to the [1, 0, 0] sentinel (digitization was skipped).
2156+
"""
2157+
ch_names = [f"EEG{i:03d}" for i in range(1, 11)] + ["MEG0111"]
2158+
ch_types = ["eeg"] * 10 + ["grad"]
2159+
info = create_info(ch_names=ch_names, sfreq=1000.0, ch_types=ch_types)
2160+
2161+
# Simulate MEG reader behaviour when digitization is skipped:
2162+
# EEG ref position (loc[3:6]) is set to the [1, 0, 0] sentinel
2163+
with info._unlock():
2164+
for ch in info["chs"]:
2165+
if ch["ch_name"].startswith("EEG"):
2166+
ch["loc"][3:6] = [1.0, 0.0, 0.0]
2167+
2168+
data = np.zeros((1, len(ch_names), 100))
2169+
epochs = EpochsArray(data, info)
2170+
2171+
# This must not raise IndexError (regression test for GH-12011)
2172+
montage = make_standard_montage("standard_1020")
2173+
epochs.set_montage(montage, on_missing="ignore")

0 commit comments

Comments
 (0)