def test_line_integral_2(): def inner_of_box(x, y, z): if (0.5 > x > -0.5) and (0.5 > y > -0.5) and (0.5 > z > -0.5): return 1 return 0 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } number_rays = {'dim_1': 1, 'dim_2': 1} number_ktrans = 1 radius = 10 steps = 4 x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) line_value = line_integral(rays_d[0][0], x_cor, y_cor, z_cor, values) np.testing.assert_almost_equal(line_value, 1, decimal=2)
def compare(steps, number_ktrans, number_rays, fineness, inner_of_box): radius = 3 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) discrete_model = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) one_d_model = threed_to_oned(discrete_model, cuboid_coordinates, steps) source_point_d = create_source_point_d(radius, number_ktrans, perc_circle=0.125) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) cont_results = create_con_results(rays_d, inner_of_box, fineness) dis_results = k_trafo_one_dim_all(liam_all, one_d_model) return cont_results, dis_results
def test_k_transform(): def inner_of_box(x, y, z): if (1 > x > -1) and (1 > y > -1) and (1 > z > -1): return 13 return 0 steps = 4 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } number_rays = {'dim_1': 1, 'dim_2': 1} number_ktrans = 1 radius = 3 fineness = 10**3 x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) liam = generate_line_integral_array_matrix(rays_d[0], cuboid_coordinates, steps, fineness) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) values_1d = threed_to_oned(values, cuboid_coordinates, steps) result_k_1d = k_trafo_one_dim(liam, values_1d) res_c = np.exp(-line_integral(rays_d[0][0], x_cor, y_cor, z_cor, values)) np.testing.assert_almost_equal(result_k_1d, res_c, decimal=2)
def test_threed_to_oned_4(): """ here a function is discretized in a threed model, transformed to a oned array and transformed back to a threed model, the 3d models are gonna be compared""" def inner_of_box(x, y, z): rad_pos = (x**2 + y**2 + z**2)**0.5 if rad_pos <= 0.2: return 1 if rad_pos <= 0.5: return 0.5 if rad_pos <= 0.8: return 0.1 return 0 steps = 20 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) oned = threed_to_oned(values, cuboid_coordinates, steps) threed_reconstruction = make_threed_back(oned, steps, cuboid_coordinates) np.testing.assert_array_equal(values, threed_reconstruction)
def test_k_transform_oned_vs_threed(): """ test if the three dimensional K-transform leads to the same result as the one dimensional K transform for symmetric inner_of_box functions """ def inner_of_box(x, y, z): """ Different shells around the origin. """ rad_pos = (x**2 + y**2 + z**2)**0.5 if rad_pos <= 0.2: return 1 if rad_pos <= 0.5: return 0.8 if rad_pos <= 0.8: return 0.5 return 0 steps = 20 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } number_rays = {'dim_1': 5, 'dim_2': 5} number_ktrans = 10 big_line_matrix_array, ktra_v, touched, values, x_cor, y_cor, z_cor, rays_d =\ generate_everything(number_ktrans, cuboid_coordinates, number_rays, steps, inner_of_box) result_3d = k_transform(values, big_line_matrix_array) fineness = 10**3 radius = 2 source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) values_1d = threed_to_oned(values, cuboid_coordinates, steps) result_1d = k_trafo_one_dim_all(liam_all, values_1d) assert (result_3d == result_1d).all()
def test_line_integral(): steps = 10 cuboid_coordinates = { 'x1': 1, 'x2': 4, 'y1': -3, 'y2': 3, 'z1': -3, 'z2': 3 } def ray(t): return np.array([2.5 * t, 0, 0]) def inner_of_box(x, y, z): return -1 x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) line_value = line_integral(ray, x_cor, y_cor, z_cor, values) np.testing.assert_almost_equal(line_value, -3, decimal=2) steps = 20 cuboid_coordinates = {'x1': 1, 'x2': 2, 'y1': 1, 'y2': 2, 'z1': 1, 'z2': 2} def ray(t): return np.array([1.5 * t, 1.5 * t, 1.5 * t]) def inner_of_box(x, y, z): return 2 x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) line_value = line_integral(ray, x_cor, y_cor, z_cor, values) np.testing.assert_almost_equal(line_value, 2 * 3**0.5, decimal=2)
def test_create_con_results(): """ testing of the cont results of the k_transform are the same as the one for the 1d k_transform, if the object is discrete """ def inner_of_box(x, y, z): if (0.5 > x > -0.5) and (0.5 > y > -0.5) and (0.5 > z > -0.5): return 1 return 0 number_rays = {'dim_1': 1, 'dim_2': 1} cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } steps = 4 radius = 2 fineness = 10**4 number_ktrans = 5 source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) cont_results = create_con_results(rays_d, inner_of_box, fineness) liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) values_1d = threed_to_oned(values, cuboid_coordinates, steps) result_1d = k_trafo_one_dim_all(liam_all, values_1d) print(cont_results) print(result_1d) np.testing.assert_array_almost_equal(cont_results, result_1d, decimal=2)
def test_k_trafo_cont_2(): def inner_of_box(x, y, z): if (0.5 > x > -0.5) and (0.5 > y > -0.5) and (0.5 > z > -0.5): return 1 return 0 number_rays = {'dim_1': 1, 'dim_2': 1} cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } radius = 2 fineness = 10**4 number_ktrans = 1 steps = 4 source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) k_cont = k_trafo_cont(rays_d[0], inner_of_box, fineness) print("k_kont", k_cont) x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) liam = generate_line_integral_array_matrix(rays_d[0], cuboid_coordinates, steps, fineness) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) values_1d = threed_to_oned(values, cuboid_coordinates, steps) result_1d = k_trafo_one_dim(liam, values_1d) print(result_1d) np.testing.assert_almost_equal(k_cont, result_1d, decimal=2)
steps, fineness) np.save("liam_all", liam_all) print("3/8") if True: cont_results = create_con_results(rays_d, inner_of_box, fineness) np.save("cont_results", cont_results) print("4/8") #compare_values = compare_values_original_function(cuboid_coordinates, steps, inner_of_box) print("5/8") x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) print("6/8") values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) print("7/8") x = 0.5 * np.ones(different_lengths) y = 0.5 * np.ones(different_lengths) z = 0.5 * np.ones(different_lengths) reg = dist_count_array * 0.003 gamma = 25 perc_noise = 0 cont_results = np.load("cont_results.npy") liam_all = np.load("liam_all.npy") discrete_model = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps)
def reconstruction(steps, alpha, letter="a"): cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) dist = distance_creater(x_cor, y_cor, z_cor, steps) dist_array = dist_array_maker(dist) different_lengths = len(dist_array) number_ktrans = int(different_lengths * 10) number_rays = {'dim_1': 9, 'dim_2': 9} fineness = 10**3 radii = np.linspace( 2, 30, different_lengths) #[2.1, 2.3, 2.5, 2.7, 2.9, 3.1, 3.3, 3.5] start_index = [ i * int(number_ktrans / len(radii)) for i in range(len(radii)) ] end_index = [ i * int(number_ktrans / len(radii)) for i in range(1, len(radii) + 1) ] source_point_d = {} ################################################################################ for i, r in enumerate(radii): new_source_points = create_source_point_d(r, end_index[i], perc_circle=0.125, start_index=start_index[i]) source_point_d.update(new_source_points) number_ktrans = len(source_point_d) print("1/8") rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) print("2/8") if True: liam_all = generate_all_line_integral_array_matrix( rays_d, cuboid_coordinates, steps, fineness) np.save("liam_all", liam_all) print("3/8") if True: cont_results = create_con_results(rays_d, inner_of_box, fineness) np.save("cont_results", cont_results) print("4/8") #compare_values = compare_values_original_function(cuboid_coordinates, steps, inner_of_box) print("5/8") x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) print("6/8") values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) print("7/8") x = np.ones(different_lengths) y = np.ones(different_lengths) z = np.ones(different_lengths) alpha_not_normed = alpha alpha /= x.size gamma = 1 perc_noise = 0.01 cont_results = np.load("cont_results.npy") liam_all = np.load("liam_all.npy") print("8/8") v1d = threed_to_oned(values, cuboid_coordinates, steps) cont_results_noise = cont_results + perc_noise * np.max( cont_results) * 2 * (np.random.random(number_ktrans) - 0.5) ################################################################################ ####################### Make wD data for comparing############################## ################################################################################ x_comp = np.arange(-1, 1, 0.01) y_comp = np.arange(-1, 1, 0.01) cont = np.zeros((x_comp.size, y_comp.size)) for i in range(x_comp.size): for j in range(y_comp.size): cont[i, j] = inner_of_box(x_comp[i], y_comp[j], 0) ################################################################################ for i in range(5000): x_old, y_old, z_old = x, y, z x = x_step(x, y, z, alpha, gamma) y = y_step(x, z, y, gamma, liam_all, cont_results_noise) z = z_step(x, y, z) tv_reg_value = get_tv(y) f = open( "logbook" + "-" + str(steps) + "-" + letter + "-" + str(alpha_not_normed) + ".txt", "a+") f.write(str(i).ljust(8) + str(tv_reg_value).ljust(20) + str(y) + "\n") print(i, "--------", np.linalg.norm(y - y_old)) if i % 100 == 0: results3d = make_threed_back(x, steps, cuboid_coordinates) fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(7, 3.5)) ax[0].imshow(cont, vmin=0, vmax=1.2) ax[0].axis(False) ax[1].imshow(results3d[int(steps / 2)], vmin=0, vmax=1.2) ax[1].axis(False) plt.savefig(letter + "-" + str(steps) + "-" + str(alpha_not_normed) + "-" + str(i) + ".png") plt.close()
def reconstruction(steps, alpha, perc_noise, obj): cuboid_coordinates = {'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1} x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) dist = distance_creater(x_cor, y_cor, z_cor, steps) dist_array = dist_array_maker(dist) different_lengths = len(dist_array) number_ktrans = int(different_lengths*10) number_rays = {'dim_1': 15, 'dim_2': 15} fineness = 10**3 radii = np.linspace(2,30, different_lengths) #[2.1, 2.3, 2.5, 2.7, 2.9, 3.1, 3.3, 3.5] start_index = [i * int(number_ktrans / len(radii)) for i in range(len(radii))] end_index = [i * int(number_ktrans / len(radii)) for i in range(1, len(radii) + 1)] source_point_d = {} ################################################################################ for i, r in enumerate(radii): new_source_points = create_source_point_d(r, end_index[i], perc_circle=0.125, start_index=start_index[i]) source_point_d.update(new_source_points) number_ktrans = len(source_point_d) print("1/8") rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) print("2/8") if True: liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) np.save("liam_all", liam_all) print("3/8") if True: if obj == 1: cont_results = create_con_results(rays_d, inner_of_box_1, fineness) if obj == 2: cont_results = create_con_results(rays_d, inner_of_box_2, fineness) if obj == 3: cont_results = create_con_results(rays_d, inner_of_box_3, fineness) np.save("cont_results", cont_results) print("4/8") #compare_values = compare_values_original_function(cuboid_coordinates, steps, inner_of_box) print("5/8") x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) print("6/8") if obj == 1: values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box_1, steps) if obj == 2: values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box_2, steps) if obj == 3: values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box_3, steps) print("7/8") x = np.ones(different_lengths) y = np.ones(different_lengths) z = np.ones(different_lengths) alpha_not_normed = alpha alpha /= x.size gamma = 1 #perc_noise = 0.01 cont_results = np.load("cont_results.npy") liam_all = np.load("liam_all.npy") print("8/8") v1d = threed_to_oned(values, cuboid_coordinates, steps) cont_results_noise = cont_results + perc_noise * np.max(cont_results) * 2 * (np.random.random(number_ktrans) - 0.5) ################################################################################ ####################### Make wD data for comparing############################## ################################################################################ x_comp = np.arange(-1, 1, 0.01) y_comp = np.arange(-1, 1, 0.01) cont = np.zeros((x_comp.size, y_comp.size)) for i in range(x_comp.size): for j in range(y_comp.size): if obj == 1: cont[i,j] = inner_of_box_1(x_comp[i], y_comp[j], 0) if obj == 2: cont[i,j] = inner_of_box_2(x_comp[i], y_comp[j], 0) if obj == 3: cont[i,j] = inner_of_box_3(x_comp[i], y_comp[j], 0) ################################################################################ f = open("logbook" + "-" + str(steps) + "-" + str(perc_noise) + "-" + str(alpha_not_normed) + ".txt", "a+") f.write("discretization" + " , " + "perc_noise" + " , " + "alpha_not_normed" + ", " + "step"+ " , " + "tv_reg_value" + " , " + "y" + " , " + "x" + "\n" ) for i in range(5001): x_old, y_old, z_old = x, y, z x = x_step(x, y, z, alpha, gamma) y = y_step(x, z, y, gamma, liam_all, cont_results_noise) z = z_step(x, y, z) tv_reg_value = get_tv(y) f.write(str(steps) + " , " + str(perc_noise) + " , " + str(alpha_not_normed) + ", " + str(i)+ " , " + str(tv_reg_value) + " , " + str(y) + " , " + str(x) + "\n" ) print(i, "--------", np.linalg.norm(y - y_old))
def run_example(steps, lamb): """ runs a full reconstruction""" cuboid_coordinates = {'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1} x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) dist = distance_creater(x_cor, y_cor, z_cor, steps) dist_array = dist_array_maker(dist) different_lengths = len(dist_array) number_ktrans = different_lengths * 2 number_rays = {'dim_1': 7, 'dim_2': 7} fineness = 10**3 radius = 3 ################################################################################ source_point_d = create_source_point_d(radius, number_ktrans, perc_circle=0.125) print("1/8") rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) print("2/8") liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) np.save("liam_all", liam_all) #liam_all = np.load("liam_all.npy") print("3/8") cont_results = create_con_results(rays_d, inner_of_box, fineness) np.save("cont_results", cont_results) # cont_results = np.load("cont_results.npy") print("4/8") compare_values = compare_values_original_function(cuboid_coordinates, steps, inner_of_box) print("5/8") x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) print("6/8") values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) print("7/8") # discrete_model = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) # one_d_model = threed_to_oned(discrete_model, cuboid_coordinates, steps) # dis_results = k_trafo_one_dim_all(liam_all, one_d_model) #cont_results = dis_results #np.save("cont_results", cont_results) perc_noise = 0.0 # rho = 0.001 rho = 2*lamb cont_results_noise = (np.load("cont_results.npy") + 2 * perc_noise * (np.random.random(number_ktrans) - 0.5)) v1d = threed_to_oned(values, cuboid_coordinates, steps) ################################################################################ u = np.zeros(different_lengths) letter = "z" if lamb == 0.01: letter = "a" if lamb == 0.05: letter = "b" if lamb == 0.1: letter = "c" if lamb == 0.2: letter = "d" if lamb == 0.3: letter = "e" if lamb == 0.5: letter = "f" if lamb == 1.0: letter = "g" for i in range(1001): print(str(i) + "/ 1000") ################################################################################ ######################### U STEP ############################################# ################################################################################ if i > 0: x = tv_denoising_algorithm(v.x, lamb ) ################################################################################ ######################### V STEP ############################################# ################################################################################ v = scipy.optimize.minimize( v_step_function, u, args = (liam_all, cont_results_noise, steps, cuboid_coordinates, rho, u), method = 'L-BFGS-B', bounds = scipy.optimize.Bounds(0,1), options = {'disp': False, 'maxcor': 10, 'ftol': 10**-20, 'gtol': 10**-20, 'eps': 10**-8, # if to high stops fast 'maxiter': 10**7, 'maxls': 100, 'maxfun': 10**7}) old_solution = v.x tv_reg_value = get_tv(v.x) f = open("logbook" + "-" + str(steps) + "-" + letter + "-" + str(lamb) + ".txt", "a+") f.write(str(i).ljust(8) + str(tv_reg_value).ljust(20) + str(v.x) + "\n" ) if i % 50 == 0: plt.plot(v1d, "-o") plt.plot(v.x, "-o") plt.savefig(str(steps) + "-" + letter + "-" + str(lamb) + "-" + str(i) + ".png") plt.close()
def generate_everything(number_ktrans, cuboid_coordinates, number_rays, steps, inner_of_box): """ Generates the source points and k-tra values by simply moving the source around them. we firmly assume here that the k-transforms lie at the same distance in a circle (with radius 2) around the box """ source_point_d = {} pv_d = {} nv_d = {} vx_d = {} vy_d = {} factor_x_d = {} factor_y_d = {} factor_x_array_d = {} factor_y_array_d = {} points_d = {} rays_d = {} ktra_v = np.zeros(number_ktrans) middle = middle_of_cuboid(cuboid_coordinates) diag = length_main_diagonal(cuboid_coordinates) for i in np.arange(number_ktrans): source_point = 2 * np.array([ np.sin(2 * np.pi * i / number_ktrans), np.cos(2 * np.pi * i / number_ktrans), 0 ]) source_point_d[i] = source_point pv, nv = shadow_plane_maker(source_point, cuboid_coordinates) pv_d[i] = pv nv_d[i] = nv vx, vy = get_parameter_plane_vectors(nv) vx_d[i] = vx vy_d[i] = vy factor_x = length_vector_for_diag(vx, diag) factor_y = length_vector_for_diag(vy, diag) factor_x_d[i] = factor_x factor_y_d[i] = factor_y factor_array_x = divide_the_factor(factor_x, number_rays['dim_1']) factor_array_y = divide_the_factor(factor_y, number_rays['dim_2']) factor_x_array_d[i] = factor_array_x factor_y_array_d[i] = factor_array_y points = create_ray_points(factor_array_x, factor_array_y, vx, vy, middle) points_d[i] = points rays = create_rays(source_point, points) rays_d[i] = rays x_cor, y_cor, z_cor = coordinate_discretization( cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) line_matrices_d = lineintegral_matrices_maker(rays_d, steps, x_cor, y_cor, z_cor) big_line_matrix_array = np.array([ np.array(list(line_matrices_d[i].values())) for i in range(number_ktrans) ]) ktra_v = k_transform(values, big_line_matrix_array) touched = important_mini_cuboids(x_cor, y_cor, z_cor, steps, rays_d, values) return big_line_matrix_array, ktra_v, touched, values, x_cor, y_cor, z_cor, rays_d