示例#1
0
def test_array_array():
    rn = odl.rn(5)
    weight_arr = _pos_array(rn)
    weight_elem = rn.element(weight_arr)

    weighting_arr = NumpyFnArrayWeighting(weight_arr)
    weighting_elem = NumpyFnArrayWeighting(weight_elem)

    assert isinstance(weighting_arr.array, np.ndarray)
    assert isinstance(weighting_elem.array, NumpyFnVector)
示例#2
0
def test_array_norm(fn, exponent):
    xarr, x = noise_elements(fn)

    weight_arr = _pos_array(fn)
    weighting_arr = NumpyFnArrayWeighting(weight_arr, exponent=exponent)

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

    assert almost_equal(weighting_arr.norm(x), true_norm)

    # With free function
    pnorm_vec = npy_weighted_norm(weight_arr, exponent=exponent)
    assert almost_equal(pnorm_vec(x), true_norm)
示例#3
0
def test_array_dist_using_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)

    weight_arr = _pos_array(fn)
    w = NumpyFnArrayWeighting(weight_arr)

    true_dist = np.linalg.norm(np.sqrt(weight_arr) * (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):
        NumpyFnArrayWeighting(weight_arr, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = npy_weighted_dist(weight_arr, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist, places=3)
示例#4
0
def test_array_dist(fn, exponent):
    [xarr, yarr], [x, y] = noise_elements(fn, n=2)

    weight_arr = _pos_array(fn)
    weighting_arr = NumpyFnArrayWeighting(weight_arr, exponent=exponent)

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

    assert almost_equal(weighting_arr.dist(x, y), true_dist)

    # With free function
    pdist_vec = npy_weighted_dist(weight_arr, exponent=exponent)
    assert almost_equal(pdist_vec(x, y), true_dist)
示例#5
0
def test_array_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)

    weight_arr = _pos_array(fn)
    weighting_arr = NumpyFnArrayWeighting(weight_arr)

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

    assert almost_equal(weighting_arr.inner(x, y), true_inner)

    # With free function
    inner_vec = npy_weighted_inner(weight_arr)

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

    # Exponent != 2 -> no inner product, should raise
    with pytest.raises(NotImplementedError):
        NumpyFnArrayWeighting(weight_arr, exponent=1.0).inner(x, y)
示例#6
0
def test_matrix_equiv():
    fn = odl.rn(5)
    sparse_mat = _sparse_matrix(fn)
    sparse_mat_as_dense = sparse_mat.todense()
    dense_mat = _dense_matrix(fn)
    different_dense_mat = dense_mat.copy()
    different_dense_mat[0, 0] = -10

    w_sparse = NumpyFnMatrixWeighting(sparse_mat)
    w_sparse2 = NumpyFnMatrixWeighting(sparse_mat)
    w_sparse_as_dense = NumpyFnMatrixWeighting(sparse_mat_as_dense)
    w_dense = NumpyFnMatrixWeighting(dense_mat)
    w_dense_copy = NumpyFnMatrixWeighting(dense_mat.copy())
    w_different_dense = NumpyFnMatrixWeighting(different_dense_mat)

    # Equal -> True
    assert w_sparse.equiv(w_sparse)
    assert w_sparse.equiv(w_sparse2)
    # Equivalent matrices -> True
    assert w_sparse.equiv(w_sparse_as_dense)
    assert w_dense.equiv(w_dense_copy)
    # Different matrices -> False
    assert not w_dense.equiv(w_different_dense)

    # Test shortcuts
    sparse_eye = scipy.sparse.eye(5)
    w_eye = NumpyFnMatrixWeighting(sparse_eye)
    w_dense_eye = NumpyFnMatrixWeighting(sparse_eye.todense())
    w_eye_vec = NumpyFnArrayWeighting(np.ones(5))

    w_eye_wrong_exp = NumpyFnMatrixWeighting(sparse_eye, exponent=1)

    sparse_smaller_eye = scipy.sparse.eye(4)
    w_smaller_eye = NumpyFnMatrixWeighting(sparse_smaller_eye)

    sparse_shifted_eye = scipy.sparse.eye(5, k=1)
    w_shifted_eye = NumpyFnMatrixWeighting(sparse_shifted_eye)

    sparse_almost_eye = scipy.sparse.dia_matrix((np.ones(4), [0]), (5, 5))
    w_almost_eye = NumpyFnMatrixWeighting(sparse_almost_eye)

    assert w_eye.equiv(w_dense_eye)
    assert w_dense_eye.equiv(w_eye)
    assert w_eye.equiv(w_eye_vec)
    assert not w_eye.equiv(w_eye_wrong_exp)
    assert not w_eye.equiv(w_smaller_eye)
    assert not w_eye.equiv(w_shifted_eye)
    assert not w_smaller_eye.equiv(w_shifted_eye)
    assert not w_eye.equiv(w_almost_eye)

    # Bogus input
    assert not w_eye.equiv(True)
    assert not w_eye.equiv(object)
    assert not w_eye.equiv(None)
示例#7
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
示例#8
0
def test_array_is_valid():
    rn = odl.rn(5)
    weight_arr = _pos_array(rn)
    weighting_arr = NumpyFnArrayWeighting(weight_arr)

    assert weighting_arr.is_valid()

    # Invalid
    weight_arr[0] = 0
    weighting_arr = NumpyFnArrayWeighting(weight_arr)
    assert not weighting_arr.is_valid()
示例#9
0
def test_array_equals():
    rn = odl.rn(5)
    weight_arr = _pos_array(rn)
    weight_elem = rn.element(weight_arr)

    weighting_arr = NumpyFnArrayWeighting(weight_arr)
    weighting_arr2 = NumpyFnArrayWeighting(weight_arr)
    weighting_elem = NumpyFnArrayWeighting(weight_elem)
    weighting_elem2 = NumpyFnArrayWeighting(weight_elem)
    weighting_other_vec = NumpyFnArrayWeighting(weight_arr - 1)
    weighting_other_exp = NumpyFnArrayWeighting(weight_arr - 1, exponent=1)

    assert weighting_arr == weighting_arr2
    assert weighting_arr != weighting_elem
    assert weighting_elem == weighting_elem2
    assert weighting_arr != weighting_other_vec
    assert weighting_arr != weighting_other_exp
示例#10
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)
示例#11
0
def test_array_init(exponent):
    rn = odl.rn(5)
    weight_arr = _pos_array(rn)

    NumpyFnArrayWeighting(weight_arr, exponent=exponent)
    NumpyFnArrayWeighting(rn.element(weight_arr), exponent=exponent)