code: def _check_before_reference(inst, ref_from, ref_to, ch_type):
      """Prepare instance for referencing."""
      # Check to see that data is preloaded
      _check_preload(inst, "Applying a reference")
  
      ch_type = _get_ch_type(inst, ch_type)
      ch_dict = {
          **{type_: True for type_ in ch_type},
          "meg": False,
          "ref_meg": False,
      }
      eeg_idx = pick_types(inst.info, **ch_dict)
  
      if ref_to is None:
          ref_to = [inst.ch_names[i] for i in eeg_idx]
          extra = "EEG channels found"
      else:
          extra = "channels supplied"
      if len(ref_to) == 0:
          raise ValueError(f"No {extra} to apply the reference to")
  
      # After referencing, existing SSPs might not be valid anymore.
      projs_to_remove = []
      for i, proj in enumerate(inst.info["projs"]):
          # Remove any average reference projections
          if (
              proj["desc"] == "Average EEG reference"
              or proj["kind"] == FIFF.FIFFV_PROJ_ITEM_EEG_AVREF
          ):
              logger.info(
                  "Removing existing average EEG reference "
                  "projection."
              )
              # Don't remove the projection right away, but do this at the end of
              # this loop.
              projs_to_remove.append(i)
  
          # Inactive SSPs may block re-referencing
          elif (
              not proj["active"]
              and len(
                  [
                      ch
                      for ch in (ref_from + ref_to)
                      if ch in proj["data"]["col_names"]
                  ]
              )
              > 0
          ):
              raise RuntimeError(
                  "Inactive signal space projection (SSP) operators are "
                  "present that operate on sensors involved in the desired "
                  "referencing scheme. These projectors need to be applied "
                  "using the apply_proj() method function before the desired "
                  "reference can be set."
              )
  
      for i in projs_to_remove:
          del inst.info["projs"][i]
  
      # Need to call setup_proj after changing the projs:
      inst._projector, _ = setup_proj(
          inst.info, add_eeg_ref=False, activate=False
      )
  
      # If the reference touches EEG/ECoG/sEEG/DBS electrodes, note in the
      # info that a non-CAR has been applied.
      ref_to_channels = pick_channels(
          inst.ch_names, ref_to, ordered=True
      )
      if len(np.intersect1d(ref_to_channels, eeg_idx)) > 0:
          with inst.info._unlock():
              inst.info[
                  "custom_ref_applied"
              ] = FIFF.FIFFV_MNE_CUSTOM_REF_ON
  
      return ref_to
  
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 2 for calling a global function with example usage: _check_before_reference(inst, ref_from, ref_to, ch_type) and returns: ref_to