code: def _fit_dipole(
      min_dist_to_inner_skull,
      B_orig,
      t,
      guess_rrs,
      guess_data,
      *,
      sensors,
      fwd_data,
      whitener,
      fmin_cobyla,
      ori,
      rank,
      rhoend,
  ):
      """Fit a single bit of data."""
      B = np.dot(whitener, B_orig)
  
      # make constraint function to keep the solver within the inner skull
      if "rr" in fwd_data["inner_skull"]:  # bem
          surf = fwd_data["inner_skull"]
          constraint = partial(
              _surface_constraint,
              surf=surf,
              min_dist_to_inner_skull=min_dist_to_inner_skull,
          )
      else:  # sphere
          surf = None
          constraint = partial(
              _sphere_constraint,
              r0=fwd_data["inner_skull"]["r0"],
              R_adj=fwd_data["inner_skull"]["R"]
              - min_dist_to_inner_skull,
          )
  
      # Find a good starting point (find_best_guess in C)
      B2 = np.dot(B, B)
      if B2 == 0:
          warn("Zero field found for time %s" % t)
          return np.zeros(3), 0, np.zeros(3), 0, B
  
      idx = np.argmin(
          [
              _fit_eval(
                  guess_rrs[[fi], :],
                  B,
                  B2,
                  fwd_svd=fwd_svd,
                  fwd_data=None,
                  sensors=None,
                  whitener=None,
                  lwork=None,
              )
              for fi, fwd_svd in enumerate(guess_data["fwd_svd"])
          ]
      )
      x0 = guess_rrs[idx]
      lwork = _svd_lwork((3, B.shape[0]))
      fun = partial(
          _fit_eval,
          B=B,
          B2=B2,
          fwd_data=fwd_data,
          whitener=whitener,
          lwork=lwork,
          sensors=sensors,
          fwd_svd=None,
      )
  
      # Tested minimizers:
      #    Simplex, BFGS, CG, COBYLA, L-BFGS-B, Powell, SLSQP, TNC
      # Several were similar, but COBYLA won for having a handy constraint
      # function we can use to ensure we stay inside the inner skull /
      # smallest sphere
      rd_final = fmin_cobyla(
          fun,
          x0,
          (constraint,),
          consargs=(),
          rhobeg=5e-2,
          rhoend=rhoend,
          disp=False,
      )
  
      # simplex = _make_tetra_simplex() + x0
      # _simplex_minimize(simplex, 1e-4, 2e-4, fun)
      # rd_final = simplex[0]
  
      # Compute the dipole moment at the final point
      Q, gof, residual_noproj, n_comp = _fit_Q(
          sensors=sensors,
          fwd_data=fwd_data,
          whitener=whitener,
          B=B,
          B2=B2,
          B_orig=B_orig,
          rd=rd_final,
          ori=ori,
      )
      khi2 = (1 - gof) * B2
      nfree = rank - n_comp
      amp = np.sqrt(np.dot(Q, Q))
      norm = 1.0 if amp == 0.0 else amp
      ori = Q / norm
  
      conf = _fit_confidence(
          sensors=sensors,
          rd=rd_final,
          Q=Q,
          ori=ori,
          whitener=whitener,
          fwd_data=fwd_data,
      )
  
      msg = "---- Fitted : %7.1f ms" % (1000.0 * t)
      if surf is not None:
          dist_to_inner_skull = _compute_nearest(
              surf["rr"], rd_final[np.newaxis, :], return_dists=True
          )[1][0]
          msg += ", distance to inner skull : %2.4f mm" % (
              dist_to_inner_skull * 1000.0
          )
  
      logger.info(msg)
      return rd_final, amp, ori, gof, conf, khi2, nfree, residual_noproj
  
imports: import functools
  import re
  from copy import deepcopy
  from functools import partial
  import numpy
  from scipy.linalg import eigh
  from scipy.optimize import fmin_cobyla
  from ._fiff.constants import FIFF
  from ._fiff.pick import pick_types
  from ._fiff.proj import _needs_eeg_average_ref_proj, make_projector
  from ._freesurfer import _get_aseg, head_to_mni, head_to_mri, read_freesurfer_lut
  from .bem import _bem_find_surface, _bem_surf_name, _fit_sphere
  from .cov import _ensure_cov, compute_whitener
  from .evoked import _aspect_rev, _read_evoked, _write_evokeds
  from .fixes import _safe_svd, pinvh
  from .forward._compute_forward import _compute_forwards_meeg, _prep_field_computation
  from .forward._make_forward import _get_trans, _prep_eeg_channels, _prep_meg_channels, _setup_bem
  from .parallel import parallel_func
  from .source_space._source_space import SourceSpaces, _make_volume_source_space
  from .surface import _compute_nearest, _points_outside_surface, transform_surface_to
  from .transforms import _coord_frame_name, _print_coord_trans, apply_trans
  from .utils import ExtendedTimeMixin, TimeMixin, _check_fname, _check_option, _get_blas_funcs, _pl, _repeated_svd, _svd_lwork, _time_mask, _validate_type, _verbose_safe_false, check_fname, copy_function_doc_to_method_doc, fill_doc, logger, verbose, warn
  from .viz import plot_dipole_amplitudes, plot_dipole_locations
  from .viz.evoked import _plot_evoked
label_desc: Use image node_id 17 for calling a global function with example usage: _fit_dipole(min_dist_to_inner_skull, B_orig, t, guess_rrs, guess_data) and returns: rd_final,  amp,  ori,  gof,  conf,  khi2,  nfree,  residual_noproj, np,  int,  np,  int,  B