def test_sample_domain_regular(): # Test 2D sampling shape = np.array((10, 10), dtype=np.int32) affine = np.eye(3) invalid_affine = np.eye(2) sigma = 0 dim = len(shape) n = shape[0] * shape[1] k = 2 # Verify exception is raised with invalid affine assert_raises(ValueError, sample_domain_regular, k, shape, invalid_affine, sigma) samples = sample_domain_regular(k, shape, affine, sigma) isamples = np.array(samples, dtype=np.int32) indices = (isamples[:, 0] * shape[1] + isamples[:, 1]) # Verify correct number of points sampled assert_array_equal(samples.shape, [n // k, dim]) # Verify all sampled points are different assert_equal(len(set(indices)), len(indices)) # Verify the sampling was regular at rate k assert_equal((indices % k).sum(), 0) # Test 3D sampling shape = np.array((5, 10, 10), dtype=np.int32) affine = np.eye(4) invalid_affine = np.eye(3) sigma = 0 dim = len(shape) n = shape[0] * shape[1] * shape[2] k = 10 # Verify exception is raised with invalid affine assert_raises(ValueError, sample_domain_regular, k, shape, invalid_affine, sigma) samples = sample_domain_regular(k, shape, affine, sigma) isamples = np.array(samples, dtype=np.int32) indices = (isamples[:, 0] * shape[1] * shape[2] + isamples[:, 1] * shape[2] + isamples[:, 2]) # Verify correct number of points sampled assert_array_equal(samples.shape, [n // k, dim]) # Verify all sampled points are different assert_equal(len(set(indices)), len(indices)) # Verify the sampling was regular at rate k assert_equal((indices % k).sum(), 0)
def test_joint_pdf_gradients_sparse(): h = 1e-4 # Make sure dictionary entries are processed in the same order regardless # of the platform. Otherwise any random numbers drawn within the loop # would make the test non-deterministic even if we fix the seed before # the loop.Right now, this test does not draw any samples, but we still # sort the entries to prevent future related failures. for ttype in sorted(factors): dim = ttype[1] if dim == 2: nslices = 1 interp_method = interpolate_scalar_2d else: nslices = 45 interp_method = interpolate_scalar_3d transform = regtransforms[ttype] factor = factors[ttype] theta = transform.get_identity_parameters() static, moving, static_g2w, moving_g2w, smask, mmask, M = \ setup_random_transform(transform, factor, nslices, 5.0) parzen_hist = ParzenJointHistogram(32) parzen_hist.setup(static, moving, smask, mmask) # Sample the fixed-image domain k = 3 sigma = 0.25 seed = 1234 shape = np.array(static.shape, dtype=np.int32) samples = sample_domain_regular(k, shape, static_g2w, sigma, seed) samples = np.array(samples) samples = np.hstack((samples, np.ones(samples.shape[0])[:, None])) sp_to_static = np.linalg.inv(static_g2w) samples_static_grid = (sp_to_static.dot(samples.T).T)[..., :dim] intensities_static, inside = interp_method(static.astype(np.float32), samples_static_grid) # The routines in vector_fields operate, mostly, with float32 because # they were thought to be used for non-linear registration. We may need # to write some float64 counterparts for affine registration, where # memory is not so big issue intensities_static = np.array(intensities_static, dtype=np.float64) # Compute the gradient at theta with the implementation under test M = transform.param_to_matrix(theta) sp_to_moving = np.linalg.inv(moving_g2w).dot(M) samples_moving_grid = (sp_to_moving.dot(samples.T).T)[..., :dim] intensities_moving, inside = interp_method(moving.astype(np.float32), samples_moving_grid) intensities_moving = np.array(intensities_moving, dtype=np.float64) parzen_hist.update_pdfs_sparse(intensities_static, intensities_moving) # Get the joint distribution evaluated at theta J0 = np.copy(parzen_hist.joint) spacing = np.ones(dim + 1, dtype=np.float64) mgrad, inside = vf.sparse_gradient(moving.astype(np.float32), sp_to_moving, spacing, samples) parzen_hist.update_gradient_sparse(theta, transform, intensities_static, intensities_moving, samples[..., :dim], mgrad) # Get the gradient of the joint distribution w.r.t. the transform # parameters actual = np.copy(parzen_hist.joint_grad) # Compute the gradient using finite-diferences n = transform.get_number_of_parameters() expected = np.empty_like(actual) for i in range(n): dtheta = theta.copy() dtheta[i] += h # Update the joint distribution with the transformed moving image M = transform.param_to_matrix(dtheta) sp_to_moving = np.linalg.inv(moving_g2w).dot(M) samples_moving_grid = sp_to_moving.dot(samples.T).T intensities_moving, inside = \ interp_method(moving.astype(np.float32), samples_moving_grid) intensities_moving = np.array(intensities_moving, dtype=np.float64) parzen_hist.update_pdfs_sparse(intensities_static, intensities_moving) J1 = np.copy(parzen_hist.joint) expected[..., i] = (J1 - J0) / h # Dot product and norms of gradients of each joint histogram cell # i.e. the derivatives of each cell w.r.t. all parameters P = (expected * actual).sum(2) enorms = np.sqrt((expected**2).sum(2)) anorms = np.sqrt((actual**2).sum(2)) prodnorms = enorms * anorms # Cosine of angle between the expected and actual gradients. # Exclude very small gradients P[prodnorms > 1e-6] /= (prodnorms[prodnorms > 1e-6]) P[prodnorms <= 1e-6] = 0 # Verify that a large proportion of the gradients point almost in # the same direction. Disregard very small gradients mean_cosine = P[P != 0].mean() std_cosine = P[P != 0].std() assert (mean_cosine > 0.99) assert (std_cosine < 0.15)
def test_joint_pdf_gradients_sparse(): h = 1e-4 # Make sure dictionary entries are processed in the same order regardless of # the platform. Otherwise any random numbers drawn within the loop would make # the test non-deterministic even if we fix the seed before the loop. # Right now, this test does not draw any samples, but we still sort the entries # to prevent future related failures. for ttype in sorted(factors): dim = ttype[1] if dim == 2: nslices = 1 interp_method = vf.interpolate_scalar_2d else: nslices = 45 interp_method = vf.interpolate_scalar_3d transform = regtransforms[ttype] factor = factors[ttype] theta = transform.get_identity_parameters() static, moving, static_g2w, moving_g2w, smask, mmask, M = \ setup_random_transform(transform, factor, nslices, 5.0) parzen_hist = ParzenJointHistogram(32) parzen_hist.setup(static, moving, smask, mmask) # Sample the fixed-image domain k = 3 sigma = 0.25 seed = 1234 shape = np.array(static.shape, dtype=np.int32) samples = sample_domain_regular(k, shape, static_g2w, sigma, seed) samples = np.array(samples) samples = np.hstack((samples, np.ones(samples.shape[0])[:, None])) sp_to_static = np.linalg.inv(static_g2w) samples_static_grid = (sp_to_static.dot(samples.T).T)[..., :dim] intensities_static, inside = interp_method(static.astype(np.float32), samples_static_grid) # The routines in vector_fields operate, mostly, with float32 because # they were thought to be used for non-linear registration. We may need # to write some float64 counterparts for affine registration, where # memory is not so big issue intensities_static = np.array(intensities_static, dtype=np.float64) # Compute the gradient at theta with the implementation under test M = transform.param_to_matrix(theta) sp_to_moving = np.linalg.inv(moving_g2w).dot(M) samples_moving_grid = (sp_to_moving.dot(samples.T).T)[..., :dim] intensities_moving, inside = interp_method(moving.astype(np.float32), samples_moving_grid) intensities_moving = np.array(intensities_moving, dtype=np.float64) parzen_hist.update_pdfs_sparse(intensities_static, intensities_moving) # Get the joint distribution evaluated at theta J0 = np.copy(parzen_hist.joint) spacing = np.ones(dim + 1, dtype=np.float64) mgrad, inside = vf.sparse_gradient(moving.astype(np.float32), sp_to_moving, spacing, samples) parzen_hist.update_gradient_sparse(theta, transform, intensities_static, intensities_moving, samples[..., :dim], mgrad) # Get the gradient of the joint distribution w.r.t. the transform # parameters actual = np.copy(parzen_hist.joint_grad) # Compute the gradient using finite-diferences n = transform.get_number_of_parameters() expected = np.empty_like(actual) for i in range(n): dtheta = theta.copy() dtheta[i] += h # Update the joint distribution with the transformed moving image M = transform.param_to_matrix(dtheta) sp_to_moving = np.linalg.inv(moving_g2w).dot(M) samples_moving_grid = sp_to_moving.dot(samples.T).T intensities_moving, inside = \ interp_method(moving.astype(np.float32), samples_moving_grid) intensities_moving = np.array(intensities_moving, dtype=np.float64) parzen_hist.update_pdfs_sparse(intensities_static, intensities_moving) J1 = np.copy(parzen_hist.joint) expected[..., i] = (J1 - J0) / h # Dot product and norms of gradients of each joint histogram cell # i.e. the derivatives of each cell w.r.t. all parameters P = (expected * actual).sum(2) enorms = np.sqrt((expected ** 2).sum(2)) anorms = np.sqrt((actual ** 2).sum(2)) prodnorms = enorms*anorms # Cosine of angle between the expected and actual gradients. # Exclude very small gradients P[prodnorms > 1e-6] /= (prodnorms[prodnorms > 1e-6]) P[prodnorms <= 1e-6] = 0 # Verify that a large proportion of the gradients point almost in # the same direction. Disregard very small gradients mean_cosine = P[P != 0].mean() std_cosine = P[P != 0].std() assert(mean_cosine > 0.99) assert(std_cosine < 0.15)
def test_gradient_3d(): np.random.seed(3921116) shape = (25, 32, 15) # Create grid coordinates x_0 = np.asarray(range(shape[0])) x_1 = np.asarray(range(shape[1])) x_2 = np.asarray(range(shape[2])) X = np.zeros(shape + (4, ), dtype=np.float64) O = np.ones(shape) X[..., 0] = x_0[:, None, None] * O X[..., 1] = x_1[None, :, None] * O X[..., 2] = x_2[None, None, :] * O X[..., 3] = 1 transform = regtransforms[("RIGID", 3)] theta = np.array([0.1, 0.05, 0.12, -12.0, -15.5, -7.2]) T = transform.param_to_matrix(theta) TX = X.dot(T.T) # Eval an arbitrary (known) function at TX # f(x, y, z) = ax^2 + by^2 + cz^2 + dxy + exz + fyz # df/dx = 2ax + dy + ez # df/dy = 2by + dx + fz # df/dz = 2cz + ex + fy a, b, c = 2e-3, 3e-3, 1e-3 d, e, f = 1e-3, 2e-3, 3e-3 img = (a * TX[..., 0]**2 + b * TX[..., 1]**2 + c * TX[..., 2]**2 + d * TX[..., 0] * TX[..., 1] + e * TX[..., 0] * TX[..., 2] + f * TX[..., 1] * TX[..., 2]) img = img.astype(floating) # Test sparse gradient: choose some sample points (in space) sample = sample_domain_regular(100, np.array(shape, dtype=np.int32), T) sample = np.array(sample) # Compute the analytical gradient at all points expected = np.empty((sample.shape[0], 3), dtype=floating) expected[..., 0] = (2 * a * sample[:, 0] + d * sample[:, 1] + e * sample[:, 2]) expected[..., 1] = (2 * b * sample[:, 1] + d * sample[:, 0] + f * sample[:, 2]) expected[..., 2] = (2 * c * sample[:, 2] + e * sample[:, 0] + f * sample[:, 1]) # Get the numerical gradient with the implementation under test sp_to_grid = np.linalg.inv(T) img_spacing = np.ones(3) actual, inside = vfu.sparse_gradient(img, sp_to_grid, img_spacing, sample) img_d = cupy.asarray(img) img_spacing_d = cupy.asarray(img_spacing) sp_to_grid_d = cupy.asarray(sp_to_grid) sample_d = cupy.asarray(sample) actual_gpu, inside_gpu = sparse_gradient(img_d, sp_to_grid_d, img_spacing_d, sample_d.T) atol = rtol = 1e-5 cupy.testing.assert_allclose( actual * inside[..., np.newaxis], actual_gpu * inside_gpu[..., np.newaxis], atol=atol, rtol=rtol, ) cupy.testing.assert_array_equal(inside, inside_gpu) # TODO: test invalid inputs # # Verify exception is raised when passing invalid affine or spacings # invalid_affine = np.eye(3) # invalid_spacings = np.ones(2) # 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(shape + (3, ), dtype=floating) expected[..., 0] = 2 * a * TX[..., 0] + d * TX[..., 1] + e * TX[..., 2] expected[..., 1] = 2 * b * TX[..., 1] + d * TX[..., 0] + f * TX[..., 2] expected[..., 2] = 2 * c * TX[..., 2] + e * TX[..., 0] + f * TX[..., 1] # Get the numerical gradient with the implementation under test sp_to_grid = np.linalg.inv(T) img_spacing = np.ones(3) actual, inside = vfu.gradient(img, sp_to_grid, img_spacing, shape, T) sp_to_grid_d = cupy.asarray(sp_to_grid) img_spacing_d = cupy.asarray(img_spacing) T_d = cupy.asarray(T) actual_gpu, inside_gpu = gradient(img_d, sp_to_grid_d, img_spacing_d, shape, T_d) atol = rtol = 1e-5 cupy.testing.assert_allclose( actual * inside[..., np.newaxis], actual_gpu * inside_gpu[..., np.newaxis], atol=atol, rtol=rtol, ) cupy.testing.assert_array_equal(inside, inside_gpu)
def test_gradient_2d(): np.random.seed(3921116) sh = (25, 32) # Create grid coordinates x_0 = np.arange(sh[0]) x_1 = np.arange(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) 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) img_d = cupy.asarray(img) img_spacing_d = cupy.asarray(img_spacing) sp_to_grid_d = cupy.asarray(sp_to_grid) sample_d = cupy.asarray(sample) actual, inside = vfu.sparse_gradient(img, sp_to_grid, img_spacing, sample) actual_gpu, inside_gpu = sparse_gradient(img_d, sp_to_grid_d, img_spacing_d, sample_d.T) atol = rtol = 1e-5 cupy.testing.assert_allclose( actual * inside[..., np.newaxis], actual_gpu * inside_gpu[..., np.newaxis], atol=atol, rtol=rtol, ) cupy.testing.assert_array_equal(inside, inside_gpu) # TODO: verify exceptions # # 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) sp_to_grid_d = cupy.asarray(sp_to_grid) img_spacing_d = cupy.asarray(img_spacing) T_d = cupy.asarray(T) actual_gpu, inside_gpu = gradient(img_d, sp_to_grid_d, img_spacing_d, sh, T_d) atol = rtol = 1e-5 cupy.testing.assert_allclose( actual * inside[..., np.newaxis], actual_gpu * inside_gpu[..., np.newaxis], atol=atol, rtol=rtol, ) cupy.testing.assert_array_equal(inside, inside_gpu)