Пример #1
0
def test_getslice_index_error():
    r3 = CudaRn(3)
    xd = r3.element([1, 2, 3])

    # Bad slice
    with pytest.raises(IndexError):
        xd[10:13]
Пример #2
0
def test_setitem():
    r3 = CudaRn(3)
    x = r3.element([42, 42, 42])

    for index in [0, 1, 2, -1, -2, -3]:
        x[index] = index
        assert x[index] == index
Пример #3
0
def _test_getslice(slice):
    # Validate get against python list behaviour
    r6 = CudaRn(6)
    y = [0, 1, 2, 3, 4, 5]
    x = r6.element(y)

    assert all_equal(x[slice], y[slice])
Пример #4
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)
Пример #5
0
def test_getitem():
    r3 = CudaRn(3)
    y = [1, 2, 3]
    x = r3.element(y)

    for index in [0, 1, 2, -1, -2, -3]:
        assert x[index] == y[index]
Пример #6
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)
Пример #7
0
def test_modify():
    r3 = CudaRn(3)
    xd = r3.element([1, 2, 3])
    yd = r3.element(data_ptr=xd.data_ptr)

    yd[:] = [5, 6, 7]

    assert all_equal(xd, yd)
Пример #8
0
def test_setitem_index_error():
    r3 = CudaRn(3)
    x = r3.element([1, 2, 3])

    with pytest.raises(IndexError):
        x[-4] = 0

    with pytest.raises(IndexError):
        x[3] = 0
Пример #9
0
def _test_setslice(slice):
    # Validate set against python list behaviour
    r6 = CudaRn(6)
    z = [7, 8, 9, 10, 11, 10]
    y = [0, 1, 2, 3, 4, 5]
    x = r6.element(y)

    x[slice] = z[slice]
    y[slice] = z[slice]
    assert all_equal(x, y)
Пример #10
0
def test_ndarray_init():
    r3 = CudaRn(3)

    x0 = np.array([1., 2., 3.])
    x = r3.element(x0)
    assert all_equal(x, x0)

    x0 = np.array([1, 2, 3], dtype=float64)
    x = r3.element(x0)
    assert all_equal(x, x0)

    x0 = np.array([1, 2, 3], dtype=int)
    x = r3.element(x0)
    assert all_equal(x, x0)
Пример #11
0
def test_slice_is_view():
    # Verify that modifications of a view modify the original data
    r10 = CudaRn(10)
    xh = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    xd = r10.element(xh)

    yh = xh[1:8:2]
    yh[:] = [0, 0, 0, 0]

    yd = xd[1:8:2]
    yd[:] = [0, 0, 0, 0]

    assert all_equal(xh, xd)
    assert all_equal(yh, yd)
Пример #12
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)
Пример #13
0
def test_slice_of_slice():
    # Verify that creating slices from slices works as expected
    r10 = CudaRn(10)
    xh = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    xd = r10.element(xh)

    yh = xh[1:8:2]
    yd = xd[1:8:2]

    assert all_equal(yh, yd)

    zh = yh[1::2]
    zd = yd[1::2]

    assert all_equal(zh, zd)
Пример #14
0
def test_dist(exponent):
    r3 = CudaRn(3, exponent=exponent)
    [xarr, yarr], [x, y] = example_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)
Пример #15
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)
Пример #16
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)
Пример #17
0
def test_vector_equals():
    rn = CudaRn(5)
    weight = _pos_vector(rn)

    weighting = CudaFnVectorWeighting(weight)
    weighting2 = CudaFnVectorWeighting(weight)

    assert weighting == weighting2
Пример #18
0
def test_setslice_index_error():
    r3 = CudaRn(3)
    xd = r3.element([1, 2, 3])

    # Bad slice
    with pytest.raises(IndexError):
        xd[10:13] = [1, 2, 3]

    # Bad size of rhs
    with pytest.raises(IndexError):
        xd[:] = []

    with pytest.raises(IndexError):
        xd[:] = [1, 2]

    with pytest.raises(IndexError):
        xd[:] = [1, 2, 3, 4]
Пример #19
0
def test_incompatible_operations():
    r3 = CudaRn(3)
    R3h = odl.Rn(3)
    xA = r3.zero()
    xB = R3h.zero()

    with pytest.raises(TypeError):
        xA += xB

    with pytest.raises(TypeError):
        xA -= xB

    with pytest.raises(TypeError):
        xA + xB

    with pytest.raises(TypeError):
        xA - xB
Пример #20
0
def test_const_inner():
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, 2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant)

    true_inner = constant * np.vdot(yarr, xarr)
    assert almost_equal(weighting.inner(x, y), true_inner)
Пример #21
0
def test_vector_inner():
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, 2)

    weight = _pos_vector(CudaRn(5))

    weighting = CudaFnVectorWeighting(weight)

    true_inner = np.vdot(yarr, xarr * weight.asarray())

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

    # Same with free function
    inner_vec = odl.cu_weighted_inner(weight)

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

    # Exponent != 2 -> no inner product, should raise
    with pytest.raises(NotImplementedError):
        CudaFnVectorWeighting(weight, exponent=1.0).inner(x, y)
Пример #22
0
def test_inner():
    r3 = CudaRn(3)
    x = r3.element([1, 2, 3])
    y = r3.element([5, 3, 9])

    correct_inner = 1 * 5 + 2 * 3 + 3 * 9

    # Space function
    assert almost_equal(r3.inner(x, y), correct_inner)

    # Exponent != 2 -> no inner product
    r3 = CudaRn(3, exponent=1)
    x = r3.element([1, 2, 3])
    y = r3.element([5, 3, 9])

    with pytest.raises(NotImplementedError):
        r3.inner(x, y)
    with pytest.raises(NotImplementedError):
        x.inner(y)
Пример #23
0
def test_vector_is_valid():
    rn = CudaRn(5)
    weight = _pos_vector(rn)

    weighting = CudaFnVectorWeighting(weight)

    assert weighting.is_valid()

    # Invalid
    weight[0] = 0

    weighting = CudaFnVectorWeighting(weight)

    assert not weighting.is_valid()
Пример #24
0
def test_offset_sub_vector():
    r6 = CudaRn(6)
    r3 = CudaRn(3)
    xd = r6.element([1, 2, 3, 4, 5, 6])

    yd = r3.element(data_ptr=xd.data_ptr + 3 * xd.space.dtype.itemsize)
    yd[:] = [7, 8, 9]

    assert all_equal([1, 2, 3, 7, 8, 9], xd)
Пример #25
0
def test_sub_vector():
    r6 = CudaRn(6)
    r3 = CudaRn(3)
    xd = r6.element([1, 2, 3, 4, 5, 6])

    yd = r3.element(data_ptr=xd.data_ptr)
    yd[:] = [7, 8, 9]

    assert all_almost_equal([7, 8, 9, 4, 5, 6], xd)
Пример #26
0
def test_astype():
    # Complex not implemented
    rn = CudaRn(3, weight=1.5)
    rn_d = CudaRn(3, weight=1.5, dtype='float64')
    assert rn.astype('float32') == rn
    assert rn.astype('float64') == rn_d

    with pytest.raises(TypeError):
        rn.astype(complex)
Пример #27
0
def test_init_weighting(exponent):
    const = 1.5
    weight_vec = _pos_vector(CudaRn(3))
    weight_elem = CudaFn(3, dtype='float32').element(weight_vec)

    f3_none = CudaFn(3, dtype='float32', exponent=exponent)
    f3_const = CudaFn(3, dtype='float32', weight=const, exponent=exponent)
    f3_vec = CudaFn(3, dtype='float32', weight=weight_vec, exponent=exponent)
    f3_elem = CudaFn(3, dtype='float32', weight=weight_elem, exponent=exponent)

    weighting_none = CudaFnNoWeighting(exponent=exponent)
    weighting_const = CudaFnConstWeighting(const, exponent=exponent)
    weighting_vec = CudaFnVectorWeighting(weight_vec, exponent=exponent)
    weighting_elem = CudaFnVectorWeighting(weight_elem, exponent=exponent)

    assert f3_none.weighting == weighting_none
    assert f3_const.weighting == weighting_const
    assert f3_vec.weighting == weighting_vec
    assert f3_elem.weighting == weighting_elem
Пример #28
0
def test_iterator():
    r3 = CudaRn(3)
    y = [1, 2, 3]
    x = r3.element(y)

    assert all_equal([a for a in x], [b for b in y])
Пример #29
0
def test_vector_init():
    rn = CudaRn(5)
    weight_vec = _pos_vector(rn)

    CudaFnVectorWeighting(weight_vec)
    CudaFnVectorWeighting(rn.element(weight_vec))