code: def test_affine_transforms_2d():
      r"""
      Tests 2D affine transform functions against scipy implementation
      """
      # Create a simple invertible affine transform
      d_shape = (64, 64)
      codomain_shape = (80, 80)
      nr = d_shape[0]
      nc = d_shape[1]
  
      # Create an image of a circle
      radius = 16
      circle = vfu.create_circle(
          codomain_shape[0], codomain_shape[1], radius
      )
      circle = np.array(circle, dtype=floating)
  
      # Create grid coordinates
      x_0 = np.asarray(range(d_shape[0]))
      x_1 = np.asarray(range(d_shape[1]))
      X = np.empty((3,) + d_shape, dtype=np.float64)
      O = np.ones(d_shape)
      X[0, ...] = x_0[:, None] * O
      X[1, ...] = x_1[None, :] * O
      X[2, ...] = 1
  
      # Generate affine transforms
      t = 0.3
      trans = np.array([[1, 0, -t * nr], [0, 1, -t * nc], [0, 0, 1]])
      trans_inv = np.linalg.inv(trans)
      for theta in [
          -1 * np.pi / 5.0,
          0.0,
          np.pi / 5.0,
      ]:  # rotation angle
          for s in [0.5, 1.0, 2.0]:  # scale
              ct = np.cos(theta)
              st = np.sin(theta)
  
              rot = np.array([[ct, -st, 0], [st, ct, 0], [0, 0, 1]])
  
              scale = np.array(
                  [[1 * s, 0, 0], [0, 1 * s, 0], [0, 0, 1]]
              )
  
              gt_affine = trans_inv.dot(scale.dot(rot.dot(trans)))
  
              # Apply the affine transform to the grid coordinates
              Y = np.apply_along_axis(gt_affine.dot, 0, X)[0:2, ...]
  
              expected = map_coordinates(circle, Y, order=1)
              warped = vfu.transform_2d_affine(
                  circle, np.array(d_shape, dtype=np.int32), gt_affine
              )
              assert_array_almost_equal(warped, expected)
  
              # Test affine warping with nearest-neighbor interpolation
              expected = map_coordinates(circle, Y, order=0)
              warped = vfu.transform_2d_affine_nn(
                  circle, np.array(d_shape, dtype=np.int32), gt_affine
              )
              assert_array_almost_equal(warped, expected)
  
      # Test the affine = None case
      warped = vfu.transform_2d_affine(
          circle, np.array(codomain_shape, dtype=np.int32), None
      )
      assert_array_equal(warped, circle)
  
      warped = vfu.transform_2d_affine_nn(
          circle, np.array(codomain_shape, dtype=np.int32), None
      )
      assert_array_equal(warped, circle)
  
      # Test exception is raised when the affine transform matrix is not valid
      invalid = np.zeros((2, 2), dtype=np.float64)
      invalid_nan = np.zeros((3, 3), dtype=np.float64)
      invalid_nan[1, 1] = np.nan
      shape = np.array(codomain_shape, dtype=np.int32)
      # Exceptions from transform_2d
      assert_raises(
          ValueError, vfu.transform_2d_affine, circle, shape, invalid
      )
      assert_raises(
          ValueError,
          vfu.transform_2d_affine,
          circle,
          shape,
          invalid_nan,
      )
      # Exceptions from transform_2d_nn
      assert_raises(
          ValueError, vfu.transform_2d_affine_nn, circle, shape, invalid
      )
      assert_raises(
          ValueError,
          vfu.transform_2d_affine_nn,
          circle,
          shape,
          invalid_nan,
      )
  
imports: import numpy
  from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_almost_equal, assert_equal, assert_raises
  from scipy.ndimage import map_coordinates
  from nibabel.affines import apply_affine, from_matvec
  from dipy.core import geometry
  from dipy.align import floating
  from dipy.align import imwarp
  from dipy.align import vector_fields
  from dipy.align.transforms import regtransforms
  from dipy.align.parzenhist import sample_domain_regular
  from dipy.testing.decorators import set_random_number_generator
label_desc: Use image node_id 9 for calling a global function with example usage: test_affine_transforms_2d() without return types