Repository: mne-tools/mne-python

Issue:
Issue with mne.viz.plot_topomap on sample dataset restricted to MEG channels
#### Describe the bug
The `mne.viz.plot_topomap` does not produce the expected result on the sample dataset when limiting to MEG channels. 

#### Steps to reproduce
Run the following script:
```
import os
import mne
import numpy as np

data_path = mne.datasets.sample.data_path()
subjects_dir = os.path.join(data_path, "subjects")
data_dir = os.path.join(data_path, 'MEG', 'sample')
file_name = os.path.join(data_dir, 'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(file_name, preload=True, verbose=False)
raw.pick_types(meg='grad', eeg=False, eog=False, stim=True)
print('done')

# info of the loaded channels
info = raw.copy().pick_types(meg=True).info

X = raw.get_data(picks=['meg'])
print("Data shape:", X.shape)

# simulate random data
data = np.random.randn(203)

assert len(info['chs']) == data.size

# plot
mne.viz.plot_topomap(data, info)
```

#### Expected results
`plot_topomap` should create a topographic map as image.

#### Actual results
Got the following error:
```
ValueError: cannot reshape array of size 203 into shape (101,2,newaxis)
```
which intervenes when the function `_merge_grad_data(data, method)` is called.

#### Additional information
Platform:      macOS-10.15.7-x86_64-i386-64bit
Python:        3.7.4 (default, Aug 13 2019, 15:17:50)  [Clang 4.0.1 (tags/RELEASE_401/final)]
Executable:    /Users/cedricallain/anaconda3/bin/python
CPU:           i386: 8 cores
Memory:        16.0 GB

mne:           0.21.0
numpy:         1.17.2 {blas=mkl_rt, lapack=mkl_rt}
scipy:         1.3.1
matplotlib:    3.1.1 {backend=module://ipykernel.pylab.backend_inline}

sklearn:       0.21.3
numba:         0.45.1
nibabel:       Not found
cupy:          Not found
pandas:        0.25.1
dipy:          Not found
mayavi:        Not found
pyvista:       Not found
vtk:           Not found


Discussion:
I confirm the pb. It was working fine with version 0.19

>


Resolving patch:
diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py
--- a/mne/viz/topomap.py
+++ b/mne/viz/topomap.py
@@ -856,7 +856,7 @@ def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
             # deal with grad pairs
             picks = _pair_grad_sensors(pos, topomap_coords=False)
             pos = _find_topomap_coords(pos, picks=picks[::2], sphere=sphere)
-            data, _ = _merge_ch_data(data, ch_type, [])
+            data, _ = _merge_ch_data(data[picks], ch_type, [])
             data = data.reshape(-1)
         else:
             picks = list(range(data.shape[0]))


Test patch:
diff --git a/mne/viz/tests/test_topomap.py b/mne/viz/tests/test_topomap.py
--- a/mne/viz/tests/test_topomap.py
+++ b/mne/viz/tests/test_topomap.py
@@ -557,6 +557,19 @@ def test_plot_topomap_bads():
     plt.close('all')
 
 
+def test_plot_topomap_bads_grad():
+    """Test plotting topomap with bad gradiometer channels (gh-8802)."""
+    import matplotlib.pyplot as plt
+    data = np.random.RandomState(0).randn(203)
+    info = read_info(evoked_fname)
+    info['bads'] = ['MEG 2242']
+    picks = pick_types(info, meg='grad')
+    info = pick_info(info, picks)
+    assert len(info['chs']) == 203
+    plot_topomap(data, info, res=8)
+    plt.close('all')
+
+
 def test_plot_topomap_nirs_overlap(fnirs_epochs):
     """Test plotting nirs topomap with overlapping channels (gh-7414)."""
     fig = fnirs_epochs['A'].average(picks='hbo').plot_topomap()