code: def _setup_bem(
      bem, bem_extra, neeg, mri_head_t, allow_none=False, verbose=None
  ):
      """Set up a BEM for forward computation, making a copy and modifying."""
      if allow_none and bem is None:
          return None
      logger.info("")
      _validate_type(bem, ("path-like", ConductorModel), bem)
      if not isinstance(bem, ConductorModel):
          logger.info(
              "Setting up the BEM model using %s...\n" % bem_extra
          )
          bem = read_bem_solution(bem)
      else:
          bem = bem.copy()
      if bem["is_sphere"]:
          logger.info("Using the sphere model.\n")
          if len(bem["layers"]) == 0 and neeg > 0:
              raise RuntimeError(
                  "Spherical model has zero shells, cannot use "
                  "with EEG data"
              )
          if bem["coord_frame"] != FIFF.FIFFV_COORD_HEAD:
              raise RuntimeError(
                  "Spherical model is not in head coordinates"
              )
      else:
          if bem["surfs"][0]["coord_frame"] != FIFF.FIFFV_COORD_MRI:
              raise RuntimeError(
                  "BEM is in %s coordinates, should be in MRI"
                  % (_coord_frame_name(bem["surfs"][0]["coord_frame"]),)
              )
          if neeg > 0 and len(bem["surfs"]) == 1:
              raise RuntimeError(
                  "Cannot use a homogeneous (1-layer BEM) model "
                  "for EEG forward calculations, consider "
                  "using a 3-layer BEM instead"
              )
          logger.info(
              "Employing the head->MRI coordinate transform with the "
              "BEM model."
          )
          # fwd_bem_set_head_mri_t: Set the coordinate transformation
          bem["head_mri_t"] = _ensure_trans(mri_head_t, "head", "mri")
          logger.info(
              "BEM model %s is now set up" % op.split(bem_extra)[1]
          )
          logger.info("")
      return bem
  
imports: import os
  import os.path
  from contextlib import contextmanager
  from copy import deepcopy
  from pathlib import Path
  import numpy
  from .._fiff.compensator import get_current_comp, make_compensator
  from .._fiff.constants import FIFF, FWD
  from .._fiff.meas_info import Info, read_info
  from .._fiff.pick import _has_kit_refs, pick_info, pick_types
  from .._fiff.tag import _loc_to_coil_trans, _loc_to_eeg_loc
  from ..bem import ConductorModel, _bem_find_surface, read_bem_solution
  from ..source_estimate import VolSourceEstimate
  from ..source_space._source_space import _complete_vol_src, _ensure_src, _filter_source_spaces, _make_discrete_source_space
  from ..surface import _CheckInside, _normalize_vectors
  from ..transforms import Transform, _coord_frame_name, _ensure_trans, _get_trans, _print_coord_trans, apply_trans, invert_transform, transform_surface_to
  from ..utils import _check_fname, _pl, _validate_type, logger, verbose, warn
  from ._compute_forward import _compute_forwards
  from .forward import _FWD_ORDER, Forward, _merge_fwds, convert_forward_solution
label_desc: Use image node_id 8 for calling a global function with example usage: _setup_bem(bem, bem_extra, neeg, mri_head_t, allow_none, verbose) and returns: bem, None