code: def test_invert_vector_field_2d():
      r"""
      Inverts a synthetic, analytically invertible, displacement field
      """
      shape = (64, 64)
      nr = shape[0]
      nc = shape[1]
      # Create an arbitrary image-to-space transform
      t = 2.5  # translation factor
  
      trans = np.array([[1, 0, -t * nr], [0, 1, -t * nc], [0, 0, 1]])
      trans_inv = np.linalg.inv(trans)
  
      d, _ = vfu.create_harmonic_fields_2d(nr, nc, 0.2, 8)
      d = np.asarray(d).astype(floating)
  
      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)))
              gt_affine_inv = np.linalg.inv(gt_affine)
              dcopy = np.copy(d)
  
              # make sure the field remains invertible after the re-mapping
              vfu.reorient_vector_field_2d(dcopy, gt_affine)
  
              inv_approx = vfu.invert_vector_field_fixed_point_2d(
                  dcopy, gt_affine_inv, np.array([s, s]), 40, 1e-7
              )
  
              mapping = imwarp.DiffeomorphicMap(2, (nr, nc), gt_affine)
              mapping.forward = dcopy
              mapping.backward = inv_approx
              residual, stats = mapping.compute_inversion_error()
              assert_almost_equal(stats[1], 0, decimal=4)
              assert_almost_equal(stats[2], 0, decimal=4)
  
      # Test exception is raised when the affine transform matrix is not valid
      invalid = np.zeros((2, 2), dtype=np.float64)
      spacing = np.array([1.0, 1.0])
      assert_raises(
          ValueError,
          vfu.invert_vector_field_fixed_point_2d,
          d,
          invalid,
          spacing,
          40,
          1e-7,
          None,
      )
  
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 13 for calling a global function with example usage: test_invert_vector_field_2d() without return types