def test_mi_gradient_sparse(): # Test the gradient of mutual information h = 1e-5 for ttype in factors: transform = regtransforms[ttype] dim = ttype[1] if dim == 2: nslices = 1 interp_method = vf.interpolate_scalar_2d else: nslices = 45 interp_method = vf.interpolate_scalar_3d # Get data (pair of images related to each other by an known transform) factor = factors[ttype] static, moving, static_g2w, moving_g2w, smask, mmask, M = \ setup_random_transform(transform, factor, nslices, 5.0) smask = None mmask = None # Sample static 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) intensities_static = np.array(intensities_static, dtype=np.float64) # Prepare a MattesBase instance # The computation of the metric is done in 3 steps: # 1.Compute the joint distribution # 2.Compute the gradient of the joint distribution # 3.Compute the metric's value and gradient using results from 1 and 2 metric = MattesBase(32) metric.setup(static, moving, smask, mmask) # 1. Update the joint distribution sp_to_moving = np.linalg.inv(moving_g2w) 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) metric.update_pdfs_sparse(intensities_static, intensities_moving) # 2. Update the joint distribution gradient (the derivative of each # histogram cell w.r.t. the transform parameters). This requires # to evaluate the gradient of the moving image at the sampling points theta = transform.get_identity_parameters().copy() spacing = np.ones(dim, dtype=np.float64) shape = np.array(static.shape, dtype=np.int32) mgrad, inside = vf.sparse_gradient(moving.astype(np.float32), sp_to_moving, spacing, samples[..., :dim]) metric.update_gradient_sparse(theta, transform, intensities_static, intensities_moving, samples[..., :dim], mgrad) # 3. Update the metric (in this case, the Mutual Information) and its # gradient, which is computed from the joint density and its gradient metric.update_mi_metric(update_gradient=True) # Now we can extract the value and gradient of the metric # This is the gradient according to the implementation under test val0 = metric.metric_val actual = np.copy(metric.metric_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 M = transform.param_to_matrix(dtheta) shape = np.array(static.shape, dtype=np.int32) 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) metric.update_pdfs_sparse(intensities_static, intensities_moving) metric.update_mi_metric(update_gradient=False) val1 = metric.metric_val expected[i] = (val1 - val0) / h dp = expected.dot(actual) enorm = np.linalg.norm(expected) anorm = np.linalg.norm(actual) nprod = dp / (enorm * anorm) assert(nprod > 0.9999)
def test_mi_gradient_sparse(): # Test the gradient of mutual information h = 1e-5 for ttype in factors: transform = regtransforms[ttype] dim = ttype[1] if dim == 2: nslices = 1 interp_method = vf.interpolate_scalar_2d else: nslices = 45 interp_method = vf.interpolate_scalar_3d # Get data (pair of images related to each other by an known transform) factor = factors[ttype] static, moving, static_g2w, moving_g2w, smask, mmask, M = \ setup_random_transform(transform, factor, nslices, 5.0) smask = None mmask = None # Sample static 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) intensities_static = np.array(intensities_static, dtype=np.float64) # Prepare a MattesBase instance # The computation of the metric is done in 3 steps: # 1.Compute the joint distribution # 2.Compute the gradient of the joint distribution # 3.Compute the metric's value and gradient using results from 1 and 2 metric = MattesBase(32) metric.setup(static, moving, smask, mmask) # 1. Update the joint distribution sp_to_moving = np.linalg.inv(moving_g2w) 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) metric.update_pdfs_sparse(intensities_static, intensities_moving) # 2. Update the joint distribution gradient (the derivative of each # histogram cell w.r.t. the transform parameters). This requires # to evaluate the gradient of the moving image at the sampling points theta = transform.get_identity_parameters().copy() spacing = np.ones(dim, dtype=np.float64) shape = np.array(static.shape, dtype=np.int32) mgrad, inside = vf.sparse_gradient(moving.astype(np.float32), sp_to_moving, spacing, samples[..., :dim]) metric.update_gradient_sparse(theta, transform, intensities_static, intensities_moving, samples[..., :dim], mgrad) # 3. Update the metric (in this case, the Mutual Information) and its # gradient, which is computed from the joint density and its gradient metric.update_mi_metric(update_gradient=True) # Now we can extract the value and gradient of the metric # This is the gradient according to the implementation under test val0 = metric.metric_val actual = np.copy(metric.metric_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 M = transform.param_to_matrix(dtheta) shape = np.array(static.shape, dtype=np.int32) 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) metric.update_pdfs_sparse(intensities_static, intensities_moving) metric.update_mi_metric(update_gradient=False) val1 = metric.metric_val expected[i] = (val1 - val0) / h dp = expected.dot(actual) enorm = np.linalg.norm(expected) anorm = np.linalg.norm(actual) nprod = dp / (enorm * anorm) assert (nprod > 0.9999)
def test_mi_gradient_dense(): # Test the gradient of mutual information h = 1e-5 for ttype in factors: transform = regtransforms[ttype] dim = ttype[1] if dim == 2: nslices = 1 warp_method = vf.warp_2d_affine else: nslices = 45 warp_method = vf.warp_3d_affine # Get data (pair of images related to each other by an known transform) factor = factors[ttype] static, moving, static_g2w, moving_g2w, smask, mmask, M = \ setup_random_transform(transform, factor, nslices, 5.0) smask = None mmask = None # Prepare a MattesBase instance # The computation of the metric is done in 3 steps: # 1.Compute the joint distribution # 2.Compute the gradient of the joint distribution # 3.Compute the metric's value and gradient using results from 1 and 2 metric = MattesBase(32) metric.setup(static, moving, smask, mmask) # 1. Update the joint distribution metric.update_pdfs_dense(static.astype(np.float64), moving.astype(np.float64)) # 2. Update the joint distribution gradient (the derivative of each # histogram cell w.r.t. the transform parameters). This requires # among other things, the spatial gradient of the moving image. theta = transform.get_identity_parameters().copy() grid_to_space = np.eye(dim + 1) spacing = np.ones(dim, dtype=np.float64) shape = np.array(static.shape, dtype=np.int32) mgrad, inside = vf.gradient(moving.astype(np.float32), moving_g2w, spacing, shape, grid_to_space) metric.update_gradient_dense(theta, transform, static.astype(np.float64), moving.astype(np.float64), grid_to_space, mgrad, smask, mmask) # 3. Update the metric (in this case, the Mutual Information) and its # gradient, which is computed from the joint density and its gradient metric.update_mi_metric(update_gradient=True) # Now we can extract the value and gradient of the metric # This is the gradient according to the implementation under test val0 = metric.metric_val actual = np.copy(metric.metric_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 M = transform.param_to_matrix(dtheta) shape = np.array(static.shape, dtype=np.int32) warped = np.array(warp_method(moving.astype(np.float32), shape, M)) metric.update_pdfs_dense(static.astype(np.float64), warped.astype(np.float64)) metric.update_mi_metric(update_gradient=False) val1 = metric.metric_val expected[i] = (val1 - val0) / h dp = expected.dot(actual) enorm = np.linalg.norm(expected) anorm = np.linalg.norm(actual) nprod = dp / (enorm * anorm) assert(nprod >= 0.999)
def test_mi_gradient_dense(): # Test the gradient of mutual information h = 1e-5 for ttype in factors: transform = regtransforms[ttype] dim = ttype[1] if dim == 2: nslices = 1 warp_method = vf.warp_2d_affine else: nslices = 45 warp_method = vf.warp_3d_affine # Get data (pair of images related to each other by an known transform) factor = factors[ttype] static, moving, static_g2w, moving_g2w, smask, mmask, M = \ setup_random_transform(transform, factor, nslices, 5.0) smask = None mmask = None # Prepare a MattesBase instance # The computation of the metric is done in 3 steps: # 1.Compute the joint distribution # 2.Compute the gradient of the joint distribution # 3.Compute the metric's value and gradient using results from 1 and 2 metric = MattesBase(32) metric.setup(static, moving, smask, mmask) # 1. Update the joint distribution metric.update_pdfs_dense(static.astype(np.float64), moving.astype(np.float64)) # 2. Update the joint distribution gradient (the derivative of each # histogram cell w.r.t. the transform parameters). This requires # among other things, the spatial gradient of the moving image. theta = transform.get_identity_parameters().copy() grid_to_space = np.eye(dim + 1) spacing = np.ones(dim, dtype=np.float64) shape = np.array(static.shape, dtype=np.int32) mgrad, inside = vf.gradient(moving.astype(np.float32), moving_g2w, spacing, shape, grid_to_space) metric.update_gradient_dense(theta, transform, static.astype(np.float64), moving.astype(np.float64), grid_to_space, mgrad, smask, mmask) # 3. Update the metric (in this case, the Mutual Information) and its # gradient, which is computed from the joint density and its gradient metric.update_mi_metric(update_gradient=True) # Now we can extract the value and gradient of the metric # This is the gradient according to the implementation under test val0 = metric.metric_val actual = np.copy(metric.metric_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 M = transform.param_to_matrix(dtheta) shape = np.array(static.shape, dtype=np.int32) warped = np.array(warp_method(moving.astype(np.float32), shape, M)) metric.update_pdfs_dense(static.astype(np.float64), warped.astype(np.float64)) metric.update_mi_metric(update_gradient=False) val1 = metric.metric_val expected[i] = (val1 - val0) / h dp = expected.dot(actual) enorm = np.linalg.norm(expected) anorm = np.linalg.norm(actual) nprod = dp / (enorm * anorm) assert (nprod >= 0.999)