Repository: mne-tools/mne-python

Issue:
IndexError when calling pick_channels
#### Describe the bug
I attempted to resave a dataset which was provided in a strange format (one file for subsets of channels), and encountered some strange behavior with the ```pick_channels``` function. I remember that this worked in mne version 0.19.2, but it does not anymore in 0.21.dev0. 

#### Steps to reproduce
```python
import mne 
import numpy as np

nr_datasets = 20
nr_channels = 10
nr_samples = 20000
fs = 1000

raws = []
for i_set in range(nr_datasets):
    ch_names = ['eeg_%i_%i' %(i_set, i) for i in range(nr_channels)]
    data = np.random.randn(nr_channels, nr_samples)
    info = mne.create_info(ch_names, fs, 'eeg')
    raw = mne.io.RawArray(data, info)
    raws.append(raw)

raw1 = raws[0].add_channels(raws[1:])
raw1.pick_channels(raw.ch_names)
```


[MWE]: https://en.wikipedia.org/wiki/Minimal_Working_Example
[built-in datasets]: https://mne.tools/dev/overview/datasets_index.html
[sample]: https://mne.tools/dev/overview/datasets_index.html#sample


#### Expected results
A raw instance with a subset of channels should be returned.


#### Actual results
```
/home/n/.local/lib/python3.7/site-packages/mne/channels/channels.py in <listcomp>(.0)
    859             print(self._read_picks)
    860             print(idx)
--> 861             self._read_picks = [r[idx] for r in self._read_picks]
    862 
    863         if hasattr(self, '_cals'):

IndexError: index 190 is out of bounds for axis 0 with size 29
```



Discussion:
can you share the file?

does it work with 0.20.1 ?

Probably due to #7507 so I would expect this to work on 0.20 and 0.20.1. Indeed having the problematic file would help
The above minimal example should execute, since it is with some random synthetic data and should also throw an error with 0.21.dev0. It works with v.0.20.1. Not sure about data sharing, it is the MNI Open iEEG Atlas data which I want to group according to subjects, the provided format is grouped according to regions.

Resolving patch:
diff --git a/mne/channels/channels.py b/mne/channels/channels.py
--- a/mne/channels/channels.py
+++ b/mne/channels/channels.py
@@ -964,9 +964,11 @@ def add_channels(self, add_list, force_update_info=False):
                                          for inst in [self] + add_list])
             # We should never use these since data are preloaded, let's just
             # set it to something large and likely to break (2 ** 31 - 1)
-            extra_idx = [2147483647] * len(add_list)
+            extra_idx = [2147483647] * sum(info['nchan'] for info in infos[1:])
+            assert all(len(r) == infos[0]['nchan'] for r in self._read_picks)
             self._read_picks = [
                 np.concatenate([r, extra_idx]) for r in self._read_picks]
+            assert all(len(r) == self.info['nchan'] for r in self._read_picks)
         return self
 
 


Test patch:
diff --git a/mne/io/fiff/tests/test_raw_fiff.py b/mne/io/fiff/tests/test_raw_fiff.py
--- a/mne/io/fiff/tests/test_raw_fiff.py
+++ b/mne/io/fiff/tests/test_raw_fiff.py
@@ -1225,6 +1225,18 @@ def test_add_channels():
     raw_meg.copy().add_channels([raw_arr], force_update_info=True)
     # Make sure that values didn't get overwritten
     assert_object_equal(raw_arr.info['dev_head_t'], orig_head_t)
+    # Make sure all variants work
+    for simult in (False, True):  # simultaneous adding or not
+        raw_new = raw_meg.copy()
+        if simult:
+            raw_new.add_channels([raw_eeg, raw_stim])
+        else:
+            raw_new.add_channels([raw_eeg])
+            raw_new.add_channels([raw_stim])
+        for other in (raw_meg, raw_stim, raw_eeg):
+            assert_allclose(
+                raw_new.copy().pick_channels(other.ch_names).get_data(),
+                other.get_data())
 
     # Now test errors
     raw_badsf = raw_eeg.copy()