def test_discretizedspace_init(): """Test initialization and basic properties of DiscretizedSpace.""" # Real space part = odl.uniform_partition([0, 0], [1, 1], (2, 4)) tspace = odl.rn(part.shape) discr = DiscretizedSpace(part, tspace) assert discr.tspace == tspace assert discr.partition == part assert discr.exponent == tspace.exponent assert discr.axis_labels == ('$x$', '$y$') assert discr.is_real # Complex space tspace_c = odl.cn(part.shape) discr = DiscretizedSpace(part, tspace_c) assert discr.is_complex # Make sure repr shows something assert repr(discr) != '' # Error scenarios part_1d = odl.uniform_partition(0, 1, 2) with pytest.raises(ValueError): DiscretizedSpace(part_1d, tspace) # wrong dimensionality part_diffshp = odl.uniform_partition([0, 0], [1, 1], (3, 4)) with pytest.raises(ValueError): DiscretizedSpace(part_diffshp, tspace) # shape mismatch
def test_all_imports(): import odl # Create Cn odl.cn(3) odl.space.cn(3) C3 = odl.space.space_utils.cn(3) # Three ways of creating the identity odl.IdentityOperator(C3) odl.operator.IdentityOperator(C3) odl.operator.default_ops.IdentityOperator(C3) # Test that utility needs to be explicitly imported odl.util.utility.array1d_repr with pytest.raises(AttributeError): odl.array1d_repr
def test_mat_op_adjoint(fn): # Square cases sparse_mat = _sparse_matrix(fn) dense_mat = _dense_matrix(fn) op_sparse = MatrixOperator(sparse_mat, fn, fn) op_dense = MatrixOperator(dense_mat, fn, fn) # Just test if it runs, nothing interesting to test here op_sparse.adjoint op_dense.adjoint # Rectangular case rect_mat = 2 * np.eye(2, 3) r2, r3 = odl.rn(2), odl.rn(3) c2 = odl.cn(2) op = MatrixOperator(rect_mat, r3, r2) op_adj = op.adjoint assert op_adj.domain == op.range assert op_adj.range == op.domain assert np.array_equal(op_adj.matrix, op.matrix.conj().T) assert np.array_equal(op_adj.adjoint.matrix, op.matrix) # The operator Rn -> Cn has no adjoint op_noadj = MatrixOperator(rect_mat, r3, c2) with pytest.raises(NotImplementedError): op_noadj.adjoint
def test_all_imports(): import odl # Create Cn odl.cn(3) odl.space.cn(3) C3 = odl.space.space_utils.cn(3) # Three ways of creating the identity odl.IdentityOperator(C3) odl.operator.IdentityOperator(C3) odl.operator.default_ops.IdentityOperator(C3) # Test that utility needs to be explicitly imported odl.util.utility.array1d_repr with pytest.raises(AttributeError): odl.array1d_repr
def test_pnorm(exponent): for fn in (odl.rn(3, exponent=exponent), odl.cn(3, exponent=exponent)): xarr, x = noise_elements(fn) correct_norm = np.linalg.norm(xarr, ord=exponent) assert almost_equal(fn.norm(x), correct_norm) assert almost_equal(x.norm(), correct_norm)
def test_init(exponent): # Validate that the different init patterns work and do not crash. space = odl.FunctionSpace(odl.IntervalProd(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.IntervalProd(0, 1), field=odl.ComplexNumbers()) cn = odl.cn(10, exponent=exponent) odl.DiscreteLp(complex_space, part, cn, exponent=exponent) space = odl.FunctionSpace(odl.IntervalProd([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_nearest_interpolation_1d_complex(fn_impl): intv = odl.IntervalProd(0, 1) part = odl.uniform_partition_fromintv(intv, 5, nodes_on_bdry=False) # Coordinate vectors are: # [0.1, 0.3, 0.5, 0.7, 0.9] space = odl.FunctionSpace(intv, field=odl.ComplexNumbers()) dspace = odl.cn(part.size) interp_op = NearestInterpolation(space, part, dspace) function = interp_op([0 + 1j, 1 + 2j, 2 + 3j, 3 + 4j, 4 + 5j]) # Evaluate at single point val = function(0.35) # closest to index 1 -> 1 + 2j assert val == 1.0 + 2.0j # Input array, with and without output array pts = np.array([0.4, 0.0, 0.65, 0.95]) true_arr = [1 + 2j, 0 + 1j, 3 + 4j, 4 + 5j] assert all_equal(function(pts), true_arr) # Should also work with a (1, N) array pts = pts[None, :] assert all_equal(function(pts), true_arr) out = np.empty(4, dtype='complex128') function(pts, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array # Same as array for 1d mg = sparse_meshgrid([0.4, 0.0, 0.65, 0.95]) true_mg = [1 + 2j, 0 + 1j, 3 + 4j, 4 + 5j] assert all_equal(function(mg), true_mg) 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.IntervalProd(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.IntervalProd(0, 1), field=odl.ComplexNumbers()) cn = odl.cn(10, exponent=exponent) odl.DiscreteLp(complex_space, part, cn, exponent=exponent) space = odl.FunctionSpace(odl.IntervalProd([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_nearest_interpolation_1d_complex(odl_tspace_impl): """Test nearest neighbor interpolation in 1d with complex values.""" impl = odl_tspace_impl # TODO: not used! intv = odl.IntervalProd(0, 1) part = odl.uniform_partition_fromintv(intv, 5, nodes_on_bdry=False) # Coordinate vectors are: # [0.1, 0.3, 0.5, 0.7, 0.9] fspace = odl.FunctionSpace(intv, out_dtype=complex) tspace = odl.cn(part.shape) interp_op = NearestInterpolation(fspace, part, tspace) function = interp_op([0 + 1j, 1 + 2j, 2 + 3j, 3 + 4j, 4 + 5j]) # Evaluate at single point val = function(0.35) # closest to index 1 -> 1 + 2j assert val == 1.0 + 2.0j # Input array, with and without output array pts = np.array([0.4, 0.0, 0.65, 0.95]) true_arr = [1 + 2j, 0 + 1j, 3 + 4j, 4 + 5j] assert all_equal(function(pts), true_arr) # Should also work with a (1, N) array pts = pts[None, :] assert all_equal(function(pts), true_arr) out = np.empty(4, dtype='complex128') function(pts, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array # Same as array for 1d mg = sparse_meshgrid([0.4, 0.0, 0.65, 0.95]) true_mg = [1 + 2j, 0 + 1j, 3 + 4j, 4 + 5j] assert all_equal(function(mg), true_mg) function(mg, out=out) assert all_equal(out, true_mg) assert repr(interp_op) != ''
def test_scalar_method(): # Too large space space = odl.rn(2) element = space.one() with pytest.raises(TypeError): int(element) with pytest.raises(TypeError): float(element) with pytest.raises(TypeError): complex(element) if PYTHON2: with pytest.raises(TypeError): long(element) # Size 1 real space space = odl.rn(1) value = 1.5 element = space.element(value) assert int(element) == int(value) assert float(element) == float(value) assert complex(element) == complex(value) if PYTHON2: assert long(element) == long(value) # Size 1 complex space space = odl.cn(1) value = 1.5 + 0.5j element = space.element(value) assert complex(element) == complex(value)
def test_nearest_interpolation_1d_complex(fn_impl): intv = odl.IntervalProd(0, 1) part = odl.uniform_partition_fromintv(intv, 5, nodes_on_bdry=False) # Coordinate vectors are: # [0.1, 0.3, 0.5, 0.7, 0.9] space = odl.FunctionSpace(intv, field=odl.ComplexNumbers()) dspace = odl.cn(part.size) interp_op = NearestInterpolation(space, part, dspace) function = interp_op([0 + 1j, 1 + 2j, 2 + 3j, 3 + 4j, 4 + 5j]) # Evaluate at single point val = function(0.35) # closest to index 1 -> 1 + 2j assert val == 1.0 + 2.0j # Input array, with and without output array pts = np.array([0.4, 0.0, 0.65, 0.95]) true_arr = [1 + 2j, 0 + 1j, 3 + 4j, 4 + 5j] assert all_equal(function(pts), true_arr) # Should also work with a (1, N) array pts = pts[None, :] assert all_equal(function(pts), true_arr) out = np.empty(4, dtype='complex128') function(pts, out=out) assert all_equal(out, true_arr) # Input meshgrid, with and without output array # Same as array for 1d mg = sparse_meshgrid([0.4, 0.0, 0.65, 0.95]) true_mg = [1 + 2j, 0 + 1j, 3 + 4j, 4 + 5j] assert all_equal(function(mg), true_mg) function(mg, out=out) assert all_equal(out, true_mg)
def test_init(): # Test run NumpyNtuples(3, int) NumpyNtuples(3, float) NumpyNtuples(3, complex) NumpyNtuples(3, 'S1') # Fn NumpyFn(3, int) NumpyFn(3, float) NumpyFn(3, complex) # Fn only works on scalars with pytest.raises(TypeError): NumpyFn(3, 'S1') # Rn odl.rn(3, float) # Rn only works on reals with pytest.raises(TypeError): odl.rn(3, complex) with pytest.raises(TypeError): odl.rn(3, 'S1') with pytest.raises(TypeError): odl.rn(3, int) # Cn odl.cn(3, complex) # Cn only works on reals with pytest.raises(TypeError): odl.cn(3, float) with pytest.raises(TypeError): odl.cn(3, 'S1') # Backported int from future fails (not recognized by numpy.dtype()) # (Python 2 only) from builtins import int as future_int import sys if sys.version_info.major != 3: with pytest.raises(TypeError): NumpyFn(3, future_int) # Init with weights or custom space functions const = 1.5 weight_arr = _pos_array(odl.rn(3, float)) weight_mat = _dense_matrix(odl.rn(3, float)) odl.rn(3, weighting=const) odl.rn(3, weighting=weight_arr) odl.rn(3, weighting=weight_mat) # Different exponents exponents = [0.5, 1.0, 2.0, 5.0, float('inf')] for exponent in exponents: odl.cn(3, exponent=exponent)
def test_pdist(exponent): for fn in (odl.rn(3, exponent=exponent), odl.cn(3, exponent=exponent)): [xarr, yarr], [x, y] = noise_elements(fn, n=2) correct_dist = np.linalg.norm(xarr - yarr, ord=exponent) assert almost_equal(fn.dist(x, y), correct_dist) assert almost_equal(x.dist(y), correct_dist)
def test_astype(): rn = odl.rn(3, weighting=1.5) cn = odl.cn(3, weighting=1.5) rn_s = odl.rn(3, weighting=1.5, dtype='float32') cn_s = odl.cn(3, weighting=1.5, dtype='complex64') # Real assert rn.astype('float32') == rn_s assert rn.astype('float64') is rn assert rn.real_space is rn assert rn.astype('complex64') == cn_s assert rn.astype('complex128') == cn assert rn.complex_space == cn # Complex assert cn.astype('complex64') == cn_s assert cn.astype('complex128') is cn assert cn.real_space == rn assert cn.astype('float32') == rn_s assert cn.astype('float64') == rn assert cn.complex_space is cn
def space(request): name = request.param.strip() if name == 'product_space': space = odl.ProductSpace(odl.uniform_discr(0, 1, 3, dtype=complex), odl.cn(2)) elif name == 'power_space': space = odl.ProductSpace(odl.uniform_discr(0, 1, 3, dtype=complex), 2) else: raise ValueError('undefined space') return space
def test_discretelp_init(): """Test initialization and basic properties of DiscreteLp.""" # Real space fspace = odl.FunctionSpace(odl.IntervalProd([0, 0], [1, 1])) part = odl.uniform_partition_fromintv(fspace.domain, (2, 4)) tspace = odl.rn(part.shape) discr = DiscreteLp(fspace, part, tspace) assert discr.fspace == fspace assert discr.tspace == tspace assert discr.partition == part assert discr.interp == 'nearest' assert discr.interp_byaxis == ('nearest', 'nearest') assert discr.exponent == tspace.exponent assert discr.axis_labels == ('$x$', '$y$') assert discr.is_real discr = DiscreteLp(fspace, part, tspace, interp='linear') assert discr.interp == 'linear' assert discr.interp_byaxis == ('linear', 'linear') discr = DiscreteLp(fspace, part, tspace, interp=['nearest', 'linear']) assert discr.interp == ('nearest', 'linear') assert discr.interp_byaxis == ('nearest', 'linear') # Complex space fspace_c = odl.FunctionSpace(odl.IntervalProd([0, 0], [1, 1]), out_dtype=complex) tspace_c = odl.cn(part.shape) discr = DiscreteLp(fspace_c, part, tspace_c) assert discr.is_complex # Make sure repr shows something assert repr(discr) # Error scenarios with pytest.raises(ValueError): DiscreteLp(fspace, part, tspace_c) # mixes real & complex with pytest.raises(ValueError): DiscreteLp(fspace_c, part, tspace) # mixes complex & real part_1d = odl.uniform_partition(0, 1, 2) with pytest.raises(ValueError): DiscreteLp(fspace, part_1d, tspace) # wrong dimensionality part_diffshp = odl.uniform_partition_fromintv(fspace.domain, (3, 4)) with pytest.raises(ValueError): DiscreteLp(fspace, part_diffshp, tspace) # shape mismatch
def test_real_imag_and_conj(): """Verify that .real .imag and .conj() work for product space elements.""" space = odl.ProductSpace(odl.uniform_discr(0, 1, 3, dtype=complex), odl.cn(2)) x = noise_element(space) # Test real expected_result = space.real_space.element([x[0].real, x[1].real]) assert x.real == expected_result # Test imag expected_result = space.real_space.element([x[0].imag, x[1].imag]) assert x.imag == expected_result # Test conj. Note that ProductSpace does not implement asarray if # is_power_space is false. Hence the construction below expected_result = space.element([x[0].conj(), x[1].conj()]) x_conj = x.conj() assert x_conj[0] == expected_result[0] assert x_conj[1] == expected_result[1]
def test_matrix_op_init(matrix): """Test initialization and properties of matrix operators.""" dense_matrix = matrix sparse_matrix = scipy.sparse.coo_matrix(dense_matrix) # Just check if the code runs MatrixOperator(dense_matrix) MatrixOperator(sparse_matrix) # Test default domain and range mat_op = MatrixOperator(dense_matrix) assert mat_op.domain == odl.tensor_space(4, matrix.dtype) assert mat_op.range == odl.tensor_space(3, matrix.dtype) assert np.all(mat_op.matrix == dense_matrix) sparse_matrix = scipy.sparse.coo_matrix(dense_matrix) mat_op = MatrixOperator(sparse_matrix) assert mat_op.domain == odl.tensor_space(4, matrix.dtype) assert mat_op.range == odl.tensor_space(3, matrix.dtype) assert (mat_op.matrix != sparse_matrix).getnnz() == 0 # Explicit domain and range dom = odl.tensor_space(4, matrix.dtype) ran = odl.tensor_space(3, matrix.dtype) mat_op = MatrixOperator(dense_matrix, domain=dom, range=ran) assert mat_op.domain == dom assert mat_op.range == ran mat_op = MatrixOperator(sparse_matrix, domain=dom, range=ran) assert mat_op.domain == dom assert mat_op.range == ran # Bad 1d sizes with pytest.raises(ValueError): MatrixOperator(dense_matrix, domain=odl.cn(4), range=odl.cn(4)) with pytest.raises(ValueError): MatrixOperator(dense_matrix, range=odl.cn(4)) # Invalid range dtype with pytest.raises(ValueError): MatrixOperator(dense_matrix.astype(complex), range=odl.rn(4)) # Data type promotion # real space, complex matrix -> complex space dom = odl.rn(4) mat_op = MatrixOperator(dense_matrix.astype(complex), domain=dom) assert mat_op.domain == dom assert mat_op.range == odl.cn(3) # complex space, real matrix -> complex space dom = odl.cn(4) mat_op = MatrixOperator(dense_matrix.real, domain=dom) assert mat_op.domain == dom assert mat_op.range == odl.cn(3) # Multi-dimensional spaces dom = odl.tensor_space((6, 5, 4), matrix.dtype) ran = odl.tensor_space((6, 5, 3), matrix.dtype) mat_op = MatrixOperator(dense_matrix, domain=dom, axis=2) assert mat_op.range == ran mat_op = MatrixOperator(dense_matrix, domain=dom, range=ran, axis=2) assert mat_op.range == ran with pytest.raises(ValueError): bad_dom = odl.tensor_space((6, 6, 6), matrix.dtype) # wrong shape MatrixOperator(dense_matrix, domain=bad_dom) with pytest.raises(ValueError): dom = odl.tensor_space((6, 5, 4), matrix.dtype) bad_ran = odl.tensor_space((6, 6, 6), matrix.dtype) # wrong shape MatrixOperator(dense_matrix, domain=dom, range=bad_ran) with pytest.raises(ValueError): MatrixOperator(dense_matrix, domain=dom, axis=1) with pytest.raises(ValueError): MatrixOperator(dense_matrix, domain=dom, axis=0) with pytest.raises(ValueError): bad_ran = odl.tensor_space((6, 3, 4), matrix.dtype) MatrixOperator(dense_matrix, domain=dom, range=bad_ran, axis=2) with pytest.raises(ValueError): bad_dom_for_sparse = odl.rn((6, 5, 4)) MatrixOperator(sparse_matrix, domain=bad_dom_for_sparse, axis=2) # Init with uniform_discr space (subclass of TensorSpace) dom = odl.uniform_discr(0, 1, 4, dtype=dense_matrix.dtype) ran = odl.uniform_discr(0, 1, 3, dtype=dense_matrix.dtype) MatrixOperator(dense_matrix, domain=dom, range=ran) # Make sure this runs and returns something string-like assert str(mat_op) > '' assert repr(mat_op) > ''
def test_mat_op_init_and_basic_properties(): """Test initialization and basic properties of MatrixOperator.""" # Test default domain and range r2 = odl.rn(2) op_real = MatrixOperator([[1.0, 2], [-1, 0.5]]) assert op_real.domain == r2 assert op_real.range == r2 c2 = odl.cn(2) op_complex = MatrixOperator([[1.0, 2 + 1j], [-1 - 1j, 0.5]]) assert op_complex.domain == c2 assert op_complex.range == c2 int2 = odl.fn(2, dtype=int) op_int = MatrixOperator([[1, 2], [-1, 0]]) assert op_int.domain == int2 assert op_int.range == int2 # Rectangular rect_mat = 2 * np.eye(2, 3) r3 = odl.rn(3) op = MatrixOperator(rect_mat) assert op.domain == r3 assert op.range == r2 MatrixOperator(rect_mat, domain=r3, range=r2) with pytest.raises(ValueError): MatrixOperator(rect_mat, domain=r2, range=r2) with pytest.raises(ValueError): MatrixOperator(rect_mat, domain=r3, range=r3) with pytest.raises(ValueError): MatrixOperator(rect_mat, domain=r2, range=r3) # Rn to Cn okay MatrixOperator(rect_mat, domain=r3, range=odl.cn(2)) # Cn to Rn not okay (no safe cast) with pytest.raises(TypeError): MatrixOperator(rect_mat, domain=odl.cn(3), range=r2) # Complex matrix between real spaces not okay rect_complex_mat = rect_mat + 1j with pytest.raises(TypeError): MatrixOperator(rect_complex_mat, domain=r3, range=r2) # Init with array-like structure (including numpy.matrix) op = MatrixOperator(rect_mat, domain=r3, range=r2) assert isinstance(op.matrix, np.ndarray) op = MatrixOperator(np.asmatrix(rect_mat), domain=r3, range=r2) assert isinstance(op.matrix, np.ndarray) op = MatrixOperator(rect_mat.tolist(), domain=r3, range=r2) assert isinstance(op.matrix, np.ndarray) assert not op.matrix_issparse sparse_mat = _sparse_matrix(odl.rn(5)) op = MatrixOperator(sparse_mat, domain=odl.rn(5), range=odl.rn(5)) assert isinstance(op.matrix, scipy.sparse.spmatrix) assert op.matrix_issparse # Init with uniform_discr space (subclass of FnBase) dom = odl.uniform_discr(0, 1, 3) ran = odl.uniform_discr(0, 1, 2) MatrixOperator(rect_mat, domain=dom, range=ran)
copy=False) # Make symmetric and positive definite return mat + mat.conj().T + fn.size * np.eye(fn.size, dtype=fn.dtype) def _sparse_matrix(fn): """Create a sparse positive definite Hermitian matrix for `fn`.""" return scipy.sparse.coo_matrix(_dense_matrix(fn)) exponent = simple_fixture('exponent', [2.0, 1.0, float('inf'), 3.5, 1.5]) fn = simple_fixture('fn', [ odl.rn(10, np.float64), odl.rn(10, np.float32), odl.cn(10, np.complex128), odl.cn(10, np.complex64), odl.rn(100), odl.uniform_discr(0, 1, 5) ]) # ---- PointwiseNorm ---- def test_pointwise_norm_init_properties(): # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1, exponent=1) # Make sure the code runs and test the properties pwnorm = PointwiseNorm(vfspace)
"""Run the standardized diagonstics suite on some of the spaces.""" import odl print('\n\n TESTING FOR Lp SPACE \n\n') discr = odl.uniform_discr(0, 1, 10) odl.diagnostics.SpaceTest(discr).run_tests() print('\n\n TESTING FOR rn SPACE \n\n') spc = odl.rn(10) odl.diagnostics.SpaceTest(spc).run_tests() print('\n\n TESTING FOR cn SPACE \n\n') spc = odl.cn(10) odl.diagnostics.SpaceTest(spc).run_tests() if 'cuda' in odl.fn_impl_names(): print('\n\n TESTING FOR CUDA rn SPACE \n\n') spc = odl.rn(10, impl='cuda') odl.diagnostics.SpaceTest(spc, tol=0.0001).run_tests()
values[pepper_indices] = -low_val values = values.reshape(vector.space.shape) return vector.space.element(values) if __name__ == '__main__': # Show the phantoms import odl from odl.util.testutils import run_doctests r100 = odl.rn(100) white_noise(r100).show('white_noise') uniform_noise(r100).show('uniform_noise') white_noise(r100, mean=5).show('white_noise with mean') c100 = odl.cn(100) white_noise(c100).show('complex white_noise') uniform_noise(c100).show('complex uniform_noise') discr = odl.uniform_discr([-1, -1], [1, 1], [300, 300]) white_noise(discr).show('white_noise 2d') uniform_noise(discr).show('uniform_noise 2d') vector = odl.phantom.shepp_logan(discr, modified=True) poisson_noise(vector * 100).show('poisson_noise 2d') salt_pepper_noise(vector).show('salt_pepper_noise 2d') # Run also the doctests run_doctests()
# Copyright 2014-2017 The ODL contributors # # This file is part of ODL. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. from __future__ import division import pytest import odl from odl.util.testutils import simple_fixture, noise_element # --- pytest fixtures --- # hilbert_spaces = [odl.rn(3), odl.cn(3), odl.uniform_discr(0, 1, 3)] normed_spaces = [odl.rn(3, exponent=1)] + hilbert_spaces metric_spaces = normed_spaces linear_spaces = metric_spaces hilbert_space = simple_fixture('hilbert_space', hilbert_spaces) normed_space = simple_fixture('normed_space', normed_spaces) metric_space = simple_fixture('metric_space', metric_spaces) linear_space = simple_fixture('linear_space', linear_spaces) # --- LinearSpace tests --- # def test_hash(linear_space): """Verify that hashing spaces works but elements doesnt.""" hsh = hash(linear_space)
# # You should have received a copy of the GNU General Public License # along with ODL. If not, see <http://www.gnu.org/licenses/>. """Run the standardized diagonstics suite on some of the spaces.""" import odl print('\n\n TESTING FOR Lp SPACE \n\n') discr = odl.uniform_discr(0, 1, 10) odl.diagnostics.SpaceTest(discr).run_tests() print('\n\n TESTING FOR rn SPACE \n\n') spc = odl.rn(10) odl.diagnostics.SpaceTest(spc).run_tests() print('\n\n TESTING FOR cn SPACE \n\n') spc = odl.cn(10) odl.diagnostics.SpaceTest(spc).run_tests() if 'cuda' in odl.FN_IMPLS: print('\n\n TESTING FOR CUDA rn SPACE \n\n') spc = odl.rn(10, impl='cuda') odl.diagnostics.SpaceTest(spc, tol=0.0001).run_tests()
int(fraction * vector.size)] values[salt_indices] = high_val values[pepper_indices] = -low_val return vector.space.element(values) if __name__ == '__main__': # Show the phantoms import odl r100 = odl.rn(100) white_noise(r100).show('white_noise') white_noise(r100, mean=5).show('white_noise with mean') c100 = odl.cn(100) white_noise(c100).show('complex white_noise') discr = odl.uniform_discr([-1, -1], [1, 1], [300, 300]) white_noise(discr).show('white_noise 2d') vector = odl.phantom.shepp_logan(discr, modified=True) poisson_noise(vector * 100).show('poisson_noise 2d') salt_pepper_noise(vector).show('salt_pepper_noise 2d') # Run also the doctests # pylint: disable=wrong-import-position from odl.util.testutils import run_doctests run_doctests()