code: def test_invert_vector_field_3d():
      r"""
      Inverts a synthetic, analytically invertible, displacement field
      """
      shape = (64, 64, 64)
      ns = shape[0]
      nr = shape[1]
      nc = shape[2]
  
      # Create an arbitrary image-to-space transform
  
      # Select an arbitrary rotation axis
      axis = np.array([2.0, 0.5, 1.0])
      t = 2.5  # translation factor
  
      trans = np.array(
          [
              [1, 0, 0, -t * ns],
              [0, 1, 0, -t * nr],
              [0, 0, 1, -t * nc],
              [0, 0, 0, 1],
          ]
      )
      trans_inv = np.linalg.inv(trans)
  
      d, _ = vfu.create_harmonic_fields_3d(ns, 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
              rot = np.zeros(shape=(4, 4))
              rot[:3, :3] = geometry.rodrigues_axis_rotation(
                  axis, theta
              )
              rot[3, 3] = 1.0
              scale = np.array(
                  [
                      [1 * s, 0, 0, 0],
                      [0, 1 * s, 0, 0],
                      [0, 0, 1 * s, 0],
                      [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_3d(dcopy, gt_affine)
  
              # Note: the spacings are used just to check convergence, so they
              # don't need to be very accurate. Here we are passing (0.5 * s) to
              # force the algorithm to make more iterations: in ANTS, there is a
              # hard-coded bound on the maximum residual, that's why we cannot
              # force more iteration by changing the parameters.
              # We will investigate this issue with more detail in the future.
  
              inv_approx = vfu.invert_vector_field_fixed_point_3d(
                  dcopy,
                  gt_affine_inv,
                  np.array([s, s, s]) * 0.5,
                  40,
                  1e-7,
              )
  
              mapping = imwarp.DiffeomorphicMap(3, (nr, nc), gt_affine)
              mapping.forward = dcopy
              mapping.backward = inv_approx
              residual, stats = mapping.compute_inversion_error()
              assert_almost_equal(stats[1], 0, decimal=3)
              assert_almost_equal(stats[2], 0, decimal=3)
  
      # Test exception is raised when the affine transform matrix is not valid
      invalid = np.zeros((3, 3), dtype=np.float64)
      spacing = np.array([1.0, 1.0, 1.0])
      assert_raises(
          ValueError,
          vfu.invert_vector_field_fixed_point_3d,
          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 14 for calling a global function with example usage: test_invert_vector_field_3d() without return types