Ejemplo n.º 1
0
def test_partition_insert():
    vec11 = [2, 4, 5, 7]
    vec12 = [-4, -3, 0, 1, 4]
    min_pt1 = [1, -4]
    max_pt1 = [7, 5]
    grid1 = odl.RectGrid(vec11, vec12)
    intv1 = odl.IntervalProd(min_pt1, max_pt1)
    part1 = odl.RectPartition(intv1, grid1)

    vec21 = [-2, 0, 3]
    vec22 = [0]
    min_pt2 = [-2, -2]
    max_pt2 = [4, 0]
    grid2 = odl.RectGrid(vec21, vec22)
    intv2 = odl.IntervalProd(min_pt2, max_pt2)
    part2 = odl.RectPartition(intv2, grid2)

    part = part1.insert(0, part2)
    assert all_equal(part.min_pt, [-2, -2, 1, -4])
    assert all_equal(part.max_pt, [4, 0, 7, 5])
    assert all_equal(part.grid.min_pt, [-2, 0, 2, -4])
    assert all_equal(part.grid.max_pt, [3, 0, 7, 4])

    part = part1.insert(1, part2)
    assert all_equal(part.min_pt, [1, -2, -2, -4])
    assert all_equal(part.max_pt, [7, 4, 0, 5])
    assert all_equal(part.grid.min_pt, [2, -2, 0, -4])
    assert all_equal(part.grid.max_pt, [7, 3, 0, 4])
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def test_partition_init_raise():
    # Check different error scenarios
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    grid = odl.RectGrid(vec1, vec2)
    min_pt = [2, -5]
    max_pt = [10, 4]

    min_pt_toolarge = (2, -3.5)
    max_pt_toosmall = (7, 1)
    min_pt_badshape = (-1, 2, 0)
    max_pt_badshape = (2, )

    with pytest.raises(ValueError):
        odl.RectPartition(odl.IntervalProd(min_pt_toolarge, max_pt), grid)

    with pytest.raises(ValueError):
        odl.RectPartition(odl.IntervalProd(min_pt, max_pt_toosmall), grid)

    with pytest.raises(ValueError):
        odl.RectPartition(odl.IntervalProd(min_pt_badshape, max_pt_badshape),
                          grid)

    with pytest.raises(TypeError):
        odl.RectPartition(None, grid)

    with pytest.raises(TypeError):
        odl.RectPartition(odl.IntervalProd(min_pt_toolarge, max_pt), None)
Ejemplo n.º 4
0
def test_partition_init():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    min_pt = [2, -5]
    max_pt = [10, 4]

    # Simply test if code runs
    odl.RectPartition(odl.IntervalProd(min_pt, max_pt),
                      odl.RectGrid(vec1, vec2))
    odl.RectPartition(odl.IntervalProd(min_pt[0], max_pt[0]),
                      odl.RectGrid(vec1))

    # Degenerate dimensions should work, too
    vec2 = np.array([1.0])
    odl.RectPartition(odl.IntervalProd(min_pt, max_pt),
                      odl.RectGrid(vec1, vec2))
Ejemplo n.º 5
0
def test_partition_getitem():
    vec1 = [2, 4, 5, 7]
    vec2 = [-4, -3, 0, 1, 4]
    vec3 = [-2, 0, 3]
    vec4 = [0]
    vecs = [vec1, vec2, vec3, vec4]
    min_pt = [1, -4, -2, -2]
    max_pt = [7, 5, 4, 0]
    grid = odl.RectGrid(*vecs)
    intv = odl.IntervalProd(min_pt, max_pt)
    part = odl.RectPartition(intv, grid)

    # Test a couple of slices
    slc = (1, -2, 2, 0)
    slc_vecs = [v[i] for i, v in zip(slc, vecs)]
    slc_part = part[slc]
    assert slc_part.grid == odl.RectGrid(*slc_vecs)
    slc_min_pt = [3, 0.5, 1.5, -2]
    slc_max_pt = [4.5, 2.5, 4, 0]
    assert slc_part.set == odl.IntervalProd(slc_min_pt, slc_max_pt)

    slc = (slice(None), slice(None, None, 2), slice(None, 2), 0)
    slc_vecs = [v[i] for i, v in zip(slc, vecs)]
    slc_part = part[slc]
    assert slc_part.grid == odl.RectGrid(*slc_vecs)
    slc_min_pt = [1, -4, -2, -2]
    slc_max_pt = [7, 5, 1.5, 0]
    assert slc_part.set == odl.IntervalProd(slc_min_pt, slc_max_pt)

    # Fewer indices
    assert part[1] == part[1, :, :, :] == part[1, ...]
    assert part[1, 2:] == part[1, 2:, :, :] == part[1, 2:, ...]
    assert part[1, 2:, ::2] == part[1, 2:, ::2, :] == part[1, 2:, ::2, ...]

    # Index list using indices 0 and 2
    lst_min_pt = [1, -4, -2, -2]
    lst_max_pt = [6, 5, 4, 0]
    lst_intv = odl.IntervalProd(lst_min_pt, lst_max_pt)
    lst_vec1 = [2, 5]
    lst_grid = odl.RectGrid(lst_vec1, vec2, vec3, vec4)
    lst_part = odl.RectPartition(lst_intv, lst_grid)
    assert part[[0, 2]] == lst_part
Ejemplo n.º 6
0
def test_equals():
    """Test partition equality checks and hash."""
    vec1 = np.array([2, 3, 4, 5])
    vec2 = np.array([-4, -2, 0, 2, 4])

    part1 = odl.RectPartition(odl.IntervalProd(1, 6), odl.RectGrid(vec1))
    part2 = odl.RectPartition(odl.IntervalProd([1, -5], [6, 5]),
                              odl.RectGrid(vec1, vec2))
    part2_again = odl.RectPartition(odl.IntervalProd([1, -5], [6, 5]),
                                    odl.RectGrid(vec1, vec2))
    part2_rev = odl.RectPartition(odl.IntervalProd([-5, 1], [5, 6]),
                                  odl.RectGrid(vec2, vec1))

    _test_eq(part1, part1)
    _test_eq(part2, part2)
    _test_eq(part2, part2_again)

    _test_neq(part1, part2)
    _test_neq(part2, part2_rev)
    assert part2 != (vec1, vec2)
Ejemplo n.º 7
0
def test_astra_projection_geometry():
    """Create ASTRA projection geometry from geometry objects."""

    with pytest.raises(TypeError):
        odl.tomo.astra_projection_geometry(None)

    apart = odl.uniform_partition(0, 2 * np.pi, 5)
    dpart = odl.uniform_partition(-40, 40, 10)

    # motion sampling grid, detector sampling grid but not uniform
    dpart_0 = odl.RectPartition(odl.IntervalProd(0, 3), odl.RectGrid([0, 1,
                                                                      3]))
    geom_p2d = odl.tomo.Parallel2dGeometry(apart, dpart=dpart_0)
    with pytest.raises(ValueError):
        odl.tomo.astra_projection_geometry(geom_p2d)

    # detector sampling grid, motion sampling grid
    geom_p2d = odl.tomo.Parallel2dGeometry(apart, dpart)
    odl.tomo.astra_projection_geometry(geom_p2d)

    # Parallel 2D geometry
    geom_p2d = odl.tomo.Parallel2dGeometry(apart, dpart)
    astra_geom = odl.tomo.astra_projection_geometry(geom_p2d)
    assert astra_geom['type'] == 'parallel'

    # Fan flat
    src_rad = 10
    det_rad = 5
    geom_ff = odl.tomo.FanFlatGeometry(apart, dpart, src_rad, det_rad)
    astra_geom = odl.tomo.astra_projection_geometry(geom_ff)
    assert astra_geom['type'] == 'fanflat_vec'

    dpart = odl.uniform_partition([-40, -3], [40, 3], (10, 5))

    # Parallel 3D geometry
    geom_p3d = odl.tomo.Parallel3dAxisGeometry(apart, dpart)
    odl.tomo.astra_projection_geometry(geom_p3d)
    astra_geom = odl.tomo.astra_projection_geometry(geom_p3d)
    assert astra_geom['type'] == 'parallel3d_vec'

    # Circular conebeam flat
    geom_ccf = odl.tomo.ConeFlatGeometry(apart, dpart, src_rad, det_rad)
    astra_geom = odl.tomo.astra_projection_geometry(geom_ccf)
    assert astra_geom['type'] == 'cone_vec'

    # Helical conebeam flat
    pitch = 1
    geom_hcf = odl.tomo.ConeFlatGeometry(apart,
                                         dpart,
                                         src_rad,
                                         det_rad,
                                         pitch=pitch)
    astra_geom = odl.tomo.astra_projection_geometry(geom_hcf)
    assert astra_geom['type'] == 'cone_vec'
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
def test_partition_set():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    grid = odl.RectGrid(vec1, vec2)

    min_pt = [1, -4]
    max_pt = [10, 5]
    intv = odl.IntervalProd(min_pt, max_pt)

    part = odl.RectPartition(intv, grid)
    assert part.set == odl.IntervalProd(min_pt, max_pt)
    assert all_equal(part.min_pt, min_pt)
    assert all_equal(part.min(), min_pt)
    assert all_equal(part.max_pt, max_pt)
    assert all_equal(part.max(), max_pt)
Ejemplo n.º 10
0
def test_partition_cell_boundary_vecs():
    vec1 = np.array([2, 4, 5, 7])
    vec2 = np.array([-4, -3, 0, 1, 4])
    grid = odl.RectGrid(vec1, vec2)

    midpts1 = [3, 4.5, 6]
    midpts2 = [-3.5, -1.5, 0.5, 2.5]

    min_pt = [2, -6]
    max_pt = [10, 4]
    intv = odl.IntervalProd(min_pt, max_pt)

    true_bvec1 = [2] + midpts1 + [10]
    true_bvec2 = [-6] + midpts2 + [4]

    part = odl.RectPartition(intv, grid)
    assert all_equal(part.cell_boundary_vecs, (true_bvec1, true_bvec2))
Ejemplo n.º 11
0
def test_resizing_op_raise():

    # domain not a uniformely discretized Lp
    with pytest.raises(TypeError):
        odl.ResizingOperator(odl.rn(5), ran_shp=(10, ))

    grid = odl.RectGrid([0, 2, 3])
    part = odl.RectPartition(odl.IntervalProd(0, 3), grid)
    fspace = odl.FunctionSpace(odl.IntervalProd(0, 3))
    dspace = odl.rn(3)
    space = odl.DiscreteLp(fspace, part, dspace)
    with pytest.raises(ValueError):
        odl.ResizingOperator(space, ran_shp=(10, ))

    # different cell sides in domain and range
    space = odl.uniform_discr(0, 1, 10)
    res_space = odl.uniform_discr(0, 1, 15)
    with pytest.raises(ValueError):
        odl.ResizingOperator(space, res_space)

    # non-integer multiple of cell sides used as shift (grid of the
    # resized space shifted)
    space = odl.uniform_discr(0, 1, 5)
    res_space = odl.uniform_discr(-0.5, 1.5, 10)
    with pytest.raises(ValueError):
        odl.ResizingOperator(space, res_space)

    # need either range or ran_shp
    with pytest.raises(ValueError):
        odl.ResizingOperator(space)

    # offset cannot be combined with range
    space = odl.uniform_discr([0, -1], [1, 1], (10, 5))
    res_space = odl.uniform_discr([0, -3], [2, 3], (20, 15))
    with pytest.raises(ValueError):
        odl.ResizingOperator(space, res_space, offset=(0, 0))

    # bad pad_mode
    with pytest.raises(ValueError):
        odl.ResizingOperator(space, res_space, pad_mode='something')
Ejemplo n.º 12
0
# Set a volume shift. This should not have any influence on the back-projected
# 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,
Ejemplo n.º 13
0
and is not aligned to a rotation axis.
"""

import numpy as np
import odl


# Reconstruction space: discretized functions on the cube
# [-20, 20]^3 with 300 samples per dimension.
reco_space = odl.uniform_discr(
    min_pt=[-20, -20, -20], max_pt=[20, 20, 20], shape=[300, 300, 300],
    dtype='float32')

# Make a parallel beam geometry with flat detector
# Angles: 20 x 20 Euler angles corresponding to an octant of the 3D unit sphere
angle_grid = odl.RectGrid(np.linspace(0, np.pi / 2, 20),
                          np.linspace(0, np.pi / 2, 20))
angle_partition = odl.uniform_partition_fromgrid(angle_grid)

# Detector: uniformly sampled, n = (500, 500), min = (-40, -40), max = (40, 40)
detector_partition = odl.uniform_partition([-40, -40], [40, 40], [500, 500])
# Geometry with tilted axis.
geometry = odl.tomo.Parallel3dEulerGeometry(angle_partition,
                                            detector_partition)

# Ray transform (= forward projection).
ray_trafo = odl.tomo.RayTransform(reco_space, geometry)

# Create a Shepp-Logan phantom (modified version)
phantom = odl.phantom.shepp_logan(reco_space, modified=True)

# Create projection data by calling the ray transform on the phantom
Ejemplo n.º 14
0
@author: Rob Tovey
'''
from os.path import join
RECORD = join('store', 'DogBones_small')
RECORD = None
if RECORD is not None:
    import matplotlib
    matplotlib.use('Agg')
import odl
from numpy import sqrt, loadtxt, asarray, pi, ascontiguousarray
from PIL import Image

# Import data:
angles = loadtxt(join('DogBones', 'Sample_A2_Tilt_Series_tiltcorr_cut.rawtlt'))
angles = odl.RectPartition(odl.IntervalProd(-pi / 2, pi / 2),
                           odl.RectGrid((pi / 180) * angles))
# angles = odl.uniform_partition(-(pi / 180) * 69, (pi / 180) * 73, 71)
data = Image.open(join('DogBones', 'Sample A2 Tilt Series_tiltcorr_cut.tif'))
data = [asarray(data).T for i in range(data.n_frames) if data.seek(i) is None]
# First dimension is angle, second is width, third is slice
data = asarray(data)
# import matplotlib
# matplotlib.use('Agg')
from matplotlib import pyplot as plt, animation as mv
# writer = mv.writers['ffmpeg'](fps=5, metadata={'title': 'DogBones'})
fig = plt.figure()
# writer.setup(fig, 'DogBones' + '.mp4', dpi=100)
for i in range(data.shape[0]):
    plt.gca().clear()
    plt.imshow(data[i, :, 500:564].T)
    plt.pause(0.1)
Ejemplo n.º 15
0
# and shifts along the projection axis should only change cone effects.
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)

# Check projections at 0, 90, 180 and 270 degrees
grid = odl.RectGrid([0, np.pi / 2, np.pi, 3 * np.pi / 2])
angle_partition = odl.uniform_partition_fromgrid(grid)

# Make detector large enough to cover the object
src_radius = 500
det_radius = 1000
cone_angle = np.arctan(vol_max_pt[2] / src_radius)
det_size = np.floor(1.1 * (src_radius + det_radius) * np.sin(cone_angle))
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)

# Sum manually using Numpy
import odl
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)