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:
opened 06:55AM - 30 Jun 22 UTC
BUG
First reported on the forum at https://mne.discourse.group/t/can-not-reorder-cha… nnels-after-adding-a-reference-channel/5180/2
Works:
```python
# %%
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:
```python
# %%
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:
```python
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
```
Best wishes,
Richard