Esempio n. 1
0
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
Esempio n. 2
0
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)
Esempio n. 3
0
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 = 0.5 * np.ones(different_lengths)
y = 0.5 * np.ones(different_lengths)
z = 0.5 * np.ones(different_lengths)
Esempio n. 4
0
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))
Esempio n. 6
0
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()