code: def clean_img(
      imgs,
      runs=None,
      detrend=True,
      standardize=True,
      confounds=None,
      low_pass=None,
      high_pass=None,
      t_r=None,
      ensure_finite=False,
      mask_img=None,
      **kwargs,
  ):
      """Improve :term:`SNR` on masked :term:`fMRI` signals.
  
      This function can do several things on the input signals, in
      the following order:
  
      - detrend
      - low- and high-pass filter
      - remove confounds
      - standardize
  
      Low-pass filtering improves specificity.
  
      High-pass filtering should be kept small, to keep some sensitivity.
  
      Filtering is only meaningful on evenly-sampled signals.
  
      According to Lindquist et al. (2018), removal of confounds will be done
      orthogonally to temporal filters (low- and/or high-pass filters), if both
      are specified.
  
      .. versionadded:: 0.2.5
  
      Parameters
      ----------
      imgs : Niimg-like object
          4D image. The signals in the last dimension are filtered (see
          :ref:`extracting_data` for a detailed description of the valid input
          types).
  
      runs : :class:`numpy.ndarray`, default=``None``
          Add a run level to the cleaning process. Each run will be
          cleaned independently. Must be a 1D array of n_samples elements.
  
          .. warning::
  
              'runs' replaces 'sessions' after release 0.10.0.
              Using 'session' will result in an error after release 0.10.0.
  
  
      detrend : :obj:`bool`, default=True
          If detrending should be applied on timeseries
          (before confound removal).
  
      standardize : :obj:`bool`, default=True
          If True, returned signals are set to unit variance.
  
      confounds : :class:`numpy.ndarray`, :obj:`str` or :obj:`list` of
          Confounds timeseries. optional
          Shape must be (instant number, confound number),
          or just (instant number,)
          The number of time instants in signals and confounds must be
          identical (i.e. signals.shape[0] == confounds.shape[0]).
          If a string is provided, it is assumed to be the name of a csv file
          containing signals as columns, with an optional one-line header.
          If a list is provided, all confounds are removed from the input
          signal, as if all were in the same array.
  
      low_pass : :obj:`float`, optional
          Low cutoff frequencies, in Hertz.
  
      high_pass : :obj:`float`, optional
          High cutoff frequencies, in Hertz.
  
      t_r : :obj:`float`, optional
          Repetition time, in second (sampling period). Set to None if not
          specified. Mandatory if used together with `low_pass` or `high_pass`.
  
      ensure_finite : :obj:`bool`, default=False
          If True, the non-finite values (NaNs and infs) found in the images
          will be replaced by zeros.
  
      mask_img : Niimg-like object, optional
          If provided, signal is only cleaned from voxels inside the mask. If
          mask is provided, it should have same shape and affine as imgs.
          If not provided, all voxels are used.
          See :ref:`extracting_data`.
  
      kwargs : dict
          Keyword arguments to be passed to functions called
          within this function.
          Kwargs prefixed with ``'clean__'`` will be passed to
          :func:`~nilearn.signal.clean`.
          Within :func:`~nilearn.signal.clean`, kwargs prefixed with
          ``'butterworth__'`` will be passed to the Butterworth filter
          (i.e., ``clean__butterworth__``).
  
      Returns
      -------
      Niimg-like object
          Input images, cleaned. Same shape as `imgs`.
  
      Notes
      -----
      Confounds removal is based on a projection on the orthogonal
      of the signal space [:footcite:t:`Friston1994`].
  
      Orthogonalization between temporal filters and confound removal is based on
      suggestions in [:footcite:t:`Lindquist2018`].
  
      References
      ----------
      .. footbibliography::
  
      See Also
      --------
          nilearn.signal.clean
  
      """
      # Avoid circular import
      from .. import masking
      from .image import new_img_like
  
      imgs_ = check_niimg_4d(imgs)
  
      # Check if t_r is set, otherwise propose t_r from imgs header
      if (
          low_pass is not None or high_pass is not None
      ) and t_r is None:
          # We raise an error, instead of using the header's t_r as this
          # value is considered to be non-reliable
          raise ValueError(
              "Repetition time (t_r) must be specified for filtering. "
              "You specified None. "
              f"imgs header suggest it to be {imgs.header.get_zooms()[3]}"
          )
  
      # Prepare signal for cleaning
      if mask_img is not None:
          signals = masking.apply_mask(imgs_, mask_img)
      else:
          signals = get_data(imgs_).reshape(-1, imgs_.shape[-1]).T
  
      # Clean signal
      clean_kwargs = {
          k[7:]: v for k, v in kwargs.items() if k.startswith("clean__")
      }
      data = signal.clean(
          signals,
          runs=runs,
          detrend=detrend,
          standardize=standardize,
          confounds=confounds,
          low_pass=low_pass,
          high_pass=high_pass,
          t_r=t_r,
          ensure_finite=ensure_finite,
          **clean_kwargs,
      )
  
      # Put results back into Niimg-like object
      if mask_img is not None:
          imgs_ = masking.unmask(data, mask_img)
      elif "sample_mask" in clean_kwargs:
          sample_shape = (
              imgs_.shape[:3] + clean_kwargs["sample_mask"].shape
          )
          imgs_ = new_img_like(
              imgs_, data.T.reshape(sample_shape), copy_header=True
          )
      else:
          imgs_ = new_img_like(
              imgs_, data.T.reshape(imgs_.shape), copy_header=True
          )
  
      return imgs_
  
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 20 for calling a global function with example usage: clean_img(imgs, runs, detrend, standardize, confounds, low_pass, high_pass, t_r, ensure_finite, mask_img) and returns: imgs_