code: def test_gradient_2d(rng):
      sh = (25, 32)
      # Create grid coordinates
      x_0 = np.asarray(range(sh[0]))
      x_1 = np.asarray(range(sh[1]))
      X = np.empty(sh + (3,), dtype=np.float64)
      O = np.ones(sh)
      X[..., 0] = x_0[:, None] * O
      X[..., 1] = x_1[None, :] * O
      X[..., 2] = 1
  
      transform = regtransforms[("RIGID", 2)]
      theta = np.array([0.1, 5.0, 2.5])
      T = transform.param_to_matrix(theta)
      TX = X.dot(T.T)
      # Eval an arbitrary (known) function at TX
      # f(x, y) = ax^2 + bxy + cy^{2}
      # df/dx = 2ax + by
      # df/dy = 2cy + bx
      a = 2e-3
      b = 5e-3
      c = 7e-3
      img = (
          a * TX[..., 0] ** 2
          + b * TX[..., 0] * TX[..., 1]
          + c * TX[..., 1] ** 2
      )
      img = img.astype(floating)
      # img is an image sampled at X with grid-to-space transform T
  
      # Test sparse gradient: choose some sample points (in space)
      sample = sample_domain_regular(
          20, np.array(sh, dtype=np.int32), T, rng=rng
      )
      sample = np.array(sample)
      # Compute the analytical gradient at all points
      expected = np.empty((sample.shape[0], 2), dtype=floating)
      expected[..., 0] = 2 * a * sample[:, 0] + b * sample[:, 1]
      expected[..., 1] = 2 * c * sample[:, 1] + b * sample[:, 0]
      # Get the numerical gradient with the implementation under test
      sp_to_grid = np.linalg.inv(T)
      img_spacing = np.ones(2)
      actual, inside = vfu.sparse_gradient(
          img, sp_to_grid, img_spacing, sample
      )
      diff = np.abs(expected - actual).mean(1) * inside
      # The finite differences are really not accurate, especially with float32
      assert_equal(diff.max() < 1e-3, True)
      # Verify exception is raised when passing invalid affine or spacings
      invalid_affine = np.eye(2)
      invalid_spacings = np.ones(1)
      assert_raises(
          ValueError,
          vfu.sparse_gradient,
          img,
          invalid_affine,
          img_spacing,
          sample,
      )
      assert_raises(
          ValueError,
          vfu.sparse_gradient,
          img,
          sp_to_grid,
          invalid_spacings,
          sample,
      )
  
      # Test dense gradient
      # Compute the analytical gradient at all points
      expected = np.empty(sh + (2,), dtype=floating)
      expected[..., 0] = 2 * a * TX[..., 0] + b * TX[..., 1]
      expected[..., 1] = 2 * c * TX[..., 1] + b * TX[..., 0]
      # Get the numerical gradient with the implementation under test
      sp_to_grid = np.linalg.inv(T)
      img_spacing = np.ones(2)
      actual, inside = vfu.gradient(img, sp_to_grid, img_spacing, sh, T)
      diff = np.abs(expected - actual).mean(2) * inside
      # In the dense case, we are evaluating at the exact points (sample points
      # are not slightly moved like in the sparse case) so we have more precision
      assert_equal(diff.max() < 1e-5, True)
      # Verify exception is raised when passing invalid affine or spacings
      assert_raises(
          ValueError,
          vfu.gradient,
          img,
          invalid_affine,
          img_spacing,
          sh,
          T,
      )
      assert_raises(
          ValueError,
          vfu.gradient,
          img,
          sp_to_grid,
          img_spacing,
          sh,
          invalid_affine,
      )
      assert_raises(
          ValueError,
          vfu.gradient,
          img,
          sp_to_grid,
          invalid_spacings,
          sh,
          T,
      )
  
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 24 for calling a global function with example usage: test_gradient_2d(rng) without return types