Skip to content
Open
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
1 change: 1 addition & 0 deletions doc/changes/dev/14287.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Exclude pre-existing bad channels that are not bridged from :func:`~mne.preprocessing.interpolate_bridged_electrodes` interpolation, by `Deepesh Sonar`_.
11 changes: 10 additions & 1 deletion mne/preprocessing/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def interpolate_bridged_electrodes(inst, bridged_idx, bad_limit=4):
that to aid in interpolation rather than completely discarding the
data from the two channels.

Channels in ``inst.info["bads"]`` that are not part of the bridged

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
Channels in ``inst.info["bads"]`` that are not part of the bridged
.. versionchanged:: 1.14
Pre-existing bad channels that are not part of the bridged set are
now excluded from the interpolation.

set are excluded from the interpolation: they are neither used to
compute the interpolated values nor interpolated themselves.

Parameters
----------
inst : instance of Epochs, Evoked, or Raw
Expand Down Expand Up @@ -189,7 +193,12 @@ def interpolate_bridged_electrodes(inst, bridged_idx, bad_limit=4):

# use the virtual channels to interpolate
inst.info["bads"] = list(bads)
inst.interpolate_bads()
# Exclude pre-existing bad channels that are not part of the bridged set
# so they can neither contribute to the interpolation nor be interpolated
# themselves. Channels that are both bridged and marked bad remain
# interpolation targets, as before.
exclude_bads = [ch for ch in bads_orig if ch not in bads]
inst.interpolate_bads(exclude=exclude_bads)

# drop virtual channels
inst.drop_channels(list(virtual_chs.keys()))
Expand Down
45 changes: 43 additions & 2 deletions mne/preprocessing/tests/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,59 @@ def test_interpolate_bridged_electrodes():
idx0 = inst.ch_names.index("EEG 001")
idx1 = inst.ch_names.index("EEG 002")
ch_names_orig = inst.ch_names.copy()
inst.info["bads"] = ["EEG 003"]
bads_orig = inst.info["bads"].copy()
# reference interpolation using the same source channels, i.e.
# excluding the pre-existing bad like the bridged version does
inst2 = inst.copy()
inst2.info["bads"] = ["EEG 001", "EEG 002"]
inst2.info["bads"] = ["EEG 001", "EEG 002", "EEG 003"]
inst2.interpolate_bads()
data_interp_reg = inst2.get_data(picks=["EEG 001", "EEG 002"])
# a pre-existing bad that is not bridged must not contribute: poisoning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# a pre-existing bad that is not bridged must not contribute: poisoning
# Verify non-bridged bad doesn't influence the interpolation result

# its data must leave the bridged interpolation result unchanged ...
inst_clean = interpolate_bridged_electrodes(inst.copy(), [(idx0, idx1)])
data_clean = inst_clean.get_data(picks=["EEG 001", "EEG 002"])
inst_pois = inst.copy()
inst_pois.apply_function(lambda x: np.full_like(x, 1.0), picks=["EEG 003"])
data_bad_poisoned = inst_pois.get_data(picks="EEG 003").copy()
inst_pois = interpolate_bridged_electrodes(inst_pois, [(idx0, idx1)])
assert np.array_equal(
inst_pois.get_data(picks=["EEG 001", "EEG 002"]), data_clean
)
# ... and the pre-existing bad itself must be left untouched
assert np.array_equal(inst_pois.get_data(picks="EEG 003"), data_bad_poisoned)
assert inst_pois.info["bads"] == bads_orig
inst = interpolate_bridged_electrodes(inst, [(idx0, idx1)])
data_interp = inst.get_data(picks=["EEG 001", "EEG 002"])
assert not any(["virtual" in ch for ch in inst.ch_names])
assert inst.ch_names == ch_names_orig
assert inst.info["bads"] == bads_orig
assert np.array_equal(data_interp, data_clean)
inst.info["bads"] = []
# check closer to regular interpolation than original data
assert 1e-6 < np.mean(np.abs(data_interp - data_interp_reg)) < 5.4e-5
assert 1e-6 < np.mean(np.abs(data_interp - data_interp_reg)) < 6.5e-5

# a channel that is both pre-existing bad and bridged must still be

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# a channel that is both pre-existing bad and bridged must still be
# Bridged channel that is also marked bad should be repaired

# repaired as a bridged target rather than being excluded (checked on
# copies so the shared instances stay pristine for the checks below)
for inst in (raw, epochs, evoked):
idx0 = inst.ch_names.index("EEG 001")
idx1 = inst.ch_names.index("EEG 002")
inst_pois = inst.copy()
inst_pois.info["bads"] = ["EEG 001", "EEG 003"]
bads_orig = inst_pois.info["bads"].copy()
data_bad_before = inst_pois.get_data(picks="EEG 003").copy()
inst_pois.apply_function(lambda x: np.full_like(x, 1.0), picks=["EEG 001"])
data_bridged_poisoned = inst_pois.get_data(picks="EEG 001").copy()
inst_pois = interpolate_bridged_electrodes(inst_pois, [(idx0, idx1)])
assert not any(["virtual" in ch for ch in inst_pois.ch_names])
assert inst_pois.info["bads"] == bads_orig
# the bridged bad was repaired ...
assert not np.array_equal(
inst_pois.get_data(picks="EEG 001"), data_bridged_poisoned
)
# ... while the non-bridged bad was left untouched
assert np.array_equal(inst_pois.get_data(picks="EEG 003"), data_bad_before)

for inst in (raw, epochs, evoked):
idx0 = inst.ch_names.index("EEG 001")
Expand Down
Loading