Exemple #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
Exemple #2
0
def test_generate_all_line_integral_array_matrix():

    cuboid_coordinates = {
        'x1': -1,
        'x2': 1,
        'y1': -1,
        'y2': 1,
        'z1': -1,
        'z2': 1
    }
    steps = 4
    fineness = 10**4
    number_rays = {'dim_1': 1, 'dim_2': 1}
    number_ktrans = 2
    radius = 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_all = generate_all_line_integral_array_matrix(rays_d,
                                                       cuboid_coordinates,
                                                       steps, fineness)

    liam_all_controll = np.array([[[1, 1, 0, 0]], [[1, 1, 0, 0]]])

    np.testing.assert_array_almost_equal(liam_all,
                                         liam_all_controll,
                                         decimal=2)
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
def test_line_int_cont_2():
    """ now we let also create the ray """
    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

    source_point_d = create_source_point_d(radius, number_ktrans)
    rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays)
    integral = line_integral_cont(rays_d[0][0], inner_of_box, fineness)

    np.testing.assert_almost_equal(integral, 1, decimal=3)
Exemple #6
0
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()
Exemple #7
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)
Exemple #8
0
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)
Exemple #9
0
import sympy
import numpy as np

from ktra.k_transform import create_source_point_d
from ktra.k_transform import generate_rays_d
from ktra.k_transform import generate_all_line_integral_array_matrix
from ktra.k_transform import coordinate_discretization
from ktra.k_transform import discretization_of_model
from ktra.k_transform import k_trafo_one_dim_all

from ktra.tv_denoising_condat import tv_denoising_algorithm

from ktra.optimization_process import create_con_results
from ktra.optimization_process import compare_values_original_function
from ktra.optimization_process import important_mini_cuboids
from ktra.optimization_process import threed_to_oned
from ktra.optimization_process import v_step_function

steps = 4
number_rays = {'dim_1': 4, 'dim_2': 4}
number_ktrans = 10

cuboid_coordinates = {'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1}
fineness = 10**3
radius = 3  # from which the source points are took

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)
Exemple #10
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))
Exemple #12
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()