code: def test_affreg_all_transforms(rng):
      # Test affine registration using all transforms with typical settings
  
      # Make sure dictionary entries are processed in the same order regardless
      # of the platform. Otherwise any random numbers drawn within the loop would
      # make the test non-deterministic even if we fix the seed before the loop.
      # Right now, this test does not draw any samples, but we still sort the
      # entries to prevent future related failures.
      for ttype in sorted(factors):
          dim = ttype[1]
          if dim == 2:
              nslices = 1
          else:
              nslices = 45
          factor = factors[ttype][0]
          sampling_pc = factors[ttype][1]
          trans = regtransforms[ttype]
          # Shorthand:
          srt = setup_random_transform
          static, moving, static_g2w, moving_g2w, smask, mmask, T = srt(
              trans, factor, nslices, 1.0, rng=rng
          )
          # Sum of absolute differences
          start_sad = np.abs(static - moving).sum()
          metric = imaffine.MutualInformationMetric(32, sampling_pc)
          affreg = imaffine.AffineRegistration(
              metric,
              [1000, 100, 50],
              [3, 1, 0],
              [4, 2, 1],
              "L-BFGS-B",
              None,
              options=None,
          )
          x0 = trans.get_identity_parameters()
  
          # test warning for using masks (even if all ones) with sparse sampling
          if sampling_pc not in [1.0, None]:
              affine_map = assert_warns(
                  UserWarning,
                  affreg.optimize,
                  static,
                  moving,
                  trans,
                  x0,
                  static_g2w,
                  moving_g2w,
                  None,
                  None,
                  smask,
                  mmask,
              )
          else:
              affine_map = affreg.optimize(
                  static,
                  moving,
                  trans,
                  x0,
                  static_g2w,
                  moving_g2w,
                  None,
                  None,
                  smask,
                  mmask,
              )
  
          transformed = affine_map.transform(moving)
          # Sum of absolute differences
          end_sad = np.abs(static - transformed).sum()
          reduction = 1 - end_sad / start_sad
          print("%s>>%f" % (ttype, reduction))
          assert reduction > 0.9
  
      # Verify that exception is raised if level_iters is empty
      metric = imaffine.MutualInformationMetric(32)
      assert_raises(ValueError, imaffine.AffineRegistration, metric, [])
  
      # Verify that exception is raised if masks are all zeros
      affine_map = assert_warns(
          UserWarning,
          affreg.optimize,
          static,
          moving,
          trans,
          x0,
          static_g2w,
          moving_g2w,
          None,
          None,
          np.zeros_like(smask),
          np.zeros_like(mmask),
      )
  
file_path: dipy/dipy/align/tests/test_imaffine.py
imports: import numpy
  import numpy.linalg
  from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_equal, assert_raises, assert_warns
  from dipy.core import geometry
  from dipy.align import vector_fields
  from dipy.align import imaffine
  from dipy.align.imaffine import AffineInversionError, AffineInvalidValuesError, AffineMap, _number_dim_affine_matrix
  from dipy.align.transforms import regtransforms
  from dipy.align.tests.test_parzenhist import setup_random_transform
  from dipy.testing.decorators import set_random_number_generator
label_desc: Use image node_id 4 for calling a global function with example usage: test_affreg_all_transforms(rng) without return types