Repository: mne-tools/mne-python

Issue:
BUG: Compensation channels pick error
While trying to execute `evoked.interpolate_bads()`, I ran into the following error:
```
  mne\channels\interpolation.py", line 190, in _interpolate_bads_meg
    info_to = pick_info(inst.info, picks_bad)

  mne\io\pick.py", line 411, in pick_info
    'channels' % (current_comp, comps_missing))

RuntimeError: Compensation grade 3 has been applied, but compensation channels are missing: ['BG1-2805', 'BG2-2805', 'BG3-2805', 'G11-2805', 'G12-2805', 'G13-2805', 'G22-2805', 'G23-2805', 'P11-2805', 'P13-2805', 'Q11-2805', 'Q13-2805', 'R11-2805', 'R12-2805', 'R13-2805', 'R22-2805', 'R23-2805']
Either remove compensation or pick compensation channels
```
This error seems to be raised automatically when attempting to make any pick that does not include all `ref_meg` channels. I dug a bit into compensation grades, and it seems that it can only be removed for Raw data using the `apply_gradient_compensation(0)` method.

Maybe it would be better to turn this error into a warning? Indeed in my case, compensation channels are not removed from data, the function only attempts to separately pick bad channels to compute interpolation.
Another way could be to always include these channels, similar to the `ref_meg='auto'` argument of `pick_types()`, but I'm not sure it would make sense in every case (especially this one).
Thanks,


Discussion:
Indeed this should probably not be an error, see our current plan for fixing these sorts of issues [here](https://github.com/mne-tools/mne-python/issues/5221#issuecomment-392057909).

Although for the case of `interpolate_bads`, I'm not sure what the right thing to do would be -- maybe we should go back to grade 0, interpolate, then re-apply whatever compensation was there originally. @bloyl @agramfort any ideas on this one?
I just tried to run my source reconstruction scripts, and it turns out this bug is everywhere (evoked.plot_joint, beamformer.make_lcmv), nothing works anymore since I upgraded from v0.15 to v0.16 last week.
Oof, that's not good. While we work on a proper fix, you could turn it into a `warnings.warn` in your local copy of the MNE code
could be related to https://github.com/mne-tools/mne-python/pull/5295

can you share a gist. We'll fix ASAP.

we need more MNE developers with CTF data...

@Heneark #5295 has been merged. Can you provide some test snippet so we can fix ASAP and release 0.16.2 

thanks
I downloaded the master branch, evoked.plot_joint() is indeed fixed.
Here's a test snippet for the interpolation error:
https://gist.github.com/Heneark/2e18280f88f956ae5faef3cea40e4bd0

Same error with beamformer (no bad channel involved):
```
filters = mne.beamformer.make_lcmv(evoked[k].info, fwd_surf, data_cov, noise_cov=noise_cov, pick_ori='vector')
Traceback (most recent call last):

  File "mne/beamformer/_lcmv.py", line 137, in make_lcmv
    _check_one_ch_type(info, picks, noise_cov)

  File "mne/beamformer/_compute_beamformer.py", line 119, in _check_one_ch_type
    info_pick = pick_info(info, sel=picks)

  File "mne/io/pick.py", line 408, in pick_info
    'channels' % (current_comp, comps_missing))

RuntimeError: Compensation grade 3 has been applied, but compensation channels are missing: ['BG1-2805', 'BG2-2805', 'BG3-2805', 'G11-2805', 'G12-2805', 'G13-2805', 'G22-2805', 'G23-2805', 'P11-2805', 'P13-2805', 'Q11-2805', 'Q13-2805', 'R11-2805', 'R12-2805', 'R13-2805', 'R22-2805', 'R23-2805']
Either remove compensation or pick compensation channels
```

Cheers,

Resolving patch:
diff --git a/mne/channels/interpolation.py b/mne/channels/interpolation.py
--- a/mne/channels/interpolation.py
+++ b/mne/channels/interpolation.py
@@ -171,8 +171,10 @@ def _interpolate_bads_meg(inst, mode='accurate', verbose=None):
         If not None, override default verbose level (see :func:`mne.verbose`
         and :ref:`Logging documentation <tut_logging>` for more).
     """
-    picks_meg = pick_types(inst.info, meg=True, eeg=False, exclude=[])
-    picks_good = pick_types(inst.info, meg=True, eeg=False, exclude='bads')
+    picks_meg = pick_types(inst.info, meg=True, eeg=False,
+                           ref_meg=True, exclude=[])
+    picks_good = pick_types(inst.info, meg=True, eeg=False,
+                            ref_meg=True, exclude='bads')
     meg_ch_names = [inst.info['ch_names'][p] for p in picks_meg]
     bads_meg = [ch for ch in inst.info['bads'] if ch in meg_ch_names]
 
@@ -186,7 +188,9 @@ def _interpolate_bads_meg(inst, mode='accurate', verbose=None):
     # return without doing anything if there are no meg channels
     if len(picks_meg) == 0 or len(picks_bad) == 0:
         return
-    info_from = pick_info(inst.info, picks_good)
-    info_to = pick_info(inst.info, picks_bad)
+    inst_info = inst.info.copy()
+    inst_info['comps'] = []
+    info_from = pick_info(inst_info, picks_good)
+    info_to = pick_info(inst_info, picks_bad)
     mapping = _map_meg_channels(info_from, info_to, mode=mode)
     _do_interp_dots(inst, mapping, picks_good, picks_bad)


Test patch:
diff --git a/mne/channels/tests/test_interpolation.py b/mne/channels/tests/test_interpolation.py
--- a/mne/channels/tests/test_interpolation.py
+++ b/mne/channels/tests/test_interpolation.py
@@ -8,6 +8,7 @@
 
 from mne import io, pick_types, pick_channels, read_events, Epochs
 from mne.channels.interpolation import _make_interpolation_matrix
+from mne.datasets import testing
 from mne.utils import run_tests_if_main
 
 base_dir = op.join(op.dirname(__file__), '..', '..', 'io', 'tests', 'data')
@@ -154,4 +155,15 @@ def test_interpolation():
     assert_true(np.corrcoef(data1, data2)[0, 1] > thresh)
 
 
+@testing.requires_testing_data
+def test_interpolation_ctf_comp():
+    """Test interpolation with compensated CTF data."""
+    ctf_dir = op.join(testing.data_path(download=False), 'CTF')
+    raw_fname = op.join(ctf_dir, 'somMDYO-18av.ds')
+    raw = io.read_raw_ctf(raw_fname, preload=True)
+    raw.info['bads'] = [raw.ch_names[5], raw.ch_names[-5]]
+    raw.interpolate_bads(mode='fast')
+    assert raw.info['bads'] == []
+
+
 run_tests_if_main()