code: def threshold_stats_img(
      stat_img=None,
      mask_img=None,
      alpha=0.001,
      threshold=3.0,
      height_control="fpr",
      cluster_threshold=0,
      two_sided=True,
  ):
      """Compute the required threshold level and return the thresholded map.
  
      Parameters
      ----------
      stat_img : Niimg-like object or None, optional
         Statistical image (presumably in z scale) whenever height_control
         is 'fpr' or None, stat_img=None is acceptable.
         If it is 'fdr' or 'bonferroni', an error is raised if stat_img is None.
  
      mask_img : Niimg-like object, optional,
          Mask image
  
      alpha : float or list, default=0.001
          Number controlling the thresholding (either a p-value or q-value).
          Its actual meaning depends on the height_control parameter.
          This function translates alpha to a z-scale threshold.
  
      threshold : float, default=3.0
         Desired threshold in z-scale.
         This is used only if height_control is None.
  
      height_control : string, or None optional, default='fpr'
          False positive control meaning of cluster forming
          threshold: None|'fpr'|'fdr'|'bonferroni'
  
      cluster_threshold : float, default=0
          cluster size threshold. In the returned thresholded map,
          sets of connected voxels (`clusters`) with size smaller
          than this number will be removed.
  
      two_sided : Bool, default=True
          Whether the thresholding should yield both positive and negative
          part of the maps.
          In that case, alpha is corrected by a factor of 2.
  
      Returns
      -------
      thresholded_map : Nifti1Image,
          The stat_map thresholded at the prescribed voxel- and cluster-level.
  
      threshold : float
          The voxel-level threshold used actually.
  
      Notes
      -----
      If the input image is not z-scaled (i.e. some z-transformed statistic)
      the computed threshold is not rigorous and likely meaningless
  
      See Also
      --------
      nilearn.image.threshold_img :
          Apply an explicit voxel-level (and optionally cluster-level) threshold
          without correction.
  
      """
      height_control_methods = [
          "fpr",
          "fdr",
          "bonferroni",
          "all-resolution-inference",
          None,
      ]
      if height_control not in height_control_methods:
          raise ValueError(
              "height control should be one of {0}",
              height_control_methods,
          )
  
      # if two-sided, correct alpha by a factor of 2
      alpha_ = alpha / 2 if two_sided else alpha
  
      # if height_control is 'fpr' or None, we don't need to look at the data
      # to compute the threshold
      if height_control == "fpr":
          threshold = norm.isf(alpha_)
  
      # In this case, and if stat_img is None, we return
      if stat_img is None:
          if height_control in ["fpr", None]:
              return None, threshold
          else:
              raise ValueError(
                  "Map_threshold requires stat_img not to be None "
                  "when the height_control procedure "
                  'is "bonferroni" or "fdr"'
              )
  
      if mask_img is None:
          masker = NiftiMasker(mask_strategy="background").fit(stat_img)
      else:
          masker = NiftiMasker(mask_img=mask_img).fit()
      stats = np.ravel(masker.transform(stat_img))
      n_voxels = np.size(stats)
  
      # Thresholding
      if two_sided:
          # replace stats by their absolute value
          stats = np.abs(stats)
  
      if height_control == "fdr":
          threshold = fdr_threshold(stats, alpha_)
      elif height_control == "bonferroni":
          threshold = norm.isf(alpha_ / n_voxels)
  
      # Apply cluster-extent thresholding with new cluster-defining threshold
      stat_img = threshold_img(
          img=stat_img,
          threshold=threshold,
          cluster_threshold=cluster_threshold,
          two_sided=two_sided,
          mask_img=mask_img,
          copy=True,
      )
  
      return stat_img, threshold
  
file_path: nilearn/nilearn/glm/thresholding.py
imports: import warnings
  import numpy
  from scipy.ndimage import label
  from scipy.stats import norm
  from nilearn.image import get_data, math_img, threshold_img
  from nilearn.maskers import NiftiMasker
label_desc: Use image node_id 5 for calling a global function with example usage: threshold_stats_img(stat_img, mask_img, alpha, threshold, height_control, cluster_threshold, two_sided) and returns: stat_img,  threshold, None,  threshold