Пример #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_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)
Пример #3
0
def test_matrix_matrix():
    fn = odl.rn(5)
    sparse_mat = _sparse_matrix(fn)
    dense_mat = _dense_matrix(fn)

    w_sparse = NumpyFnMatrixWeighting(sparse_mat)
    w_dense = NumpyFnMatrixWeighting(dense_mat)

    assert isinstance(w_sparse.matrix, scipy.sparse.spmatrix)
    assert isinstance(w_dense.matrix, np.ndarray)
Пример #4
0
def test_matrix_init(fn, exponent):
    sparse_mat = _sparse_matrix(fn)
    dense_mat = _dense_matrix(fn)

    # Just test if the code runs
    NumpyFnMatrixWeighting(dense_mat, exponent=exponent)
    if exponent in (1.0, 2.0, float('inf')):
        NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)
    else:
        with pytest.raises(NotImplementedError):
            NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)

    nonsquare_mat = np.eye(10, 5)
    with pytest.raises(ValueError):
        NumpyFnMatrixWeighting(nonsquare_mat)
Пример #5
0
def test_matrix_is_valid():
    fn = odl.rn(5)
    sparse_mat = _sparse_matrix(fn)
    dense_mat = _dense_matrix(fn)
    bad_mat = np.eye(5)
    bad_mat[0, 0] = 0

    w_sparse = NumpyFnMatrixWeighting(sparse_mat)
    w_dense = NumpyFnMatrixWeighting(dense_mat)
    w_bad = NumpyFnMatrixWeighting(bad_mat)

    with pytest.raises(NotImplementedError):
        w_sparse.is_valid()

    assert w_dense.is_valid()
    assert not w_bad.is_valid()
Пример #6
0
def test_matrix_equals(fn, exponent):
    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] -= 1

    if exponent in (1.0, 2.0, float('inf')):
        w_sparse = NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)
        w_sparse2 = NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)
    w_sparse_as_dense = NumpyFnMatrixWeighting(sparse_mat_as_dense,
                                               exponent=exponent)
    w_dense = NumpyFnMatrixWeighting(dense_mat, exponent=exponent)
    w_dense2 = NumpyFnMatrixWeighting(dense_mat, exponent=exponent)
    w_different_mat = NumpyFnMatrixWeighting(different_dense_mat,
                                             exponent=exponent)
    diff_exp = exponent + 1 if np.isfinite(exponent) else 1
    w_different_exp = NumpyFnMatrixWeighting(dense_mat, exponent=diff_exp)

    # Identical objects -> True
    assert w_dense == w_dense
    # Identical matrices -> True
    if exponent in (1.0, 2.0, float('inf')):
        assert w_sparse == w_sparse2
    assert w_dense == w_dense2
    # Equivalent but not identical matrices -> False
    if exponent in (1.0, 2.0, float('inf')):
        assert w_sparse != w_sparse_as_dense
        assert w_sparse_as_dense != w_sparse
    # Not equivalent -> False
    assert w_dense != w_different_mat
    # Different exponents -> False
    assert w_dense != w_different_exp
Пример #7
0
def test_matrix_dist_using_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)
    mat = _dense_matrix(fn)

    w = NumpyFnMatrixWeighting(mat, dist_using_inner=True)

    true_dist = np.sqrt(np.vdot(xarr - yarr, np.dot(mat, 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):
        NumpyFnMatrixWeighting(mat, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = npy_weighted_dist(mat, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist)
Пример #8
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
Пример #9
0
def test_matrix_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)
    sparse_mat = _sparse_matrix(fn)
    sparse_mat_as_dense = np.asarray(sparse_mat.todense())
    dense_mat = _dense_matrix(fn)

    true_inner_sparse = np.vdot(yarr, np.dot(sparse_mat_as_dense, xarr))
    true_inner_dense = np.vdot(yarr, np.dot(dense_mat, xarr))

    w_sparse = NumpyFnMatrixWeighting(sparse_mat)
    w_dense = NumpyFnMatrixWeighting(dense_mat)
    assert almost_equal(w_sparse.inner(x, y), true_inner_sparse)
    assert almost_equal(w_dense.inner(x, y), true_inner_dense)

    # With free functions
    w_sparse_inner = npy_weighted_inner(sparse_mat)
    w_dense_inner = npy_weighted_inner(dense_mat)
    assert almost_equal(w_sparse_inner(x, y), true_inner_sparse)
    assert almost_equal(w_dense_inner(x, y), true_inner_dense)

    # Exponent != 2 -> no inner
    w_dense = NumpyFnMatrixWeighting(dense_mat, exponent=1)
    with pytest.raises(NotImplementedError):
        w_dense.inner(x, y)
Пример #10
0
def test_matrix_norm(fn, exponent):
    xarr, x = noise_elements(fn)
    sparse_mat = _sparse_matrix(fn)
    sparse_mat_as_dense = np.asarray(sparse_mat.todense())
    dense_mat = _dense_matrix(fn)

    # Compute true matrix-weighted norm
    if exponent == 1.0:  # ||x||_{A,1} = ||Ax||_1
        true_norm_sparse = np.linalg.norm(np.dot(sparse_mat_as_dense, xarr),
                                          ord=exponent)
        true_norm_dense = np.linalg.norm(np.dot(dense_mat, xarr),
                                         ord=exponent)
    elif exponent == 2.0:  # ||x||_{A,2} = sqrt(<x, Ax>)
        true_norm_sparse = np.sqrt(
            np.vdot(xarr, np.dot(sparse_mat_as_dense, xarr)))
        true_norm_dense = np.sqrt(np.vdot(xarr, np.dot(dense_mat, xarr)))
    elif exponent == float('inf'):  # ||x||_{A,inf} = ||Ax||_inf
        true_norm_sparse = np.linalg.norm(sparse_mat_as_dense.dot(xarr),
                                          ord=exponent)
        true_norm_dense = np.linalg.norm(dense_mat.dot(xarr), ord=exponent)
    else:  # ||x||_{A,p} = ||A^{1/p} x||_p
        # Calculate matrix power
        eigval, eigvec = scipy.linalg.eigh(dense_mat)
        eigval **= 1.0 / exponent
        mat_pow = (eigval * eigvec).dot(eigvec.conj().T)
        true_norm_dense = np.linalg.norm(np.dot(mat_pow, xarr), ord=exponent)

    # Test weighting
    if exponent in (1.0, 2.0, float('inf')):
        w_sparse = NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse.norm(x), true_norm_sparse)

    w_dense = NumpyFnMatrixWeighting(dense_mat, exponent=exponent)
    assert almost_equal(w_dense.norm(x), true_norm_dense)

    # With free functions
    if exponent not in (1.0, 2.0, float('inf')):
        with pytest.raises(NotImplementedError):
            npy_weighted_norm(sparse_mat, exponent=exponent)
    else:
        w_sparse_norm = npy_weighted_norm(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse_norm(x), true_norm_sparse)

    w_dense_norm = npy_weighted_norm(dense_mat, exponent=exponent)
    assert almost_equal(w_dense_norm(x), true_norm_dense)
Пример #11
0
def test_matrix_dist(fn, exponent):
    [xarr, yarr], [x, y] = noise_elements(fn, n=2)
    sparse_mat = _sparse_matrix(fn)
    sparse_mat_as_dense = np.asarray(sparse_mat.todense())
    dense_mat = _dense_matrix(fn)

    if exponent == 1.0:  # d(x, y)_{A,1} = ||A(x-y)||_1
        true_dist_sparse = np.linalg.norm(np.dot(sparse_mat_as_dense,
                                                 xarr - yarr),
                                          ord=exponent)
        true_dist_dense = np.linalg.norm(np.dot(dense_mat, xarr - yarr),
                                         ord=exponent)
    elif exponent == 2.0:  # d(x, y)_{A,2} = sqrt(<x-y, A(x-y)>)
        true_dist_sparse = np.sqrt(
            np.vdot(xarr - yarr, np.dot(sparse_mat_as_dense, xarr - yarr)))
        true_dist_dense = np.sqrt(
            np.vdot(xarr - yarr, np.dot(dense_mat, xarr - yarr)))
    elif exponent == float('inf'):  # d(x, y)_{A,inf} = ||A(x-y)||_inf
        true_dist_sparse = np.linalg.norm(sparse_mat_as_dense.dot(xarr - yarr),
                                          ord=exponent)
        true_dist_dense = np.linalg.norm(dense_mat.dot(xarr - yarr),
                                         ord=exponent)
    else:  # d(x, y)_{A,p} = ||A^{1/p} (x-y)||_p
        # Calculate matrix power
        eigval, eigvec = scipy.linalg.eigh(dense_mat)
        eigval **= 1.0 / exponent
        mat_pow = (eigval * eigvec).dot(eigvec.conj().T)
        true_dist_dense = np.linalg.norm(np.dot(mat_pow, xarr - yarr),
                                         ord=exponent)

    if exponent in (1.0, 2.0, float('inf')):
        w_sparse = NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse.dist(x, y), true_dist_sparse)

    w_dense = NumpyFnMatrixWeighting(dense_mat, exponent=exponent)
    assert almost_equal(w_dense.dist(x, y), true_dist_dense)

    # With free functions
    if exponent in (1.0, 2.0, float('inf')):
        w_sparse_dist = npy_weighted_dist(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse_dist(x, y), true_dist_sparse)

    w_dense_dist = npy_weighted_dist(dense_mat, exponent=exponent)
    assert almost_equal(w_dense_dist(x, y), true_dist_dense)
Пример #12
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)
Пример #13
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)