code: def test_warping_2d():
      r"""
      Tests the cython implementation of the 2d warpings against scipy
      """
      sh = (64, 64)
      nr = sh[0]
      nc = sh[1]
  
      # Create an image of a circle
      radius = 24
      circle = vfu.create_circle(nr, nc, radius)
      circle = np.array(circle, dtype=floating)
  
      # Create a displacement field for warping
      d, dinv = vfu.create_harmonic_fields_2d(nr, nc, 0.2, 8)
      d = np.asarray(d).astype(floating)
  
      # Create grid coordinates
      x_0 = np.asarray(range(sh[0]))
      x_1 = np.asarray(range(sh[1]))
      X = np.empty((3,) + sh, dtype=np.float64)
      O = np.ones(sh)
      X[0, ...] = x_0[:, None] * O
      X[1, ...] = x_1[None, :] * O
      X[2, ...] = 1
  
      # Select an arbitrary translation matrix
      t = 0.1
      trans = np.array([[1, 0, -t * nr], [0, 1, -t * nc], [0, 0, 1]])
      trans_inv = np.linalg.inv(trans)
  
      # Select arbitrary rotation and scaling matrices
      for theta in [
          -1 * np.pi / 6.0,
          0.0,
          np.pi / 6.0,
      ]:  # rotation angle
          for s in [0.42, 1.3, 2.15]:  # 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]]
              )
  
              aff = trans_inv.dot(scale.dot(rot.dot(trans)))
  
              # Select arbitrary (but different) grid-to-space transforms
              sampling_grid2world = scale
              field_grid2world = aff
              field_world2grid = np.linalg.inv(field_grid2world)
              image_grid2world = aff.dot(scale)
              image_world2grid = np.linalg.inv(image_grid2world)
  
              A = field_world2grid.dot(sampling_grid2world)
              B = image_world2grid.dot(sampling_grid2world)
              C = image_world2grid
  
              # Reorient the displacement field according to its grid-to-space
              # transform
              dcopy = np.copy(d)
              vfu.reorient_vector_field_2d(dcopy, field_grid2world)
              extended_dcopy = np.zeros(
                  (nr + 2, nc + 2, 2), dtype=floating
              )
              extended_dcopy[1 : nr + 1, 1 : nc + 1, :] = dcopy
  
              # Compute the warping coordinates (see warp_2d documentation)
              Y = np.apply_along_axis(A.dot, 0, X)[0:2, ...]
              Z = np.zeros_like(X)
              Z[0, ...] = map_coordinates(
                  extended_dcopy[..., 0], Y + 1, order=1
              )
              Z[1, ...] = map_coordinates(
                  extended_dcopy[..., 1], Y + 1, order=1
              )
              Z[2, ...] = 0
              Z = np.apply_along_axis(C.dot, 0, Z)[0:2, ...]
              T = np.apply_along_axis(B.dot, 0, X)[0:2, ...]
              W = T + Z
  
              # Test bilinear interpolation
              expected = map_coordinates(circle, W, order=1)
              warped = vfu.warp_2d(
                  circle, dcopy, A, B, C, np.array(sh, dtype=np.int32)
              )
              assert_array_almost_equal(warped, expected)
  
              # Test nearest neighbor interpolation
              expected = map_coordinates(circle, W, order=0)
              warped = vfu.warp_2d_nn(
                  circle, dcopy, A, B, C, np.array(sh, dtype=np.int32)
              )
              assert_array_almost_equal(warped, expected)
  
      # Test exception is raised when the affine transform matrix is not valid
      val = np.zeros((2, 3), dtype=np.float64)
      inval = np.zeros((2, 2), dtype=np.float64)
      sh = np.array(sh, dtype=np.int32)
      # Exceptions from warp_2d
      assert_raises(
          ValueError, vfu.warp_2d, circle, d, inval, val, val, sh
      )
      assert_raises(
          ValueError, vfu.warp_2d, circle, d, val, inval, val, sh
      )
      assert_raises(
          ValueError, vfu.warp_2d, circle, d, val, val, inval, sh
      )
      # Exceptions from warp_2d_nn
      assert_raises(
          ValueError, vfu.warp_2d_nn, circle, d, inval, val, val, sh
      )
      assert_raises(
          ValueError, vfu.warp_2d_nn, circle, d, val, inval, val, sh
      )
      assert_raises(
          ValueError, vfu.warp_2d_nn, circle, d, val, val, inval, sh
      )
  
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 7 for calling a global function with example usage: test_warping_2d() without return types