code: def threshold_img(
      img,
      threshold,
      cluster_threshold=0,
      two_sided=True,
      mask_img=None,
      copy=True,
  ):
      """Threshold the given input image, mostly statistical or atlas images.
  
      Thresholding can be done based on direct image intensities or selection
      threshold with given percentile.
  
      .. versionchanged:: 0.9.0
          New ``cluster_threshold`` and ``two_sided`` parameters added.
  
      .. versionadded:: 0.2
  
      Parameters
      ----------
      img : a 3D/4D Niimg-like object
          Image containing statistical or atlas maps which should be thresholded.
  
      threshold : :obj:`float` or :obj:`str`
          Voxels with intensities less than the requested threshold
          will be set to zero.
          Those with intensities greater or equal than the requested threshold
          will keep their original value.
          If float, we threshold the image based on image intensities.
          The given value should be within the range of minimum and maximum
          intensity of the input image.
          If string, it should finish with percent sign e.g. "80%"
          and we threshold based on the score obtained
          using this percentile on the image data.
          The given string should be within the range of "0%" to "100%".
          The percentile rank is computed using
          :func:`scipy.stats.scoreatpercentile`.
  
      cluster_threshold : :obj:`float`, default=0
          Cluster size threshold, in voxels. In the returned thresholded map,
          sets of connected voxels (``clusters``) with size smaller
          than this number will be removed.
  
          .. versionadded:: 0.9.0
  
      two_sided : :obj:`bool`, default=True
          Whether the thresholding should yield both positive and negative
          part of the maps.
  
          .. versionadded:: 0.9.0
  
      mask_img : Niimg-like object, default=None
          Mask image applied to mask the input data.
          If None, no masking will be applied.
  
      copy : :obj:`bool`, default=True
          If True, input array is not modified. True by default: the filtering
          is not performed in-place.
  
      Returns
      -------
      :class:`~nibabel.nifti1.Nifti1Image`
          Thresholded image of the given input image.
  
      See Also
      --------
      nilearn.glm.threshold_stats_img :
          Threshold a statistical image using the alpha value, optionally with
          false positive control.
  
      """
      from .. import masking
      from . import resampling
  
      img = check_niimg(img)
      img_data = safe_get_data(img, ensure_finite=True, copy_data=copy)
      affine = img.affine
  
      if mask_img is not None:
          mask_img = check_niimg_3d(mask_img)
          if not check_same_fov(img, mask_img):
              mask_img = resampling.resample_img(
                  mask_img,
                  target_affine=affine,
                  target_shape=img.shape[:3],
                  interpolation="nearest",
              )
  
          mask_data, _ = masking.load_mask_img(mask_img)
          # Set as 0 for the values which are outside of the mask
          img_data[mask_data == 0.0] = 0.0
  
      cutoff_threshold = check_threshold(
          threshold,
          img_data,
          percentile_func=scoreatpercentile,
          name="threshold",
      )
  
      # Apply threshold
      if two_sided:
          img_data[np.abs(img_data) < cutoff_threshold] = 0.0
      else:
          img_data[img_data < cutoff_threshold] = 0.0
  
      # Expand to 4D to support both 3D and 4D
      expand_to_4d = img_data.ndim == 3
      if expand_to_4d:
          img_data = img_data[:, :, :, None]
  
      # Perform cluster thresholding, if requested
      if cluster_threshold > 0:
          for i_vol in range(img_data.shape[3]):
              img_data[..., i_vol] = _apply_cluster_size_threshold(
                  img_data[..., i_vol],
                  cluster_threshold,
              )
  
      if expand_to_4d:
          # Reduce back to 3D
          img_data = img_data[:, :, :, 0]
  
      # Reconstitute img object
      thresholded_img = new_img_like(img, img_data, affine)
  
      return thresholded_img
  
imports: import collections.abc
  import copy
  import itertools
  import warnings
  import nibabel
  import numpy
  from joblib import Memory, Parallel, delayed
  from scipy.ndimage import gaussian_filter1d, generate_binary_structure, label
  from scipy.stats import scoreatpercentile
  from ..None import signal
  from .._utils import _repr_niimgs, as_ndarray, check_niimg, check_niimg_3d, check_niimg_4d, fill_doc
  from .._utils.exceptions import DimensionError
  from .._utils.helpers import rename_parameters, stringify_path
  from .._utils.niimg import _get_data, safe_get_data
  from .._utils.niimg_conversions import _index_img, check_same_fov, iter_check_niimg
  from .._utils.param_validation import check_threshold
  from .._utils.path_finding import resolve_globbing
label_desc: Use image node_id 17 for calling a global function with example usage: threshold_img(img, threshold, cluster_threshold, two_sided, mask_img, copy) and returns: thresholded_img