示例#1
0
def test_uniform_partition_fromgrid():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    begin = [0, -4]
    end = [7, 8]
    beg_calc = [2 - (4 - 2) / 2, -4 - (-3 + 4) / 2]
    end_calc = [7 + (7 - 5) / 2, 4 + (4 - 1) / 2]

    # Default case
    grid = odl.TensorGrid(vec1, vec2)
    part = odl.uniform_partition_fromgrid(grid)
    assert part.set == odl.IntervalProd(beg_calc, end_calc)

    # Explicit begin / end, full vectors
    part = odl.uniform_partition_fromgrid(grid, begin=begin)
    assert part.set == odl.IntervalProd(begin, end_calc)
    part = odl.uniform_partition_fromgrid(grid, end=end)
    assert part.set == odl.IntervalProd(beg_calc, end)

    # begin / end as dictionaries
    beg_dict = {0: 0.5}
    end_dict = {-1: 8}
    part = odl.uniform_partition_fromgrid(grid, begin=beg_dict, end=end_dict)
    true_beg = [0.5, beg_calc[1]]
    true_end = [end_calc[0], 8]
    assert part.set == odl.IntervalProd(true_beg, true_end)

    # Degenerate dimension, needs both explicit begin and end
    grid = odl.TensorGrid(vec1, [1.0])
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, begin=begin)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, end=end)
示例#2
0
def test_uniform_partition_fromgrid():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    begin = [0, -4]
    end = [7, 8]
    beg_calc = [2 - (4 - 2) / 2, -4 - (-3 + 4) / 2]
    end_calc = [7 + (7 - 5) / 2, 4 + (4 - 1) / 2]

    # Default case
    grid = odl.TensorGrid(vec1, vec2)
    part = odl.uniform_partition_fromgrid(grid)
    assert part.set == odl.IntervalProd(beg_calc, end_calc)

    # Explicit begin / end, full vectors
    part = odl.uniform_partition_fromgrid(grid, begin=begin)
    assert part.set == odl.IntervalProd(begin, end_calc)
    part = odl.uniform_partition_fromgrid(grid, end=end)
    assert part.set == odl.IntervalProd(beg_calc, end)

    # begin / end as dictionaries
    beg_dict = {0: 0.5}
    end_dict = {-1: 8}
    part = odl.uniform_partition_fromgrid(grid, begin=beg_dict, end=end_dict)
    true_beg = [0.5, beg_calc[1]]
    true_end = [end_calc[0], 8]
    assert part.set == odl.IntervalProd(true_beg, true_end)

    # Degenerate dimension, needs both explicit begin and end
    grid = odl.TensorGrid(vec1, [1.0])
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, begin=begin)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, end=end)
示例#3
0
def __touniformpartition(arr):
    if isinstance(arr, odl.RectPartition):
        return arr
    elif hasattr(arr, 'grid'):
        arr = arr.grid
    elif isscalar(arr[0]):
        arr = odl.RectGrid(arr)
    else:
        arr = odl.RectGrid(*arr)

    return odl.uniform_partition_fromgrid(arr)
示例#4
0
def test_uniform_partition_fromgrid():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    min_pt = [0, -4]
    max_pt = [7, 8]
    min_pt_calc = [2 - (4 - 2) / 2, -4 - (-3 + 4) / 2]
    max_pt_calc = [7 + (7 - 5) / 2, 4 + (4 - 1) / 2]

    # Default case
    grid = odl.RectGrid(vec1, vec2)
    part = odl.uniform_partition_fromgrid(grid)
    assert part.set == odl.IntervalProd(min_pt_calc, max_pt_calc)

    # Explicit min_pt / max_pt, full vectors
    part = odl.uniform_partition_fromgrid(grid, min_pt=min_pt)
    assert part.set == odl.IntervalProd(min_pt, max_pt_calc)
    part = odl.uniform_partition_fromgrid(grid, max_pt=max_pt)
    assert part.set == odl.IntervalProd(min_pt_calc, max_pt)

    # min_pt / max_pt as dictionaries
    min_pt_dict = {0: 0.5}
    max_pt_dict = {-1: 8}
    part = odl.uniform_partition_fromgrid(grid,
                                          min_pt=min_pt_dict,
                                          max_pt=max_pt_dict)
    true_min_pt = [0.5, min_pt_calc[1]]
    true_max_pt = [max_pt_calc[0], 8]
    assert part.set == odl.IntervalProd(true_min_pt, true_max_pt)

    # Degenerate dimension, needs both explicit min_pt and max_pt
    grid = odl.RectGrid(vec1, [1.0])
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, min_pt=min_pt)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, max_pt=max_pt)
示例#5
0
def test_uniform_partition_fromgrid():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    min_pt = [0, -4]
    max_pt = [7, 8]
    min_pt_calc = [2 - (4 - 2) / 2, -4 - (-3 + 4) / 2]
    max_pt_calc = [7 + (7 - 5) / 2, 4 + (4 - 1) / 2]

    # Default case
    grid = odl.RectGrid(vec1, vec2)
    part = odl.uniform_partition_fromgrid(grid)
    assert part.set == odl.IntervalProd(min_pt_calc, max_pt_calc)

    # Explicit min_pt / max_pt, full vectors
    part = odl.uniform_partition_fromgrid(grid, min_pt=min_pt)
    assert part.set == odl.IntervalProd(min_pt, max_pt_calc)
    part = odl.uniform_partition_fromgrid(grid, max_pt=max_pt)
    assert part.set == odl.IntervalProd(min_pt_calc, max_pt)

    # min_pt / max_pt as dictionaries
    min_pt_dict = {0: 0.5}
    max_pt_dict = {-1: 8}
    part = odl.uniform_partition_fromgrid(
        grid, min_pt=min_pt_dict, max_pt=max_pt_dict)
    true_min_pt = [0.5, min_pt_calc[1]]
    true_max_pt = [max_pt_calc[0], 8]
    assert part.set == odl.IntervalProd(true_min_pt, true_max_pt)

    # Degenerate dimension, needs both explicit min_pt and max_pt
    grid = odl.RectGrid(vec1, [1.0])
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, min_pt=min_pt)
    with pytest.raises(ValueError):
        odl.uniform_partition_fromgrid(grid, max_pt=max_pt)
示例#6
0
# data.
shift = np.array([0.0, 25.0, 0.0])

vol_shape = (100, 150, 200)
vol_max_pt = np.array(vol_shape, dtype=float) / 2
vol_min_pt = -vol_max_pt
reco_space = odl.uniform_discr(vol_min_pt + shift,
                               vol_max_pt + shift,
                               vol_shape,
                               dtype='float32')
phantom = odl.phantom.indicate_proj_axis(reco_space)

assert np.allclose(reco_space.cell_sides, 1)

grid = odl.RectGrid(np.linspace(0, 2 * np.pi, 360, endpoint=False))
angle_partition = odl.uniform_partition_fromgrid(grid)

# Make detector large enough to cover the object
det_size = np.floor(1.1 * np.sqrt(np.sum(np.square(vol_shape))))
det_shape = (int(det_size), int(det_size))
det_max_pt = np.array([det_size / 2, det_size / 2])
det_min_pt = -det_max_pt
detector_partition = odl.uniform_partition(det_min_pt, det_max_pt, det_shape)

assert np.allclose(detector_partition.cell_sides, 1)

# %% Test case 1: axis = [0, 0, 1]

geometry = odl.tomo.Parallel3dAxisGeometry(angle_partition,
                                           detector_partition,
                                           axis=[0, 0, 1])
示例#7
0
def test_fourier_trafo_completely():
    # Complete explicit test of all FT components on two small examples

    # Discretization with 4 points
    discr = odl.uniform_discr(-2, 2, 4, dtype="complex")
    # Interval boundaries -2, -1, 0, 1, 2
    assert np.allclose(discr.partition.cell_boundary_vecs[0], [-2, -1, 0, 1, 2])
    # Grid points -1.5, -0.5, 0.5, 1.5
    assert np.allclose(discr.grid.coord_vectors[0], [-1.5, -0.5, 0.5, 1.5])

    # First test function, symmetric. Can be represented exactly in the
    # discretization.
    def f(x):
        return (x >= -1) & (x <= 1)

    def fhat(x):
        return np.sqrt(2 / np.pi) * sinc(x)

    # Discretize f, check values
    f_discr = discr.element(f)
    assert np.allclose(f_discr, [0, 1, 1, 0])

    # "s" = shifted, "n" = not shifted

    # Reciprocal grids
    recip_s = reciprocal_grid(discr.grid, shift=True)
    recip_n = reciprocal_grid(discr.grid, shift=False)
    assert np.allclose(recip_s.coord_vectors[0], np.linspace(-np.pi, np.pi / 2, 4))
    assert np.allclose(recip_n.coord_vectors[0], np.linspace(-3 * np.pi / 4, 3 * np.pi / 4, 4))

    # Range
    range_part_s = odl.uniform_partition_fromgrid(recip_s)
    range_s = odl.uniform_discr_frompartition(range_part_s, dtype="complex")
    range_part_n = odl.uniform_partition_fromgrid(recip_n)
    range_n = odl.uniform_discr_frompartition(range_part_n, dtype="complex")

    # Pre-processing
    preproc_s = [1, -1, 1, -1]
    preproc_n = [np.exp(1j * 3 / 4 * np.pi * k) for k in range(4)]

    fpre_s = dft_preprocess_data(f_discr, shift=True)
    fpre_n = dft_preprocess_data(f_discr, shift=False)
    assert np.allclose(fpre_s, f_discr * discr.element(preproc_s))
    assert np.allclose(fpre_n, f_discr * discr.element(preproc_n))

    # FFT step, replicating the _call_numpy method
    fft_s = np.fft.fftn(fpre_s, s=discr.shape, axes=[0])
    fft_n = np.fft.fftn(fpre_n, s=discr.shape, axes=[0])
    assert np.allclose(fft_s, [0, -1 + 1j, 2, -1 - 1j])
    assert np.allclose(
        fft_n, [np.exp(1j * np.pi * (3 - 2 * k) / 4) + np.exp(1j * np.pi * (3 - 2 * k) / 2) for k in range(4)]
    )

    # Interpolation kernel FT
    interp_s = np.sinc(np.linspace(-1 / 2, 1 / 4, 4)) / np.sqrt(2 * np.pi)
    interp_n = np.sinc(np.linspace(-3 / 8, 3 / 8, 4)) / np.sqrt(2 * np.pi)
    assert np.allclose(interp_s, _interp_kernel_ft(np.linspace(-1 / 2, 1 / 4, 4), interp="nearest"))
    assert np.allclose(interp_n, _interp_kernel_ft(np.linspace(-3 / 8, 3 / 8, 4), interp="nearest"))

    # Post-processing
    postproc_s = np.exp(1j * np.pi * np.linspace(-3 / 2, 3 / 4, 4))
    postproc_n = np.exp(1j * np.pi * np.linspace(-9 / 8, 9 / 8, 4))

    fpost_s = dft_postprocess_data(
        range_s.element(fft_s), real_grid=discr.grid, recip_grid=recip_s, shift=[True], axes=(0,), interp="nearest"
    )
    fpost_n = dft_postprocess_data(
        range_n.element(fft_n), real_grid=discr.grid, recip_grid=recip_n, shift=[False], axes=(0,), interp="nearest"
    )

    assert np.allclose(fpost_s, fft_s * postproc_s * interp_s)
    assert np.allclose(fpost_n, fft_n * postproc_n * interp_n)

    # Comparing to the known result sqrt(2/pi) * sinc(x)
    assert np.allclose(fpost_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(fpost_n, fhat(recip_n.coord_vectors[0]))

    # Doing the exact same with direct application of the FT operator
    ft_op_s = FourierTransform(discr, shift=True)
    ft_op_n = FourierTransform(discr, shift=False)
    assert ft_op_s.range.grid == recip_s
    assert ft_op_n.range.grid == recip_n

    ft_f_s = ft_op_s(f)
    ft_f_n = ft_op_n(f)
    assert np.allclose(ft_f_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(ft_f_n, fhat(recip_n.coord_vectors[0]))

    # Second test function, asymmetric. Can also be represented exactly in the
    # discretization.
    def f(x):
        return (x >= 0) & (x <= 1)

    def fhat(x):
        return np.exp(-1j * x / 2) * sinc(x / 2) / np.sqrt(2 * np.pi)

    # Discretize f, check values
    f_discr = discr.element(f)
    assert np.allclose(f_discr, [0, 0, 1, 0])

    # Pre-processing
    fpre_s = dft_preprocess_data(f_discr, shift=True)
    fpre_n = dft_preprocess_data(f_discr, shift=False)
    assert np.allclose(fpre_s, [0, 0, 1, 0])
    assert np.allclose(fpre_n, [0, 0, -1j, 0])

    # FFT step
    fft_s = np.fft.fftn(fpre_s, s=discr.shape, axes=[0])
    fft_n = np.fft.fftn(fpre_n, s=discr.shape, axes=[0])
    assert np.allclose(fft_s, [1, -1, 1, -1])
    assert np.allclose(fft_n, [-1j, 1j, -1j, 1j])

    fpost_s = dft_postprocess_data(
        range_s.element(fft_s), real_grid=discr.grid, recip_grid=recip_s, shift=[True], axes=(0,), interp="nearest"
    )
    fpost_n = dft_postprocess_data(
        range_n.element(fft_n), real_grid=discr.grid, recip_grid=recip_n, shift=[False], axes=(0,), interp="nearest"
    )

    assert np.allclose(fpost_s, fft_s * postproc_s * interp_s)
    assert np.allclose(fpost_n, fft_n * postproc_n * interp_n)

    # Comparing to the known result exp(-1j*x/2) * sinc(x/2) / sqrt(2*pi)
    assert np.allclose(fpost_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(fpost_n, fhat(recip_n.coord_vectors[0]))

    # Doing the exact same with direct application of the FT operator
    ft_f_s = ft_op_s(f)
    ft_f_n = ft_op_n(f)
    assert np.allclose(ft_f_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(ft_f_n, fhat(recip_n.coord_vectors[0]))
示例#8
0
def test_fourier_trafo_completely():
    # Complete explicit test of all FT components on two small examples

    # Discretization with 4 points
    discr = odl.uniform_discr(-2, 2, 4, dtype='complex')
    # Interval boundaries -2, -1, 0, 1, 2
    assert np.allclose(discr.partition.cell_boundary_vecs[0],
                       [-2, -1, 0, 1, 2])
    # Grid points -1.5, -0.5, 0.5, 1.5
    assert np.allclose(discr.grid.coord_vectors[0], [-1.5, -0.5, 0.5, 1.5])

    # First test function, symmetric. Can be represented exactly in the
    # discretization.
    def f(x):
        return (x >= -1) & (x <= 1)

    def fhat(x):
        return np.sqrt(2 / np.pi) * sinc(x)

    # Discretize f, check values
    f_discr = discr.element(f)
    assert np.allclose(f_discr, [0, 1, 1, 0])

    # "s" = shifted, "n" = not shifted

    # Reciprocal grids
    recip_s = reciprocal_grid(discr.grid, shift=True)
    recip_n = reciprocal_grid(discr.grid, shift=False)
    assert np.allclose(recip_s.coord_vectors[0],
                       np.linspace(-np.pi, np.pi / 2, 4))
    assert np.allclose(recip_n.coord_vectors[0],
                       np.linspace(-3 * np.pi / 4, 3 * np.pi / 4, 4))

    # Range
    range_part_s = odl.uniform_partition_fromgrid(recip_s)
    range_s = odl.uniform_discr_frompartition(range_part_s, dtype='complex')
    range_part_n = odl.uniform_partition_fromgrid(recip_n)
    range_n = odl.uniform_discr_frompartition(range_part_n, dtype='complex')

    # Pre-processing
    preproc_s = [1, -1, 1, -1]
    preproc_n = [np.exp(1j * 3 / 4 * np.pi * k) for k in range(4)]

    fpre_s = dft_preprocess_data(f_discr, shift=True)
    fpre_n = dft_preprocess_data(f_discr, shift=False)
    assert np.allclose(fpre_s, f_discr * discr.element(preproc_s))
    assert np.allclose(fpre_n, f_discr * discr.element(preproc_n))

    # FFT step, replicating the _call_numpy method
    fft_s = np.fft.fftn(fpre_s, s=discr.shape, axes=[0])
    fft_n = np.fft.fftn(fpre_n, s=discr.shape, axes=[0])
    assert np.allclose(fft_s, [0, -1 + 1j, 2, -1 - 1j])
    assert np.allclose(fft_n, [
        np.exp(1j * np.pi * (3 - 2 * k) / 4) + np.exp(1j * np.pi *
                                                      (3 - 2 * k) / 2)
        for k in range(4)
    ])

    # Interpolation kernel FT
    interp_s = np.sinc(np.linspace(-1 / 2, 1 / 4, 4)) / np.sqrt(2 * np.pi)
    interp_n = np.sinc(np.linspace(-3 / 8, 3 / 8, 4)) / np.sqrt(2 * np.pi)
    assert np.allclose(
        interp_s,
        _interp_kernel_ft(np.linspace(-1 / 2, 1 / 4, 4), interp='nearest'))
    assert np.allclose(
        interp_n,
        _interp_kernel_ft(np.linspace(-3 / 8, 3 / 8, 4), interp='nearest'))

    # Post-processing
    postproc_s = np.exp(1j * np.pi * np.linspace(-3 / 2, 3 / 4, 4))
    postproc_n = np.exp(1j * np.pi * np.linspace(-9 / 8, 9 / 8, 4))

    fpost_s = dft_postprocess_data(range_s.element(fft_s),
                                   real_grid=discr.grid,
                                   recip_grid=recip_s,
                                   shift=[True],
                                   axes=(0, ),
                                   interp='nearest')
    fpost_n = dft_postprocess_data(range_n.element(fft_n),
                                   real_grid=discr.grid,
                                   recip_grid=recip_n,
                                   shift=[False],
                                   axes=(0, ),
                                   interp='nearest')

    assert np.allclose(fpost_s, fft_s * postproc_s * interp_s)
    assert np.allclose(fpost_n, fft_n * postproc_n * interp_n)

    # Comparing to the known result sqrt(2/pi) * sinc(x)
    assert np.allclose(fpost_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(fpost_n, fhat(recip_n.coord_vectors[0]))

    # Doing the exact same with direct application of the FT operator
    ft_op_s = FourierTransform(discr, shift=True)
    ft_op_n = FourierTransform(discr, shift=False)
    assert ft_op_s.range.grid == recip_s
    assert ft_op_n.range.grid == recip_n

    ft_f_s = ft_op_s(f)
    ft_f_n = ft_op_n(f)
    assert np.allclose(ft_f_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(ft_f_n, fhat(recip_n.coord_vectors[0]))

    # Second test function, asymmetric. Can also be represented exactly in the
    # discretization.
    def f(x):
        return (x >= 0) & (x <= 1)

    def fhat(x):
        return np.exp(-1j * x / 2) * sinc(x / 2) / np.sqrt(2 * np.pi)

    # Discretize f, check values
    f_discr = discr.element(f)
    assert np.allclose(f_discr, [0, 0, 1, 0])

    # Pre-processing
    fpre_s = dft_preprocess_data(f_discr, shift=True)
    fpre_n = dft_preprocess_data(f_discr, shift=False)
    assert np.allclose(fpre_s, [0, 0, 1, 0])
    assert np.allclose(fpre_n, [0, 0, -1j, 0])

    # FFT step
    fft_s = np.fft.fftn(fpre_s, s=discr.shape, axes=[0])
    fft_n = np.fft.fftn(fpre_n, s=discr.shape, axes=[0])
    assert np.allclose(fft_s, [1, -1, 1, -1])
    assert np.allclose(fft_n, [-1j, 1j, -1j, 1j])

    fpost_s = dft_postprocess_data(range_s.element(fft_s),
                                   real_grid=discr.grid,
                                   recip_grid=recip_s,
                                   shift=[True],
                                   axes=(0, ),
                                   interp='nearest')
    fpost_n = dft_postprocess_data(range_n.element(fft_n),
                                   real_grid=discr.grid,
                                   recip_grid=recip_n,
                                   shift=[False],
                                   axes=(0, ),
                                   interp='nearest')

    assert np.allclose(fpost_s, fft_s * postproc_s * interp_s)
    assert np.allclose(fpost_n, fft_n * postproc_n * interp_n)

    # Comparing to the known result exp(-1j*x/2) * sinc(x/2) / sqrt(2*pi)
    assert np.allclose(fpost_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(fpost_n, fhat(recip_n.coord_vectors[0]))

    # Doing the exact same with direct application of the FT operator
    ft_f_s = ft_op_s(f)
    ft_f_n = ft_op_n(f)
    assert np.allclose(ft_f_s, fhat(recip_s.coord_vectors[0]))
    assert np.allclose(ft_f_n, fhat(recip_n.coord_vectors[0]))
from numpy import linspace, pi, ascontiguousarray
from GaussDictCode.bin.manager import myManager
from GaussDictCode.dictionary_def import AtomSpace, VolSpace, ProjSpace, VolElement, \
    ProjElement
from GaussDictCode.atomFuncs import GaussTomo
from GaussDictCode.transport_loss import l2_squared_loss
from GaussDictCode.regularisation import null
from KL_GaussRadon import doKL_ProjGDStep_iso

with mrc.FileReaderMRC(join('store',
                            'jasenko_1p8A_nonsharpened_absscale.mrc')) as f:
    _, gt = f.read()
    gt[gt < 0] = 0
    gt /= gt.max() / 2
angles = odl.RectGrid(linspace(-pi / 2, pi / 2, 40), linspace(0, pi / 2, 20))
angles = odl.uniform_partition_fromgrid(angles)

vol = list(gt.shape)
vol = odl.uniform_partition([-1] * 3, [1] * 3, vol)

vol = odl.uniform_discr_frompartition(vol, dtype='float32')
gt = ascontiguousarray(gt, dtype='float32')

PSpace = (angles, odl.uniform_partition([-1] * 2, [1] * 2, [64] * 2))
PSpace = odl.tomo.Parallel3dEulerGeometry(*PSpace)

# Operators
Radon = odl.tomo.RayTransform(vol, PSpace)
data = Radon(gt)

with myManager(device='cpu', order='C', fType='float32',