Пример #1
0
def test_constant_equals():
    n = 10
    constant = 1.5

    w_const = NumpyFnConstWeighting(constant)
    w_const2 = NumpyFnConstWeighting(constant)
    w_other_const = NumpyFnConstWeighting(constant + 1)
    w_other_exp = NumpyFnConstWeighting(constant, exponent=1)

    const_sparse_mat = scipy.sparse.dia_matrix(([constant] * n, [0]),
                                               shape=(n, n))
    const_dense_mat = constant * np.eye(n)
    w_matrix_sp = NumpyFnMatrixWeighting(const_sparse_mat)
    w_matrix_de = NumpyFnMatrixWeighting(const_dense_mat)

    assert w_const == w_const
    assert w_const == w_const2
    assert w_const2 == w_const
    # Equivalent but not equal -> False
    assert w_const != w_matrix_sp
    assert w_const != w_matrix_de

    # Different
    assert w_const != w_other_const
    assert w_const != w_other_exp
Пример #2
0
def test_constant_init(exponent):
    constant = 1.5

    # Just test if the code runs
    NumpyFnConstWeighting(constant, exponent=exponent)

    with pytest.raises(ValueError):
        NumpyFnConstWeighting(0)
    with pytest.raises(ValueError):
        NumpyFnConstWeighting(-1)
    with pytest.raises(ValueError):
        NumpyFnConstWeighting(float('inf'))
Пример #3
0
def test_constant_dist(fn, exponent):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)

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

    w_const = NumpyFnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.dist(x, y), true_dist)

    # With free function
    w_const_dist = npy_weighted_dist(constant, exponent=exponent)
    assert almost_equal(w_const_dist(x, y), true_dist)
Пример #4
0
def test_constant_norm(fn, exponent):
    xarr, x = noise_elements(fn)

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

    w_const = NumpyFnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.norm(x), true_norm)

    # With free function
    w_const_norm = npy_weighted_norm(constant, exponent=exponent)
    assert almost_equal(w_const_norm(x), true_norm)
Пример #5
0
def test_const_dist_using_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)

    constant = 1.5
    w = NumpyFnConstWeighting(constant)

    true_dist = np.sqrt(constant) * 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)

    # Only possible for exponent=2
    with pytest.raises(ValueError):
        NumpyFnConstWeighting(constant, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = npy_weighted_dist(constant, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist, places=3)
Пример #6
0
def test_init_weighting(exponent):
    const = 1.5
    weight_arr = _pos_array(odl.rn(3, float))
    weight_mat = _dense_matrix(odl.rn(3, float))

    spaces = [NumpyFn(3, complex, exponent=exponent, weighting=const),
              NumpyFn(3, complex, exponent=exponent, weighting=weight_arr),
              NumpyFn(3, complex, exponent=exponent, weighting=weight_mat)]
    weightings = [NumpyFnConstWeighting(const, exponent=exponent),
                  NumpyFnArrayWeighting(weight_arr, exponent=exponent),
                  NumpyFnMatrixWeighting(weight_mat, exponent=exponent)]

    for spc, weight in zip(spaces, weightings):
        assert spc.weighting == weight
Пример #7
0
def test_constant_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)

    constant = 1.5
    true_result_const = constant * np.vdot(yarr, xarr)

    w_const = NumpyFnConstWeighting(constant)
    assert almost_equal(w_const.inner(x, y), true_result_const)

    # Exponent != 2 -> no inner
    w_const = NumpyFnConstWeighting(constant, exponent=1)
    with pytest.raises(NotImplementedError):
        w_const.inner(x, y)
Пример #8
0
def test_array_equiv():
    rn = odl.rn(5)
    weight_arr = _pos_array(rn)
    weight_elem = rn.element(weight_arr)
    diag_mat = weight_arr * np.eye(5)
    different_vec = weight_arr - 1

    w_vec = NumpyFnArrayWeighting(weight_arr)
    w_elem = NumpyFnArrayWeighting(weight_elem)
    w_diag_mat = NumpyFnMatrixWeighting(diag_mat)
    w_different_vec = NumpyFnArrayWeighting(different_vec)

    # Equal -> True
    assert w_vec.equiv(w_vec)
    assert w_vec.equiv(w_elem)
    # Equivalent matrix -> True
    assert w_vec.equiv(w_diag_mat)
    # Different vector -> False
    assert not w_vec.equiv(w_different_vec)

    # Test shortcuts
    const_vec = np.ones(5) * 1.5

    w_vec = NumpyFnArrayWeighting(const_vec)
    w_const = NumpyFnConstWeighting(1.5)
    w_wrong_const = NumpyFnConstWeighting(1)
    w_wrong_exp = NumpyFnConstWeighting(1.5, exponent=1)

    assert w_vec.equiv(w_const)
    assert not w_vec.equiv(w_wrong_const)
    assert not w_vec.equiv(w_wrong_exp)

    # Bogus input
    assert not w_vec.equiv(True)
    assert not w_vec.equiv(object)
    assert not w_vec.equiv(None)
Пример #9
0
def test_constant_equiv():
    n = 10
    constant = 1.5

    w_const = NumpyFnConstWeighting(constant)
    w_const2 = NumpyFnConstWeighting(constant)

    const_sparse_mat = scipy.sparse.dia_matrix(([constant] * n, [0]),
                                               shape=(n, n))
    const_dense_mat = constant * np.eye(n)
    w_matrix_sp = NumpyFnMatrixWeighting(const_sparse_mat)
    w_matrix_de = NumpyFnMatrixWeighting(const_dense_mat)

    # Equal -> True
    assert w_const.equiv(w_const)
    assert w_const.equiv(w_const2)
    # Equivalent matrix representation -> True
    assert w_const.equiv(w_matrix_sp)
    assert w_const.equiv(w_matrix_de)

    w_different_const = NumpyFnConstWeighting(2.5)
    assert not w_const.equiv(w_different_const)

    # Bogus input
    assert not w_const.equiv(True)
    assert not w_const.equiv(object)
    assert not w_const.equiv(None)