code: def compute_contrast(
      labels, regression_result, con_val, stat_type=None
  ):
      """Compute the specified :term:`contrast` given an estimated glm.
  
      Parameters
      ----------
      labels : array of shape (n_voxels,)
          A map of values on voxels used to identify the corresponding model
  
      regression_result : dict
          With keys corresponding to the different labels
          values are RegressionResults instances corresponding to the voxels.
  
      con_val : numpy.ndarray of shape (p) or (q, p)
          Where q = number of :term:`contrast` vectors
          and p = number of regressors.
  
      stat_type : {None, 't', 'F'}, optional
          Type of the :term:`contrast`.
          If None, then defaults to 't' for 1D `con_val`
          and 'F' for 2D `con_val`.
  
      contrast_type :
  
          .. deprecated:: 0.11.0
  
              Use ``stat_type`` instead (see above).
  
      Returns
      -------
      con : Contrast instance,
          Yields the statistics of the :term:`contrast`
          (:term:`effects<Parameter Estimate>`, variance, p-values).
  
      """
      con_val = np.asarray(con_val)
      dim = 1
      if con_val.ndim > 1:
          dim = con_val.shape[0]
  
      if stat_type is None:
          stat_type = "t" if dim == 1 else "F"
  
      acceptable_stat_types = ["t", "F"]
      if stat_type not in acceptable_stat_types:
          raise ValueError(
              f"'{stat_type}' is not a known contrast type. "
              f"Allowed types are {acceptable_stat_types}."
          )
  
      if stat_type == "t":
          effect_ = np.zeros((1, labels.size))
          var_ = np.zeros(labels.size)
          for label_ in regression_result:
              label_mask = labels == label_
              reg = regression_result[label_].Tcontrast(con_val)
              effect_[:, label_mask] = reg.effect.T
              var_[label_mask] = (reg.sd**2).T
  
      elif stat_type == "F":
          from scipy.linalg import sqrtm
  
          effect_ = np.zeros((dim, labels.size))
          var_ = np.zeros(labels.size)
          # TODO
          # explain why we cannot simply do
          # reg = regression_result[label_].Tcontrast(con_val)
          # like above or refactor the code so it can be done
          for label_ in regression_result:
              label_mask = labels == label_
              reg = regression_result[label_]
              con_val = pad_contrast(
                  con_val=con_val, theta=reg.theta, stat_type=stat_type
              )
              cbeta = np.atleast_2d(np.dot(con_val, reg.theta))
              invcov = np.linalg.inv(
                  np.atleast_2d(
                      reg.vcov(matrix=con_val, dispersion=1.0)
                  )
              )
              wcbeta = np.dot(sqrtm(invcov), cbeta)
              rss = reg.dispersion
              effect_[:, label_mask] = wcbeta
              var_[label_mask] = rss
  
      dof_ = regression_result[label_].df_residuals
      return Contrast(
          effect=effect_,
          variance=var_,
          dim=dim,
          dof=dof_,
          contrast_type=stat_type,
      )
  
imports: from warnings import warn
  import numpy
  import pandas
  import scipy.stats
  from nilearn._utils import rename_parameters
  from nilearn.glm._utils import pad_contrast, z_score
  from nilearn.maskers import NiftiMasker
label_desc: Use image node_id 2 for calling a global function with example usage: compute_contrast(labels, regression_result, con_val, stat_type) and returns: Contrast