code: def set_eeg_reference(
      inst,
      ref_channels="average",
      copy=True,
      projection=False,
      ch_type="auto",
      forward=None,
      *,
      joint=False,
      verbose=None,
  ):
      """Specify which reference to use for EEG data.
  
      Use this function to explicitly specify the desired reference for EEG.
      This can be either an existing electrode or a new virtual channel.
      This function will re-reference the data according to the desired
      reference.
  
      Note that it is also possible to re-reference the signal using a
      Laplacian (LAP) "reference-free" transformation using the
      :func:`.compute_current_source_density` function.
  
      Parameters
      ----------
      inst : instance of Raw | Epochs | Evoked
          Instance of Raw or Epochs with EEG channels and reference channel(s).
      %(ref_channels_set_eeg_reference)s
      copy : bool
          Specifies whether the data will be copied (True) or modified in-place
          (False). Defaults to True.
      %(projection_set_eeg_reference)s
      %(ch_type_set_eeg_reference)s
      %(forward_set_eeg_reference)s
      %(joint_set_eeg_reference)s
      %(verbose)s
  
      Returns
      -------
      inst : instance of Raw | Epochs | Evoked
          Data with EEG channels re-referenced. If ``ref_channels='average'`` and
          ``projection=True`` a projection will be added instead of directly
          re-referencing the data.
      ref_data : array
          Array of reference data subtracted from EEG channels. This will be
          ``None`` if ``projection=True`` or ``ref_channels='REST'``.
      %(set_eeg_reference_see_also_notes)s
      """
      from ..forward import Forward
  
      _check_can_reref(inst)
  
      ch_type = _get_ch_type(inst, ch_type)
  
      if projection:  # average reference projector
          if ref_channels != "average":
              raise ValueError(
                  'Setting projection=True is only supported for ref_channels="average", '
                  f"got {ref_channels!r}."
              )
          # We need verbose='error' here in case we add projs sequentially
          if _has_eeg_average_ref_proj(
              inst.info, ch_type=ch_type, verbose="error"
          ):
              warn(
                  "An average reference projection was already added. The data "
                  "has been left untouched."
              )
          else:
              # Creating an average reference may fail. In this case, make
              # sure that the custom_ref_applied flag is left untouched.
              custom_ref_applied = inst.info["custom_ref_applied"]
  
              try:
                  with inst.info._unlock():
                      inst.info[
                          "custom_ref_applied"
                      ] = FIFF.FIFFV_MNE_CUSTOM_REF_OFF
                  if joint:
                      inst.add_proj(
                          make_eeg_average_ref_proj(
                              inst.info, ch_type=ch_type, activate=False
                          )
                      )
                  else:
                      for this_ch_type in ch_type:
                          inst.add_proj(
                              make_eeg_average_ref_proj(
                                  inst.info,
                                  ch_type=this_ch_type,
                                  activate=False,
                              )
                          )
              except Exception:
                  with inst.info._unlock():
                      inst.info[
                          "custom_ref_applied"
                      ] = custom_ref_applied
                  raise
              # If the data has been preloaded, projections will no
              # longer be automatically applied.
              if inst.preload:
                  logger.info(
                      "Average reference projection was added, "
                      "but has not been applied yet. Use the "
                      "apply_proj method to apply it."
                  )
          return inst, None
      del projection  # not used anymore
  
      inst = inst.copy() if copy else inst
      ch_dict = {
          **{type_: True for type_ in ch_type},
          "meg": False,
          "ref_meg": False,
      }
      ch_sel = [
          inst.ch_names[i] for i in pick_types(inst.info, **ch_dict)
      ]
  
      if ref_channels == "REST":
          _validate_type(
              forward, Forward, 'forward when ref_channels="REST"'
          )
      else:
          forward = None  # signal to _apply_reference not to do REST
  
      if ref_channels in ("average", "REST"):
          logger.info(f"Applying {ref_channels} reference.")
          ref_channels = ch_sel
  
      if ref_channels == []:
          logger.info(
              "EEG data marked as already having the desired reference."
          )
      else:
          logger.info(
              "Applying a custom "
              f"{tuple(DEFAULTS['titles'][type_] for type_ in ch_type)} "
              "reference."
          )
  
      return _apply_reference(
          inst, ref_channels, ch_sel, forward, ch_type=ch_type
      )
  
file_path: mne-python/mne/_fiff/reference.py
imports: import numpy
  from ..defaults import DEFAULTS
  from ..fixes import pinv
  from ..utils import _check_option, _check_preload, _on_missing, _validate_type, fill_doc, logger, verbose, warn
  from .constants import FIFF
  from .meas_info import _check_ch_keys
  from .pick import _ELECTRODE_CH_TYPES, pick_channels, pick_channels_forward, pick_types
  from .proj import _has_eeg_average_ref_proj, make_eeg_average_ref_proj, setup_proj
label_desc: Use image node_id 6 for calling a global function with example usage: set_eeg_reference(inst, ref_channels, copy, projection, ch_type, forward) and returns: _apply_reference, inst,  None