code: def gamma_map(
      evoked,
      forward,
      noise_cov,
      alpha,
      loose="auto",
      depth=0.8,
      xyz_same_gamma=True,
      maxit=10000,
      tol=1e-6,
      update_mode=1,
      gammas=None,
      pca=True,
      return_residual=False,
      return_as_dipoles=False,
      rank=None,
      pick_ori=None,
      verbose=None,
  ):
      """Hierarchical Bayes (Gamma-MAP) sparse source localization method.
  
      Models each source time course using a zero-mean Gaussian prior with an
      unknown variance (gamma) parameter. During estimation, most gammas are
      driven to zero, resulting in a sparse source estimate, as in
      :footcite:`WipfEtAl2007` and :footcite:`WipfNagarajan2009`.
  
      For fixed-orientation forward operators, a separate gamma is used for each
      source time course, while for free-orientation forward operators, the same
      gamma is used for the three source time courses at each source space point
      (separate gammas can be used in this case by using xyz_same_gamma=False).
  
      Parameters
      ----------
      evoked : instance of Evoked
          Evoked data to invert.
      forward : dict
          Forward operator.
      noise_cov : instance of Covariance
          Noise covariance to compute whitener.
      alpha : float
          Regularization parameter (noise variance).
      %(loose)s
      %(depth)s
      xyz_same_gamma : bool
          Use same gamma for xyz current components at each source space point.
          Recommended for free-orientation forward solutions.
      maxit : int
          Maximum number of iterations.
      tol : float
          Tolerance parameter for convergence.
      update_mode : int
          Update mode, 1: MacKay update (default), 2: Modified MacKay update.
      gammas : array, shape=(n_sources,)
          Initial values for posterior variances (gammas). If None, a
          variance of 1.0 is used.
      pca : bool
          If True the rank of the data is reduced to the true dimension.
      return_residual : bool
          If True, the residual is returned as an Evoked instance.
      return_as_dipoles : bool
          If True, the sources are returned as a list of Dipole instances.
      %(rank_none)s
  
          .. versionadded:: 0.18
      %(pick_ori)s
      %(verbose)s
  
      Returns
      -------
      stc : instance of SourceEstimate
          Source time courses.
      residual : instance of Evoked
          The residual a.k.a. data not explained by the sources.
          Only returned if return_residual is True.
  
      References
      ----------
      .. footbibliography::
      """
      _check_reference(evoked)
  
      (
          forward,
          gain,
          gain_info,
          whitener,
          source_weighting,
          mask,
      ) = _prepare_gain(
          forward, evoked.info, noise_cov, pca, depth, loose, rank
      )
      _check_ori(pick_ori, forward)
  
      group_size = (
          1 if (is_fixed_orient(forward) or not xyz_same_gamma) else 3
      )
  
      # get the data
      sel = [
          evoked.ch_names.index(name) for name in gain_info["ch_names"]
      ]
      M = evoked.data[sel]
  
      # whiten the data
      logger.info("Whitening data matrix.")
      M = np.dot(whitener, M)
  
      # run the optimization
      X, active_set = _gamma_map_opt(
          M,
          gain,
          alpha,
          maxit=maxit,
          tol=tol,
          update_mode=update_mode,
          gammas=gammas,
          group_size=group_size,
          verbose=verbose,
      )
  
      if len(active_set) == 0:
          raise Exception("No active dipoles found. alpha is too big.")
  
      M_estimate = gain[:, active_set] @ X
  
      # Reapply weights to have correct unit
      X = _reapply_source_weighting(X, source_weighting, active_set)
  
      if return_residual:
          residual = _compute_residual(
              forward, evoked, X, active_set, gain_info
          )
  
      if group_size == 1 and not is_fixed_orient(forward):
          # make sure each source has 3 components
          idx, offset = divmod(active_set, 3)
          active_src = np.unique(idx)
          if len(X) < 3 * len(active_src):
              X_xyz = np.zeros(
                  (len(active_src), 3, X.shape[1]), dtype=X.dtype
              )
              idx = np.searchsorted(active_src, idx)
              X_xyz[idx, offset, :] = X
              X_xyz.shape = (len(active_src) * 3, X.shape[1])
              X = X_xyz
          active_set = (
              active_src[:, np.newaxis] * 3 + np.arange(3)
          ).ravel()
      source_weighting[source_weighting == 0] = 1  # zeros
      gain_active = gain[:, active_set] / source_weighting[active_set]
      del source_weighting
  
      tmin = evoked.times[0]
      tstep = 1.0 / evoked.info["sfreq"]
  
      if return_as_dipoles:
          out = _make_dipoles_sparse(
              X,
              active_set,
              forward,
              tmin,
              tstep,
              M,
              gain_active,
              active_is_idx=True,
          )
      else:
          out = _make_sparse_stc(
              X,
              active_set,
              forward,
              tmin,
              tstep,
              active_is_idx=True,
              pick_ori=pick_ori,
              verbose=verbose,
          )
  
      _log_exp_var(M, M_estimate, prefix="")
      logger.info("[done]")
  
      if return_residual:
          out = out, residual
  
      return out
  
imports: import numpy
  from ..fixes import _safe_svd
  from ..forward import is_fixed_orient
  from ..minimum_norm.inverse import _check_reference, _log_exp_var
  from ..utils import logger, verbose, warn
  from .mxne_inverse import _check_ori, _compute_residual, _make_dipoles_sparse, _make_sparse_stc, _prepare_gain, _reapply_source_weighting
label_desc: Use image node_id 2 for calling a global function with example usage: gamma_map(evoked, forward, noise_cov, alpha, loose, depth, xyz_same_gamma, maxit, tol, update_mode, gammas, pca, return_residual, return_as_dipoles, rank, pick_ori, verbose) and returns: out