コード例 #1
0
ファイル: ntuples_test.py プロジェクト: rajmund/odl
def test_dist(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, n=2)

    correct_dist = np.linalg.norm(xarr - yarr)

    assert almost_equal(fn.dist(x, y), correct_dist)
    assert almost_equal(x.dist(y), correct_dist)
コード例 #2
0
def test_const_norm(exponent):
    rn = CudaRn(5)
    xarr, x = example_vectors(rn)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant, exponent=exponent)

    factor = 1 if exponent == float('inf') else constant ** (1 / exponent)
    true_norm = factor * np.linalg.norm(xarr, ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.norm(x)
    else:
        assert almost_equal(weighting.norm(x), true_norm)

    # Same with free function
    pnorm = odl.cu_weighted_norm(constant, exponent=exponent)
    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pnorm(x)
    else:
        assert almost_equal(pnorm(x), true_norm)
コード例 #3
0
ファイル: ntuples_test.py プロジェクト: rajmund/odl
def test_norm(fn):
    xarr, x = example_vectors(fn)

    correct_norm = np.linalg.norm(xarr)

    assert almost_equal(fn.norm(x), correct_norm)
    assert almost_equal(x.norm(), correct_norm)
コード例 #4
0
ファイル: ntuples_test.py プロジェクト: wjp/odl
def test_pnorm(exponent):
    for fn in (Rn(3, exponent=exponent), Cn(3, exponent=exponent)):
        xarr, x = _vectors(fn)
        correct_norm = np.linalg.norm(xarr, ord=exponent)

        assert almost_equal(fn.norm(x), correct_norm)
        assert almost_equal(x.norm(), correct_norm)
コード例 #5
0
def test_vector_dist(exponent):
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, n=2)

    weight = _pos_vector(CudaRn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr),
                                   ord=exponent)
    else:
        true_dist = np.linalg.norm(
            weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.dist(x, y)
    else:
        assert almost_equal(weighting.dist(x, y), true_dist)

    # Same with free function
    pdist = odl.cu_weighted_dist(weight, exponent=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist(x, y)
    else:
        assert almost_equal(pdist(x, y), true_dist)
コード例 #6
0
ファイル: ntuples_test.py プロジェクト: wjp/odl
def test_norm(fn):
    xarr, x = _vectors(fn)

    correct_norm = np.linalg.norm(xarr)

    assert almost_equal(fn.norm(x), correct_norm)
    assert almost_equal(x.norm(), correct_norm)
コード例 #7
0
def test_vector_norm(exponent):
    rn = CudaRn(5)
    xarr, x = example_vectors(rn)

    weight = _pos_vector(CudaRn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_norm = np.linalg.norm(weight.asarray() * xarr, ord=exponent)
    else:
        true_norm = np.linalg.norm(weight.asarray() ** (1 / exponent) * xarr,
                                   ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.norm(x)
    else:
        assert almost_equal(weighting.norm(x), true_norm)

    # Same with free function
    pnorm = odl.cu_weighted_norm(weight, exponent=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pnorm(x)
    else:
        assert almost_equal(pnorm(x), true_norm)
コード例 #8
0
ファイル: ntuples_test.py プロジェクト: wjp/odl
def test_custom_dist(fn):
    xarr, yarr, x, y = _vectors(fn, 2)

    def dist(x, y):
        return np.linalg.norm(x - y)

    def other_dist(x, y):
        return np.linalg.norm(x - y, ord=1)

    w = FnCustomDist(dist)
    w_same = FnCustomDist(dist)
    w_other = FnCustomDist(other_dist)

    assert w == w
    assert w == w_same
    assert w != w_other

    with pytest.raises(NotImplementedError):
        w.inner(x, y)

    with pytest.raises(NotImplementedError):
        w.norm(x)

    true_dist = np.linalg.norm(xarr - yarr)
    assert almost_equal(w.dist(x, y), true_dist)
    assert almost_equal(w.dist(x, x), 0)

    with pytest.raises(TypeError):
        FnCustomDist(1)
コード例 #9
0
ファイル: ntuples_test.py プロジェクト: wjp/odl
def test_inner(fn):
    xd = _element(fn)
    yd = _element(fn)

    correct_inner = np.vdot(yd, xd)
    assert almost_equal(fn.inner(xd, yd), correct_inner)
    assert almost_equal(xd.inner(yd), correct_inner)
コード例 #10
0
ファイル: cu_ntuples_test.py プロジェクト: LarsWestergren/odl
def test_vector_inner():
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, 2)

    weight_vec = _pos_array(odl.Rn(5))
    weight_elem = rn.element(weight_vec)

    weighting_vec = CudaFnVectorWeighting(weight_vec)
    weighting_elem = CudaFnVectorWeighting(weight_elem)

    true_inner = np.vdot(yarr, xarr * weight_vec)

    assert almost_equal(weighting_vec.inner(x, y), true_inner)
    assert almost_equal(weighting_elem.inner(x, y), true_inner)

    # Same with free function
    inner_vec = odl.cu_weighted_inner(weight_vec)
    inner_elem = odl.cu_weighted_inner(weight_elem)

    assert almost_equal(inner_vec(x, y), true_inner)
    assert almost_equal(inner_elem(x, y), true_inner)

    # Exponent != 2 -> no inner product, should raise
    with pytest.raises(NotImplementedError):
        CudaFnVectorWeighting(weight_vec, exponent=1.0).inner(x, y)
コード例 #11
0
ファイル: pspace_test.py プロジェクト: LarsWestergren/odl
def test_norm():
    H = odl.Rn(2)
    v1 = H.element([1, 2])
    v2 = H.element([5, 3])

    # 1-norm
    HxH = odl.ProductSpace(H, H, ord=1.0)
    w = HxH.element([v1, v2])
    assert almost_equal(HxH.norm(w), H.norm(v1) + H.norm(v2))

    # 2-norm
    HxH = odl.ProductSpace(H, H, ord=2.0)
    w = HxH.element([v1, v2])
    assert almost_equal(
        HxH.norm(w), (H.norm(v1) ** 2 + H.norm(v2) ** 2) ** (1 / 2.0))

    # -inf norm
    HxH = odl.ProductSpace(H, H, ord=-float('inf'))
    w = HxH.element([v1, v2])
    assert almost_equal(HxH.norm(w), min(H.norm(v1), H.norm(v2)))

    # inf norm
    HxH = odl.ProductSpace(H, H, ord=float('inf'))
    w = HxH.element([v1, v2])
    assert almost_equal(HxH.norm(w), max(H.norm(v1), H.norm(v2)))

    # Custom norm
    def my_norm(x):
        return np.sum(x)  # Same as 1-norm
    HxH = odl.ProductSpace(H, H, prod_norm=my_norm)
    w = HxH.element([v1, v2])
    assert almost_equal(HxH.norm(w), H.norm(v1) + H.norm(v2))
コード例 #12
0
ファイル: cu_ntuples_test.py プロジェクト: wjp/odl
def test_const_dist(exponent):
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, n=2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant, exponent=exponent)

    factor = 1 if exponent == float('inf') else constant ** (1 / exponent)
    true_dist = factor * np.linalg.norm(xarr - yarr, ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.dist(x, y)
    else:
        assert almost_equal(weighting.dist(x, y), true_dist)

    # Same with free function
    pdist = odl.cu_weighted_dist(constant, exponent=exponent)
    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist(x, y)
    else:
        assert almost_equal(pdist(x, y), true_dist)
コード例 #13
0
def test_quadratic_form(space):
    """Test the quadratic form functional."""
    operator = odl.IdentityOperator(space)
    vector = space.one()
    constant = 0.363
    func = odl.solvers.QuadraticForm(operator, vector, constant)

    x = noise_element(space)

    # Checking that values is stored correctly
    assert func.operator == operator
    assert func.vector == vector
    assert func.constant == constant

    # Evaluation of the functional
    expected_result = x.inner(operator(x)) + vector.inner(x) + constant
    assert almost_equal(func(x), expected_result)

    # Also test with some values as none
    func_no_offset = odl.solvers.QuadraticForm(operator, constant=constant)
    expected_result = x.inner(operator(x)) + constant
    assert almost_equal(func_no_offset(x), expected_result)

    func_no_operator = odl.solvers.QuadraticForm(vector=vector,
                                                 constant=constant)
    expected_result = vector.inner(x) + constant
    assert almost_equal(func_no_operator(x), expected_result)

    # The gradient
    expected_gradient = 2 * operator(x) + vector
    assert all_almost_equal(func.gradient(x), expected_gradient)

    # The convex conjugate
    assert isinstance(func.convex_conj, odl.solvers.QuadraticForm)
コード例 #14
0
ファイル: cu_ntuples_test.py プロジェクト: wjp/odl
def test_vector_norm(exponent):
    rn = odl.CudaRn(5)
    xarr, x = _vectors(rn)

    weight = _pos_vector(odl.CudaRn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_norm = np.linalg.norm(weight.asarray() * xarr, ord=exponent)
    else:
        true_norm = np.linalg.norm(weight.asarray() ** (1 / exponent) * xarr,
                                   ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.norm(x)
    else:
        assert almost_equal(weighting.norm(x), true_norm)

    # Same with free function
    pnorm = odl.cu_weighted_norm(weight, exponent=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pnorm(x)
    else:
        assert almost_equal(pnorm(x), true_norm)
コード例 #15
0
ファイル: cu_ntuples_test.py プロジェクト: wjp/odl
def test_vector_dist(exponent):
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, n=2)

    weight = _pos_vector(odl.CudaRn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr),
                                   ord=exponent)
    else:
        true_dist = np.linalg.norm(
            weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.dist(x, y)
    else:
        assert almost_equal(weighting.dist(x, y), true_dist)

    # Same with free function
    pdist = odl.cu_weighted_dist(weight, exponent=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist(x, y)
    else:
        assert almost_equal(pdist(x, y), true_dist)
コード例 #16
0
ファイル: cu_ntuples_test.py プロジェクト: wjp/odl
def test_custom_norm(fn):
    xarr, yarr, x, y = _vectors(fn, 2)

    norm = np.linalg.norm

    def other_norm(x):
        return np.linalg.norm(x, ord=1)

    w = CudaFnCustomNorm(norm)
    w_same = CudaFnCustomNorm(norm)
    w_other = CudaFnCustomNorm(other_norm)

    assert w == w
    assert w == w_same
    assert w != w_other

    with pytest.raises(NotImplementedError):
        w.inner(x, y)

    true_norm = np.linalg.norm(xarr)
    assert almost_equal(w.norm(x), true_norm)

    true_dist = np.linalg.norm(xarr - yarr)
    assert almost_equal(w.dist(x, y), true_dist)
    assert almost_equal(w.dist(x, x), 0)

    with pytest.raises(TypeError):
        CudaFnCustomNorm(1)
コード例 #17
0
def test_metric():
    H = odl.rn(2)
    v11 = H.element([1, 2])
    v12 = H.element([5, 3])

    v21 = H.element([1, 2])
    v22 = H.element([8, 9])

    # 1-norm
    HxH = odl.ProductSpace(H, H, exponent=1.0)
    w1 = HxH.element([v11, v12])
    w2 = HxH.element([v21, v22])
    assert almost_equal(HxH.dist(w1, w2),
                        H.dist(v11, v21) + H.dist(v12, v22))

    # 2-norm
    HxH = odl.ProductSpace(H, H, exponent=2.0)
    w1 = HxH.element([v11, v12])
    w2 = HxH.element([v21, v22])
    assert almost_equal(
        HxH.dist(w1, w2),
        (H.dist(v11, v21) ** 2 + H.dist(v12, v22) ** 2) ** (1 / 2.0))

    # inf norm
    HxH = odl.ProductSpace(H, H, exponent=float('inf'))
    w1 = HxH.element([v11, v12])
    w2 = HxH.element([v21, v22])
    assert almost_equal(
        HxH.dist(w1, w2),
        max(H.dist(v11, v21), H.dist(v12, v22)))
コード例 #18
0
ファイル: ntuples_test.py プロジェクト: wjp/odl
def test_dist(fn):
    xarr, yarr, x, y = _vectors(fn, n=2)

    correct_dist = np.linalg.norm(xarr - yarr)

    assert almost_equal(fn.dist(x, y), correct_dist)
    assert almost_equal(x.dist(y), correct_dist)
コード例 #19
0
def test_const_dist(exponent):
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, n=2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant, exponent=exponent)

    factor = 1 if exponent == float('inf') else constant ** (1 / exponent)
    true_dist = factor * np.linalg.norm(xarr - yarr, ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.dist(x, y)
    else:
        assert almost_equal(weighting.dist(x, y), true_dist)

    # Same with free function
    pdist = odl.cu_weighted_dist(constant, exponent=exponent)
    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist(x, y)
    else:
        assert almost_equal(pdist(x, y), true_dist)
コード例 #20
0
def test_custom_inner(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    def inner(x, y):
        return np.vdot(y, x)

    w = CudaFnCustomInnerProduct(inner)
    w_same = CudaFnCustomInnerProduct(inner)
    w_other = CudaFnCustomInnerProduct(np.dot)
    w_d = CudaFnCustomInnerProduct(inner, dist_using_inner=False)

    assert w == w
    assert w == w_same
    assert w != w_other
    assert w != w_d

    true_inner = inner(xarr, yarr)
    assert almost_equal(w.inner(x, y), true_inner)

    true_norm = np.linalg.norm(xarr)
    assert almost_equal(w.norm(x), true_norm)

    true_dist = np.linalg.norm(xarr - yarr)
    # Using 3 places (single precision default) since the result is always
    # double even if the underlying computation was only single precision
    assert almost_equal(w.dist(x, y), true_dist, places=3)
    assert almost_equal(w_d.dist(x, y), true_dist)

    with pytest.raises(TypeError):
        CudaFnCustomInnerProduct(1)
コード例 #21
0
ファイル: ntuples_test.py プロジェクト: rajmund/odl
def test_pnorm(exponent):
    for fn in (Rn(3, exponent=exponent), Cn(3, exponent=exponent)):
        xarr, x = example_vectors(fn)
        correct_norm = np.linalg.norm(xarr, ord=exponent)

        assert almost_equal(fn.norm(x), correct_norm)
        assert almost_equal(x.norm(), correct_norm)
コード例 #22
0
def test_dist(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, n=2)

    correct_dist = np.linalg.norm(xarr - yarr)

    assert almost_equal(fn.dist(x, y), correct_dist)
    assert almost_equal(x.dist(y), correct_dist)
コード例 #23
0
def test_norm(fn):
    xarr, x = noise_elements(fn)

    correct_norm = np.linalg.norm(xarr)

    assert almost_equal(fn.norm(x), correct_norm)
    assert almost_equal(x.norm(), correct_norm)
コード例 #24
0
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)
コード例 #25
0
def test_inner(fn):
    xd = noise_element(fn)
    yd = noise_element(fn)

    correct_inner = np.vdot(yd, xd)
    assert almost_equal(fn.inner(xd, yd), correct_inner)
    assert almost_equal(xd.inner(yd), correct_inner)
コード例 #26
0
ファイル: pspace_test.py プロジェクト: NikEfth/odl
def test_metric():
    H = odl.Rn(2)
    v11 = H.element([1, 2])
    v12 = H.element([5, 3])

    v21 = H.element([1, 2])
    v22 = H.element([8, 9])

    # 1-norm
    HxH = odl.ProductSpace(H, H, exponent=1.0)
    w1 = HxH.element([v11, v12])
    w2 = HxH.element([v21, v22])
    assert almost_equal(HxH.dist(w1, w2),
                        H.dist(v11, v21) + H.dist(v12, v22))

    # 2-norm
    HxH = odl.ProductSpace(H, H, exponent=2.0)
    w1 = HxH.element([v11, v12])
    w2 = HxH.element([v21, v22])
    assert almost_equal(
        HxH.dist(w1, w2),
        (H.dist(v11, v21) ** 2 + H.dist(v12, v22) ** 2) ** (1 / 2.0))

    # inf norm
    HxH = odl.ProductSpace(H, H, exponent=float('inf'))
    w1 = HxH.element([v11, v12])
    w2 = HxH.element([v21, v22])
    assert almost_equal(
        HxH.dist(w1, w2),
        max(H.dist(v11, v21), H.dist(v12, v22)))
コード例 #27
0
def test_custom_norm(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    norm = np.linalg.norm

    def other_norm(x):
        return np.linalg.norm(x, ord=1)

    w = CudaFnCustomNorm(norm)
    w_same = CudaFnCustomNorm(norm)
    w_other = CudaFnCustomNorm(other_norm)

    assert w == w
    assert w == w_same
    assert w != w_other

    with pytest.raises(NotImplementedError):
        w.inner(x, y)

    true_norm = np.linalg.norm(xarr)
    assert almost_equal(w.norm(x), true_norm)

    true_dist = np.linalg.norm(xarr - yarr)
    assert almost_equal(w.dist(x, y), true_dist)

    with pytest.raises(TypeError):
        CudaFnCustomNorm(1)
コード例 #28
0
def test_custom_funcs():
    # Checking the standard 1-norm and standard inner product, just to
    # see that the functions are handled correctly.

    r2 = odl.rn(2)
    r2x = r2.element([1, -1])
    r2y = r2.element([-2, 3])
    # inner = -5, dist = 5, norms = (sqrt(2), sqrt(13))

    r3 = odl.rn(3)
    r3x = r3.element([3, 4, 4])
    r3y = r3.element([1, -2, 1])
    # inner = -1, dist = 7, norms = (sqrt(41), sqrt(6))

    pspace_2 = odl.ProductSpace(r2, r3, exponent=2.0)
    x = pspace_2.element((r2x, r3x))
    y = pspace_2.element((r2y, r3y))

    pspace_custom = odl.ProductSpace(r2, r3, inner=custom_inner)
    xc = pspace_custom.element((r2x, r3x))
    yc = pspace_custom.element((r2y, r3y))
    assert almost_equal(x.inner(y), xc.inner(yc))

    pspace_1 = odl.ProductSpace(r2, r3, exponent=1.0)
    x = pspace_1.element((r2x, r3x))
    y = pspace_1.element((r2y, r3y))

    pspace_custom = odl.ProductSpace(r2, r3, norm=custom_norm)
    xc = pspace_custom.element((r2x, r3x))
    assert almost_equal(x.norm(), xc.norm())

    pspace_custom = odl.ProductSpace(r2, r3, dist=custom_dist)
    xc = pspace_custom.element((r2x, r3x))
    yc = pspace_custom.element((r2y, r3y))
    assert almost_equal(x.dist(y), xc.dist(yc))

    with pytest.raises(TypeError):
        odl.ProductSpace(r2, r3, a=1)  # extra keyword argument

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, inner=custom_inner)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, dist=custom_dist, inner=custom_inner)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, dist=custom_dist)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, exponent=1.0)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, weighting=2.0)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, dist=custom_dist, weighting=2.0)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, inner=custom_inner, weighting=2.0)
コード例 #29
0
ファイル: pspace_test.py プロジェクト: NikEfth/odl
def test_custom_funcs():
    # Checking the standard 1-norm and standard inner product, just to
    # see that the functions are handled correctly.

    r2 = odl.Rn(2)
    r2x = r2.element([1, -1])
    r2y = r2.element([-2, 3])
    # inner = -5, dist = 5, norms = (sqrt(2), sqrt(13))

    r3 = odl.Rn(3)
    r3x = r3.element([3, 4, 4])
    r3y = r3.element([1, -2, 1])
    # inner = -1, dist = 7, norms = (sqrt(41), sqrt(6))

    pspace_2 = odl.ProductSpace(r2, r3, exponent=2.0)
    x = pspace_2.element((r2x, r3x))
    y = pspace_2.element((r2y, r3y))

    pspace_custom = odl.ProductSpace(r2, r3, inner=custom_inner)
    xc = pspace_custom.element((r2x, r3x))
    yc = pspace_custom.element((r2y, r3y))
    assert almost_equal(x.inner(y), xc.inner(yc))

    pspace_1 = odl.ProductSpace(r2, r3, exponent=1.0)
    x = pspace_1.element((r2x, r3x))
    y = pspace_1.element((r2y, r3y))

    pspace_custom = odl.ProductSpace(r2, r3, norm=custom_norm)
    xc = pspace_custom.element((r2x, r3x))
    assert almost_equal(x.norm(), xc.norm())

    pspace_custom = odl.ProductSpace(r2, r3, dist=custom_dist)
    xc = pspace_custom.element((r2x, r3x))
    yc = pspace_custom.element((r2y, r3y))
    assert almost_equal(x.dist(y), xc.dist(yc))

    with pytest.raises(TypeError):
        odl.ProductSpace(r2, r3, a=1)  # extra keyword argument

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, inner=custom_inner)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, dist=custom_dist, inner=custom_inner)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, dist=custom_dist)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, exponent=1.0)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, norm=custom_norm, weight=2.0)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, dist=custom_dist, weight=2.0)

    with pytest.raises(ValueError):
        odl.ProductSpace(r2, r3, inner=custom_inner, weight=2.0)
コード例 #30
0
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)
コード例 #31
0
ファイル: ntuples_test.py プロジェクト: wjp/odl
def test_pdist(exponent):
    for fn in (Rn(3, exponent=exponent), Cn(3, exponent=exponent)):
        xarr, yarr, x, y = _vectors(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)
コード例 #32
0
ファイル: ntuples_test.py プロジェクト: rajmund/odl
def test_pdist(exponent):
    for fn in (Rn(3, exponent=exponent), Cn(3, exponent=exponent)):
        [xarr, yarr], [x, y] = example_vectors(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)
コード例 #33
0
def test_dist(tspace):
    weighting_const = tspace.weighting.const

    [xarr, yarr], [x, y] = noise_elements(tspace, 2)

    correct_dist = np.linalg.norm(xarr - yarr) * np.sqrt(weighting_const)

    assert almost_equal(tspace.dist(x, y), correct_dist, places=2)
    assert almost_equal(x.dist(y), correct_dist, places=2)
コード例 #34
0
ファイル: fn_base_slow_test.py プロジェクト: rajmund/odl
def test_dist(fn):
    weighting = np.sqrt(fn_weighting(fn))

    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    correct_dist = np.linalg.norm(xarr - yarr) * weighting

    assert almost_equal(fn.dist(x, y), correct_dist, places=2)
    assert almost_equal(x.dist(y), correct_dist, places=2)
コード例 #35
0
def test_norm(tspace):
    weighting_const = tspace.weighting.const

    xarr, x = noise_elements(tspace)

    correct_norm = np.linalg.norm(xarr) * np.sqrt(weighting_const)

    assert almost_equal(tspace.norm(x), correct_norm, places=2)
    assert almost_equal(x.norm(), correct_norm, places=2)
コード例 #36
0
def test_norm(fn):
    weighting = np.sqrt(fn_weighting(fn))

    xarr, x = noise_elements(fn)

    correct_norm = np.linalg.norm(xarr) * weighting

    assert almost_equal(fn.norm(x), correct_norm, places=2)
    assert almost_equal(x.norm(), correct_norm, places=2)
コード例 #37
0
ファイル: fn_base_slow_test.py プロジェクト: rajmund/odl
def test_norm(fn):
    weighting = np.sqrt(fn_weighting(fn))

    xarr, x = example_vectors(fn)

    correct_norm = np.linalg.norm(xarr) * weighting

    assert almost_equal(fn.norm(x), correct_norm, places=2)
    assert almost_equal(x.norm(), correct_norm, places=2)
コード例 #38
0
ファイル: fn_base_slow_test.py プロジェクト: arcaduf/odl
def test_dist(fn):
    weighting = np.sqrt(fn_weighting(fn))

    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    correct_dist = np.linalg.norm(xarr - yarr) * weighting

    assert almost_equal(fn.dist(x, y), correct_dist, places=2)
    assert almost_equal(x.dist(y), correct_dist, places=2)
コード例 #39
0
def test_norm(fn):
    weighting = np.sqrt(fn_weighting(fn))

    xarr, x = noise_elements(fn)

    correct_norm = np.linalg.norm(xarr) * weighting

    assert almost_equal(fn.norm(x), correct_norm, places=2)
    assert almost_equal(x.norm(), correct_norm, places=2)
コード例 #40
0
ファイル: fn_base_slow_test.py プロジェクト: arcaduf/odl
def test_norm(fn):
    weighting = np.sqrt(fn_weighting(fn))

    xarr, x = example_vectors(fn)

    correct_norm = np.linalg.norm(xarr) * weighting

    assert almost_equal(fn.norm(x), correct_norm, places=2)
    assert almost_equal(x.norm(), correct_norm, places=2)
コード例 #41
0
ファイル: fn_base_slow_test.py プロジェクト: arcaduf/odl
def test_inner(fn):
    weighting = fn_weighting(fn)

    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    correct_inner = np.vdot(yarr, xarr) * weighting

    assert almost_equal(fn.inner(x, y), correct_inner, places=2)
    assert almost_equal(x.inner(y), correct_inner, places=2)
コード例 #42
0
def test_inner(tspace):
    weighting_const = tspace.weighting.const

    [xarr, yarr], [x, y] = noise_elements(tspace, 2)

    correct_inner = np.vdot(yarr, xarr) * weighting_const

    assert almost_equal(tspace.inner(x, y), correct_inner, places=2)
    assert almost_equal(x.inner(y), correct_inner, places=2)
コード例 #43
0
ファイル: fn_base_slow_test.py プロジェクト: rajmund/odl
def test_inner(fn):
    weighting = fn_weighting(fn)

    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    correct_inner = np.vdot(yarr, xarr) * weighting

    assert almost_equal(fn.inner(x, y), correct_inner, places=2)
    assert almost_equal(x.inner(y), correct_inner, places=2)
コード例 #44
0
def test_L2_norm(space, sigma):
    """Test the L2-norm."""
    func = odl.solvers.L2Norm(space)
    x = noise_element(space)
    x_norm = x.norm()

    # Test functional evaluation
    expected_result = np.sqrt((x ** 2).inner(space.one()))
    assert almost_equal(func(x), expected_result)

    # Test gradient
    if x_norm > 0:
        expected_result = x / x.norm()
        assert all_almost_equal(func.gradient(x), expected_result)

    # Verify that the gradient at zero is zero
    assert all_almost_equal(func.gradient(func.domain.zero()), space.zero())

    # Test proximal operator - expecting
    # x * (1 - sigma/||x||) if ||x|| > sigma, 0 else
    norm_less_than_sigma = 0.99 * sigma * x / x_norm
    assert all_almost_equal(func.proximal(sigma)(norm_less_than_sigma),
                            space.zero())

    norm_larger_than_sigma = 1.01 * sigma * x / x_norm
    expected_result = (norm_larger_than_sigma *
                       (1.0 - sigma / norm_larger_than_sigma.norm()))
    assert all_almost_equal(func.proximal(sigma)(norm_larger_than_sigma),
                            expected_result)

    # Test convex conjugate
    func_cc = func.convex_conj

    # Test evaluation of the convex conjugate - expecting
    # 0 if ||x|| < 1, infty else
    norm_larger_than_one = 1.01 * x / x_norm
    assert func_cc(norm_larger_than_one) == np.inf

    norm_less_than_one = 0.99 * x / x_norm
    assert func_cc(norm_less_than_one) == 0

    # Gradient of the convex conjugate (not implemeted)
    with pytest.raises(NotImplementedError):
        func_cc.gradient

    # Test the proximal of the convex conjugate - expecting
    # x if ||x||_2 < 1, x/||x|| else
    if x_norm < 1:
        expected_result = x
    else:
        expected_result = x / x_norm
    assert all_almost_equal(func_cc.proximal(sigma)(x), expected_result)

    # Verify that the biconjugate is the functional itself
    func_cc_cc = func_cc.convex_conj
    assert almost_equal(func_cc_cc(x), func(x))
コード例 #45
0
ファイル: diff_ops_test.py プロジェクト: NikEfth/odl
def test_part_deriv_cpu():
    """Discretized partial derivative."""

    with pytest.raises(TypeError):
        PartialDerivative(odl.Rn(1))

    # discretized space
    space = odl.uniform_discr([0, 0], [2, 1], DATA_2D.shape)

    # operator
    partial_0 = PartialDerivative(space, axis=0, method='central',
                                  padding_method='constant')
    partial_1 = PartialDerivative(space, axis=1, method='central',
                                  padding_method='constant')

    # discretized space vector
    vec = partial_0.domain.element(DATA_2D)

    # partial derivative
    partial_vec_0 = partial_0(vec)
    partial_vec_1 = partial_1(vec)

    # explicit calculation of finite difference

    # axis: 0
    diff_0 = np.zeros_like(DATA_2D)
    # interior: second-order accurate differences
    diff_0[1:-1, :] = (DATA_2D[2:, :] - DATA_2D[:-2, :]) / 2.0
    # boundary: second-order accurate central differences with zero padding
    diff_0[0, :] = DATA_2D[1, :] / 2.0
    diff_0[-1, :] = -DATA_2D[-2, :] / 2.0
    diff_0 /= space.cell_sides[0]

    # axis: 1
    diff_1 = np.zeros_like(DATA_2D)
    # interior: second-order accurate differences
    diff_1[:, 1:-1] = (DATA_2D[:, 2:] - DATA_2D[:, :-2]) / 2.0
    # boundary: second-order accurate central differences with zero padding
    diff_1[:, 0] = DATA_2D[:, 1] / 2.0
    diff_1[:, -1] = -DATA_2D[:, -2] / 2.0
    diff_1 /= space.cell_sides[1]

    # assert `dfe0` and `dfe1` do differ
    assert (diff_0 != diff_1).any()

    assert partial_vec_0 != partial_vec_1
    assert all_equal(partial_vec_0.asarray(), diff_0)
    assert all_equal(partial_vec_1.asarray(), diff_1)

    # adjoint operator
    vec1 = space.element(DATA_2D)
    vec2 = space.element(DATA_2D + np.random.rand(*DATA_2D.shape))
    assert almost_equal(partial_0(vec1).inner(vec2),
                        vec1.inner(partial_0.adjoint(vec2)))
    assert almost_equal(partial_1(vec1).inner(vec2),
                        vec1.inner(partial_1.adjoint(vec2)))
コード例 #46
0
ファイル: functional_test.py プロジェクト: sushmita13/odl
def test_multiplication_with_vector(space):
    """Test for multiplying a functional with a vector, both left and right."""
    # Less strict checking for single precision
    places = 3 if space.dtype == np.float32 else 5

    x = noise_element(space)
    y = noise_element(space)
    func = odl.solvers.L2NormSquared(space)

    wrong_space = odl.uniform_discr(1, 2, 10)
    y_other_space = noise_element(wrong_space)

    # Multiplication from the right. Make sure it is a
    # FunctionalRightVectorMult
    func_times_y = func * y
    assert isinstance(func_times_y, odl.solvers.FunctionalRightVectorMult)

    expected_result = func(y * x)
    assert almost_equal(func_times_y(x), expected_result, places=places)

    # Test for the gradient.
    # Explicit calculations: 2*y*y*x
    expected_result = 2.0 * y * y * x
    assert all_almost_equal(func_times_y.gradient(x),
                            expected_result,
                            places=places)

    # Test for convex_conj
    cc_func_times_y = func_times_y.convex_conj
    # Explicit calculations: 1/4 * ||x/y||_2^2
    expected_result = 1.0 / 4.0 * (x / y).norm()**2
    assert almost_equal(cc_func_times_y(x), expected_result, places=places)

    # Make sure that right muliplication is not allowed with vector from
    # another space
    with pytest.raises(TypeError):
        func * y_other_space

    # Multiplication from the left. Make sure it is a FunctionalLeftVectorMult
    y_times_func = y * func
    assert isinstance(y_times_func, odl.FunctionalLeftVectorMult)

    expected_result = y * func(x)
    assert all_almost_equal(y_times_func(x), expected_result, places=places)

    # Now, multiplication with vector from another space is ok (since it is the
    # same as scaling that vector with the scalar returned by the functional).
    y_other_times_func = y_other_space * func
    assert isinstance(y_other_times_func, odl.FunctionalLeftVectorMult)

    expected_result = y_other_space * func(x)
    assert all_almost_equal(y_other_times_func(x),
                            expected_result,
                            places=places)
コード例 #47
0
ファイル: domain_test.py プロジェクト: LarsWestergren/odl
def test_begin():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.begin, 1)

    set_ = IntervalProd(-np.inf, 0)
    assert almost_equal(set_.begin, -np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.begin, 1)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.begin, [1, 2, 3])
コード例 #48
0
def test_begin():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.begin, 1)

    set_ = IntervalProd(-np.inf, 0)
    assert almost_equal(set_.begin, -np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.begin, 1)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.begin, [1, 2, 3])
コード例 #49
0
ファイル: domain_test.py プロジェクト: LarsWestergren/odl
def test_end():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.end, 2)

    set_ = IntervalProd(0, np.inf)
    assert almost_equal(set_.end, np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.end, 2)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.end, [5, 6, 7])
コード例 #50
0
def test_max_pt():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.max_pt, 2)

    set_ = IntervalProd(0, np.inf)
    assert almost_equal(set_.max_pt, np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.max_pt, 2)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.max_pt, [5, 6, 7])
コード例 #51
0
def test_end():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.end, 2)

    set_ = IntervalProd(0, np.inf)
    assert almost_equal(set_.end, np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.end, 2)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.end, [5, 6, 7])
コード例 #52
0
ファイル: domain_test.py プロジェクト: chongchenmath/odl
def test_max_pt():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.max_pt, 2)

    set_ = IntervalProd(0, np.inf)
    assert almost_equal(set_.max_pt, np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.max_pt, 2)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.max_pt, [5, 6, 7])
コード例 #53
0
def test_min_pt():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.min_pt, 1)

    set_ = IntervalProd(-np.inf, 0)
    assert almost_equal(set_.min_pt, -np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.min_pt, 1)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.min_pt, [1, 2, 3])
コード例 #54
0
ファイル: domain_test.py プロジェクト: chongchenmath/odl
def test_min_pt():
    set_ = IntervalProd(1, 2)
    assert almost_equal(set_.min_pt, 1)

    set_ = IntervalProd(-np.inf, 0)
    assert almost_equal(set_.min_pt, -np.inf)

    set_ = IntervalProd([1], [2])
    assert almost_equal(set_.min_pt, 1)

    set_ = IntervalProd([1, 2, 3], [5, 6, 7])
    assert all_equal(set_.min_pt, [1, 2, 3])
コード例 #55
0
ファイル: functional_test.py プロジェクト: odlgroup/odl
def test_multiplication_with_vector(space):
    """Test for multiplying a functional with a vector, both left and right."""
    # Less strict checking for single precision
    places = 3 if space.dtype == np.float32 else 5

    x = noise_element(space)
    y = noise_element(space)
    func = odl.solvers.L2NormSquared(space)

    wrong_space = odl.uniform_discr(1, 2, 10)
    y_other_space = noise_element(wrong_space)

    # Multiplication from the right. Make sure it is a
    # FunctionalRightVectorMult
    func_times_y = func * y
    assert isinstance(func_times_y, odl.solvers.FunctionalRightVectorMult)

    expected_result = func(y * x)
    assert almost_equal(func_times_y(x), expected_result, places=places)

    # Test for the gradient.
    # Explicit calculations: 2*y*y*x
    expected_result = 2.0 * y * y * x
    assert all_almost_equal(func_times_y.gradient(x), expected_result,
                            places=places)

    # Test for convex_conj
    cc_func_times_y = func_times_y.convex_conj
    # Explicit calculations: 1/4 * ||x/y||_2^2
    expected_result = 1.0 / 4.0 * (x / y).norm()**2
    assert almost_equal(cc_func_times_y(x), expected_result, places=places)

    # Make sure that right muliplication is not allowed with vector from
    # another space
    with pytest.raises(TypeError):
        func * y_other_space

    # Multiplication from the left. Make sure it is a FunctionalLeftVectorMult
    y_times_func = y * func
    assert isinstance(y_times_func, odl.FunctionalLeftVectorMult)

    expected_result = y * func(x)
    assert all_almost_equal(y_times_func(x), expected_result, places=places)

    # Now, multiplication with vector from another space is ok (since it is the
    # same as scaling that vector with the scalar returned by the functional).
    y_other_times_func = y_other_space * func
    assert isinstance(y_other_times_func, odl.FunctionalLeftVectorMult)

    expected_result = y_other_space * func(x)
    assert all_almost_equal(y_other_times_func(x), expected_result,
                            places=places)
コード例 #56
0
ファイル: cu_ntuples_test.py プロジェクト: wjp/odl
def test_norm(exponent):
    r3 = odl.CudaRn(3, exponent=exponent)
    xarr, x = _vectors(r3)

    correct_norm = np.linalg.norm(xarr, ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            r3.norm(x)
            x.norm()
    else:
        assert almost_equal(r3.norm(x), correct_norm)
        assert almost_equal(x.norm(), correct_norm)
コード例 #57
0
def test_norm(exponent):
    r3 = CudaRn(3, exponent=exponent)
    xarr, x = example_vectors(r3)

    correct_norm = np.linalg.norm(xarr, ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            r3.norm(x)
            x.norm()
    else:
        assert almost_equal(r3.norm(x), correct_norm)
        assert almost_equal(x.norm(), correct_norm)
コード例 #58
0
ファイル: cu_ntuples_test.py プロジェクト: wjp/odl
def test_dist(exponent):
    r3 = odl.CudaRn(3, exponent=exponent)
    xarr, yarr, x, y = _vectors(r3, n=2)

    correct_dist = np.linalg.norm(xarr - yarr, ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            r3.dist(x, y)
        with pytest.raises(NotImplementedError):
            x.dist(y)
    else:
        assert almost_equal(r3.dist(x, y), correct_dist)
        assert almost_equal(x.dist(y), correct_dist)