Beispiel #1
0
def test_one_dimensional_line_integral_maker():

    cuboid_coordinates = {
        'x1': -1.5,
        'x2': 1.5,
        'y1': -1.5,
        'y2': 1.5,
        'z1': -1.5,
        'z2': 1.5
    }
    steps = 3
    points_cuboid = np.array([[[1, 6, 5], [4, 13, 19], [2.5, 7.3, 2.1]],
                              [[1.5, 60, 51], [32, 16, 19], [1.1, 1.3, 1.6]],
                              [[21, 0.9, 2.9], [1.6, 11.1, 0.2], [2.8, 4.4,
                                                                  7]]])

    x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps)
    dist = distance_creater(x_cor, y_cor, z_cor, steps)
    dist_fru = dist_array_maker(dist)

    integral_array = one_dimensional_line_integral_maker(
        dist, dist_fru, points_cuboid)
    integral_array_c = np.array([16, 136.4, 98.6, 44.3])

    np.testing.assert_array_almost_equal(integral_array, integral_array_c)
def td_to_od_nonsymmetric(td, steps, cuboid_coordinates):
    """sums up all the values with the same distance"""
    x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps)
    dist = distance_creater(x_cor, y_cor, z_cor, steps)
    dist_fru = dist_array_maker(dist)

    integral_array = np.zeros(dist_fru.size)
    #   need to round since we did it in the dist_array_maker
    dist_r = np.round(dist, 6)
    for i, j in enumerate(dist_fru):
        d = (dist_r == j) * td
        integral_array[i] = np.sum(d)
    return integral_array
def compare_values_original_function(cuboid_coordinates, steps, inner_of_box):
    """
    creates an array of the original value of the inner_of_box function for
    comparisson
    """

    x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps)
    dist = distance_creater(x_cor, y_cor, z_cor, steps)
    dist_fru = dist_array_maker(dist)
    compare_values = np.zeros(dist_fru.size)

    for i in range(len(compare_values)):
        compare_values[i] = inner_of_box(dist_fru[i], 0, 0)

    return compare_values
def threed_to_oned(values, cuboid_coordinates, steps):
    """takes symmetric 3d value array and creates 1d array from it
    this function was completly reworked in commit 53, 20.04.20
    """

    x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps)
    dist = distance_creater(x_cor, y_cor, z_cor, steps)
    dist_fru = dist_array_maker(dist)

    values_oned = 0 * dist_fru

    for j, i in enumerate(dist_fru):
        x, y, z = np.where(np.round(dist, 6) == i)
        values_oned[j] = values[x[0], y[0], z[0]]

    return values_oned
def make_threed_back(array_oned, steps, cuboid_coordinates):
    """
    create the 3D matrix again from the 1D array.
    """

    x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps)

    dist = np.round(distance_creater(x_cor, y_cor, z_cor, steps), 6)
    dist_fru = dist_array_maker(dist)

    model_threed = np.zeros((steps, steps, steps))

    for i in range(len(dist_fru)):
        model_threed += (dist == dist_fru[i]) * array_oned[i]

    return model_threed
Beispiel #6
0
def test_dist_array_maker():

    cuboid_coordinates = {
        'x1': -1.5,
        'x2': 1.5,
        'y1': -1.5,
        'y2': 1.5,
        'z1': -1.5,
        'z2': 1.5
    }
    steps = 3
    x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps)

    dist = distance_creater(x_cor, y_cor, z_cor, steps)
    dist_fru = dist_array_maker(dist)

    dist_fru_c = np.array([0, 1, np.sqrt(2), np.sqrt(3)])

    np.testing.assert_array_almost_equal(dist_fru, dist_fru_c)
Beispiel #7
0
def test_dist_array_maker_2():

    cuboid_coordinates2 = {
        'x1': -2,
        'x2': 2,
        'y1': -2,
        'y2': 2,
        'z1': -2,
        'z2': 2
    }
    steps2 = 4
    x_cor2, y_cor2, z_cor2 = coordinate_discretization(cuboid_coordinates2,
                                                       steps2)
    dist2 = distance_creater(x_cor2, y_cor2, z_cor2, steps2)
    dist_fru2 = dist_array_maker(dist2)

    dist_fru_c2 = np.array(
        [np.sqrt(0.75),
         np.sqrt(2.75),
         np.sqrt(4.75),
         np.sqrt(6.75)])

    np.testing.assert_array_almost_equal(dist_fru2, dist_fru_c2)
Beispiel #8
0

def z_step(x, y, z):

    z = z + y - x

    return z


steps = 5

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)
dist_count_array = count_edges_for_reg(dist, dist_array)

different_lengths = len(dist_array)

number_ktrans = int(different_lengths * 10)
number_rays = {'dim_1': 5, 'dim_2': 5}
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)
Beispiel #9
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))
Beispiel #11
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()