docstr: Parameters
  ----------
  static_image_files : string
      Path of the static image file.
  
  moving_image_files : string
      Path to the moving image file.
  
  prealign_file : string, optional
      The text file containing pre alignment information via an
       affine matrix.
  
  inv_static : boolean, optional
      Apply the inverse mapping to the static image.
  
  level_iters : variable int, optional
      The number of iterations at each level of the gaussian pyramid.
  
  metric : string, optional
      The metric to be used.
      metric available: cc (Cross Correlation), ssd (Sum Squared
      Difference), em (Expectation-Maximization).
  
  mopt_sigma_diff : float, optional
      Metric option applied on Cross correlation (CC).
      The standard deviation of the Gaussian smoothing kernel to be
      applied to the update field at each iteration.
  
  mopt_radius : int, optional
      Metric option applied on Cross correlation (CC).
      the radius of the squared (cubic) neighborhood at each voxel to
      be considered to compute the cross correlation.
  
  mopt_smooth : float, optional
      Metric option applied on Sum Squared Difference (SSD) and
      Expectation Maximization (EM). Smoothness parameter, the
      larger the value the smoother the deformation field.
      (default 1.0 for EM, 4.0 for SSD)
  
  mopt_inner_iter : int, optional
      Metric option applied on Sum Squared Difference (SSD) and
      Expectation Maximization (EM). This is number of iterations to be
      performed at each level of the multi-resolution Gauss-Seidel
      optimization algorithm (this is not the number of steps per
      Gaussian Pyramid level, that parameter must be set for the
      optimizer, not the metric). Default 5 for EM, 10 for SSD.
  
  mopt_q_levels : int, optional
      Metric option applied on Expectation Maximization (EM).
      Number of quantization levels (Default: 256 for EM)
  
  mopt_double_gradient : bool, optional
      Metric option applied on Expectation Maximization (EM).
      if True, the gradient of the expected static image under the moving
      modality will be added to the gradient of the moving image,
      similarly, the gradient of the expected moving image under the
      static modality will be added to the gradient of the static image.
  
  mopt_step_type : string, optional
      Metric option applied on Sum Squared Difference (SSD) and
      Expectation Maximization (EM). The optimization schedule to be
      used in the multi-resolution Gauss-Seidel optimization algorithm
      (not used if Demons Step is selected). Possible value:
      ('gauss_newton', 'demons'). default: 'gauss_newton' for EM,
      'demons' for SSD.
  
  step_length : float, optional
      the length of the maximum displacement vector of the update
       displacement field at each iteration.
  
  ss_sigma_factor : float, optional
      parameter of the scale-space smoothing kernel. For example, the
       std. dev. of the kernel will be factor*(2^i) in the isotropic case
       where i = 0, 1, ..., n_scales is the scale.
  
  opt_tol : float, optional
      the optimization will stop when the estimated derivative of the
       energy profile w.r.t. time falls below this threshold.
  
  inv_iter : int, optional
      the number of iterations to be performed by the displacement field
       inversion algorithm.
  
  inv_tol : float, optional
      the displacement field inversion algorithm will stop iterating
       when the inversion error falls below this threshold.
  
  out_dir : string, optional
      Directory to save the transformed files (default current directory).
  
  out_warped : string, optional
      Name of the warped file.
  
  out_inv_static : string, optional
      Name of the file to save the static image after applying the
       inverse mapping.
  
  out_field : string, optional
      Name of the file to save the diffeomorphic map.
code: def run(
      self,
      static_image_files,
      moving_image_files,
      prealign_file="",
      inv_static=False,
      level_iters=(10, 10, 5),
      metric="cc",
      mopt_sigma_diff=2.0,
      mopt_radius=4,
      mopt_smooth=0.0,
      mopt_inner_iter=0,
      mopt_q_levels=256,
      mopt_double_gradient=True,
      mopt_step_type="",
      step_length=0.25,
      ss_sigma_factor=0.2,
      opt_tol=1e-5,
      inv_iter=20,
      inv_tol=1e-3,
      out_dir="",
      out_warped="warped_moved.nii.gz",
      out_inv_static="inc_static.nii.gz",
      out_field="displacement_field.nii.gz",
  ):
      """
      Parameters
      ----------
      static_image_files : string
          Path of the static image file.
  
      moving_image_files : string
          Path to the moving image file.
  
      prealign_file : string, optional
          The text file containing pre alignment information via an
           affine matrix.
  
      inv_static : boolean, optional
          Apply the inverse mapping to the static image.
  
      level_iters : variable int, optional
          The number of iterations at each level of the gaussian pyramid.
  
      metric : string, optional
          The metric to be used.
          metric available: cc (Cross Correlation), ssd (Sum Squared
          Difference), em (Expectation-Maximization).
  
      mopt_sigma_diff : float, optional
          Metric option applied on Cross correlation (CC).
          The standard deviation of the Gaussian smoothing kernel to be
          applied to the update field at each iteration.
  
      mopt_radius : int, optional
          Metric option applied on Cross correlation (CC).
          the radius of the squared (cubic) neighborhood at each voxel to
          be considered to compute the cross correlation.
  
      mopt_smooth : float, optional
          Metric option applied on Sum Squared Difference (SSD) and
          Expectation Maximization (EM). Smoothness parameter, the
          larger the value the smoother the deformation field.
          (default 1.0 for EM, 4.0 for SSD)
  
      mopt_inner_iter : int, optional
          Metric option applied on Sum Squared Difference (SSD) and
          Expectation Maximization (EM). This is number of iterations to be
          performed at each level of the multi-resolution Gauss-Seidel
          optimization algorithm (this is not the number of steps per
          Gaussian Pyramid level, that parameter must be set for the
          optimizer, not the metric). Default 5 for EM, 10 for SSD.
  
      mopt_q_levels : int, optional
          Metric option applied on Expectation Maximization (EM).
          Number of quantization levels (Default: 256 for EM)
  
      mopt_double_gradient : bool, optional
          Metric option applied on Expectation Maximization (EM).
          if True, the gradient of the expected static image under the moving
          modality will be added to the gradient of the moving image,
          similarly, the gradient of the expected moving image under the
          static modality will be added to the gradient of the static image.
  
      mopt_step_type : string, optional
          Metric option applied on Sum Squared Difference (SSD) and
          Expectation Maximization (EM). The optimization schedule to be
          used in the multi-resolution Gauss-Seidel optimization algorithm
          (not used if Demons Step is selected). Possible value:
          ('gauss_newton', 'demons'). default: 'gauss_newton' for EM,
          'demons' for SSD.
  
      step_length : float, optional
          the length of the maximum displacement vector of the update
           displacement field at each iteration.
  
      ss_sigma_factor : float, optional
          parameter of the scale-space smoothing kernel. For example, the
           std. dev. of the kernel will be factor*(2^i) in the isotropic case
           where i = 0, 1, ..., n_scales is the scale.
  
      opt_tol : float, optional
          the optimization will stop when the estimated derivative of the
           energy profile w.r.t. time falls below this threshold.
  
      inv_iter : int, optional
          the number of iterations to be performed by the displacement field
           inversion algorithm.
  
      inv_tol : float, optional
          the displacement field inversion algorithm will stop iterating
           when the inversion error falls below this threshold.
  
      out_dir : string, optional
          Directory to save the transformed files (default current directory).
  
      out_warped : string, optional
          Name of the warped file.
  
      out_inv_static : string, optional
          Name of the file to save the static image after applying the
           inverse mapping.
  
      out_field : string, optional
          Name of the file to save the diffeomorphic map.
  
      """
      io_it = self.get_io_iterator()
      metric = metric.lower()
      if metric not in ["ssd", "cc", "em"]:
          raise ValueError(
              "Invalid similarity metric: Please"
              " provide a valid metric like 'ssd', 'cc', 'em'"
          )
  
      logging.info("Starting Diffeomorphic Registration")
      logging.info("Using {0} Metric".format(metric.upper()))
  
      # Init parameter if they are not setup
      init_param = {
          "ssd": {
              "mopt_smooth": 4.0,
              "mopt_inner_iter": 10,
              "mopt_step_type": "demons",
          },
          "em": {
              "mopt_smooth": 1.0,
              "mopt_inner_iter": 5,
              "mopt_step_type": "gauss_newton",
          },
      }
  
      mopt_smooth = (
          mopt_smooth
          if mopt_smooth or metric == "cc"
          else init_param[metric]["mopt_smooth"]
      )
      mopt_inner_iter = (
          mopt_inner_iter
          if mopt_inner_iter or metric == "cc"
          else init_param[metric]["mopt_inner_iter"]
      )
  
      # If using the 'cc' metric, force the `mopt_step_type` parameter to an
      # empty value since the 'cc' metric does not use it; for the rest of
      # the metrics, the `step_type` parameter will be initialized to their
      # corresponding default values in `init_param`.
      if metric == "cc":
          mopt_step_type = ""
  
      for (
          static_file,
          moving_file,
          owarped_file,
          oinv_static_file,
          omap_file,
      ) in io_it:
          logging.info("Loading static file {0}".format(static_file))
          logging.info("Loading moving file {0}".format(moving_file))
  
          # Loading the image data from the input files into object.
          static_image, static_grid2world = load_nifti(static_file)
          moving_image, moving_grid2world = load_nifti(moving_file)
  
          # Sanity check for the input image dimensions.
          check_dimensions(static_image, moving_image)
  
          # Loading the affine matrix.
          prealign = (
              np.loadtxt(prealign_file) if prealign_file else None
          )
  
          # Note that `step_type` is initialized to the default value in
          # `init_param` for the metric that was not specified as a
          # parameter or if the `mopt_step_type` is empty.
          l_metric = {
              "ssd": SSDMetric(
                  static_image.ndim,
                  smooth=mopt_smooth,
                  inner_iter=mopt_inner_iter,
                  step_type=mopt_step_type
                  if (mopt_step_type and mopt_step_type.strip())
                  and metric == "ssd"
                  else init_param["ssd"]["mopt_step_type"],
              ),
              "cc": CCMetric(
                  static_image.ndim,
                  sigma_diff=mopt_sigma_diff,
                  radius=mopt_radius,
              ),
              "em": EMMetric(
                  static_image.ndim,
                  smooth=mopt_smooth,
                  inner_iter=mopt_inner_iter,
                  step_type=mopt_step_type
                  if (mopt_step_type and mopt_step_type.strip())
                  and metric == "em"
                  else init_param["em"]["mopt_step_type"],
                  q_levels=mopt_q_levels,
                  double_gradient=mopt_double_gradient,
              ),
          }
  
          current_metric = l_metric.get(metric.lower())
  
          sdr = SymmetricDiffeomorphicRegistration(
              metric=current_metric,
              level_iters=level_iters,
              step_length=step_length,
              ss_sigma_factor=ss_sigma_factor,
              opt_tol=opt_tol,
              inv_iter=inv_iter,
              inv_tol=inv_tol,
          )
  
          mapping = sdr.optimize(
              static_image,
              moving_image,
              static_grid2world,
              moving_grid2world,
              prealign,
          )
  
          mapping_data = np.array(
              [mapping.forward.T, mapping.backward.T]
          ).T
          warped_moving = mapping.transform(moving_image)
          mapping.is_inverse = True
          inv_static = mapping.transform(static_image)
  
          # Saving
          logging.info("Saving warped {0}".format(owarped_file))
          save_nifti(owarped_file, warped_moving, static_grid2world)
          logging.info(
              "Saving inverse transformes static {0}".format(
                  out_inv_static
              )
          )
          save_nifti(out_inv_static, inv_static, static_grid2world)
          logging.info("Saving Diffeomorphic map {0}".format(omap_file))
          save_nifti(
              omap_file, mapping_data, mapping.codomain_world2grid
          )
  
imports: import logging
  from warnings import warn
  import numpy
  import nibabel
  from os.path import join
  from dipy.utils.optpkg import optional_package
  from dipy.align.imaffine import AffineMap
  from dipy.align.imwarp import SymmetricDiffeomorphicRegistration, DiffeomorphicMap
  from dipy.align.metrics import CCMetric, SSDMetric, EMMetric
  from dipy.align.reslice import reslice
  from dipy.align import affine_registration, motion_correction
  from dipy.align.streamlinear import slr_with_qbx
  from dipy.tracking.streamline import set_number_of_points
  from dipy.align.streamwarp import bundlewarp
  from dipy.core.gradients import mask_non_weighted_bvals, gradient_table
  from dipy.io.image import save_nifti, load_nifti, save_qa_metric
  from dipy.io.gradients import read_bvals_bvecs
  from dipy.tracking.streamline import transform_streamlines
  from dipy.workflows.workflow import Workflow
label_desc: Use image node_id 1 for calling the SynRegistrationFlow obj's underlying member method code with example usage: obj.run(static_image_files, moving_image_files, prealign_file, inv_static, level_iters, metric, mopt_sigma_diff, mopt_radius, mopt_smooth, mopt_inner_iter, mopt_q_levels, mopt_double_gradient, mopt_step_type, step_length, ss_sigma_factor, opt_tol, inv_iter, inv_tol, out_dir, out_warped, out_inv_static, out_field) without return types