code: def test_set_eeg_reference():
      """Test rereference eeg data."""
      raw = read_raw_fif(fif_fname, preload=True)
      with raw.info._unlock():
          raw.info["projs"] = []
  
      # Test setting an average reference projection
      assert not _has_eeg_average_ref_proj(raw.info)
      reref, ref_data = set_eeg_reference(raw, projection=True)
      assert _has_eeg_average_ref_proj(reref.info)
      assert not reref.info["projs"][0]["active"]
      assert ref_data is None
      reref.apply_proj()
      eeg_chans = [
          raw.ch_names[ch]
          for ch in pick_types(raw.info, meg=False, eeg=True)
      ]
      _test_reference(
          raw,
          reref,
          ref_data,
          [ch for ch in eeg_chans if ch not in raw.info["bads"]],
      )
  
      # Test setting an average reference when one was already present
      with pytest.warns(RuntimeWarning, match="untouched"):
          reref, ref_data = set_eeg_reference(
              raw, copy=False, projection=True
          )
      assert ref_data is None
  
      # Test setting an average reference on non-preloaded data
      raw_nopreload = read_raw_fif(fif_fname, preload=False)
      with raw_nopreload.info._unlock():
          raw_nopreload.info["projs"] = []
      reref, ref_data = set_eeg_reference(
          raw_nopreload, projection=True
      )
      assert _has_eeg_average_ref_proj(reref.info)
      assert not reref.info["projs"][0]["active"]
  
      # Rereference raw data by creating a copy of original data
      reref, ref_data = set_eeg_reference(
          raw, ["EEG 001", "EEG 002"], copy=True
      )
      assert reref.info["custom_ref_applied"]
      _test_reference(raw, reref, ref_data, ["EEG 001", "EEG 002"])
  
      # Test that data is modified in place when copy=False
      reref, ref_data = set_eeg_reference(
          raw, ["EEG 001", "EEG 002"], copy=False
      )
      assert raw is reref
  
      # Test moving from custom to average reference
      reref, ref_data = set_eeg_reference(raw, ["EEG 001", "EEG 002"])
      reref, _ = set_eeg_reference(reref, projection=True)
      assert _has_eeg_average_ref_proj(reref.info)
      assert not reref.info["custom_ref_applied"]
  
      # When creating an average reference fails, make sure the
      # custom_ref_applied flag remains untouched.
      reref = raw.copy()
      with reref.info._unlock():
          reref.info[
              "custom_ref_applied"
          ] = FIFF.FIFFV_MNE_CUSTOM_REF_ON
      reref.pick(picks="meg")  # Cause making average ref fail
      # should have turned it off
      assert (
          reref.info["custom_ref_applied"]
          == FIFF.FIFFV_MNE_CUSTOM_REF_OFF
      )
      with pytest.raises(ValueError, match="found to rereference"):
          set_eeg_reference(reref, projection=True)
  
      # Test moving from average to custom reference
      reref, ref_data = set_eeg_reference(raw, projection=True)
      reref, _ = set_eeg_reference(reref, ["EEG 001", "EEG 002"])
      assert not _has_eeg_average_ref_proj(reref.info)
      assert len(reref.info["projs"]) == 0
      assert (
          reref.info["custom_ref_applied"]
          == FIFF.FIFFV_MNE_CUSTOM_REF_ON
      )
  
      # Test that disabling the reference does not change the data
      assert _has_eeg_average_ref_proj(raw.info)
      reref, _ = set_eeg_reference(raw, [])
      assert_array_equal(raw._data, reref._data)
      assert not _has_eeg_average_ref_proj(reref.info)
  
      # make sure ref_channels=[] removes average reference projectors
      assert _has_eeg_average_ref_proj(raw.info)
      reref, _ = set_eeg_reference(raw, [])
      assert not _has_eeg_average_ref_proj(reref.info)
  
      # Test that average reference gives identical results when calculated
      # via SSP projection (projection=True) or directly (projection=False)
      with raw.info._unlock():
          raw.info["projs"] = []
      reref_1, _ = set_eeg_reference(raw.copy(), projection=True)
      reref_1.apply_proj()
      reref_2, _ = set_eeg_reference(raw.copy(), projection=False)
      assert_allclose(
          reref_1._data, reref_2._data, rtol=1e-6, atol=1e-15
      )
  
      # Test average reference without projection
      reref, ref_data = set_eeg_reference(
          raw.copy(), ref_channels="average", projection=False
      )
      _test_reference(raw, reref, ref_data, eeg_chans)
  
      with pytest.raises(
          ValueError, match='supported for ref_channels="averag'
      ):
          set_eeg_reference(raw, [], True, True)
      with pytest.raises(
          ValueError, match='supported for ref_channels="averag'
      ):
          set_eeg_reference(raw, ["EEG 001"], True, True)
  
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 3 for calling a global function with example usage: test_set_eeg_reference() without return types