code: def test_image_registration(rng):
      with TemporaryDirectory() as temp_out_dir:
          (
              static,
              moving,
              static_g2w,
              moving_g2w,
              smask,
              mmask,
              M,
          ) = setup_random_transform(
              transform=regtransforms[("AFFINE", 3)],
              rfactor=0.1,
              rng=rng,
          )
  
          save_nifti(
              pjoin(temp_out_dir, "b0.nii.gz"),
              data=static,
              affine=static_g2w,
          )
          save_nifti(
              pjoin(temp_out_dir, "t1.nii.gz"),
              data=moving,
              affine=moving_g2w,
          )
  
          static_image_file = pjoin(temp_out_dir, "b0.nii.gz")
          moving_image_file = pjoin(temp_out_dir, "t1.nii.gz")
  
          image_registration_flow = ImageRegistrationFlow()
  
          def read_distance(qual_fname):
              with open(pjoin(temp_out_dir, qual_fname), "r") as f:
                  return float(f.readlines()[-1])
  
          def test_com():
              out_moved = pjoin(temp_out_dir, "com_moved.nii.gz")
              out_affine = pjoin(temp_out_dir, "com_affine.txt")
  
              image_registration_flow._force_overwrite = True
              image_registration_flow.run(
                  static_image_file,
                  moving_image_file,
                  transform="com",
                  out_dir=temp_out_dir,
                  out_moved=out_moved,
                  out_affine=out_affine,
              )
              check_existence(out_moved, out_affine)
  
          def test_translation():
              out_moved = pjoin(temp_out_dir, "trans_moved.nii.gz")
              out_affine = pjoin(temp_out_dir, "trans_affine.txt")
  
              image_registration_flow._force_overwrite = True
              image_registration_flow.run(
                  static_image_file,
                  moving_image_file,
                  transform="trans",
                  out_dir=temp_out_dir,
                  out_moved=out_moved,
                  out_affine=out_affine,
                  save_metric=True,
                  level_iters=[100, 10, 1],
                  out_quality="trans_q.txt",
              )
  
              dist = read_distance("trans_q.txt")
              npt.assert_almost_equal(
                  float(dist), -0.42097809101318934, 1
              )
              check_existence(out_moved, out_affine)
  
          def test_rigid():
              out_moved = pjoin(temp_out_dir, "rigid_moved.nii.gz")
              out_affine = pjoin(temp_out_dir, "rigid_affine.txt")
  
              image_registration_flow._force_overwrite = True
              image_registration_flow.run(
                  static_image_file,
                  moving_image_file,
                  transform="rigid",
                  out_dir=temp_out_dir,
                  out_moved=out_moved,
                  out_affine=out_affine,
                  save_metric=True,
                  level_iters=[100, 10, 1],
                  out_quality="rigid_q.txt",
              )
  
              dist = read_distance("rigid_q.txt")
              npt.assert_almost_equal(dist, -0.6900534794005155, 1)
              check_existence(out_moved, out_affine)
  
          def test_rigid_isoscaling():
              out_moved = pjoin(
                  temp_out_dir, "rigid_isoscaling_moved.nii.gz"
              )
              out_affine = pjoin(
                  temp_out_dir, "rigid_isoscaling_affine.txt"
              )
  
              image_registration_flow._force_overwrite = True
              image_registration_flow.run(
                  static_image_file,
                  moving_image_file,
                  transform="rigid_isoscaling",
                  out_dir=temp_out_dir,
                  out_moved=out_moved,
                  out_affine=out_affine,
                  save_metric=True,
                  level_iters=[100, 10, 1],
                  out_quality="rigid_isoscaling_q.txt",
              )
  
              dist = read_distance("rigid_isoscaling_q.txt")
              npt.assert_almost_equal(dist, -0.6960044668271375, 1)
              check_existence(out_moved, out_affine)
  
          def test_rigid_scaling():
              out_moved = pjoin(
                  temp_out_dir, "rigid_scaling_moved.nii.gz"
              )
              out_affine = pjoin(
                  temp_out_dir, "rigid_scaling_affine.txt"
              )
  
              image_registration_flow._force_overwrite = True
              image_registration_flow.run(
                  static_image_file,
                  moving_image_file,
                  transform="rigid_scaling",
                  out_dir=temp_out_dir,
                  out_moved=out_moved,
                  out_affine=out_affine,
                  save_metric=True,
                  level_iters=[100, 10, 1],
                  out_quality="rigid_scaling_q.txt",
              )
  
              dist = read_distance("rigid_scaling_q.txt")
              npt.assert_almost_equal(dist, -0.698688892993124, 1)
              check_existence(out_moved, out_affine)
  
          def test_affine():
              out_moved = pjoin(temp_out_dir, "affine_moved.nii.gz")
              out_affine = pjoin(temp_out_dir, "affine_affine.txt")
  
              image_registration_flow._force_overwrite = True
              image_registration_flow.run(
                  static_image_file,
                  moving_image_file,
                  transform="affine",
                  out_dir=temp_out_dir,
                  out_moved=out_moved,
                  out_affine=out_affine,
                  save_metric=True,
                  level_iters=[100, 10, 1],
                  out_quality="affine_q.txt",
              )
  
              dist = read_distance("affine_q.txt")
              npt.assert_almost_equal(dist, -0.7670650775914811, 1)
              check_existence(out_moved, out_affine)
  
          # Creating the erroneous behavior
          def test_err():
              image_registration_flow._force_overwrite = True
              npt.assert_raises(
                  ValueError,
                  image_registration_flow.run,
                  static_image_file,
                  moving_image_file,
                  transform="notransform",
              )
  
              image_registration_flow._force_overwrite = True
              npt.assert_raises(
                  ValueError,
                  image_registration_flow.run,
                  static_image_file,
                  moving_image_file,
                  metric="wrong_metric",
              )
  
          def check_existence(movedfile, affine_mat_file):
              assert os.path.exists(movedfile)
              assert os.path.exists(affine_mat_file)
              return True
  
          test_com()
          test_translation()
          test_rigid()
          test_rigid_isoscaling()
          test_rigid_scaling()
          test_affine()
          test_err()
  
imports: from os.path import join
  import os.path
  from tempfile import TemporaryDirectory
  import numpy.testing
  import numpy
  import nibabel
  import pytest
  from dipy.utils.optpkg import optional_package
  from dipy.align.tests.test_imwarp import get_synthetic_warped_circle
  from dipy.align.tests.test_parzenhist import setup_random_transform
  from dipy.align.transforms import regtransforms
  from dipy.data import get_fnames
  from dipy.io.image import save_nifti, load_nifti_data
  from dipy.io.stateful_tractogram import Space, StatefulTractogram
  from dipy.io.streamline import load_tractogram, save_tractogram
  from dipy.tracking.streamline import Streamlines
  from dipy.workflows.align import ImageRegistrationFlow, SynRegistrationFlow, ApplyTransformFlow, ResliceFlow, SlrWithQbxFlow, MotionCorrectionFlow, BundleWarpFlow
  from dipy.testing.decorators import set_random_number_generator
label_desc: Use image node_id 3 for calling a global function with example usage: test_image_registration(rng) and returns: True, float