def test_partition_init(): vec1 = np.array([2, 4, 5, 7]) vec2 = np.array([-4, -3, 0, 1, 4]) begin = [2, -5] end = [10, 4] # Simply test if code runs odl.RectPartition(odl.Rectangle(begin, end), odl.TensorGrid(vec1, vec2)) odl.RectPartition(odl.Interval(begin[0], end[0]), odl.TensorGrid(vec1)) # Degenerate dimensions should work, too vec2 = np.array([1.0]) odl.RectPartition(odl.Rectangle(begin, end), odl.TensorGrid(vec1, vec2))
def test_rectangle(): # Continuous definition of problem space = odl.FunctionSpace(odl.Rectangle([0, 0], [1, 1])) # Complicated functions to check performance n = 5 m = 7 # Discretization d = odl.uniform_discr(space, (n, m), impl='cuda') fun = d.element([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) diff = ForwardDiff2D(d) derivative = diff(fun) assert all_almost_equal( derivative[0].asarray(), [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 1, -1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) assert all_almost_equal( derivative[1].asarray(), [[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) # Verify that the adjoint is ok # -gradient.T(gradient(x)) is the laplacian laplacian = -diff.adjoint(derivative) assert all_almost_equal( laplacian.asarray(), [[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 1, -4, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
def make_projector(n): # Discrete reconstruction space discr_reco_space = odl.uniform_discr([-20, -20, -20], [20, 20, 20], [n] * 3, dtype='float32') # Geometry src_rad = 1000 det_rad = 100 angle_intvl = odl.Interval(0, 2 * np.pi) dparams = odl.Rectangle([-50, -50], [50, 50]) agrid = odl.uniform_sampling(angle_intvl, n) dgrid = odl.uniform_sampling(dparams, [n] * 2) geom = odl.tomo.CircularConeFlatGeometry(angle_intvl, dparams, src_rad, det_rad, agrid, dgrid) # X-ray transform projector = odl.tomo.XrayTransform(discr_reco_space, geom, backend='astra_cuda') phantom = projector.domain.one() projector._adjoint *= projector(phantom).inner( projector(phantom)) / phantom.inner( projector.adjoint(projector(phantom))) return 0.08 * projector
def test_fspace_vector_equality(): rect = odl.Rectangle([0, 0], [1, 2]) fspace = FunctionSpace(rect) f_novec = fspace.element(func_2d_novec, vectorized=False) f_vec_oop = fspace.element(func_2d_vec_oop, vectorized=True) f_vec_oop_2 = fspace.element(func_2d_vec_oop, vectorized=True) f_vec_ip = fspace.element(func_2d_vec_ip, vectorized=True) f_vec_ip_2 = fspace.element(func_2d_vec_ip, vectorized=True) f_vec_dual = fspace.element(func_2d_vec_dual, vectorized=True) f_vec_dual_2 = fspace.element(func_2d_vec_dual, vectorized=True) assert f_novec == f_novec assert f_novec != f_vec_oop assert f_novec != f_vec_ip assert f_novec != f_vec_dual assert f_vec_oop == f_vec_oop assert f_vec_oop == f_vec_oop_2 assert f_vec_oop != f_vec_ip assert f_vec_oop != f_vec_dual assert f_vec_ip == f_vec_ip assert f_vec_ip == f_vec_ip_2 assert f_vec_ip != f_vec_dual assert f_vec_dual == f_vec_dual assert f_vec_dual == f_vec_dual_2
def test_nearest_interpolation_2d_string(): rect = odl.Rectangle([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2], nodes_on_bdry=False) # Coordinate vectors are: # [0.125, 0.375, 0.625, 0.875], [0.25, 0.75] space = odl.FunctionSet(rect, odl.Strings(1)) dspace = odl.Ntuples(part.size, dtype='U1') interp_op = NearestInterpolation(space, part, dspace) values = np.array([c for c in 'mystring']) function = interp_op(values) # Evaluate at single point val = function([0.3, 0.6]) # closest to index (1, 1) -> 3 assert val == 't' # Input array, with and without output array pts = np.array([[0.3, 0.6], [1.0, 1.0]]) true_arr = ['t', 'g'] assert all_equal(function(pts.T), true_arr) out = np.empty(2, dtype='U1') function(pts.T, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array mg = sparse_meshgrid([0.3, 1.0], [0.4, 1.0]) # Indices: (1, 3) x (0, 1) true_mg = [['s', 't'], ['n', 'g']] assert all_equal(function(mg), true_mg) out = np.empty((2, 2), dtype='U1') function(mg, out=out) assert all_equal(out, true_mg)
def test_fspace_vector_init(): # 1d, real intv = odl.Interval(0, 1) fspace = FunctionSpace(intv) fspace.element(func_1d_oop) fspace.element(func_1d_oop, vectorized=False) fspace.element(func_1d_oop, vectorized=True) fspace.element(func_1d_ip, vectorized=True) fspace.element(func_1d_dual, vectorized=True) # 2d, real rect = odl.Rectangle([0, 0], [1, 2]) fspace = FunctionSpace(rect) fspace.element(func_2d_novec, vectorized=False) fspace.element(func_2d_vec_oop) fspace.element(func_2d_vec_oop, vectorized=True) fspace.element(func_2d_vec_ip, vectorized=True) fspace.element(func_2d_vec_dual, vectorized=True) # 2d, complex fspace = FunctionSpace(rect, field=odl.ComplexNumbers()) fspace.element(cfunc_2d_novec, vectorized=False) fspace.element(cfunc_2d_vec_oop) fspace.element(cfunc_2d_vec_oop, vectorized=True) fspace.element(cfunc_2d_vec_ip, vectorized=True) fspace.element(cfunc_2d_vec_dual, vectorized=True)
def test_nearest_interpolation_2d_float(): rect = odl.Rectangle([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2], nodes_on_bdry=False) # Coordinate vectors are: # [0.125, 0.375, 0.625, 0.875], [0.25, 0.75] space = odl.FunctionSpace(rect) dspace = odl.Rn(part.size) interp_op = NearestInterpolation(space, part, dspace) function = interp_op([0, 1, 2, 3, 4, 5, 6, 7]) # Evaluate at single point val = function([0.3, 0.6]) # closest to index (1, 1) -> 3 assert val == 3.0 # Input array, with and without output array pts = np.array([[0.3, 0.6], [1.0, 1.0]]) true_arr = [3, 7] assert all_equal(function(pts.T), true_arr) out = np.empty(2, dtype='float64') function(pts.T, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array mg = sparse_meshgrid([0.3, 1.0], [0.4, 1.0]) # Indices: (1, 3) x (0, 1) true_mg = [[2, 3], [6, 7]] assert all_equal(function(mg), true_mg) out = np.empty((2, 2), dtype='float64') function(mg, out=out) assert all_equal(out, true_mg)
def test_init(exponent): # Validate that the different init patterns work and do not crash. space = odl.FunctionSpace(odl.Interval(0, 1)) part = odl.uniform_partition_fromintv(space.domain, 10) rn = odl.Rn(10, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent, interp='linear') # Normal discretization of unit interval with complex complex_space = odl.FunctionSpace(odl.Interval(0, 1), field=odl.ComplexNumbers()) cn = odl.Cn(10, exponent=exponent) odl.DiscreteLp(complex_space, part, cn, exponent=exponent) space = odl.FunctionSpace(odl.Rectangle([0, 0], [1, 1])) part = odl.uniform_partition_fromintv(space.domain, (10, 10)) rn = odl.Rn(100, exponent=exponent) odl.DiscreteLp(space, part, rn, exponent=exponent, interp=['nearest', 'linear']) # Real space should not work with complex with pytest.raises(ValueError): odl.DiscreteLp(space, part, cn) # Complex space should not work with reals with pytest.raises(ValueError): odl.DiscreteLp(complex_space, part, rn) # Wrong size of underlying space rn_wrong_size = odl.Rn(20) with pytest.raises(ValueError): odl.DiscreteLp(space, part, rn_wrong_size)
def test_bwt2d(): # 2D test n = 16 x = np.zeros((n, n)) x[5:10, 5:10] = 1 wbasis = 'josbiorth5' nscales = 3 # Define a discretized domain domain = odl.FunctionSpace(odl.Rectangle([-1, -1], [1, 1])) nPoints = np.array([n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Bop = BiorthWaveletTransform(disc_domain, nscales, wbasis) Bop2 = InverseAdjBiorthWaveletTransform(disc_domain, nscales, wbasis) # Compute the discrete wavelet transform of discrete imput image coeffs = Bop(disc_phantom) coeffs2 = Bop2(disc_phantom) reconstruction = Bop.inverse(coeffs) reconstruction2 = Bop2.inverse(coeffs2) assert all_almost_equal(reconstruction.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x)
def test_per_axis_interpolation(): rect = odl.Rectangle([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2], nodes_on_bdry=False) # Coordinate vectors are: # [0.125, 0.375, 0.625, 0.875], [0.25, 0.75] space = odl.FunctionSpace(rect) dspace = odl.Rn(part.size) schemes = ['linear', 'nearest'] variants = [None, 'right'] interp_op = PerAxisInterpolation(space, part, dspace, schemes=schemes, nn_variants=variants) values = np.arange(1, 9, dtype='float64') function = interp_op(values) rvals = values.reshape([4, 2]) # Evaluate at single point val = function([0.3, 0.5]) l1 = (0.3 - 0.125) / (0.375 - 0.125) # 0.5 equally far from both neighbors -> 'right' chooses 0.75 true_val = (1 - l1) * rvals[0, 1] + l1 * rvals[1, 1] assert almost_equal(val, true_val) # Input array, with and without output array pts = np.array([[0.3, 0.6], [0.1, 0.25], [1.0, 1.0]]) l1 = (0.3 - 0.125) / (0.375 - 0.125) true_val_1 = (1 - l1) * rvals[0, 1] + l1 * rvals[1, 1] l1 = (0.125 - 0.1) / (0.375 - 0.125) true_val_2 = (1 - l1) * rvals[0, 0] # only lower left contributes l1 = (1.0 - 0.875) / (0.875 - 0.625) true_val_3 = (1 - l1) * rvals[3, 1] # lower left only true_arr = [true_val_1, true_val_2, true_val_3] assert all_equal(function(pts.T), true_arr) out = np.empty(3, dtype='float64') function(pts.T, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array mg = sparse_meshgrid([0.3, 1.0], [0.4, 0.85]) # Indices: (1, 3) x (0, 1) lx1 = (0.3 - 0.125) / (0.375 - 0.125) lx2 = (1.0 - 0.875) / (0.875 - 0.625) true_val_11 = (1 - lx1) * rvals[0, 0] + lx1 * rvals[1, 0] true_val_12 = ((1 - lx1) * rvals[0, 1] + lx1 * rvals[1, 1]) true_val_21 = (1 - lx2) * rvals[3, 0] true_val_22 = (1 - lx2) * rvals[3, 1] true_mg = [[true_val_11, true_val_12], [true_val_21, true_val_22]] assert all_equal(function(mg), true_mg) out = np.empty((2, 2), dtype='float64') function(mg, out=out) assert all_equal(out, true_mg)
def test_collocation_cuda(): rect = odl.Rectangle([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2]) space = odl.FunctionSpace(rect) dspace = odl.CudaRn(part.size) coll_op = PointCollocation(space, part, dspace) interp_op = LinearInterpolation(space, part, dspace) values = np.arange(1, 9, dtype='float64') ident_values = coll_op(interp_op(values)) assert all_almost_equal(ident_values, values)
def test_proj(): # `DiscreteLp` volume space vol_shape = (100,) * 3 discr_vol_space = odl.uniform_discr([-50] * 3, [50] * 3, vol_shape, dtype='float32') # Angles: 0 and pi/2 angle_intvl = odl.Interval(0, np.pi / 2) angle_grid = odl.uniform_sampling(angle_intvl, 2, as_midp=False) # agrid = angle_grid.points() / np.pi # Detector dparams = odl.Rectangle([-50] * 2, [50] * 2) det_grid = odl.uniform_sampling(dparams, (100, 100)) def projector(index): axis = np.roll(np.array([1, 0, 0]), index) axis2 = np.roll(axis, 1) geom = odl.tomo.Parallel3dGeometry(angle_intvl, dparams, angle_grid, det_grid, axis=axis, origin_to_det=axis2) # Projection space proj_space = odl.FunctionSpace(geom.params) discr_data = odl.util.phantom.indicate_proj_axis(discr_vol_space, 0.5) # `DiscreteLp` projection space proj_shape = geom.grid.shape discr_proj_space = odl.uniform_discr_fromspace(proj_space, proj_shape, dtype='float32') # Forward proj_data = odl.tomo.astra_cuda_forward_projector( discr_data, geom, discr_proj_space) return proj_data proj_data = projector(0) proj_data.show(indices=(0, np.s_[:], np.s_[:])) proj_data.show(indices=(1, np.s_[:], np.s_[:])) proj_data = projector(1) proj_data.show(indices=(0, np.s_[:], np.s_[:])) proj_data.show(indices=(1, np.s_[:], np.s_[:])) proj_data = projector(2) proj_data.show(indices=(0, np.s_[:], np.s_[:])) proj_data.show(indices=(1, np.s_[:], np.s_[:])) plt.show(block=True)
def test_norm_rectangle(exponent): # Test the function f(x) = x_0^2 * x_1^3 on (0, 1) x (-1, 1). Its # L^p-norm is ((1 + 2*p) * (1 + 3 * p) / 2)^(-1/p) for finite p # and 1 for p=inf p = exponent fspace = odl.FunctionSpace(odl.Rectangle([0, -1], [1, 1])) lpdiscr = odl.uniform_discr_fromspace(fspace, (20, 30), exponent=p) testfunc = fspace.element(lambda x: x[0] ** 2 * x[1] ** 3) discr_testfunc = lpdiscr.element(testfunc) if p == float('inf'): assert discr_testfunc.norm() <= 1 # Max at boundary not hit else: true_norm = ((1 + 2 * p) * (1 + 3 * p) / 2) ** (-1 / p) assert almost_equal(discr_testfunc.norm(), true_norm, places=2)
def test_fspace_out_dtype(): rect = odl.Rectangle([0, 0], [3, 5]) points = np.array([[0, 1], [0, 3], [3, 4], [2, 5]], dtype='int').T vec1 = np.array([0, 1, 3])[:, None] vec2 = np.array([1, 2, 4, 5])[None, :] mg = (vec1, vec2) true_arr = func_2d_vec_oop(points) true_mg = func_2d_vec_oop(mg) fspace = FunctionSpace(rect, out_dtype='int') f_vec = fspace.element(func_2d_vec_oop) assert all_equal(f_vec(points), true_arr) assert all_equal(f_vec(mg), true_mg) assert f_vec(points).dtype == np.dtype('int') assert f_vec(mg).dtype == np.dtype('int')
def test_fspace_init(): intv = odl.Interval(0, 1) FunctionSpace(intv) FunctionSpace(intv, field=odl.RealNumbers()) FunctionSpace(intv, field=odl.ComplexNumbers()) rect = odl.Rectangle([0, 0], [1, 2]) FunctionSpace(rect) FunctionSpace(rect, field=odl.RealNumbers()) FunctionSpace(rect, field=odl.ComplexNumbers()) cube = odl.Cuboid([0, 0, 0], [1, 2, 3]) FunctionSpace(cube) FunctionSpace(cube, field=odl.RealNumbers()) FunctionSpace(cube, field=odl.ComplexNumbers()) ndbox = odl.IntervalProd([0] * 10, np.arange(1, 11)) FunctionSpace(ndbox) FunctionSpace(ndbox, field=odl.RealNumbers()) FunctionSpace(ndbox, field=odl.ComplexNumbers())
def test_collocation_interpolation_identity(): # Check if interpolation followed by collocation on the same grid # is the identity rect = odl.Rectangle([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2]) space = odl.FunctionSpace(rect) dspace = odl.Rn(part.size) coll_op_c = PointCollocation(space, part, dspace, order='C') coll_op_f = PointCollocation(space, part, dspace, order='F') interp_ops_c = [ NearestInterpolation(space, part, dspace, variant='left', order='C'), NearestInterpolation(space, part, dspace, variant='right', order='C'), LinearInterpolation(space, part, dspace, order='C'), PerAxisInterpolation(space, part, dspace, order='C', schemes=['linear', 'nearest']) ] interp_ops_f = [ NearestInterpolation(space, part, dspace, variant='left', order='F'), NearestInterpolation(space, part, dspace, variant='right', order='F'), LinearInterpolation(space, part, dspace, order='F'), PerAxisInterpolation(space, part, dspace, order='F', schemes=['linear', 'nearest']) ] values = np.arange(1, 9, dtype='float64') for interp_op_c in interp_ops_c: ident_values = coll_op_c(interp_op_c(values)) assert all_almost_equal(ident_values, values) for interp_op_f in interp_ops_f: ident_values = coll_op_f(interp_op_f(values)) assert all_almost_equal(ident_values, values)
def test_yzproj(): # `DiscreteLp` volume space vol_shape = (100,) * 3 discr_vol_space = odl.uniform_discr([-50] * 3, [50] * 3, vol_shape, dtype='float32') # Angles: 0 and pi/2 angle_intvl = odl.Interval(0, np.pi / 2) angle_grid = odl.uniform_sampling(angle_intvl, 2, as_midp=False) # agrid = angle_grid.points() / np.pi # Detector dparams = odl.Rectangle([-50] * 2, [50] * 2) det_grid = odl.uniform_sampling(dparams, (100, 100)) axis = (0, 1, 0) origin_to_det = (0, 0, 1) geom = odl.tomo.Parallel3dGeometry(angle_intvl, dparams, angle_grid, det_grid, axis=axis, origin_to_det=origin_to_det) # Projection space proj_space = odl.FunctionSpace(geom.params) discr_data = odl.util.phantom.indicate_proj_axis(discr_vol_space, 0.5) # `DiscreteLp` projection space proj_shape = geom.grid.shape discr_proj_space = odl.uniform_discr_fromspace(proj_space, proj_shape, dtype='float32') # Forward proj_data = odl.tomo.astra_cuda_forward_projector(discr_data, geom, discr_proj_space) plt.switch_backend('qt4agg') proj_data.show(indices=np.s_[0, :, :]) plt.show()
def test_norm_rectangle_boundary(impl, exponent): # Check the constant function 1 in different situations regarding the # placement of the outermost grid points. if exponent == float('inf'): pytest.xfail('inf-norm not implemented in CUDA') rect = odl.Rectangle([-1, -2], [1, 2]) # Standard case discr = odl.uniform_discr_fromspace(odl.FunctionSpace(rect), (4, 8), impl=impl, exponent=exponent) if exponent == float('inf'): assert discr.one().norm() == 1 else: assert almost_equal(discr.one().norm(), (rect.volume) ** (1 / exponent)) # Nodes on the boundary (everywhere) discr = odl.uniform_discr_fromspace( odl.FunctionSpace(rect), (4, 8), exponent=exponent, impl=impl, nodes_on_bdry=True) if exponent == float('inf'): assert discr.one().norm() == 1 else: assert almost_equal(discr.one().norm(), (rect.volume) ** (1 / exponent)) # Nodes on the boundary (selective) discr = odl.uniform_discr_fromspace( odl.FunctionSpace(rect), (4, 8), exponent=exponent, impl=impl, nodes_on_bdry=((False, True), False)) if exponent == float('inf'): assert discr.one().norm() == 1 else: assert almost_equal(discr.one().norm(), (rect.volume) ** (1 / exponent)) discr = odl.uniform_discr_fromspace( odl.FunctionSpace(rect), (4, 8), exponent=exponent, impl=impl, nodes_on_bdry=(False, (True, False))) if exponent == float('inf'): assert discr.one().norm() == 1 else: assert almost_equal(discr.one().norm(), (rect.volume) ** (1 / exponent)) # Completely arbitrary boundary grid = odl.RegularGrid([0, 0], [1, 1], (4, 4)) part = odl.RectPartition(rect, grid) weight = 1.0 if exponent == float('inf') else part.cell_volume dspace = odl.Rn(part.size, exponent=exponent, weight=weight) discr = DiscreteLp(odl.FunctionSpace(rect), part, dspace, impl=impl, exponent=exponent) if exponent == float('inf'): assert discr.one().norm() == 1 else: assert almost_equal(discr.one().norm(), (rect.volume) ** (1 / exponent))
def _standard_setup_2d(): rect = odl.Rectangle([0, 0], [1, 2]) points = _points(rect, num=5) mg = _meshgrid(rect, shape=(2, 3)) return rect, points, mg
def test_linear_interpolation_2d(): rect = odl.Rectangle([0, 0], [1, 1]) part = odl.uniform_partition_fromintv(rect, [4, 2], nodes_on_bdry=False) # Coordinate vectors are: # [0.125, 0.375, 0.625, 0.875], [0.25, 0.75] space = odl.FunctionSpace(rect) dspace = odl.Rn(part.size) interp_op = LinearInterpolation(space, part, dspace) values = np.arange(1, 9, dtype='float64') function = interp_op(values) rvals = values.reshape([4, 2]) # Evaluate at single point val = function([0.3, 0.6]) l1 = (0.3 - 0.125) / (0.375 - 0.125) l2 = (0.6 - 0.25) / (0.75 - 0.25) true_val = ((1 - l1) * (1 - l2) * rvals[0, 0] + (1 - l1) * l2 * rvals[0, 1] + l1 * (1 - l2) * rvals[1, 0] + l1 * l2 * rvals[1, 1]) assert almost_equal(val, true_val) # Input array, with and without output array pts = np.array([[0.3, 0.6], [0.1, 0.25], [1.0, 1.0]]) l1 = (0.3 - 0.125) / (0.375 - 0.125) l2 = (0.6 - 0.25) / (0.75 - 0.25) true_val_1 = ((1 - l1) * (1 - l2) * rvals[0, 0] + (1 - l1) * l2 * rvals[0, 1] + l1 * (1 - l2) * rvals[1, 0] + l1 * l2 * rvals[1, 1]) l1 = (0.125 - 0.1) / (0.375 - 0.125) # l2 = 0 true_val_2 = (1 - l1) * rvals[0, 0] # only lower left contributes l1 = (1.0 - 0.875) / (0.875 - 0.625) l2 = (1.0 - 0.75) / (0.75 - 0.25) true_val_3 = (1 - l1) * (1 - l2) * rvals[3, 1] # lower left only true_arr = [true_val_1, true_val_2, true_val_3] assert all_equal(function(pts.T), true_arr) out = np.empty(3, dtype='float64') function(pts.T, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array mg = sparse_meshgrid([0.3, 1.0], [0.4, 0.75]) # Indices: (1, 3) x (0, 1) lx1 = (0.3 - 0.125) / (0.375 - 0.125) lx2 = (1.0 - 0.875) / (0.875 - 0.625) ly1 = (0.4 - 0.25) / (0.75 - 0.25) # ly2 = 0 true_val_11 = ((1 - lx1) * (1 - ly1) * rvals[0, 0] + (1 - lx1) * ly1 * rvals[0, 1] + lx1 * (1 - ly1) * rvals[1, 0] + lx1 * ly1 * rvals[1, 1]) true_val_12 = ((1 - lx1) * rvals[0, 1] + lx1 * rvals[1, 1]) # ly2 = 0 true_val_21 = ( (1 - lx2) * (1 - ly1) * rvals[3, 0] + (1 - lx2) * ly1 * rvals[3, 1] ) # high node 1.0, no upper true_val_22 = (1 - lx2) * rvals[3, 1] # ly2 = 0, no upper for 1.0 true_mg = [[true_val_11, true_val_12], [true_val_21, true_val_22]] assert all_equal(function(mg), true_mg) out = np.empty((2, 2), dtype='float64') function(mg, out=out) assert all_equal(out, true_mg)
return ((x-0.1)**2 + y**2 <= 0.5**2).astype(float) def kernel(x): mean = [0.0, 0.5] std = [0.05, 0.05] return np.exp(-(((x[0] - mean[0]) / std[0]) ** 2 + ((x[1] - mean[1]) / std[1]) ** 2)) def adjkernel(x): return kernel((-x[0], -x[1])) # Continuous definition of problem domain = odl.FunctionSpace(odl.Rectangle([-1, -1], [1, 1])) kernel_domain = odl.FunctionSpace(odl.Rectangle([-2, -2], [2, 2])) # Complicated functions to check performance kernel = kernel_domain.element(kernel) adjkernel = kernel_domain.element(adjkernel) phantom = domain.element(ind_fun) # Discretization parameters n = 50 npoints = np.array([n+1, n+1]) npoints_kernel = np.array([2*n+1, 2*n+1]) # Discretization spaces disc_domain = odl.uniform_discr_fromspace(domain, npoints) disc_kernel_domain = odl.uniform_discr_fromspace(kernel_domain,
def test_xray_trafo_parallel3d(): """3D parallel-beam discrete X-ray transform with ASTRA CUDA.""" # Discrete reconstruction space xx = 5 nn = 4 * 5 # xx = 5.5 # nn = 11 discr_vol_space3 = odl.uniform_discr([-xx] * 3, [xx] * 3, [nn] * 3, dtype='float32') # Angle angle_intvl = odl.Interval(0, 2 * np.pi) - np.pi / 4 agrid = odl.uniform_sampling(angle_intvl, 4) # Detector # yy = 11 # mm = 11 yy = 10.5 mm = 1*21 dparams2 = odl.Rectangle([-yy, -yy], [yy, yy]) dgrid2 = odl.uniform_sampling(dparams2, [mm] * 2) # Geometry geom = odl.tomo.Parallel3dGeometry(angle_intvl, dparams2, agrid, dgrid2) # Projection space proj_space = odl.FunctionSpace(geom.params) # `DiscreteLp` projection space proj_shape = geom.grid.shape discr_proj_space = odl.uniform_discr_fromspace(proj_space, proj_shape, dtype='float32') # X-ray transform A = odl.tomo.XrayTransform(discr_vol_space3, geom, backend='astra_cuda') # Domain element f = A.domain.one() # Forward projection Af = A(f) A0f = odl.tomo.astra_cuda_forward_projector(f, geom, discr_proj_space) # Range element g = A.range.one() # Back projection Adg = A.adjoint(g) Adg0 = odl.tomo.astra_cuda_back_projector(g, geom, discr_vol_space3) print('\nvol stride', discr_vol_space3.grid.stride) print('proj stride', geom.grid.stride) print('angle intv:', angle_intvl.size) # f = discr_vol_space3.one() # print(f.asarray()[:, :, np.round(f.shape[2]/2)]) print('forward') print(Af.asarray()[0, :, np.floor(Af.shape[2] / 2)]) print(A0f.asarray()[0, :, np.floor(Af.shape[2] / 2)]) print('backward') print(Adg.asarray()[:, :, np.round(f.shape[2] / 2)] / float( agrid.stride) / agrid.ntotal) print(Adg0.asarray()[:, :, np.round(f.shape[2] / 2)] / agrid.ntotal)
# -*- coding: utf-8 -*- """ Solve laplace equation using ODL """ import numpy as np import odl space = odl.FunctionSpace(odl.Rectangle([0, 0], [1, 1])) def boundary_values(point): x, y = point result = 0.25 * np.sin(np.pi * y) * (x == 0) result += 1.00 * np.sin(np.pi * y) * (x == 1) result += 0.50 * np.sin(np.pi * x) * (y == 0) result += 0.50 * np.sin(np.pi * x) * (y == 1) return result n_last = 1 for n in [5, 50, 500]: # Discrete reconstruction space domain = odl.uniform_discr_fromspace(space, [n, n], nodes_on_bdry=True, interp='linear') # Define right hand side rhs = domain.element(boundary_values) # Define operator laplacian = odl.Laplacian(domain) * (-1.0 / n**2)
name.replace(' ', '_'), x)), title='{} [{},:,:]'.format(name, x), indices=[x, slice(None), slice(None)]) plt.close('all') # `DiscreteLp` volume space vol_shape = (80, 70, 60) discr_vol_space = odl.uniform_discr([-40, -35, -30], [40, 35, 30], vol_shape, dtype='float32') # Angles angle_intvl = odl.Interval(0, 2 * np.pi) angle_grid = odl.uniform_sampling(angle_intvl, 90, as_midp=False) # Detector dparams = odl.Rectangle([-50, -45], [50, 45]) det_grid = odl.uniform_sampling(dparams, (100, 90)) # Cone beam parameter src_rad = 1000 det_rad = 10 pitch_factor = 0 # Create an element in the volume space # discr_data = odl.util.phantom.cuboid(discr_vol_space, # (0.1, 0.15, 0.2,), (0.4, 0.35, 0.3)) discr_data = odl.util.phantom.indicate_proj_axis(discr_vol_space) sli = 0.5 vol_cuts = np.round(sli * np.array(vol_shape)) save_ortho_slices(discr_data, 'phantom 3d cuda', vol_cuts)
return odl.ReductionOperator(odl.MultiplyOperator(scale0), odl.MultiplyOperator(scale1)) n = 200 # Discrete reconstruction space discr_reco_space = odl.uniform_discr([-20, -20, -20], [20, 20, 20], [n]*3, dtype='float32') # Geometry src_rad = 1000 det_rad = 100 angle_intvl = odl.Interval(0, 2 * np.pi) dparams = odl.Rectangle([-50, -50], [50, 50]) agrid = odl.uniform_sampling(angle_intvl, n) dgrid = odl.uniform_sampling(dparams, [n]*2) geom = odl.tomo.CircularConeFlatGeometry(angle_intvl, dparams, src_rad, det_rad, agrid, dgrid) # X-ray transform proj = odl.tomo.DiscreteXrayTransform(discr_reco_space, geom, backend='astra_cuda') # Create phantom phantom0 = odl.util.shepp_logan(discr_reco_space, True) phantom1 = odl.util.derenzo_sources(discr_reco_space)
def test_dwt(): # Verify that the operator works as axpected # 1D test n = 16 x = np.zeros(n) x[5:10] = 1 wbasis = pywt.Wavelet('db1') nscales = 2 mode = 'sym' size_list = coeff_size_list((n,), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Interval([-1], [1])) nPoints = np.array([n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Wop = WaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1.asarray(), reconstruction2.asarray()) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x) # --------------------------------------------------------------- # 2D test n = 16 x = np.zeros((n, n)) x[5:10, 5:10] = 1 wbasis = pywt.Wavelet('db1') nscales = 2 mode = 'sym' size_list = coeff_size_list((n, n), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Rectangle([-1, -1], [1, 1])) nPoints = np.array([n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Wop = WaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(3 * np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1.asarray(), reconstruction2.asarray()) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x) # ------------------------------------------------------------- # 3D test n = 16 x = np.zeros((n, n, n)) x[5:10, 5:10, 5:10] = 1 wbasis = pywt.Wavelet('db2') nscales = 1 mode = 'per' size_list = coeff_size_list((n, n, n), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Cuboid([-1, -1, -1], [1, 1, 1])) nPoints = np.array([n, n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator related to 3D transform. Wop = WaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(7 * np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1, reconstruction2) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2, disc_phantom)