docstr: Transform the input image applying this affine transform.
  
  This is a generic function to transform images using either this
  (direct) transform or its inverse.
  
  If applying the direct transform (`apply_inverse=False`):
      by default, the transformed image is sampled at a grid defined by
      `self.domain_shape` and `self.domain_grid2world`.
  If applying the inverse transform (`apply_inverse=True`):
      by default, the transformed image is sampled at a grid defined by
      `self.codomain_shape` and `self.codomain_grid2world`.
  
  If the sampling information was not provided at initialization of this
  transform then `sampling_grid_shape` is mandatory.
  
  Parameters
  ----------
  image :  2D or 3D array
      the image to be transformed
  interpolation : string, either 'linear' or 'nearest'
      the type of interpolation to be used, either 'linear'
      (for k-linear interpolation) or 'nearest' for nearest neighbor
  image_grid2world : array, shape (dim + 1, dim + 1), optional
      the grid-to-world transform associated with `image`.
      If None (the default), then the grid-to-world transform is assumed
      to be the identity.
  sampling_grid_shape : sequence, shape (dim,), optional
      the shape of the grid where the transformed image must be sampled.
      If None (the default), then `self.domain_shape` is used instead
      (which must have been set at initialization, otherwise an exception
      will be raised).
  sampling_grid2world : array, shape (dim + 1, dim + 1), optional
      the grid-to-world transform associated with the sampling grid
      (specified by `sampling_grid_shape`, or by default
      `self.domain_shape`). If None (the default), then the
      grid-to-world transform is assumed to be the identity.
  resample_only : Boolean, optional
      If False (the default) the affine transform is applied normally.
      If True, then the affine transform is not applied, and the input
      image is just re-sampled on the domain grid of this transform.
  apply_inverse : Boolean, optional
      If False (the default) the image is transformed from the codomain
      of this transform to its domain using the (direct) affine
      transform. Otherwise, the image is transformed from the domain
      of this transform to its codomain using the (inverse) affine
      transform.
  
  Returns
  -------
  transformed : array, shape `sampling_grid_shape` or `self.domain_shape`
      the transformed image, sampled at the requested grid
docstr_tok: ["Transform","the","input","image","applying","this","affine","transform",".","This","is","a","generic","function","to","transform","images","using","either","this","(","direct",")","transform","or","its","inverse",".","If","applying","the","direct","transform","(","`","apply_inverse=False","`",")",":","by","default",",","the","transformed","image","is","sampled","at","a","grid","defined","by","`","self.domain_shape","`","and","`","self.domain_grid2world","`",".","If","applying","the","inverse","transform","(","`","apply_inverse=True","`",")",":","by","default",",","the","transformed","image","is","sampled","at","a","grid","defined","by","`","self.codomain_shape","`","and","`","self.codomain_grid2world","`",".","If","the","sampling","information","was","not","provided","at","initialization","of","this","transform","then","`","sampling_grid_shape","`","is","mandatory",".","Parameters","--","--","--","--","--","image",":","2D","or","3D","array","the","image","to","be","transformed","interpolation",":","string",",","either","'linear","'","or","'nearest'","the","type","of","interpolation","to","be","used",",","either","'linear'","(","for","k-linear","interpolation",")","or","'nearest","'","for","nearest","neighbor","image_grid2world",":","array",",","shape","(","dim","+","1",",","dim","+","1",")",",","optional","the","grid-to-world","transform","associated","with","`","image","`",".","If","None","(","the","default",")",",","then","the","grid-to-world","transform","is","assumed","to","be","the","identity",".","sampling_grid_shape",":","sequence",",","shape","(","dim",",",")",",","optional","the","shape","of","the","grid","where","the","transformed","image","must","be","sampled",".","If","None","(","the","default",")",",","then","`","self.domain_shape","`","is","used","instead","(","which","must","have","been","set","at","initialization",",","otherwise","an","exception","will","be","raised",")",".","sampling_grid2world",":","array",",","shape","(","dim","+","1",",","dim","+","1",")",",","optional","the","grid-to-world","transform","associated","with","the","sampling","grid","(","specified","by","`","sampling_grid_shape","`",",","or","by","default","`","self.domain_shape","`",")",".","If","None","(","the","default",")",",","then","the","grid-to-world","transform","is","assumed","to","be","the","identity",".","resample_only",":","Boolean",",","optional","If","False","(","the","default",")","the","affine","transform","is","applied","normally",".","If","True",",","then","the","affine","transform","is","not","applied",",","and","the","input","image","is","just","re-sampled","on","the","domain","grid","of","this","transform",".","apply_inverse",":","Boolean",",","optional","If","False","(","the","default",")","the","image","is","transformed","from","the","codomain","of","this","transform","to","its","domain","using","the","(","direct",")","affine","transform",".","Otherwise",",","the","image","is","transformed","from","the","domain","of","this","transform","to","its","codomain","using","the","(","inverse",")","affine","transform",".","Returns","--","--","--","-","transformed",":","array",",","shape","`","sampling_grid_shape","`","or","`","self.domain_shape","`","the","transformed","image",",","sampled","at","the","requested","grid"]
code: def _apply_transform(
      self,
      image,
      interpolation="linear",
      image_grid2world=None,
      sampling_grid_shape=None,
      sampling_grid2world=None,
      resample_only=False,
      apply_inverse=False,
  ):
      """Transform the input image applying this affine transform.
  
      This is a generic function to transform images using either this
      (direct) transform or its inverse.
  
      If applying the direct transform (`apply_inverse=False`):
          by default, the transformed image is sampled at a grid defined by
          `self.domain_shape` and `self.domain_grid2world`.
      If applying the inverse transform (`apply_inverse=True`):
          by default, the transformed image is sampled at a grid defined by
          `self.codomain_shape` and `self.codomain_grid2world`.
  
      If the sampling information was not provided at initialization of this
      transform then `sampling_grid_shape` is mandatory.
  
      Parameters
      ----------
      image :  2D or 3D array
          the image to be transformed
      interpolation : string, either 'linear' or 'nearest'
          the type of interpolation to be used, either 'linear'
          (for k-linear interpolation) or 'nearest' for nearest neighbor
      image_grid2world : array, shape (dim + 1, dim + 1), optional
          the grid-to-world transform associated with `image`.
          If None (the default), then the grid-to-world transform is assumed
          to be the identity.
      sampling_grid_shape : sequence, shape (dim,), optional
          the shape of the grid where the transformed image must be sampled.
          If None (the default), then `self.domain_shape` is used instead
          (which must have been set at initialization, otherwise an exception
          will be raised).
      sampling_grid2world : array, shape (dim + 1, dim + 1), optional
          the grid-to-world transform associated with the sampling grid
          (specified by `sampling_grid_shape`, or by default
          `self.domain_shape`). If None (the default), then the
          grid-to-world transform is assumed to be the identity.
      resample_only : Boolean, optional
          If False (the default) the affine transform is applied normally.
          If True, then the affine transform is not applied, and the input
          image is just re-sampled on the domain grid of this transform.
      apply_inverse : Boolean, optional
          If False (the default) the image is transformed from the codomain
          of this transform to its domain using the (direct) affine
          transform. Otherwise, the image is transformed from the domain
          of this transform to its codomain using the (inverse) affine
          transform.
  
      Returns
      -------
      transformed : array, shape `sampling_grid_shape` or `self.domain_shape`
          the transformed image, sampled at the requested grid
  
      """
      # Verify valid interpolation requested
      if interpolation not in _interp_options:
          msg = "Unknown interpolation method: %s" % (interpolation,)
          raise ValueError(msg)
  
      # Obtain sampling grid
      if sampling_grid_shape is None:
          if apply_inverse:
              sampling_grid_shape = self.codomain_shape
          else:
              sampling_grid_shape = self.domain_shape
      if sampling_grid_shape is None:
          msg = "Unknown sampling info. Provide a valid sampling_grid_shape"
          raise ValueError(msg)
  
      dim = len(sampling_grid_shape)
      shape = np.array(sampling_grid_shape, dtype=np.int32)
  
      # Verify valid image dimension
      img_dim = len(image.shape)
      if img_dim < 2 or img_dim > 3:
          raise ValueError(
              "Undefined transform for dim: %d" % (img_dim,)
          )
  
      # Obtain grid-to-world transform for sampling grid
      if sampling_grid2world is None:
          if apply_inverse:
              sampling_grid2world = self.codomain_grid2world
          else:
              sampling_grid2world = self.domain_grid2world
      if sampling_grid2world is None:
          sampling_grid2world = np.eye(dim + 1)
  
      # Obtain world-to-grid transform for input image
      if image_grid2world is None:
          if apply_inverse:
              image_grid2world = self.domain_grid2world
          else:
              image_grid2world = self.codomain_grid2world
          if image_grid2world is None:
              image_grid2world = np.eye(dim + 1)
      image_world2grid = npl.inv(image_grid2world)
  
      # Compute the transform from sampling grid to input image grid
      if apply_inverse:
          aff = self.affine_inv
      else:
          aff = self.affine
  
      if (aff is None) or resample_only:
          comp = image_world2grid.dot(sampling_grid2world)
      else:
          comp = image_world2grid.dot(aff.dot(sampling_grid2world))
  
      # Transform the input image
      if interpolation == "linear":
          image = image.astype(np.float64)
      transformed = _transform_method[(dim, interpolation)](
          image, shape, comp
      )
      return transformed
  
imports: from warnings import warn
  import numpy
  import numpy.linalg
  import scipy.ndimage
  from dipy.core.optimize import Optimizer
  from dipy.core.interpolation import interpolate_scalar_2d, interpolate_scalar_3d
  from dipy.align import vector_fields
  from dipy.align import VerbosityLevels
  from dipy.align.parzenhist import ParzenJointHistogram, sample_domain_regular, compute_parzen_mi
  from dipy.align.imwarp import get_direction_and_spacings, ScaleSpace
  from dipy.align.scalespace import IsotropicScaleSpace
  from dipy.utils.deprecator import deprecated_params
label_desc: Use image node_id 7 for calling the AffineMap obj's underlying member method code with example usage: obj._apply_transform(image, interpolation, image_grid2world, sampling_grid_shape, sampling_grid2world, resample_only, apply_inverse) and returns: transformed