Skip to content

[BUG] IndexError in set_montage() for MEG+EEG recordings when digitization is skipped#13700

Open
Famous077 wants to merge 7 commits intomne-tools:mainfrom
Famous077:fix/set-montage-meg-eeg-no-digitization
Open

[BUG] IndexError in set_montage() for MEG+EEG recordings when digitization is skipped#13700
Famous077 wants to merge 7 commits intomne-tools:mainfrom
Famous077:fix/set-montage-meg-eeg-no-digitization

Conversation

@Famous077
Copy link

@Famous077 Famous077 commented Feb 27, 2026

Reference issue (if any)

Fixed issue #12011

What does this implement/fix?

Calling set_montage() on a MEG+EEG recording where digitization was
skipped raised an unhelpful IndexError: pop from empty list.

MEG readers write the sentinel value [1, 0, 0] into the EEG reference
position (ch["loc"][3:6]) when no digitization was performed. This
value incorrectly passed all three conditions guarding custom_eeg_ref_dig = True,
which then caused info["dig"].pop() to crash on an empty list.

Two changes in mne/channels/montage.py:

  • Tightened the custom_eeg_ref_dig condition to explicitly reject the
    [1, 0, 0] sentinel (root cause fix).
  • Added a guard before info["dig"].pop() that raises a clear
    RuntimeError if info["dig"] is empty, rather than crashing
    with an unrelated IndexError.

Additional information

Added regression test test_set_montage_meg_eeg_no_digitization in
mne/channels/tests/test_montage.py that reproduces the exact scenario
from the issue. All 60 existing montage tests continue to pass.

@Famous077 Famous077 changed the title Fix IndexError in set_montage for MEG+EEG with no digitization (GH-12… Fix IndexError in set_montage for MEG+EEG recordings with no digitization Feb 27, 2026
@Famous077
Copy link
Author

@mscheltienne Could you please take a look when you have time? I’d really value your perspective and any suggestions for improvement.

@Famous077 Famous077 changed the title Fix IndexError in set_montage for MEG+EEG recordings with no digitization [BUG] IndexError in set_montage() for MEG+EEG recordings when digitization is skipped Feb 27, 2026
Copy link
Member

@mscheltienne mscheltienne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to tackle this old issue! Looks great, a couple of small comments below.

@@ -0,0 +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 :gh:`Famous077`. No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the contribution, the :gh: role is legacy element before we used the automatic changelog generation. If you never contributed to MNE, you should use the :newcontrib: role, if you already contributed then simply a link to your author field (which should be added to doc/changes/names.inc).

Comment on lines 2156 to 2159
import numpy as np

from mne import EpochsArray, create_info
from mne.channels import make_standard_montage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be moved at the module-level.

Comment on lines 1392 to 1399
if not info["dig"]:
raise RuntimeError(
"Cannot determine EEG reference digitization coordinate "
"frame: info['dig'] is empty. This may happen when a "
"MEG+EEG recording has EEG reference positions set to a "
"placeholder value [1, 0, 0] (digitization was not "
"performed)."
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you added not np.array_equal(ref_pos[0], [1.0, 0.0, 0.0]), custom_eeg_ref_dig is now False thus this warning is never triggered and is dead-code.

@Famous077
Copy link
Author

Hi @mscheltienne, Thank you for the thorough review and kind words. I have addressed all the feedbacks:

-Renamed the changelog file from 12011.bugfix.rst to 13700.bugfix.rst to match the PR number.
-Replaced :gh: with :newcontrib: in the changelog entry and added my name to doc/changes/names.inc.
-Removed the dead-code if not info["dig"] guard since custom_eeg_ref_dig is now always False in that scenario.
-Moved the test imports to module-level.

Please let me know if there is anything else that needs to be changed. Thank you again for your time and guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants