def test_pspace_op_weighted_init(): r3 = odl.rn(3) ran = odl.ProductSpace(r3, 2, weighting=[1, 2]) I = odl.IdentityOperator(r3) with pytest.raises(NotImplementedError): odl.ProductSpaceOperator([[I], [0]], range=ran)
def test_pspace_op_derivative(base_op): """Test derivatives with different base operators.""" A = base_op op = odl.ProductSpaceOperator([[A + 1]]) true_deriv = odl.ProductSpaceOperator([[A]]) deriv = op.derivative(op.domain.zero()) assert deriv.domain == op.domain assert deriv.range == op.range x = op.domain.one() assert all_almost_equal(deriv(x), true_deriv(x)) op = odl.ProductSpaceOperator([[A + 1, 2 * A - 1]]) deriv = op.derivative(op.domain.zero()) true_deriv = odl.ProductSpaceOperator([[A, 2 * A]]) assert deriv.domain == op.domain assert deriv.range == op.range x = op.domain.one() assert all_almost_equal(deriv(x), true_deriv(x))
def test_pspace_op_adjoint(base_op): """Test adjoints with different base operators.""" A = base_op op = odl.ProductSpaceOperator([[A]]) true_adj = odl.ProductSpaceOperator([[A.adjoint]]) adj = op.adjoint assert adj.domain == op.range assert adj.range == op.domain y = op.range.one() assert all_almost_equal(adj(y), true_adj(y)) op = odl.ProductSpaceOperator([[2 * A, -A]]) true_adj = odl.ProductSpaceOperator([[2 * A.adjoint], [-A.adjoint]]) adj = op.adjoint assert adj.domain == op.range assert adj.range == op.domain y = op.range.one() assert all_almost_equal(adj(y), true_adj(y))
def test_pspace_op_diagonal_call(): r3 = odl.rn(3) I = odl.IdentityOperator(r3) op = odl.ProductSpaceOperator([[I, 0], [0, I]]) x = r3.element([1, 2, 3]) y = r3.element([7, 8, 9]) z = op.domain.element([x, y]) assert z == op(z) assert z == op(z, out=op.range.element())
def test_pspace_op_sum_call(): r3 = odl.rn(3) I = odl.IdentityOperator(r3) op = odl.ProductSpaceOperator([I, I]) x = r3.element([1, 2, 3]) y = r3.element([7, 8, 9]) z = op.domain.element([x, y]) assert all_almost_equal(op(z)[0], x + y) assert all_almost_equal(op(z, out=op.range.element())[0], x + y)
def test_pspace_op_project_call(): r3 = odl.Rn(3) I = odl.IdentityOperator(r3) op = odl.ProductSpaceOperator([[I], [I]]) x = r3.element([1, 2, 3]) y = r3.element([7, 8, 9]) z = op.domain.element([x, y]) assert x == op(z)[0] assert x == op(z, out=op.range.element())[0]
def test_pspace_op_swap_call(): r3 = odl.rn(3) I = odl.IdentityOperator(r3) op = odl.ProductSpaceOperator([[0, I], [I, 0]]) x = r3.element([1, 2, 3]) y = r3.element([7, 8, 9]) z = op.domain.element([x, y]) result = op.domain.element([y, x]) assert result == op(z) assert result == op(z, out=op.range.element())
def test_pspace_op_project_call(): r3 = odl.rn(3) A = odl.IdentityOperator(r3) op = odl.ProductSpaceOperator([[A], [A]]) x = r3.element([1, 2, 3]) z = op.domain.element([x]) assert x == op(z)[0] assert x == op(z, out=op.range.element())[0] assert x == op(z)[1] assert x == op(z, out=op.range.element())[1]
def test_pspace_op_init(base_op): """Test initialization with different base operators.""" A = base_op op = odl.ProductSpaceOperator([[A]]) assert op.domain == A.domain**1 assert op.range == A.range**1 op = odl.ProductSpaceOperator([[A, A]]) assert op.domain == A.domain**2 assert op.range == A.range**1 op = odl.ProductSpaceOperator([[A], [A]]) assert op.domain == A.domain**1 assert op.range == A.range**2 op = odl.ProductSpaceOperator([[A, 0], [0, A]]) assert op.domain == A.domain**2 assert op.range == A.range**2 op = odl.ProductSpaceOperator([[A, None], [None, A]]) assert op.domain == A.domain**2 assert op.range == A.range**2
def test_pspace_op_init(): r3 = odl.rn(3) I = odl.IdentityOperator(r3) op = odl.ProductSpaceOperator([I]) assert op.domain == odl.ProductSpace(r3) assert op.range == odl.ProductSpace(r3) op = odl.ProductSpaceOperator([I, I]) assert op.domain == odl.ProductSpace(r3, 2) assert op.range == odl.ProductSpace(r3) op = odl.ProductSpaceOperator([[I], [I]]) assert op.domain == odl.ProductSpace(r3) assert op.range == odl.ProductSpace(r3, 2) op = odl.ProductSpaceOperator([[I, 0], [0, I]]) assert op.domain == odl.ProductSpace(r3, 2) assert op.range == odl.ProductSpace(r3, 2) op = odl.ProductSpaceOperator([[I, None], [None, I]]) assert op.domain == odl.ProductSpace(r3, 2) assert op.range == odl.ProductSpace(r3, 2)
def test_combine_proximal(): """Function to combine proximal factory functions. The combine function makes use of the separable sum property of proximal operators. """ # Image space space = odl.uniform_discr(0, 1, 10) # Factory function returning the proximal operator prox_factory = proximal_const_func(space) # Combine factory function of proximal operators combined_prox_factory = combine_proximals(prox_factory, prox_factory) # Initialize combine proximal operator prox = combined_prox_factory(1) assert isinstance(prox, odl.Operator) # Explicit construction of the combine proximal operator prox_verify = odl.ProductSpaceOperator( [[odl.IdentityOperator(space), None], [None, odl.IdentityOperator(space)]]) # Create an element in the domain of the operator x = prox_verify.domain.element([np.arange(-5, 5), np.arange(-5, 5)]) # Allocate output element out = prox_verify.range.element() # Apply explicitly constructed and factory-function-combined proximal # operators assert prox(x) == prox_verify(x) # Test output argument assert prox(x, out) == prox_verify(x) # Identity mapping assert out == x
# A11 = R11, A22 = R22, A21 = R21 o M21, where M21 is a masking operator # for the ROI in X1. # Functionals # D = data matching functional: Y1 x Y2 -> R, ||. - g1||_Y1^2 + ||. - g2||_Y2^2 # S1 = squared L2-norm: X1^2 -> R, for Tikhonov functional # S2 = (alpha * L12-Norm): X2^2 -> R, for isotropic TV # Operators # A = forward operator "matrix": X1 x X2 -> Y1 x Y2 # G1 = spatial gradient: X1 -> X1^2 # G2 = spatial gradient: X2 -> X2^2 # B1 = G1 extended to X1 x X2, B1(f1, f2) = G1(f1) # B2 = G2 extended to X1 x X2, B2(f1, f2) = G2(f2) A = odl.ProductSpaceOperator([[R11, 0], [A21, R22]]) G1 = odl.Gradient(X1, pad_mode='symmetric') G2 = odl.Gradient(X2, pad_mode='order1') # Extend gradients to product space B1 = G1 * odl.ComponentProjection(A.domain, 0) B2 = G2 * odl.ComponentProjection(A.domain, 1) # TODO: weighting for differences in sizes (avoid large region domination) D = odl.solvers.L2NormSquared(A.range).translated([g1, g2]) # For isotropic TV # alpha1 = 1e-2 # S1 = alpha1 * odl.solvers.GroupL1Norm(G1.range) # For Tikhonov alpha1 = 1e-2 S1 = alpha1 * odl.solvers.L2NormSquared(G1.range)
grad = odl.Gradient(space) grad_vec = odl.DiagonalOperator(grad, 2) cross_terms = True c = 0.5 if not cross_terms: crlb[1, 0, ...] = 0 crlb[0, 1, ...] = 0 mat_sqrt_inv = inverse_sqrt_matrix(crlb) re = ray_trafo.range.element W = odl.ProductSpaceOperator([[odl.MultiplyOperator(re(mat_sqrt_inv[0, 0])), odl.MultiplyOperator(re(mat_sqrt_inv[0, 1]))], [odl.MultiplyOperator(re(mat_sqrt_inv[1, 0])), odl.MultiplyOperator(re(mat_sqrt_inv[1, 1]))]]) op = W * A rhs = W(data) data_discrepancy = odl.solvers.L2NormSquared(A.range).translated(rhs) l1_norm = odl.solvers.L1Norm(space) huber = 2.5 * odl.solvers.MoreauEnvelope(l1_norm, sigma=0.01) my_op = MyOperatorTrace(domain=grad_vec.range, range=space, linear=False) spc_cov_matrix = [[1, -c], [-c, 1]] spc_cov_matrix_inv_sqrt = inverse_sqrt_matrix(spc_cov_matrix)
# Create phantom phantom = odl.util.shepp_logan(discr_reco_space, True) # Adjoint currently bugged, needs to be fixed A._adjoint *= A(phantom).inner(A(phantom)) / phantom.inner(A.adjoint(A(phantom))) # Create data rhs = A(phantom) # Add noise mean = rhs.ufunc.sum() / rhs.size rhs.ufunc.add(np.random.rand(A.range.size)*1.0*mean, out=rhs) # Create chambolle pock operator grad = odl.Gradient(discr_reco_space, method='forward') prod_op = odl.ProductSpaceOperator([[A], [grad]]) # Get norm repeat_phatom = prod_op.domain.element([phantom, phantom]) prod_op_norm = odl.operator.oputils.power_method_opnorm(prod_op, 10, repeat_phatom) * 2.0 # Reconstruct partial = (odl.solvers.ShowPartial(clim=[-0.1, 1.1], display_step=1) & odl.solvers.ShowPartial(indices=np.s_[0, :, n//2, n//2]) & odl.solvers.PrintTimePartial() & odl.solvers.PrintIterationPartial()) # Run algorithms rec = chambolle_pock_solver(prod_op, f_cc_prox_l2_tv(prod_op.range, rhs, lam=0.01),
grad_vec = odl.DiagonalOperator(grad, 2) cross_terms = True c = 0.0 if not cross_terms: crlb[1, 0, ...] = 0 crlb[0, 1, ...] = 0 mat_sqrt_inv = inverse_sqrt_matrix(crlb) re = ray_trafo.range.element W = odl.ProductSpaceOperator([[ odl.MultiplyOperator(re(mat_sqrt_inv[0, 0])), odl.MultiplyOperator(re(mat_sqrt_inv[0, 1])) ], [ odl.MultiplyOperator(re(mat_sqrt_inv[1, 0])), odl.MultiplyOperator(re(mat_sqrt_inv[1, 1])) ]]) mat_sqrt_inv_hat = mat_sqrt_inv.mean(axis=(2, 3)) mat_sqrt_inv_hat_inv = np.linalg.inv(mat_sqrt_inv_hat) I = odl.IdentityOperator(space) precon = odl.ProductSpaceOperator(np.multiply(mat_sqrt_inv_hat_inv, I)) precon_inv = odl.ProductSpaceOperator(np.multiply(mat_sqrt_inv_hat, I)) op = W * A rhs = W(data) data_discrepancy = odl.solvers.L2NormSquared(A.range).translated(rhs)
space = odl.uniform_discr([-129, -129], [129, 129], [200, 200]) ray_trafo = odl.tomo.RayTransform(space, geometry, impl='astra_cuda') A = odl.DiagonalOperator(ray_trafo, 2) grad = odl.Gradient(space) L = odl.DiagonalOperator(grad, 2) # Compute covariance matrix and matrix power I = odl.IdentityOperator(ray_trafo.range) cov_mat = cov_matrix(data) w_mat = spl.fractional_matrix_power(cov_mat, -0.5) # raise Exception W = odl.ProductSpaceOperator(np.multiply(w_mat, I)) op = W * A rhs = W(data) data_discrepancy = odl.solvers.L2NormSquared(A.range).translated(rhs) regularizer = 0.01 * odl.solvers.NuclearNorm(L.range) fbp_op = odl.tomo.fbp_op(ray_trafo) x = A.domain.element([fbp_op(data[0]), fbp_op(data[1])]) f = odl.solvers.IndicatorBox(A.domain, 0, 1) g = [data_discrepancy, regularizer] lin_ops = [op, L] tau = 1.0 sigma = [
spectral_proj = detector_op * proj_op # Create data phantom = spectral_proj.domain.element([phantom0, phantom1]) proj_op(phantom).show(title='materials', clim=[0, 5]) projections = spectral_proj(phantom) projections.show(title='spectral', clim=[0, 5]) # Reconstruct with big op if 0: partial = (odl.solvers.util.ShowPartial(indices=np.s_[0, :, :, :, n//2], clim=[0, 5]) & odl.solvers.util.ShowPartial(indices=np.s_[1, :, n//2, :, n//2]) & odl.solvers.util.ShowPartial(indices=np.s_[1, :, :, :, n//2], clim=[0, 1]) & odl.solvers.util.PrintIterationPartial()) bigop = odl.ProductSpaceOperator([[detector_op, 0], [-odl.IdentityOperator(detector_op.domain), proj_op]]) newrhs = bigop.range.element([projections, bigop.range[1].zero()]) x = bigop.domain.zero() for i in range(20): cgn(bigop, x, newrhs, niter=3, partial=partial) else: partial_proj = (odl.solvers.util.ShowPartial(indices=np.s_[:, :, :, n//2], clim=[0, 5]) & odl.solvers.util.PrintIterationPartial('projection')) partial_vol = (odl.solvers.util.ShowPartial(indices=np.s_[:, :, :, n//2], clim=[0, 1]) & odl.solvers.util.ShowPartial(indices=np.s_[:, n//2, :, n//2]) & odl.solvers.util.PrintIterationPartial('volume')) partial_proj = None separate_projections = projections.copy() / 2.0 volumes = proj_op.domain.zero() for i in range(1):
Id = odl.IdentityOperator(gradient.range) PD = [ odl.PartialDerivative(U, i, method='backward', pad_mode='symmetric') for i in range(3) ] E = odl.operator.ProductSpaceOperator([[PD[0], 0, 0], [0, PD[1], 0], [0, 0, PD[2]], [PD[1], PD[0], 0], [PD[2], 0, PD[0]], [0, PD[2], PD[1]]]) D = odl.ProductSpaceOperator([[gradient, -Id], [0, E]]) norm_D = misc.norm(D, '{}/norm_D.npy'.format(folder_param)) norm_vfield = odl.PointwiseNorm(gradient.range) def save_image(x, n, f): misc.save_image(x[0].asarray(), n, f, planes=planes, clim=clim) misc.save_image(norm_vfield(x[1]).asarray(), n + '_norm_vfield', f, planes=planes) c = float(norm_K) / float(norm_D) D *= c norm_D *= c L1 = odl.solvers.SeparableSum( *[(alpha / c) *