docstr: Prepare the metric to compute intensity densities and gradients.
  
  The histograms will be setup to compute probability densities of
  intensities within the minimum and maximum values of `static` and
  `moving`
  
  Parameters
  ----------
  transform: instance of Transform
      the transformation with respect to whose parameters the gradient
      must be computed
  static : array, shape (S, R, C) or (R, C)
      static image
  moving : array, shape (S', R', C') or (R', C')
      moving image. The dimensions of the static (S, R, C) and moving
      (S', R', C') images do not need to be the same.
  static_grid2world : array (dim+1, dim+1), optional
      the grid-to-space transform of the static image. The default is
      None, implying the transform is the identity.
  moving_grid2world : array (dim+1, dim+1)
      the grid-to-space transform of the moving image. The default is
      None, implying the spacing along all axes is 1.
  starting_affine : array, shape (dim+1, dim+1), optional
      the pre-aligning matrix (an affine transform) that roughly aligns
      the moving image towards the static image. If None, no
      pre-alignment is performed. If a pre-alignment matrix is available,
      it is recommended to provide this matrix as `starting_affine`
      instead of manually transforming the moving image to reduce
      interpolation artifacts. The default is None, implying no
      pre-alignment is performed.
  static_mask : array, shape (S, R, C) or (R, C), optional
      static image mask that defines which pixels in the static image
      are used to calculate the mutual information.
  moving_mask : array, shape (S', R', C') or (R', C'), optional
      moving image mask that defines which pixels in the moving image
      are used to calculate the mutual information.
docstr_tok: ["Prepare","the","metric","to","compute","intensity","densities","and","gradients",".","The","histograms","will","be","setup","to","compute","probability","densities","of","intensities","within","the","minimum","and","maximum","values","of","`","static","`","and","`","moving","`","Parameters","--","--","--","--","--","transform",":","instance","of","Transform","the","transformation","with","respect","to","whose","parameters","the","gradient","must","be","computed","static",":","array",",","shape","(","S",",","R",",","C",")","or","(","R",",","C",")","static","image","moving",":","array",",","shape","(","S","'",",","R","'",",","C","'",")","or","(","R","'",",","C","'",")","moving","image",".","The","dimensions","of","the","static","(","S",",","R",",","C",")","and","moving","(","S","'",",","R","'",",","C","'",")","images","do","not","need","to","be","the","same",".","static_grid2world",":","array","(","dim+1",",","dim+1",")",",","optional","the","grid-to-space","transform","of","the","static","image",".","The","default","is","None",",","implying","the","transform","is","the","identity",".","moving_grid2world",":","array","(","dim+1",",","dim+1",")","the","grid-to-space","transform","of","the","moving","image",".","The","default","is","None",",","implying","the","spacing","along","all","axes","is","1.","starting_affine",":","array",",","shape","(","dim+1",",","dim+1",")",",","optional","the","pre-aligning","matrix","(","an","affine","transform",")","that","roughly","aligns","the","moving","image","towards","the","static","image",".","If","None",",","no","pre-alignment","is","performed",".","If","a","pre-alignment","matrix","is","available",",","it","is","recommended","to","provide","this","matrix","as","`","starting_affine","`","instead","of","manually","transforming","the","moving","image","to","reduce","interpolation","artifacts",".","The","default","is","None",",","implying","no","pre-alignment","is","performed",".","static_mask",":","array",",","shape","(","S",",","R",",","C",")","or","(","R",",","C",")",",","optional","static","image","mask","that","defines","which","pixels","in","the","static","image","are","used","to","calculate","the","mutual","information",".","moving_mask",":","array",",","shape","(","S","'",",","R","'",",","C","'",")","or","(","R","'",",","C","'",")",",","optional","moving","image","mask","that","defines","which","pixels","in","the","moving","image","are","used","to","calculate","the","mutual","information","."]
code: def setup(
      self,
      transform,
      static,
      moving,
      static_grid2world=None,
      moving_grid2world=None,
      starting_affine=None,
      static_mask=None,
      moving_mask=None,
  ):
      r"""Prepare the metric to compute intensity densities and gradients.
  
      The histograms will be setup to compute probability densities of
      intensities within the minimum and maximum values of `static` and
      `moving`
  
      Parameters
      ----------
      transform: instance of Transform
          the transformation with respect to whose parameters the gradient
          must be computed
      static : array, shape (S, R, C) or (R, C)
          static image
      moving : array, shape (S', R', C') or (R', C')
          moving image. The dimensions of the static (S, R, C) and moving
          (S', R', C') images do not need to be the same.
      static_grid2world : array (dim+1, dim+1), optional
          the grid-to-space transform of the static image. The default is
          None, implying the transform is the identity.
      moving_grid2world : array (dim+1, dim+1)
          the grid-to-space transform of the moving image. The default is
          None, implying the spacing along all axes is 1.
      starting_affine : array, shape (dim+1, dim+1), optional
          the pre-aligning matrix (an affine transform) that roughly aligns
          the moving image towards the static image. If None, no
          pre-alignment is performed. If a pre-alignment matrix is available,
          it is recommended to provide this matrix as `starting_affine`
          instead of manually transforming the moving image to reduce
          interpolation artifacts. The default is None, implying no
          pre-alignment is performed.
      static_mask : array, shape (S, R, C) or (R, C), optional
          static image mask that defines which pixels in the static image
          are used to calculate the mutual information.
      moving_mask : array, shape (S', R', C') or (R', C'), optional
          moving image mask that defines which pixels in the moving image
          are used to calculate the mutual information.
  
      """
      n = transform.get_number_of_parameters()
      self.metric_grad = np.zeros(n, dtype=np.float64)
      self.dim = len(static.shape)
      if moving_grid2world is None:
          moving_grid2world = np.eye(self.dim + 1)
      if static_grid2world is None:
          static_grid2world = np.eye(self.dim + 1)
      self.transform = transform
      self.static = np.array(static).astype(np.float64)
      self.moving = np.array(moving).astype(np.float64)
      self.static_grid2world = static_grid2world
      self.static_world2grid = npl.inv(static_grid2world)
      self.moving_grid2world = moving_grid2world
      self.moving_world2grid = npl.inv(moving_grid2world)
      (
          self.static_direction,
          self.static_spacing,
      ) = get_direction_and_spacings(static_grid2world, self.dim)
      (
          self.moving_direction,
          self.moving_spacing,
      ) = get_direction_and_spacings(moving_grid2world, self.dim)
      self.starting_affine = starting_affine
  
      P = np.eye(self.dim + 1)
      if self.starting_affine is not None:
          P = self.starting_affine
  
      self.affine_map = AffineMap(
          P,
          static.shape,
          static_grid2world,
          moving.shape,
          moving_grid2world,
      )
  
      # Masks can only be used with dense sampling
      if self.sampling_proportion in [None, 1.0]:
          if static_mask is not None:
              self.static_mask = static_mask.astype(np.int32)
          else:
              self.static_mask = None
  
          if moving_mask is not None:
              self.moving_mask = moving_mask.astype(np.int32)
          else:
              self.moving_mask = None
  
      else:
          if (static_mask is not None) or (moving_mask is not None):
              wm = "Masking is not implemented for sampling_proportion < 1, "
              wm = (
                  wm
                  + "setting static_mask = None and moving_mask = None"
              )
              warn(wm, UserWarning)
  
          self.static_mask, self.moving_mask = None, None
  
      if self.dim == 2:
          self.interp_method = interpolate_scalar_2d
      else:
          self.interp_method = interpolate_scalar_3d
  
      if self.sampling_proportion is None:
          self.samples = None
          self.ns = 0
      else:
          k = int(np.ceil(1.0 / self.sampling_proportion))
          shape = np.array(static.shape, dtype=np.int32)
          self.samples = sample_domain_regular(
              k, shape, static_grid2world
          )
          self.samples = np.array(self.samples)
          self.ns = self.samples.shape[0]
          # Add a column of ones (homogeneous coordinates)
          self.samples = np.hstack(
              (self.samples, np.ones(self.ns)[:, None])
          )
          if self.starting_affine is None:
              self.samples_prealigned = self.samples
          else:
              self.samples_prealigned = self.starting_affine.dot(
                  self.samples.T
              ).T
          # Sample the static image
          static_p = self.static_world2grid.dot(self.samples.T).T
          static_p = static_p[..., : self.dim]
          self.static_vals, inside = self.interp_method(
              static, static_p
          )
          self.static_vals = np.array(
              self.static_vals, dtype=np.float64
          )
      self.histogram.setup(
          self.static, self.moving, self.static_mask, self.moving_mask
      )
  
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 2 for calling the MutualInformationMetric obj's underlying member method code with example usage: obj.setup(transform, static, moving, static_grid2world, moving_grid2world, starting_affine, static_mask, moving_mask) without return types