code: def test_set_bipolar_reference(inst_type):
      """Test bipolar referencing."""
      raw = read_raw_fif(fif_fname, preload=True)
      raw.apply_proj()
  
      if inst_type == "raw":
          inst = raw
          del raw
      elif inst_type in ["epochs", "evoked"]:
          events = find_events(raw, stim_channel="STI 014")
          epochs = Epochs(
              raw, events, tmin=-0.3, tmax=0.7, preload=True
          )
          inst = epochs
          if inst_type == "evoked":
              inst = epochs.average()
          del epochs
  
      ch_info = {"kind": FIFF.FIFFV_EOG_CH, "extra": "some extra value"}
      with pytest.raises(KeyError, match="key errantly present"):
          set_bipolar_reference(
              inst, "EEG 001", "EEG 002", "bipolar", ch_info
          )
      ch_info.pop("extra")
      reref = set_bipolar_reference(
          inst, "EEG 001", "EEG 002", "bipolar", ch_info
      )
      assert reref.info["custom_ref_applied"]
  
      # Compare result to a manual calculation
      a = inst.copy().pick(["EEG 001", "EEG 002"])
      a = a._data[..., 0, :] - a._data[..., 1, :]
      b = reref.copy().pick(["bipolar"])._data[..., 0, :]
      assert_allclose(a, b)
  
      # Original channels should be replaced by a virtual one
      assert "EEG 001" not in reref.ch_names
      assert "EEG 002" not in reref.ch_names
      assert "bipolar" in reref.ch_names
  
      # Check channel information
      bp_info = reref.info["chs"][reref.ch_names.index("bipolar")]
      an_info = inst.info["chs"][inst.ch_names.index("EEG 001")]
      for key in bp_info:
          if key == "coil_type":
              assert bp_info[key] == FIFF.FIFFV_COIL_EEG_BIPOLAR, key
          elif key == "kind":
              assert bp_info[key] == FIFF.FIFFV_EOG_CH, key
          elif key != "ch_name":
              assert_equal(bp_info[key], an_info[key], err_msg=key)
  
      # Minimalist call
      reref = set_bipolar_reference(inst, "EEG 001", "EEG 002")
      assert "EEG 001-EEG 002" in reref.ch_names
  
      # Minimalist call with twice the same anode
      reref = set_bipolar_reference(
          inst,
          ["EEG 001", "EEG 001", "EEG 002"],
          ["EEG 002", "EEG 003", "EEG 003"],
      )
      assert "EEG 001-EEG 002" in reref.ch_names
      assert "EEG 001-EEG 003" in reref.ch_names
  
      # Set multiple references at once
      reref = set_bipolar_reference(
          inst,
          ["EEG 001", "EEG 003"],
          ["EEG 002", "EEG 004"],
          ["bipolar1", "bipolar2"],
          [{"kind": FIFF.FIFFV_EOG_CH}, {"kind": FIFF.FIFFV_EOG_CH}],
      )
      a = inst.copy().pick(["EEG 001", "EEG 002", "EEG 003", "EEG 004"])
      a = np.concatenate(
          [
              a._data[..., :1, :] - a._data[..., 1:2, :],
              a._data[..., 2:3, :] - a._data[..., 3:4, :],
          ],
          axis=-2,
      )
      b = reref.copy().pick(["bipolar1", "bipolar2"])._data
      assert_allclose(a, b)
  
      # Test creating a bipolar reference that doesn't involve EEG channels:
      # it should not set the custom_ref_applied flag
      reref = set_bipolar_reference(
          inst,
          "MEG 0111",
          "MEG 0112",
          ch_info={"kind": FIFF.FIFFV_MEG_CH},
          verbose="error",
      )
      assert not reref.info["custom_ref_applied"]
      assert "MEG 0111-MEG 0112" in reref.ch_names
  
      # Test a battery of invalid inputs
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          "EEG 001",
          ["EEG 002", "EEG 003"],
          "bipolar",
      )
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          ["EEG 001", "EEG 002"],
          "EEG 003",
          "bipolar",
      )
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          ["bipolar1", "bipolar2"],
      )
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          "bipolar",
          ch_info=[{"foo": "bar"}, {"foo": "bar"}],
      )
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          ch_name="EEG 003",
      )
  
      # Test if bad anode/cathode raises error if on_bad="raise"
      inst.info["bads"] = ["EEG 001"]
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          on_bad="raise",
      )
      inst.info["bads"] = ["EEG 002"]
      pytest.raises(
          ValueError,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          on_bad="raise",
      )
  
      # Test if bad anode/cathode raises warning if on_bad="warn"
      inst.info["bads"] = ["EEG 001"]
      pytest.warns(
          RuntimeWarning,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          on_bad="warn",
      )
      inst.info["bads"] = ["EEG 002"]
      pytest.warns(
          RuntimeWarning,
          set_bipolar_reference,
          inst,
          "EEG 001",
          "EEG 002",
          on_bad="warn",
      )
  
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 6 for calling a global function with example usage: test_set_bipolar_reference(inst_type) without return types