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_threed_to_oned_3(): array_oned = np.arange(85) steps = 18 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } threed_reconstruction = make_threed_back(array_oned, steps, cuboid_coordinates) oned_back = threed_to_oned(threed_reconstruction, cuboid_coordinates, steps) np.testing.assert_array_equal(array_oned, oned_back)
def test_threed_to_oned_2(): """here it is tested if make_threed_back is exactly the opposite to threed_to_oned""" array_oned = np.arange(10) steps = 5 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } threed_reconstruction = make_threed_back(array_oned, steps, cuboid_coordinates) oned_back = threed_to_oned(threed_reconstruction, cuboid_coordinates, steps) np.testing.assert_array_equal(array_oned, oned_back)
def test_make_threed_back(): array_oned = np.array([10, 5, 1, 0]) steps = 3 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } threed_reconstruction = make_threed_back(array_oned, steps, cuboid_coordinates) threed_compare = np.array([[[0, 1, 0], [1, 5, 1], [0, 1, 0]], [[1, 5, 1], [5, 10, 5], [1, 5, 1]], [[0, 1, 0], [1, 5, 1], [0, 1, 0]]]) np.testing.assert_array_equal(threed_reconstruction, threed_compare)
cont[i, j] = inner_of_box(x[i], y[j]) res = ([ 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.38705688, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.26610687, 0.2579235, 0.209664, 0.19521278, 0.11621265, 0.11621265, 0.04358677, 0.04358677, 0.04358677, 0.04358677, 0.01065764, 0.01065764, 0.01065764, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.00957863, 0.04000303, 0.04000303, 0.04000303, 0.04000303, 0.01263719, 0.01134836, 0.01134836, 0.01134836, 0.01134836, 0.01134836, 0.01134836, 0.01134836, 0.01134836 ]) steps = 16 cuboid_coordinates = {'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1} results3d = make_threed_back(res, steps, cuboid_coordinates) #border_results = np.zeros((26, 26)) #border_results[3:-3,3:-3] = results3d[9] 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[9], vmin=0, vmax=1.2) ax[1].axis(False) plt.show()
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 generate_result_plot(obj): if obj == 1: path = "/home/cpf/Desktop/ktransform_simulation_results/files/calulations_missing/20-0.2-0.01-1/logbook-20-0.01-0.2.txt" def inner_of_box(x, y, z): rad_pos = (x**2 + y**2 + z**2)**0.5 if rad_pos <= 0.8: return 0.8 return 0 if obj == 2: path = "/home/cpf/Desktop/ktransform_simulation_results/files/calulations_missing/20-0.04-0.01-3/logbook-20-0.01-0.04.txt" def inner_of_box(x, y, z): rad_pos = (x**2 + y**2 + z**2)**0.5 if rad_pos <= 0.4: return 0.9 if rad_pos <= 0.6: return 0.7 if rad_pos <= 0.8: return 0.4 return 0 if obj == 3: path = "/home/cpf/Desktop/ktransform_simulation_results/files/calulations_missing/20-0.04-0.02-2/logbook-20-0.02-0.04.txt" def inner_of_box(x, y, z): rad_pos = (x**2 + y**2 + z**2)**0.5 if rad_pos <= 0.7: return 0 if rad_pos <= 0.8: return 0.8 return 0 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) cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } steps = 20 logbook = pd.read_csv(path) reconstruction_string = logbook.iloc[5000][5] reconstruction_string_cut = reconstruction_string[2:-2] reconstruction = np.fromstring(reconstruction_string_cut, sep=" ") results3d = make_threed_back(reconstruction, steps, cuboid_coordinates) fig, ax1 = plt.subplots(figsize=(2.0, 2.0)) left, bottom, width, height = [0.594, 0.577, 0.3, 0.3] ax2 = fig.add_axes([left, bottom, width, height]) ax1.imshow(results3d[10], vmin=0, vmax=1) ax1.axis(False) ax2.imshow(cont, vmin=0, vmax=1) ax2.axis(False) autoAxis = ax2.axis() rec = mpatches.Rectangle((autoAxis[0] - 0.7, autoAxis[2] - 0.2), (autoAxis[1] - autoAxis[0]) + 1, (autoAxis[3] - autoAxis[2]) + 0.4, fill=False, lw=1) rec = ax2.add_patch(rec) rec.set_clip_on(False) plt.savefig(figures_path + 'plot-' + str(obj) + '.pgf', bbox_inches='tight')
rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) t1 = time.time() for i in range(200): line_integral_cont(rays_d[0][0], inner_of_box, fineness=10**4) t2 = time.time() liam_all = generate_all_line_integral_array_matrix( rays_d, cuboid_coordinates, steps, fineness) 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) threedma = make_threed_back(liam_all[0][0], steps, cuboid_coordinates) t3 = time.time() for i in range(200): np.sum(threedma * discrete_model) t4 = time.time() list_1 += [(t2 - t1) / 200.] list_2 += [(t4 - t3) / 200.] if (steps % 5 == 0): np.save("for_loop_times.npy", np.asarray(list_1)) np.save("matric_times.npy", np.asarray(list_2)) for_loop_times = np.load("for_loop_times.npy") matrix_times = np.load("matric_times.npy")