code: def test_random_displacement_field_2d(rng):
      from_shape = (25, 32)
      to_shape = (33, 29)
  
      # Create grid coordinates
      x_0 = np.asarray(range(from_shape[0]))
      x_1 = np.asarray(range(from_shape[1]))
      X = np.empty((3,) + from_shape, dtype=np.float64)
      O = np.ones(from_shape)
      X[0, ...] = x_0[:, None] * O
      X[1, ...] = x_1[None, :] * O
      X[2, ...] = 1
  
      # Create an arbitrary image-to-space transform
      t = 0.15  # translation factor
  
      trans = np.array(
          [
              [1, 0, -t * from_shape[0]],
              [0, 1, -t * from_shape[1]],
              [0, 0, 1],
          ]
      )
      trans_inv = np.linalg.inv(trans)
  
      for theta in [
          -1 * np.pi / 6.0,
          0.0,
          np.pi / 5.0,
      ]:  # rotation angle
          for s in [0.83, 1.3, 2.07]:  # 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]]
              )
  
              from_grid2world = trans_inv.dot(scale.dot(rot.dot(trans)))
              to_grid2world = from_grid2world.dot(scale)
              to_world2grid = np.linalg.inv(to_grid2world)
  
              field, assignment = vfu.create_random_displacement_2d(
                  np.array(from_shape, dtype=np.int32),
                  from_grid2world,
                  np.array(to_shape, dtype=np.int32),
                  to_grid2world,
                  rng=rng,
              )
              field = np.array(field, dtype=floating)
              assignment = np.array(assignment)
              # Verify the assignments are inside the requested region
              assert_equal(0, (assignment < 0).sum())
              for i in range(2):
                  assert_equal(
                      0, (assignment[..., i] >= to_shape[i]).sum()
                  )
  
              # Compute the warping coordinates (see warp_2d documentation)
              Y = np.apply_along_axis(from_grid2world.dot, 0, X)[
                  0:2, ...
              ]
              Z = np.zeros_like(X)
              Z[0, ...] = Y[0, ...] + field[..., 0]
              Z[1, ...] = Y[1, ...] + field[..., 1]
              Z[2, ...] = 1
              W = np.apply_along_axis(to_world2grid.dot, 0, Z)[0:2, ...]
  
              # Verify the claimed assignments are correct
              assert_array_almost_equal(
                  W[0, ...], assignment[..., 0], 5
              )
              assert_array_almost_equal(
                  W[1, ...], assignment[..., 1], 5
              )
  
      # Test exception is raised when the affine transform matrix is not valid
      valid = np.zeros((2, 3), dtype=np.float64)
      invalid = np.zeros((2, 2), dtype=np.float64)
      shape = np.array(from_shape, dtype=np.int32)
      assert_raises(
          ValueError,
          vfu.create_random_displacement_2d,
          shape,
          invalid,
          shape,
          valid,
          rng=rng,
      )
      assert_raises(
          ValueError,
          vfu.create_random_displacement_2d,
          shape,
          valid,
          shape,
          invalid,
          rng=rng,
      )
  
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 1 for calling a global function with example usage: test_random_displacement_field_2d(rng) without return types