code: def _apply_reference(
      inst, ref_from, ref_to=None, forward=None, ch_type="auto"
  ):
      """Apply a custom EEG referencing scheme."""
      ref_to = _check_before_reference(inst, ref_from, ref_to, ch_type)
  
      # Compute reference
      if len(ref_from) > 0:
          # this is guaranteed below, but we should avoid the crazy pick_channels
          # behavior that [] gives all. Also use ordered=True just to make sure
          # that all supplied channels actually exist.
          assert len(ref_to) > 0
          ref_names = ref_from
          ref_from = pick_channels(
              inst.ch_names, ref_from, ordered=True
          )
          ref_to = pick_channels(inst.ch_names, ref_to, ordered=True)
  
          data = inst._data
          ref_data = data[..., ref_from, :].mean(-2, keepdims=True)
          data[..., ref_to, :] -= ref_data
          ref_data = ref_data[..., 0, :]
  
          # REST
          if forward is not None:
              # use ch_sel and the given forward
              forward = pick_channels_forward(
                  forward, ref_names, ordered=True
              )
              # 1-3. Compute a forward (G) and avg-ref'ed data (done above)
              G = forward["sol"]["data"]
              assert G.shape[0] == len(ref_names)
              # 4. Compute the forward (G) and average-reference it (Ga):
              Ga = G - np.mean(G, axis=0, keepdims=True)
              # 5. Compute the Ga_inv by SVD
              Ga_inv = pinv(Ga, rtol=1e-6)
              # 6. Compute Ra = (G @ Ga_inv) in eq (8) from G and Ga_inv
              Ra = G @ Ga_inv
              # 7-8. Compute Vp = Ra @ Va; then Vpa=average(Vp)
              Vpa = np.mean(
                  Ra @ data[..., ref_from, :], axis=-2, keepdims=True
              )
              data[..., ref_to, :] += Vpa
      else:
          ref_data = None
  
      return inst, ref_data
  
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 3 for calling a global function with example usage: _apply_reference(inst, ref_from, ref_to, forward, ch_type) and returns: inst,  ref_data