code: def _test_reference(raw, reref, ref_data, ref_from):
      """Test whether a reference has been correctly applied."""
      # Separate EEG channels from other channel types
      picks_eeg = pick_types(
          raw.info, meg=False, eeg=True, exclude="bads"
      )
      picks_other = pick_types(
          raw.info,
          meg=True,
          eeg=False,
          eog=True,
          stim=True,
          exclude="bads",
      )
  
      # Calculate indices of reference channesl
      picks_ref = [raw.ch_names.index(ch) for ch in ref_from]
  
      # Get data
      _data = raw._data
      _reref = reref._data
  
      # Check that the ref has been properly computed
      if ref_data is not None:
          assert_array_equal(
              ref_data, _data[..., picks_ref, :].mean(-2)
          )
  
      # Get the raw EEG data and other channel data
      raw_eeg_data = _data[..., picks_eeg, :]
      raw_other_data = _data[..., picks_other, :]
  
      # Get the rereferenced EEG data
      reref_eeg_data = _reref[..., picks_eeg, :]
      reref_other_data = _reref[..., picks_other, :]
  
      # Check that non-EEG channels are untouched
      assert_allclose(
          raw_other_data, reref_other_data, 1e-6, atol=1e-15
      )
  
      # Undo rereferencing of EEG channels if possible
      if ref_data is not None:
          if isinstance(raw, BaseEpochs):
              unref_eeg_data = (
                  reref_eeg_data + ref_data[:, np.newaxis, :]
              )
          else:
              unref_eeg_data = reref_eeg_data + ref_data
          assert_allclose(
              raw_eeg_data, unref_eeg_data, 1e-6, atol=1e-15
          )
  
imports: import itertools
  from contextlib import nullcontext
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal, assert_equal
  from mne import Epochs, Projection, add_reference_channels, create_info, find_events, make_forward_solution, make_sphere_model, pick_channels, pick_channels_forward, pick_types, read_events, read_evokeds, set_bipolar_reference, set_eeg_reference, setup_volume_source_space
  from mne._fiff.constants import FIFF
  from mne._fiff.proj import _has_eeg_average_ref_proj
  from mne._fiff.reference import _apply_reference
  from mne.datasets import testing
  from mne.epochs import BaseEpochs, make_fixed_length_epochs
  from mne.io import RawArray, read_raw_fif
  from mne.utils import _record_warnings, catch_logging
label_desc: Use image node_id 1 for calling a global function with example usage: _test_reference(raw, reref, ref_data, ref_from) without return types