docstr: Prepares the metric to compute one displacement field iteration.
  
  Pre-computes the transfer functions (hidden random variables) and
  variances of the estimators. Also pre-computes the gradient of both
  input images. Note that once the images are transformed to the opposite
  modality, the gradient of the transformed images can be used with the
  gradient of the corresponding modality in the same fashion as
  diff-demons does for mono-modality images. If the flag
  self.use_double_gradient is True these gradients are averaged.
docstr_tok: ["Prepares","the","metric","to","compute","one","displacement","field","iteration",".","Pre-computes","the","transfer","functions","(","hidden","random","variables",")","and","variances","of","the","estimators",".","Also","pre-computes","the","gradient","of","both","input","images",".","Note","that","once","the","images","are","transformed","to","the","opposite","modality",",","the","gradient","of","the","transformed","images","can","be","used","with","the","gradient","of","the","corresponding","modality","in","the","same","fashion","as","diff-demons","does","for","mono-modality","images",".","If","the","flag","self.use_double_gradient","is","True","these","gradients","are","averaged","."]
code: def initialize_iteration(self):
      r"""Prepares the metric to compute one displacement field iteration.
  
      Pre-computes the transfer functions (hidden random variables) and
      variances of the estimators. Also pre-computes the gradient of both
      input images. Note that once the images are transformed to the opposite
      modality, the gradient of the transformed images can be used with the
      gradient of the corresponding modality in the same fashion as
      diff-demons does for mono-modality images. If the flag
      self.use_double_gradient is True these gradients are averaged.
      """
      sampling_mask = self.static_image_mask * self.moving_image_mask
      self.sampling_mask = sampling_mask
      staticq, self.staticq_levels, hist = self.quantize(
          self.static_image, self.q_levels
      )
      staticq = np.array(staticq, dtype=np.int32)
      self.staticq_levels = np.array(self.staticq_levels)
      staticq_means, staticq_vars = self.compute_stats(
          sampling_mask, self.moving_image, self.q_levels, staticq
      )
      staticq_means[0] = 0
      self.staticq_means = np.array(staticq_means)
      self.staticq_variances = np.array(staticq_vars)
      self.staticq_sigma_sq_field = self.staticq_variances[staticq]
      self.staticq_means_field = self.staticq_means[staticq]
  
      self.gradient_moving = np.empty(
          shape=self.moving_image.shape + (self.dim,), dtype=floating
      )
  
      for i, grad in enumerate(gradient(self.moving_image)):
          self.gradient_moving[..., i] = grad
  
      # Convert moving image's gradient field from voxel to physical space
      if self.moving_spacing is not None:
          self.gradient_moving /= self.moving_spacing
      if self.moving_direction is not None:
          self.reorient_vector_field(
              self.gradient_moving, self.moving_direction
          )
  
      self.gradient_static = np.empty(
          shape=self.static_image.shape + (self.dim,), dtype=floating
      )
  
      for i, grad in enumerate(gradient(self.static_image)):
          self.gradient_static[..., i] = grad
  
      # Convert moving image's gradient field from voxel to physical space
      if self.static_spacing is not None:
          self.gradient_static /= self.static_spacing
      if self.static_direction is not None:
          self.reorient_vector_field(
              self.gradient_static, self.static_direction
          )
  
      movingq, self.movingq_levels, hist = self.quantize(
          self.moving_image, self.q_levels
      )
      movingq = np.array(movingq, dtype=np.int32)
      self.movingq_levels = np.array(self.movingq_levels)
      movingq_means, movingq_variances = self.compute_stats(
          sampling_mask, self.static_image, self.q_levels, movingq
      )
      movingq_means[0] = 0
      self.movingq_means = np.array(movingq_means)
      self.movingq_variances = np.array(movingq_variances)
      self.movingq_sigma_sq_field = self.movingq_variances[movingq]
      self.movingq_means_field = self.movingq_means[movingq]
      if self.use_double_gradient:
          for i, grad in enumerate(gradient(self.staticq_means_field)):
              self.gradient_moving[..., i] += grad
  
          for i, grad in enumerate(gradient(self.movingq_means_field)):
              self.gradient_static[..., i] += grad
  
imports: import abc
  import numpy
  from numpy import gradient
  from scipy import ndimage
  from dipy.align import vector_fields
  from dipy.align import sumsqdiff
  from dipy.align import crosscorr
  from dipy.align import expectmax
  from dipy.align import floating
label_desc: Use image node_id 3 for calling the EMMetric obj's underlying member method code with example usage: obj.initialize_iteration() without return types