Skip to content

Commit 73247a9

Browse files
authored
BUG: Fix bug with Epochs.add_reference_channels (#10912)
1 parent 8a837ec commit 73247a9

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/changes/latest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ Bugs
143143

144144
- Fix bug in coregistration GUI that prevented it from starting up if only a high-resolution head model was available (:gh:`10543` by `Richard Höchenberger`_)
145145

146+
- Fix bug with :class:`mne.Epochs.add_reference_channels` where attributes were not updated properly so subsequent `~mne.Epochs.pick_types` calls were broken (:gh:`10912` by `Eric Larson`_)
147+
-
146148
- Fix bug in the :class:`mne.viz.Brain` tool bar that prevented the buttons to call the corresponding feature (:gh:`10560` by `Guillaume Favelier`_)
147149

148150
- Fix issue with saving epochs once :func:`~mne.preprocessing.compute_current_source_density` has been used if a rejection threshold was used first (:gh:`10619` by `Alex Rockhill`_ and `Richard Höchenberger`_)

mne/io/reference.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,16 @@ def add_reference_channels(inst, ref_channels, copy=True):
241241
'loc': ref_dig_array}
242242
inst.info['chs'].append(chan_info)
243243
inst.info._update_redundant()
244+
range_ = np.arange(1, len(ref_channels) + 1)
244245
if isinstance(inst, BaseRaw):
245246
inst._cals = np.hstack((inst._cals, [1] * len(ref_channels)))
246-
range_ = np.arange(1, len(ref_channels) + 1)
247247
for pi, picks in enumerate(inst._read_picks):
248248
inst._read_picks[pi] = np.concatenate(
249249
[picks, np.max(picks) + range_])
250+
elif isinstance(inst, BaseEpochs):
251+
picks = inst.picks
252+
inst.picks = np.concatenate(
253+
[picks, np.max(picks) + range_])
250254
inst.info._check_consistency()
251255
set_eeg_reference(inst, ref_channels=ref_channels, copy=False,
252256
verbose=False)

mne/io/tests/test_reference.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
make_forward_solution, setup_volume_source_space,
1919
pick_channels_forward, read_evokeds,
2020
find_events)
21-
from mne.epochs import BaseEpochs
21+
from mne.epochs import BaseEpochs, make_fixed_length_epochs
2222
from mne.io import RawArray, read_raw_fif
2323
from mne.io.constants import FIFF
2424
from mne.io.proj import _has_eeg_average_ref_proj, Projection
@@ -604,6 +604,19 @@ def test_add_reference():
604604
with pytest.raises(TypeError, match='instance of'):
605605
add_reference_channels(raw, 1)
606606

607+
# gh-10878
608+
raw = read_raw_fif(raw_fname).crop(0, 1, include_tmax=False).load_data()
609+
data = raw.copy().add_reference_channels(['REF']).pick_types(eeg=True)
610+
data = data.get_data()
611+
epochs = make_fixed_length_epochs(raw).load_data()
612+
data_2 = epochs.copy().add_reference_channels(['REF']).pick_types(eeg=True)
613+
data_2 = data_2.get_data()[0]
614+
assert_allclose(data, data_2)
615+
evoked = epochs.average()
616+
data_3 = evoked.copy().add_reference_channels(['REF']).pick_types(eeg=True)
617+
data_3 = data_3.get_data()
618+
assert_allclose(data, data_3)
619+
607620

608621
@pytest.mark.parametrize('n_ref', (1, 2))
609622
def test_add_reorder(n_ref):

0 commit comments

Comments
 (0)