code: def test_apply_reference():
      """Test base function for rereferencing."""
      raw = read_raw_fif(fif_fname, preload=True)
  
      # Rereference raw data by creating a copy of original data
      reref, ref_data = _apply_reference(
          raw.copy(), ref_from=["EEG 001", "EEG 002"]
      )
      assert reref.info["custom_ref_applied"]
      _test_reference(raw, reref, ref_data, ["EEG 001", "EEG 002"])
  
      # The CAR reference projection should have been removed by the function
      assert not _has_eeg_average_ref_proj(reref.info)
  
      # Test that data is modified in place when copy=False
      reref, ref_data = _apply_reference(raw, ["EEG 001", "EEG 002"])
      assert raw is reref
  
      # Test that disabling the reference does not change anything
      reref, ref_data = _apply_reference(raw.copy(), [])
      assert_array_equal(raw._data, reref._data)
  
      # Test re-referencing Epochs object
      raw = read_raw_fif(fif_fname, preload=False)
      events = read_events(eve_fname)
      picks_eeg = pick_types(raw.info, meg=False, eeg=True)
      epochs = Epochs(
          raw,
          events=events,
          event_id=1,
          tmin=-0.2,
          tmax=0.5,
          picks=picks_eeg,
          preload=True,
      )
      reref, ref_data = _apply_reference(
          epochs.copy(), ref_from=["EEG 001", "EEG 002"]
      )
      assert reref.info["custom_ref_applied"]
      _test_reference(epochs, reref, ref_data, ["EEG 001", "EEG 002"])
  
      # Test re-referencing Evoked object
      evoked = epochs.average()
      reref, ref_data = _apply_reference(
          evoked.copy(), ref_from=["EEG 001", "EEG 002"]
      )
      assert reref.info["custom_ref_applied"]
      _test_reference(evoked, reref, ref_data, ["EEG 001", "EEG 002"])
  
      # Referencing needs data to be preloaded
      raw_np = read_raw_fif(fif_fname, preload=False)
      pytest.raises(RuntimeError, _apply_reference, raw_np, ["EEG 001"])
  
      # Test having inactive SSP projections that deal with channels involved
      # during re-referencing
      raw = read_raw_fif(fif_fname, preload=True)
      raw.add_proj(
          Projection(
              active=False,
              data=dict(
                  col_names=["EEG 001", "EEG 002"],
                  row_names=None,
                  data=np.array([[1, 1]]),
                  ncol=2,
                  nrow=1,
              ),
              desc="test",
              kind=1,
          )
      )
      # Projection concerns channels mentioned in projector
      with pytest.raises(RuntimeError, match="Inactive signal space"):
          _apply_reference(raw, ["EEG 001"])
  
      # Projection does not concern channels mentioned in projector, no error
      _apply_reference(raw, ["EEG 003"], ["EEG 004"])
  
      # CSD cannot be rereferenced
      with raw.info._unlock():
          raw.info["custom_ref_applied"] = FIFF.FIFFV_MNE_CUSTOM_REF_CSD
      with pytest.raises(RuntimeError, match="Cannot set.* type 'CSD'"):
          raw.set_eeg_reference()
  
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 2 for calling a global function with example usage: test_apply_reference() without return types