code: def check_niimg(
      niimg,
      ensure_ndim=None,
      atleast_4d=False,
      dtype=None,
      return_iterator=False,
      wildcards=True,
  ):
      """Check that niimg is a proper 3D/4D niimg.
  
      Turn filenames into objects.
  
      Parameters
      ----------
      niimg : Niimg-like object
          See :ref:`extracting_data`.
          If niimg is a string or pathlib.Path, consider it as a path to
          Nifti image and call nibabel.load on it. The '~' symbol is expanded to
          the user home folder.
          If it is an object, check if the affine attribute present and that
          nilearn.image.get_data returns a result, raise TypeError otherwise.
  
      ensure_ndim : integer {3, 4}, optional
          Indicate the dimensionality of the expected niimg. An
          error is raised if the niimg is of another dimensionality.
  
      atleast_4d : boolean, default=False
          Indicates if a 3d image should be turned into a single-scan 4d niimg.
  
      dtype : {None, dtype, "auto"}, default=None
          Data type toward which the data should be converted. If "auto", the
          data will be converted to int32 if dtype is discrete and float32 if it
          is continuous. If None, data will not be converted to a new data type.
  
      return_iterator : boolean, default=False
          Returns an iterator on the content of the niimg file input.
  
      wildcards : boolean, default=True
          Use niimg as a regular expression to get a list of matching input
          filenames.
          If multiple files match, the returned list is sorted using an ascending
          order.
          If no file matches the regular expression, a ValueError exception is
          raised.
  
      Returns
      -------
      result : 3D/4D Niimg-like object
          Result can be nibabel.Nifti1Image or the input, as-is. It is guaranteed
          that the returned object has an affine attribute and that its data can
          be retrieved with nilearn.image.get_data.
  
      Notes
      -----
      In nilearn, special care has been taken to make image manipulation easy.
      This method is a kind of pre-requisite for any data processing method in
      nilearn because it checks if data have a correct format and loads them if
      necessary.
  
      Its application is idempotent.
  
      See Also
      --------
          iter_check_niimg, check_niimg_3d, check_niimg_4d
  
      """
      from ..image import new_img_like  # avoid circular imports
  
      niimg = stringify_path(niimg)
  
      if isinstance(niimg, str):
          if wildcards and ni.EXPAND_PATH_WILDCARDS:
              # Ascending sorting + expand user path
              filenames = sorted(glob.glob(os.path.expanduser(niimg)))
  
              # processing filenames matching globbing expression
              if len(filenames) >= 1 and glob.has_magic(niimg):
                  niimg = filenames  # iterable case
              # niimg is an existing filename
              elif [niimg] == filenames:
                  niimg = filenames[0]
              # No files found by glob
              elif glob.has_magic(niimg):
                  # No files matching the glob expression, warn the user
                  message = (
                      "No files matching the entered niimg expression: "
                      "'%s'.\n You may have left wildcards usage "
                      "activated: please set the global constant "
                      "'nilearn.EXPAND_PATH_WILDCARDS' to False to "
                      "deactivate this behavior."
                  ) % niimg
                  raise ValueError(message)
              else:
                  raise ValueError(f"File not found: '{niimg}'")
          elif not os.path.exists(niimg):
              raise ValueError(f"File not found: '{niimg}'")
  
      # in case of an iterable
      if hasattr(niimg, "__iter__") and not isinstance(niimg, str):
          if return_iterator:
              return iter_check_niimg(
                  niimg, ensure_ndim=ensure_ndim, dtype=dtype
              )
          return ni.image.concat_imgs(
              niimg, ensure_ndim=ensure_ndim, dtype=dtype
          )
  
      # Otherwise, it should be a filename or a SpatialImage, we load it
      niimg = load_niimg(niimg, dtype=dtype)
  
      if (
          ensure_ndim == 3
          and len(niimg.shape) == 4
          and niimg.shape[3] == 1
      ):
          # "squeeze" the image.
          data = safe_get_data(niimg)
          affine = niimg.affine
          niimg = new_img_like(niimg, data[:, :, :, 0], affine)
      if atleast_4d and len(niimg.shape) == 3:
          data = _get_data(niimg).view()
          data.shape = data.shape + (1,)
          niimg = new_img_like(niimg, data, niimg.affine)
  
      if ensure_ndim is not None and len(niimg.shape) != ensure_ndim:
          raise DimensionError(len(niimg.shape), ensure_ndim)
  
      if return_iterator:
          return (_index_img(niimg, i) for i in range(niimg.shape[3]))
  
      return niimg
  
file_path: nilearn/nilearn/_utils/niimg_conversions.py
imports: import glob
  import itertools
  import os.path
  import warnings
  import numpy
  from joblib import Memory
  import nilearn
  from .cache_mixin import cache
  from .exceptions import DimensionError
  from .helpers import stringify_path
  from .niimg import _get_data, load_niimg, safe_get_data
  from .path_finding import resolve_globbing
label_desc: Use image node_id 5 for calling a global function with example usage: check_niimg(niimg, ensure_ndim, atleast_4d, dtype, return_iterator, wildcards) and returns: niimg, ni, unknown, iter_check_niimg