def test_mattes_base(): # Test the simple functionality of MattesBase, # the gradients and computation of the joint intensity distribution # will be tested independently for nbins in [15, 30, 50]: for min_int in [-10.0, 0.0, 10.0]: for intensity_range in [0.1, 1.0, 10.0]: fact = 1 max_int = min_int + intensity_range M = MattesBase(nbins) # Make a pair of 4-pixel images, introduce +/- 1 values # that will be excluded using a mask static = np.array([min_int - 1.0, min_int, max_int, max_int + 1.0]) # Multiply by an arbitrary value (make the ranges different) moving = fact * np.array([min_int, min_int - 1.0, max_int + 1.0, max_int]) # Create a mask to exclude the invalid values (beyond min and # max computed above) static_mask = np.array([0, 1, 1, 0]) moving_mask = np.array([1, 0, 0, 1]) M.setup(static, moving, static_mask, moving_mask) # Test bin_normalize_static at the boundary normalized = M.bin_normalize_static(min_int) assert_almost_equal(normalized, M.padding) index = M.bin_index(normalized) assert_equal(index, M.padding) normalized = M.bin_normalize_static(max_int) assert_almost_equal(normalized, nbins - M.padding) index = M.bin_index(normalized) assert_equal(index, nbins - 1 - M.padding) # Test bin_normalize_moving at the boundary normalized = M.bin_normalize_moving(fact * min_int) assert_almost_equal(normalized, M.padding) index = M.bin_index(normalized) assert_equal(index, M.padding) normalized = M.bin_normalize_moving(fact * max_int) assert_almost_equal(normalized, nbins - M.padding) index = M.bin_index(normalized) assert_equal(index, nbins - 1 - M.padding) # Test bin_index not at the boundary delta_s = (max_int - min_int) / (nbins - 2 * M.padding) delta_m = fact * (max_int - min_int) / (nbins - 2 * M.padding) for i in range(nbins - 2 * M.padding): normalized = M.bin_normalize_static(min_int + (i + 0.5) * delta_s) index = M.bin_index(normalized) assert_equal(index, M.padding + i) normalized = M.bin_normalize_moving(fact * min_int + (i + 0.5) * delta_m) index = M.bin_index(normalized) assert_equal(index, M.padding + i)
def test_mattes_base(): # Test the simple functionality of MattesBase, # the gradients and computation of the joint intensity distribution # will be tested independently for nbins in [15, 30, 50]: for min_int in [-10.0, 0.0, 10.0]: for intensity_range in [0.1, 1.0, 10.0]: fact = 1 max_int = min_int + intensity_range M = MattesBase(nbins) # Make a pair of 4-pixel images, introduce +/- 1 values # that will be excluded using a mask static = np.array( [min_int - 1.0, min_int, max_int, max_int + 1.0]) # Multiply by an arbitrary value (make the ranges different) moving = fact * np.array( [min_int, min_int - 1.0, max_int + 1.0, max_int]) # Create a mask to exclude the invalid values (beyond min and # max computed above) static_mask = np.array([0, 1, 1, 0]) moving_mask = np.array([1, 0, 0, 1]) M.setup(static, moving, static_mask, moving_mask) # Test bin_normalize_static at the boundary normalized = M.bin_normalize_static(min_int) assert_almost_equal(normalized, M.padding) index = M.bin_index(normalized) assert_equal(index, M.padding) normalized = M.bin_normalize_static(max_int) assert_almost_equal(normalized, nbins - M.padding) index = M.bin_index(normalized) assert_equal(index, nbins - 1 - M.padding) # Test bin_normalize_moving at the boundary normalized = M.bin_normalize_moving(fact * min_int) assert_almost_equal(normalized, M.padding) index = M.bin_index(normalized) assert_equal(index, M.padding) normalized = M.bin_normalize_moving(fact * max_int) assert_almost_equal(normalized, nbins - M.padding) index = M.bin_index(normalized) assert_equal(index, nbins - 1 - M.padding) # Test bin_index not at the boundary delta_s = (max_int - min_int) / (nbins - 2 * M.padding) delta_m = fact * (max_int - min_int) / (nbins - 2 * M.padding) for i in range(nbins - 2 * M.padding): normalized = M.bin_normalize_static(min_int + (i + 0.5) * delta_s) index = M.bin_index(normalized) assert_equal(index, M.padding + i) normalized = M.bin_normalize_moving(fact * min_int + (i + 0.5) * delta_m) index = M.bin_index(normalized) assert_equal(index, M.padding + i)
def test_mattes_densities(): # Test the computation of the joint intensity distribution # using a dense and a sparse set of values seed = 1246592 nbins = 32 nr = 30 nc = 35 ns = 20 nvals = 50 for dim in [2, 3]: if dim == 2: shape = (nr, nc) static, moving = create_random_image_pair(shape, nvals, seed) else: shape = (ns, nr, nc) static, moving = create_random_image_pair(shape, nvals, seed) # Initialize mbase = MattesBase(nbins) mbase.setup(static, moving) # Get distributions computed by dense sampling mbase.update_pdfs_dense(static, moving) actual_joint_dense = mbase.joint actual_mmarginal_dense = mbase.mmarginal actual_smarginal_dense = mbase.smarginal # Get distributions computed by sparse sampling sval = static.reshape(-1) mval = moving.reshape(-1) mbase.update_pdfs_sparse(sval, mval) actual_joint_sparse = mbase.joint actual_mmarginal_sparse = mbase.mmarginal actual_smarginal_sparse = mbase.smarginal # Compute the expected joint distribution with dense sampling expected_joint_dense = np.zeros(shape=(nbins, nbins)) for index in ndindex(shape): sv = mbase.bin_normalize_static(static[index]) mv = mbase.bin_normalize_moving(moving[index]) sbin = mbase.bin_index(sv) # The spline is centered at mv, will evaluate for all row spline_arg = np.array([i - mv for i in range(nbins)]) contribution = cubic_spline(spline_arg) expected_joint_dense[sbin, :] += contribution # Compute the expected joint distribution with sparse sampling expected_joint_sparse = np.zeros(shape=(nbins, nbins)) for index in range(sval.shape[0]): sv = mbase.bin_normalize_static(sval[index]) mv = mbase.bin_normalize_moving(mval[index]) sbin = mbase.bin_index(sv) # The spline is centered at mv, will evaluate for all row spline_arg = np.array([i - mv for i in range(nbins)]) contribution = cubic_spline(spline_arg) expected_joint_sparse[sbin, :] += contribution # Verify joint distributions expected_joint_dense /= expected_joint_dense.sum() expected_joint_sparse /= expected_joint_sparse.sum() assert_array_almost_equal(actual_joint_dense, expected_joint_dense) assert_array_almost_equal(actual_joint_sparse, expected_joint_sparse) # Verify moving marginals expected_mmarginal_dense = expected_joint_dense.sum(0) expected_mmarginal_dense /= expected_mmarginal_dense.sum() expected_mmarginal_sparse = expected_joint_sparse.sum(0) expected_mmarginal_sparse /= expected_mmarginal_sparse.sum() assert_array_almost_equal(actual_mmarginal_dense, expected_mmarginal_dense) assert_array_almost_equal(actual_mmarginal_sparse, expected_mmarginal_sparse) # Verify static marginals expected_smarginal_dense = expected_joint_dense.sum(1) expected_smarginal_dense /= expected_smarginal_dense.sum() expected_smarginal_sparse = expected_joint_sparse.sum(1) expected_smarginal_sparse /= expected_smarginal_sparse.sum() assert_array_almost_equal(actual_smarginal_dense, expected_smarginal_dense) assert_array_almost_equal(actual_smarginal_sparse, expected_smarginal_sparse)