code: def _fit_confidence(*, rd, Q, ori, whitener, fwd_data, sensors):
      # As describedd in the Xfit manual, confidence intervals can be calculated
      # by examining a linearization of model at the best-fitting location,
      # i.e. taking the Jacobian and using the whitener:
      #
      #     J = [∂b/∂x ∂b/∂y ∂b/∂z ∂b/∂Qx ∂b/∂Qy ∂b/∂Qz]
      #     C = (J.T C^-1 J)^-1
      #
      # And then the confidence interval is the diagonal of C, scaled by 1.96
      # (for 95% confidence).
      direction = np.empty((3, 3))
      # The coordinate system has the x axis aligned with the dipole orientation,
      direction[0] = ori
      # the z axis through the origin of the sphere model
      rvec = rd - fwd_data["inner_skull"]["r0"]
      direction[2] = rvec - ori * np.dot(ori, rvec)  # orthogonalize
      direction[2] /= np.linalg.norm(direction[2])
      # and the y axis perpendical with these forming a right-handed system.
      direction[1] = np.cross(direction[2], direction[0])
      assert np.allclose(np.dot(direction, direction.T), np.eye(3))
      # Get spatial deltas in dipole coordinate directions
      deltas = (-1e-4, 1e-4)
      J = np.empty((whitener.shape[0], 6))
      for ii in range(3):
          fwds = []
          for delta in deltas:
              this_r = rd[np.newaxis] + delta * direction[ii]
              fwds.append(
                  np.dot(
                      Q,
                      _dipole_forwards(
                          sensors=sensors,
                          fwd_data=fwd_data,
                          whitener=whitener,
                          rr=this_r,
                      )[0],
                  )
              )
          J[:, ii] = np.diff(fwds, axis=0)[0] / np.diff(deltas)[0]
      # Get current (Q) deltas in the dipole directions
      deltas = np.array([-0.01, 0.01]) * np.linalg.norm(Q)
      this_fwd = _dipole_forwards(
          sensors=sensors,
          fwd_data=fwd_data,
          whitener=whitener,
          rr=rd[np.newaxis],
      )[0]
      for ii in range(3):
          fwds = []
          for delta in deltas:
              fwds.append(np.dot(Q + delta * direction[ii], this_fwd))
          J[:, ii + 3] = np.diff(fwds, axis=0)[0] / np.diff(deltas)[0]
      # J is already whitened, so we don't need to do np.dot(whitener, J).
      # However, the units in the Jacobian are potentially quite different,
      # so we need to do some normalization during inversion, then revert.
      direction_norm = np.linalg.norm(J[:, :3])
      Q_norm = np.linalg.norm(J[:, 3:5])  # omit possible zero Z
      norm = np.array([direction_norm] * 3 + [Q_norm] * 3)
      J /= norm
      J = np.dot(J.T, J)
      C = pinvh(J, rtol=1e-14)
      C /= norm
      C /= norm[:, np.newaxis]
      conf = 1.96 * np.sqrt(np.diag(C))
      # The confidence volume of the dipole location is obtained from by
      # taking the eigenvalues of the upper left submatrix and computing
      # v = 4π/3 √(c^3 λ1 λ2 λ3) with c = 7.81, or:
      vol_conf = (
          4
          * np.pi
          / 3.0
          * np.sqrt(
              476.379541 * np.prod(eigh(C[:3, :3], eigvals_only=True))
          )
      )
      conf = np.concatenate([conf, [vol_conf]])
      # Now we reorder and subselect the proper columns:
      # vol, depth, long, trans, Qlong, Qtrans (discard Qdepth, assumed zero)
      conf = conf[[6, 2, 0, 1, 3, 4]]
      return conf
  
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 14 for calling a global function with example usage: _fit_confidence() and returns: conf