def test_2d(timing=False, verbose=False, print_reg_params=False): if timing is True: tic = time.time() try: X = np.loadtxt(os.path.join(dir_path, "..", "data", "fish_target.txt")) except OSError: raise Exception("Error finding data!") try: Y = np.loadtxt(os.path.join(dir_path, "..", "data", "fish_source.txt")) except OSError: raise Exception("Error finding data!") reg = deformable_registration( **{ "X": X, "Y": Y, "verbose": verbose, "print_reg_params": print_reg_params, "max_iterations": 500, }) TY, _ = reg.register() assert_array_almost_equal(X, TY, decimal=1) if timing is True: toc = time.time() print("Test 2D Affine took on fish took: {}".format(toc - tic))
def test_3d(timing=False, print_errors=False, verbose=False, print_reg_params=False, one_percent_error=0.5, five_percent_error=0.4, ten_percent_error=0.35): if timing is True: tic = time.time() try: X = np.loadtxt('../data/surface_points_bone_deformable_target.npy') except OSError: X = np.loadtxt('data/surface_points_bone_deformable_target.npy') # Below are points from a completely different knee that were already rigidly registered to X # If there isnt something to make them "somewhat" close to one another, then the registration fails. # Therefore, this first step was performed to improve testing. try: Y = np.loadtxt('../data/surface_points_bone_1_5k_points.npy') except OSError: Y = np.loadtxt('data/surface_points_bone_1_5k_points.npy') # These will not perfectly align and they will not even be "done" when we get to iteration 100. # But this is a good starting point test. reg = deformable_registration( **{ 'X': X, 'Y': Y, 'max_iterations': 100, 'alpha': 0.1, 'beta': 3, 'verbose': verbose, 'print_reg_params': print_reg_params }) TY, _ = reg.register() differences = X[:, None, :] - TY[None, :, :] distances = np.sqrt(np.sum(differences**2, axis=2)) min_x_dist_per_ty_point = np.min(distances, axis=0) sorted_distances = np.sort(min_x_dist_per_ty_point) worst_one_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.99)] worst_five_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.95)] worst_ten_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.90)] if print_errors is True: print('Worst 1% error: {}'.format(worst_one_percent_error)) print('Worst 5% error: {}'.format(worst_five_percent_error)) print('Worst 10% error: {}'.format(worst_ten_percent_error)) assert worst_one_percent_error < one_percent_error assert worst_five_percent_error < five_percent_error assert worst_ten_percent_error < ten_percent_error if timing is True: toc = time.time() print('Test 3D Deformable on knee with 5k points took: {}'.format(toc - tic))
def register_target_to_source(self, reg_type='deformable'): if reg_type == 'deformable': reg = cycpd.deformable_registration( **{ 'X': self.source_spectral_coords[ self.graph_source. get_list_rand_idxs(self.n_coords_spectral_registration ), :], 'Y': self.target_spectral_coords[ self.graph_target. get_list_rand_idxs(self.n_coords_spectral_registration ), :], 'num_eig': self.non_rigid_n_eigens, 'max_iterations': self.non_rigid_max_iterations, 'tolerance': self.non_rigid_tolerance, 'alpha': self.non_rigid_alpha, 'beta': self.non_rigid_beta }) _, self.non_rigid_params = reg.register() elif reg_type == 'affine': # Using affine instead of truly rigid, because rigid doesnt accept >3 dimensions at moment. reg = cycpd.affine_registration( **{ 'X': self.source_spectral_coords[ self.graph_source. get_list_rand_idxs(self.n_coords_spectral_registration ), :], 'Y': self.target_spectral_coords[ self.graph_target. get_list_rand_idxs(self.n_coords_spectral_registration ), :], 'max_iterations': self.rigid_reg_max_iterations, 'tolerance': self.rigid_tolerance }) _, self.rigid_params = reg.register() # Apply transform to all points (ensures all points are transformed even if not all used for registration). self.target_spectral_coords = reg.transform_point_cloud( self.target_spectral_coords)
def main(): X = np.loadtxt('../data/surface_points_bone_deformable_target.npy') # Below are points from a completely different knee that were already rigidly registered to X # If there isnt something to make them "somewhat" close to one another, then the registration fails. # Therefore, this first step was performed to improve testing. Y = np.loadtxt('../data/surface_points_bone_1_5k_points.npy') # These will not perfectly align and they will not even be "done" when we get to iteration 100. # But this is a good starting point test and shows the movement of one of the meshes over time as it tries to align # with the other mesh. fig = plt.figure(figsize=plt.figaspect(0.5)) ax1 = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122, projection='3d') ax = [ax1, ax2] callback = partial(visualize, ax=ax, fig=fig) reg = deformable_registration(**{ 'X': X, 'Y': Y, 'max_iterations': 100, 'alpha': 0.1, 'beta': 3 }) TY, _ = reg.register(callback) plt.show() differences = X[:, None, :] - TY[None, :, :] distances = np.sqrt(np.sum(differences**2, axis=2)) min_x_dist_per_ty_point = np.min(distances, axis=0) sorted_distances = np.sort(min_x_dist_per_ty_point) worst_one_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.99)] worst_five_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.95)] worst_ten_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.90)] plt.figure() plt.hist(sorted_distances) plt.show() print('Bottom 1% error: {}'.format(worst_one_percent_error)) print('Bottom 5% error: {}'.format(worst_five_percent_error)) print('Bottom 10% error: {}'.format(worst_ten_percent_error))
def test_2d(timing=False, verbose=False, print_reg_params=False): if timing is True: tic = time.time() try: X = np.loadtxt('data/fish_target.txt') except OSError: X = np.loadtxt('../data/fish_target.txt') try: Y = np.loadtxt('data/fish_source.txt') except OSError: Y = np.loadtxt('../data/fish_source.txt') reg = deformable_registration(**{ 'X': X, 'Y': Y, 'verbose': verbose, 'print_reg_params': print_reg_params }) TY, _ = reg.register() assert_array_almost_equal(X, TY, decimal=1) if timing is True: toc = time.time() print('Test 2D Affine took on fish took: {}'.format(toc - tic))
def test_3d( timing=False, print_errors=False, verbose=False, print_reg_params=False, one_percent_error=0.5, five_percent_error=0.4, ten_percent_error=0.35, ): if timing is True: tic = time.time() try: X = np.loadtxt( os.path.join(dir_path, "..", "data", "surface_points_bone_deformable_target.npy")) except OSError: raise Exception("Error finding data!") # Below are points from a completely different knee that were already rigidly registered to X # If there isnt something to make them "somewhat" close to one another, then the registration fails. # Therefore, this first step was performed to improve testing. try: Y = np.loadtxt( os.path.join(dir_path, "..", "data", "surface_points_bone_1_5k_points.npy")) except OSError: raise Exception("Error finding data!") # These will not perfectly align and they will not even be "done" when we get to iteration 100. # But this is a good starting point test. reg = deformable_registration( **{ "X": X, "Y": Y, "max_iterations": 500, "alpha": 0.1, "beta": 3, "verbose": verbose, "print_reg_params": print_reg_params, }) TY, _ = reg.register() differences = X[:, None, :] - TY[None, :, :] distances = np.sqrt(np.sum(differences**2, axis=2)) min_x_dist_per_ty_point = np.min(distances, axis=0) sorted_distances = np.sort(min_x_dist_per_ty_point) worst_one_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.99)] worst_five_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.95)] worst_ten_percent_error = sorted_distances[int( len(min_x_dist_per_ty_point) * 0.90)] if print_errors is True: print("Worst 1% error: {}".format(worst_one_percent_error)) print("Worst 5% error: {}".format(worst_five_percent_error)) print("Worst 10% error: {}".format(worst_ten_percent_error)) assert worst_one_percent_error < one_percent_error assert worst_five_percent_error < five_percent_error assert worst_ten_percent_error < ten_percent_error if timing is True: toc = time.time() print("Test 3D Deformable on knee with 5k points took: {}".format(toc - tic))