diff --git a/doc/changes/dev/14320.bugfix.rst b/doc/changes/dev/14320.bugfix.rst new file mode 100644 index 00000000000..2f8e87d3ebd --- /dev/null +++ b/doc/changes/dev/14320.bugfix.rst @@ -0,0 +1 @@ +Fix bug with :func:`mne.preprocessing.interpolate_bridged_electrodes` where ``inst.info["bads"]`` was cleared and not restored when too many electrodes were bridged together and a :class:`RuntimeError` was raised, by `Carina Forster`_. \ No newline at end of file diff --git a/mne/preprocessing/interpolate.py b/mne/preprocessing/interpolate.py index 68c196667af..c2f41a7ba28 100644 --- a/mne/preprocessing/interpolate.py +++ b/mne/preprocessing/interpolate.py @@ -124,9 +124,6 @@ def interpolate_bridged_electrodes(inst, bridged_idx, bad_limit=4): raise RuntimeError( f"Montage channel positions must be in ``head`` got {pos['coord_frame']}" ) - # store bads orig to put back at the end - bads_orig = inst.info["bads"] - inst.info["bads"] = list() # look for group of bad channels nodes = sorted(set(chain(*bridged_idx))) @@ -153,6 +150,10 @@ def interpolate_bridged_electrodes(inst, bridged_idx, bad_limit=4): "might be inaccurate." ) + # store bads orig to put back at the end + bads_orig = inst.info["bads"] + inst.info["bads"] = list() + # make virtual channels virtual_chs = dict() bads = set() diff --git a/mne/preprocessing/tests/test_interpolate.py b/mne/preprocessing/tests/test_interpolate.py index 33bb9467baa..8d4e0906a3b 100644 --- a/mne/preprocessing/tests/test_interpolate.py +++ b/mne/preprocessing/tests/test_interpolate.py @@ -129,6 +129,7 @@ def test_interpolate_bridged_electrodes(): data[:5, :] = np.ones((5, 1024)) raw = io.RawArray(data, info) raw.set_montage("spherical_1005") + raw.info["bads"] = [ch_names[10]] # a bad channel outside the bridged group bridged_idx = list(itertools.combinations(range(5), 2)) with pytest.raises( RuntimeError, @@ -136,6 +137,8 @@ def test_interpolate_bridged_electrodes(): "together and form a large area of bridged electrodes.", ): interpolate_bridged_electrodes(raw, bridged_idx, bad_limit=4) + # bads must be preserved when the error is raised + assert raw.info["bads"] == [ch_names[10]] # increase the limit to prevent raising interpolate_bridged_electrodes(raw, bridged_idx, bad_limit=5) # invalid argument