Can not reorder channels after adding a reference channel

  • MNE version: 1.0.0
  • operating system: Windows 10

Hi dear all,
I’m trying to change my reference of the EEG epochs, and I want my channel order corresponds to the standard montage. So I tried to add the reference channel “AFz” back and reorder the channels, but there was an error.

Here blow is my code:

epochs = mne.read_epochs('demo_epoch.fif')

standard_montage = mne.channels.make_standard_montage('standard_1020')

epochs.add_reference_channels('AFz')
epochs.set_channel_types({'AFz':'eeg'})
epochs.set_montage('standard_1020', on_missing = 'warn')

new_channel_order = [x for x in standard_montage.ch_names if x in epochs.ch_names]
epochs.reorder_channels(new_channel_order)

The error says IndexError: index 62 is out of bounds for axis 0 with size 62.

Actually, when I tried to pick eeg channels, it threw the same error. I don’t know what happened.

Here you can find this demo data:
demo data Google Drive

Thanks advance!

1 Like

Hello @LeiGuo0812 and welcome to the forum!

I believe you’ve found a bug there, I can reproduce this with our sample data, but only for Epochs, not for Raw:

Works:

# %%
import mne

sample_dir = mne.datasets.sample.data_path()
sample_fname = sample_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'

raw = mne.io.read_raw_fif(sample_fname).crop(tmax=10).load_data()
raw.add_reference_channels(['REF'])
raw.pick_types(eeg=True)

Doesn’t work:

# %%
import mne

sample_dir = mne.datasets.sample.data_path()
sample_fname = sample_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'

raw = mne.io.read_raw_fif(sample_fname).crop(tmax=60).load_data()
events = mne.find_events(raw, stim_channel='STI 014')
epochs = mne.Epochs(raw, events=events, preload=True)

epochs.add_reference_channels(['REF'])
epochs.pick_types(eeg=True)

and yields:

IndexError                                Traceback (most recent call last)
Input In [30], in <cell line: 12>()
     10 epochs = mne.Epochs(raw, events=events, preload=True)
     11 epochs.add_reference_channels(['REF'])
---> 12 epochs.pick_types(eeg=True)

File <decorator-gen-43>:12, in pick_types(self, meg, eeg, stim, eog, ecg, emg, ref_meg, misc, resp, chpi, exci, ias, syst, seeg, dipole, gof, bio, ecog, fnirs, csd, dbs, include, exclude, selection, verbose)

File ~/Development/mne-python/mne/channels/channels.py:681, in UpdateChannelsMixin.pick_types(self, meg, eeg, stim, eog, ecg, emg, ref_meg, misc, resp, chpi, exci, ias, syst, seeg, dipole, gof, bio, ecog, fnirs, csd, dbs, include, exclude, selection, verbose)
    597 """Pick some channels by type and names.
    598 
    599 Parameters
   (...)
    672 .. versionadded:: 0.9.0
    673 """
    674 idx = pick_types(
    675     self.info, meg=meg, eeg=eeg, stim=stim, eog=eog, ecg=ecg, emg=emg,
    676     ref_meg=ref_meg, misc=misc, resp=resp, chpi=chpi, exci=exci,
    677     ias=ias, syst=syst, seeg=seeg, dipole=dipole, gof=gof, bio=bio,
    678     ecog=ecog, fnirs=fnirs, csd=csd, dbs=dbs, include=include,
    679     exclude=exclude, selection=selection)
--> 681 self._pick_drop_channels(idx)
    683 # remove dropped channel types from reject and flat
    684 if getattr(self, 'reject', None) is not None:
    685     # use list(self.reject) to avoid RuntimeError for changing
    686     # dictionary size during iteration

File <decorator-gen-46>:12, in _pick_drop_channels(self, idx, verbose)

File ~/Development/mne-python/mne/channels/channels.py:855, in UpdateChannelsMixin._pick_drop_channels(self, idx, verbose)
    852     _check_preload(self, msg)
    854 if getattr(self, 'picks', None) is not None:
--> 855     self.picks = self.picks[idx]
    857 if getattr(self, '_read_picks', None) is not None:
    858     self._read_picks = [r[idx] for r in self._read_picks]

IndexError: index 376 is out of bounds for axis 0 with size 376

I have opened an issue on GitHub:

Best wishes,
Richard

Dear Richard,
Many thanks for your attention. Hope this bug can be fixed soon. :slight_smile:

Lei Guo

Hello @LeiGuo0812, the problem has been fixed in the latest development version of MNE, and the fix will be included in the next release (MNE-Python 1.1)

Best wishes,
Richard

Dear @richard,
Thank you and the other MNE team members’ efforts. Have a nice day! ::

Best wishes,
Lei Guo