code: def test_compose_vector_fields_3d(rng):
      r"""
      Creates two random displacement field that exactly map pixels from an input
      image to an output image. The resulting displacements and their
      composition, although operating in physical space, map the points exactly
      (up to numerical precision).
      """
      input_shape = (10, 10, 10)
      tgt_sh = (10, 10, 10)
      # create a simple affine transformation
      ns = input_shape[0]
      nr = input_shape[1]
      nc = input_shape[2]
      s = 1.5
      t = 2.5
      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)
      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(trans))
  
      # create two random displacement fields
      input_grid2world = gt_affine
      target_grid2world = gt_affine
  
      disp1, assign1 = vfu.create_random_displacement_3d(
          np.array(input_shape, dtype=np.int32),
          input_grid2world,
          np.array(tgt_sh, dtype=np.int32),
          target_grid2world,
          rng=rng,
      )
      disp1 = np.array(disp1, dtype=floating)
      assign1 = np.array(assign1)
  
      disp2, assign2 = vfu.create_random_displacement_3d(
          np.array(input_shape, dtype=np.int32),
          input_grid2world,
          np.array(tgt_sh, dtype=np.int32),
          target_grid2world,
          rng=rng,
      )
      disp2 = np.array(disp2, dtype=floating)
      assign2 = np.array(assign2)
  
      # create a random image (with decimal digits) to warp
      moving_image = np.empty(tgt_sh, dtype=floating)
      moving_image[...] = rng.integers(
          0, 10, np.size(moving_image)
      ).reshape(tuple(tgt_sh))
      # set boundary values to zero so we don't test wrong interpolation due to
      # floating point precision
      moving_image[0, :, :] = 0
      moving_image[-1, :, :] = 0
      moving_image[:, 0, :] = 0
      moving_image[:, -1, :] = 0
      moving_image[:, :, 0] = 0
      moving_image[:, :, -1] = 0
  
      # evaluate the composed warping using the exact assignments
      # (first 1 then 2)
  
      warp1 = moving_image[
          (assign2[..., 0], assign2[..., 1], assign2[..., 2])
      ]
      expected = warp1[
          (assign1[..., 0], assign1[..., 1], assign1[..., 2])
      ]
  
      # compose the displacement fields
      target_world2grid = np.linalg.inv(target_grid2world)
      premult_index = target_world2grid.dot(input_grid2world)
      premult_disp = target_world2grid
  
      for time_scaling in [0.25, 0.5, 1.0, 2.0, 4.0]:
          composition, stats = vfu.compose_vector_fields_3d(
              disp1,
              disp2 / time_scaling,
              premult_index,
              premult_disp,
              time_scaling,
              None,
          )
          # apply the implementation under test
          warped = np.array(
              vfu.warp_3d(
                  moving_image,
                  composition,
                  None,
                  premult_index,
                  premult_disp,
              )
          )
          assert_array_almost_equal(warped, expected)
  
          # test also using nearest neighbor interpolation
          warped = np.array(
              vfu.warp_3d_nn(
                  moving_image,
                  composition,
                  None,
                  premult_index,
                  premult_disp,
              )
          )
          assert_array_almost_equal(warped, expected)
  
          # test updating the displacement field instead of creating a new one
          composition = disp1.copy()
          vfu.compose_vector_fields_3d(
              composition,
              disp2 / time_scaling,
              premult_index,
              premult_disp,
              time_scaling,
              composition,
          )
          # apply the implementation under test
          warped = np.array(
              vfu.warp_3d(
                  moving_image,
                  composition,
                  None,
                  premult_index,
                  premult_disp,
              )
          )
          assert_array_almost_equal(warped, expected)
  
          # test also using nearest neighbor interpolation
          warped = np.array(
              vfu.warp_3d_nn(
                  moving_image,
                  composition,
                  None,
                  premult_index,
                  premult_disp,
              )
          )
          assert_array_almost_equal(warped, expected)
  
      # Test non-overlapping case
      x_0 = np.asarray(range(input_shape[0]))
      x_1 = np.asarray(range(input_shape[1]))
      x_2 = np.asarray(range(input_shape[2]))
      X = np.empty(input_shape + (3,), dtype=np.float64)
      O = np.ones(input_shape)
      X[..., 0] = x_0[:, None, None] * O
      X[..., 1] = x_1[None, :, None] * O
      X[..., 2] = x_2[None, None, :] * O
      sz = input_shape[0] * input_shape[1] * input_shape[2] * 3
      random_labels = rng.integers(0, 2, sz)
      random_labels = random_labels.reshape(input_shape + (3,))
      values = np.array([-1, tgt_sh[0]])
      disp1 = (values[random_labels] - X).astype(floating)
      composition, stats = vfu.compose_vector_fields_3d(
          disp1, disp2, None, None, 1.0, None
      )
      assert_array_almost_equal(composition, np.zeros_like(composition))
  
      # test updating the displacement field instead of creating a new one
      composition = disp1.copy()
      vfu.compose_vector_fields_3d(
          composition, disp2, None, None, 1.0, composition
      )
      assert_array_almost_equal(composition, np.zeros_like(composition))
  
      # Test exception is raised when the affine transform matrix is not valid
      valid = np.zeros((3, 4), dtype=np.float64)
      invalid = np.zeros((3, 3), dtype=np.float64)
      assert_raises(
          ValueError,
          vfu.compose_vector_fields_3d,
          disp1,
          disp2,
          invalid,
          valid,
          1.0,
          None,
      )
      assert_raises(
          ValueError,
          vfu.compose_vector_fields_3d,
          disp1,
          disp2,
          valid,
          invalid,
          1.0,
          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 12 for calling a global function with example usage: test_compose_vector_fields_3d(rng) without return types