code: def test_interpolation_meg():
      """Test interpolation of MEG channels."""
      # speed accuracy tradeoff: channel subselection is faster but the
      # correlation drops
      thresh = 0.68
  
      raw, epochs_meg = _load_data("meg")
  
      # check that interpolation works when non M/EEG channels are present
      # before MEG channels
      raw.crop(0, 0.1).load_data().pick(epochs_meg.ch_names)
      raw.info.normalize_proj()
      raw.set_channel_types(
          {raw.ch_names[0]: "stim"}, on_unit_change="ignore"
      )
      raw.info["bads"] = [raw.ch_names[1]]
      raw.load_data()
      raw.interpolate_bads(mode="fast")
      del raw
  
      # check that interpolation works for MEG
      epochs_meg.info["bads"] = ["MEG 0141"]
      evoked = epochs_meg.average()
      pick = pick_channels(
          epochs_meg.info["ch_names"], epochs_meg.info["bads"]
      )
  
      # MEG -- raw
      raw_meg = RawArray(data=epochs_meg._data[0], info=epochs_meg.info)
      raw_meg.info["bads"] = ["MEG 0141"]
      data1 = raw_meg[pick, :][0][0]
  
      raw_meg.info.normalize_proj()
      data2 = raw_meg.interpolate_bads(reset_bads=False, mode="fast")[
          pick, :
      ][0][0]
      assert np.corrcoef(data1, data2)[0, 1] > thresh
      # the same number of bads as before
      assert len(raw_meg.info["bads"]) == len(raw_meg.info["bads"])
  
      # MEG -- epochs
      data1 = epochs_meg.get_data(pick).ravel()
      epochs_meg.info.normalize_proj()
      epochs_meg.interpolate_bads(mode="fast")
      data2 = epochs_meg.get_data(pick).ravel()
      assert np.corrcoef(data1, data2)[0, 1] > thresh
      assert len(epochs_meg.info["bads"]) == 0
  
      # MEG -- evoked (plus auto origin)
      data1 = evoked.data[pick]
      evoked.info.normalize_proj()
      data2 = evoked.interpolate_bads(origin="auto").data[pick]
      assert np.corrcoef(data1, data2)[0, 1] > thresh
  
      # MEG -- with exclude
      evoked.info["bads"] = ["MEG 0141", "MEG 0121"]
      pick = pick_channels(
          evoked.ch_names, evoked.info["bads"], ordered=True
      )
      evoked.data[pick[-1]] = 1e10
      data1 = evoked.data[pick]
      evoked.info.normalize_proj()
      data2 = evoked.interpolate_bads(
          origin="auto", exclude=["MEG 0121"]
      ).data[pick]
      assert np.corrcoef(data1[0], data2[0])[0, 1] > thresh
      assert np.all(data2[1] == 1e10)
  
imports: from itertools import compress
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal
  from mne import Epochs, pick_channels, pick_types, read_events
  from mne._fiff.constants import FIFF
  from mne._fiff.proj import _has_eeg_average_ref_proj
  from mne.channels import make_dig_montage
  from mne.channels.interpolation import _make_interpolation_matrix
  from mne.datasets import testing
  from mne.io import RawArray, read_raw_ctf, read_raw_fif, read_raw_nirx
  from mne.preprocessing.nirs import beer_lambert_law, optical_density, scalp_coupling_index
  from mne.utils import _record_warnings
label_desc: Use image node_id 3 for calling a global function with example usage: test_interpolation_meg() without return types