code: def resample(
      image, target, mapping, shape, order=3, mode="constant", cval=0.0
  ):
      """Resample `image` to `target` CoordinateMap
  
      Use a "world-to-world" mapping `mapping` and spline interpolation of a
      `order`.
  
      Here, "world-to-world" refers to the fact that mapping should be a
      callable that takes a physical coordinate in "target" and gives a
      physical coordinate in "image".
  
      Parameters
      ----------
      image : Image instance
         image that is to be resampled.
      target : CoordinateMap
         coordinate map for output image.
      mapping : callable or tuple or array
         transformation from target.function_range to
         image.coordmap.function_range, i.e. 'world-to-world mapping'. Can
         be specified in three ways: a callable, a tuple (A, b)
         representing the mapping y=dot(A,x)+b or a representation of this
         mapping as an affine array, in homogeneous coordinates.
      shape : sequence of int
         shape of output array, in target.function_domain.
      order : int, optional
         what order of interpolation to use in ``scipy.ndimage``.
      mode : str, optional
          Points outside the boundaries of the input are filled according to the
          given mode ('constant', 'nearest', 'reflect' or 'wrap'). Default is
          'constant'.
      cval : scalar, optional
          Value used for points outside the boundaries of the input if
          mode='constant'. Default is 0.0.
  
      Returns
      -------
      output : Image instance
         Image has interpolated data and output.coordmap == target.
      """
      if not callable(mapping):
          if type(mapping) is type(()):
              mapping = from_matvec(*mapping)
          # image world to target world mapping
          TW2IW = AffineTransform(
              target.function_range,
              image.coordmap.function_range,
              mapping,
          )
      else:
          if isinstance(mapping, AffineTransform):
              TW2IW = mapping
          else:
              TW2IW = CoordinateMap(
                  target.function_range,
                  image.coordmap.function_range,
                  mapping,
              )
      # target voxel to image world mapping
      TV2IW = compose(TW2IW, target)
      # CoordinateMap describing mapping from target voxel to
      # image world coordinates
      if not isinstance(TV2IW, AffineTransform):
          # interpolator evaluates image at values image.coordmap.function_range,
          # i.e. physical coordinates rather than voxel coordinates
          grid = ArrayCoordMap.from_shape(TV2IW, shape)
          interp = ImageInterpolator(
              image, order=order, mode=mode, cval=cval
          )
          idata = interp.evaluate(grid.transposed_values)
          del interp
      else:  # it is an affine transform, but, what if we compose?
          TV2IV = compose(image.coordmap.inverse(), TV2IW)
          if isinstance(TV2IV, AffineTransform):  # still affine
              A, b = to_matvec(TV2IV.affine)
              idata = affine_transform(
                  image.get_fdata(),
                  A,
                  offset=b,
                  output_shape=shape,
                  order=order,
                  mode=mode,
                  cval=cval,
              )
          else:  # not affine anymore
              interp = ImageInterpolator(
                  image, order=order, mode=mode, cval=cval
              )
              grid = ArrayCoordMap.from_shape(TV2IV, shape)
              idata = interp.evaluate(grid.values)
              del interp
      return Image(idata, copy.copy(target))
  
file_path: nipy/nipy/algorithms/resample.py
imports: import copy
  import numpy
  from nibabel.affines import from_matvec, to_matvec
  from scipy.ndimage import affine_transform
  from ..core.api import AffineTransform, ArrayCoordMap, CoordinateMap, Image, compose
  from .interpolation import ImageInterpolator
label_desc: Use image node_id 2 for calling a global function with example usage: resample(image, target, mapping, shape, order, mode, cval) and returns: Image