code: def math_img(formula, **imgs):
      """Interpret a numpy based string formula using niimg in named parameters.
  
      .. versionadded:: 0.2.3
  
      Parameters
      ----------
      formula : :obj:`str`
          The mathematical formula to apply to image internal data. It can use
          numpy imported as 'np'.
  
      imgs : images (:class:`~nibabel.nifti1.Nifti1Image` or file names)
          Keyword arguments corresponding to the variables in the formula as
          Nifti images. All input images should have the same geometry (shape,
          affine).
  
      Returns
      -------
      :class:`~nibabel.nifti1.Nifti1Image`
          Result of the formula as a Nifti image. Note that the dimension of the
          result image can be smaller than the input image. The affine is the
          same as the input image.
  
      See Also
      --------
      nilearn.image.mean_img : To simply compute the mean of multiple images
  
      Examples
      --------
      Let's load an image using nilearn datasets module::
  
       >>> from nilearn import datasets
       >>> anatomical_image = datasets.load_mni152_template()
  
      Now we can use any numpy function on this image::
  
       >>> from nilearn.image import math_img
       >>> log_img = math_img("np.log(img)", img=anatomical_image)
  
      We can also apply mathematical operations on several images::
  
       >>> result_img = math_img("img1 + img2",
       ...                       img1=anatomical_image, img2=log_img)
  
      Notes
      -----
      This function is the Python equivalent of ImCal in SPM or fslmaths
      in FSL.
  
      """
      try:
          niimgs = [check_niimg(image) for image in imgs.values()]
          check_same_fov(*niimgs, raise_error=True)
      except Exception as exc:
          exc.args = (
              "Input images cannot be compared, "
              f"you provided '{imgs.values()}',",
          ) + exc.args
          raise
  
      # Computing input data as a dictionary of numpy arrays. Keep a reference
      # niimg for building the result as a new niimg.
      niimg = None
      data_dict = {}
      for key, img in imgs.items():
          niimg = check_niimg(img)
          data_dict[key] = safe_get_data(niimg)
  
      # Add a reference to numpy in the kwargs of eval so that numpy functions
      # can be called from there.
      data_dict["np"] = np
      try:
          result = eval(formula, data_dict)
      except Exception as exc:
          exc.args = (
              f"Input formula couldn't be processed, you provided '{formula}',",
          ) + exc.args
          raise
  
      return new_img_like(niimg, result, niimg.affine)
  
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 18 for calling a global function with example usage: math_img(formula) and returns: new_img_like