示例#1
0
def test_getitem():
    r3 = CudaFn(3)
    y = [1, 2, 3]
    x = r3.element(y)

    for index in [0, 1, 2, -1, -2, -3]:
        assert x[index] == y[index]
示例#2
0
def test_astype():
    # Complex not implemented
    rn = CudaFn(3, weighting=1.5)
    assert rn.astype('float32') == rn

    with pytest.raises(TypeError):
        rn.astype(complex)
示例#3
0
def _test_getslice(slice):
    # Validate get against python list behaviour
    r6 = CudaFn(6)
    y = [0, 1, 2, 3, 4, 5]
    x = r6.element(y)

    assert all_equal(x[slice], y[slice])
示例#4
0
def test_setitem():
    r3 = CudaFn(3)
    x = r3.element([42, 42, 42])

    for index in [0, 1, 2, -1, -2, -3]:
        x[index] = index
        assert x[index] == index
示例#5
0
def test_astype():
    # Complex not implemented
    rn = CudaFn(3, weight=1.5)
    assert rn.astype('float32') == rn

    with pytest.raises(TypeError):
        rn.astype(complex)
示例#6
0
def test_getslice_index_error():
    r3 = CudaFn(3)
    xd = r3.element([1, 2, 3])

    # Bad slice
    with pytest.raises(IndexError):
        xd[10:13]
示例#7
0
def _test_getslice(slice):
    # Validate get against python list behaviour
    r6 = CudaFn(6)
    y = [0, 1, 2, 3, 4, 5]
    x = r6.element(y)

    assert all_equal(x[slice], y[slice])
示例#8
0
def test_getslice_index_error():
    r3 = CudaFn(3)
    xd = r3.element([1, 2, 3])

    # Bad slice
    with pytest.raises(IndexError):
        xd[10:13]
示例#9
0
def test_setitem():
    r3 = CudaFn(3)
    x = r3.element([42, 42, 42])

    for index in [0, 1, 2, -1, -2, -3]:
        x[index] = index
        assert x[index] == index
示例#10
0
def test_getitem():
    r3 = CudaFn(3)
    y = [1, 2, 3]
    x = r3.element(y)

    for index in [0, 1, 2, -1, -2, -3]:
        assert x[index] == y[index]
示例#11
0
def test_vector_dist(exponent):
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, n=2)

    weight = _pos_vector(CudaFn(5))

    weighting = CudaFnArrayWeighting(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 = 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)
示例#12
0
def test_vector_norm(exponent):
    rn = CudaFn(5)
    xarr, x = noise_elements(rn)

    weight = _pos_vector(CudaFn(5))

    weighting = CudaFnArrayWeighting(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 = 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)
示例#13
0
def test_modify():
    r3 = CudaFn(3)
    xd = r3.element([1, 2, 3])
    yd = r3.element(data_ptr=xd.data_ptr)

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

    assert all_equal(xd, yd)
示例#14
0
def test_modify():
    r3 = CudaFn(3)
    xd = r3.element([1, 2, 3])
    yd = r3.element(data_ptr=xd.data_ptr)

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

    assert all_equal(xd, yd)
示例#15
0
def test_offset_sub_vector():
    r6 = CudaFn(6)
    r3 = CudaFn(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)
示例#16
0
def test_setitem_index_error():
    r3 = CudaFn(3)
    x = r3.element([1, 2, 3])

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

    with pytest.raises(IndexError):
        x[3] = 0
示例#17
0
def test_setitem_index_error():
    r3 = CudaFn(3)
    x = r3.element([1, 2, 3])

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

    with pytest.raises(IndexError):
        x[3] = 0
示例#18
0
def test_sub_vector():
    r6 = CudaFn(6)
    r3 = CudaFn(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)
示例#19
0
def _test_dtype(dt):
    if dt not in CUDA_DTYPES:
        with pytest.raises(TypeError):
            r3 = CudaFn(3, dt)
    else:
        r3 = CudaFn(3, dt)
        x = r3.element([1, 2, 3])
        y = r3.element([4, 5, 6])
        z = x + y
        assert all_almost_equal(z, [5, 7, 9])
示例#20
0
def _test_setslice(slice):
    # Validate set against python list behaviour
    r6 = CudaFn(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)
示例#21
0
def _test_dtype(dt):
    if dt not in CUDA_DTYPES:
        with pytest.raises(TypeError):
            r3 = CudaFn(3, dt)
    else:
        r3 = CudaFn(3, dt)
        x = r3.element([1, 2, 3])
        y = r3.element([4, 5, 6])
        z = x + y
        assert all_almost_equal(z, [5, 7, 9])
示例#22
0
def _test_setslice(slice):
    # Validate set against python list behaviour
    r6 = CudaFn(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)
示例#23
0
def test_norm(exponent):
    r3 = CudaFn(3, exponent=exponent)
    xarr, x = noise_elements(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)
示例#24
0
def test_ndarray_init():
    r3 = CudaFn(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)
示例#25
0
def test_slice_is_view():
    # Verify that modifications of a view modify the original data
    r10 = CudaFn(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)
示例#26
0
def test_slice_is_view():
    # Verify that modifications of a view modify the original data
    r10 = CudaFn(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)
示例#27
0
def test_norm(exponent):
    r3 = CudaFn(3, exponent=exponent)
    xarr, x = noise_elements(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)
示例#28
0
def test_ndarray_init():
    r3 = CudaFn(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)
示例#29
0
def test_dist(exponent):
    r3 = CudaFn(3, exponent=exponent)
    [xarr, yarr], [x, y] = noise_elements(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)
示例#30
0
def test_dist(exponent):
    r3 = CudaFn(3, exponent=exponent)
    [xarr, yarr], [x, y] = noise_elements(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)
示例#31
0
def test_slice_of_slice():
    # Verify that creating slices from slices works as expected
    r10 = CudaFn(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)
示例#32
0
def test_slice_of_slice():
    # Verify that creating slices from slices works as expected
    r10 = CudaFn(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)
示例#33
0
def test_const_dist(exponent):
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(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 = 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)
示例#34
0
def test_const_norm(exponent):
    rn = CudaFn(5)
    xarr, x = noise_elements(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 = 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)
示例#35
0
def test_incompatible_operations():
    r3 = CudaFn(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
示例#36
0
def test_vector_equals():
    rn = CudaFn(5)
    weight = _pos_vector(rn)

    weighting = CudaFnArrayWeighting(weight)
    weighting2 = CudaFnArrayWeighting(weight)

    assert weighting == weighting2
示例#37
0
def test_setslice_index_error():
    r3 = CudaFn(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]
示例#38
0
def test_setslice_index_error():
    r3 = CudaFn(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]
示例#39
0
def test_incompatible_operations():
    r3 = CudaFn(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
示例#40
0
def test_const_inner():
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, 2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant)

    true_inner = constant * np.vdot(yarr, xarr)
    assert almost_equal(weighting.inner(x, y), true_inner)
示例#41
0
def test_vector_inner():
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, 2)

    weight = _pos_vector(CudaFn(5))

    weighting = CudaFnArrayWeighting(weight)

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

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

    # Same with free function
    inner_vec = cu_weighted_inner(weight)

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

    # Exponent != 2 -> no inner product, should raise
    with pytest.raises(NotImplementedError):
        CudaFnArrayWeighting(weight, exponent=1.0).inner(x, y)
示例#42
0
def test_init_weighting(exponent):
    const = 1.5
    weight_vec = _pos_vector(CudaFn(3))
    weight_elem = CudaFn(3, dtype='float32').element(weight_vec)

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

    weighting_none = CudaFnNoWeighting(exponent=exponent)
    weighting_const = CudaFnConstWeighting(const, exponent=exponent)
    weighting_vec = CudaFnArrayWeighting(weight_vec, exponent=exponent)
    weighting_elem = CudaFnArrayWeighting(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
示例#43
0
def test_inner():
    r3 = CudaFn(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 = CudaFn(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)
示例#44
0
def test_vector_is_valid():
    rn = CudaFn(5)
    weight = _pos_vector(rn)

    weighting = CudaFnArrayWeighting(weight)

    assert weighting.is_valid()

    # Invalid
    weight[0] = 0

    weighting = CudaFnArrayWeighting(weight)

    assert not weighting.is_valid()
示例#45
0
def test_sub_vector():
    r6 = CudaFn(6)
    r3 = CudaFn(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)
示例#46
0
def test_offset_sub_vector():
    r6 = CudaFn(6)
    r3 = CudaFn(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)
示例#47
0
def test_inner():
    r3 = CudaFn(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 = CudaFn(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)
示例#48
0
def test_vector_init():
    rn = CudaFn(5)
    weight_vec = _pos_vector(rn)

    CudaFnVectorWeighting(weight_vec)
    CudaFnVectorWeighting(rn.element(weight_vec))
示例#49
0
def fn(request):
    size, dtype = request.param.split()
    return CudaFn(int(size), dtype=dtype)
示例#50
0
def test_vector_init():
    rn = CudaFn(5)
    weight_vec = _pos_vector(rn)

    CudaFnArrayWeighting(weight_vec)
    CudaFnArrayWeighting(rn.element(weight_vec))
示例#51
0
    return CudaFn(int(size), dtype=dtype)


# Simply modify exp_params to modify the fixture
exp_params = [2.0, 1.0, float('inf'), 0.5, 1.5, 3.0]
exp_ids = [' p = {} '.format(p) for p in exp_params]
exp_fixture = pytest.fixture(scope="module", ids=exp_ids, params=exp_params)


@exp_fixture
def exponent(request):
    return request.param


# Simply modify dtype_params to modify the fixture
dtype_params = CudaFn.available_dtypes()
dtype_ids = [' dtype = {} '.format(t) for t in dtype_params]
dtype_fixture = pytest.fixture(scope="module", ids=dtype_ids,
                               params=dtype_params)


@dtype_fixture
def dtype(request):
    return request.param


ufunc_params = [ufunc for ufunc in odl.util.ufuncs.UFUNCS]
ufunc_ids = [' ufunc={} '.format(p[0]) for p in ufunc_params]


@pytest.fixture(scope="module", ids=ufunc_ids, params=ufunc_params)
示例#52
0
def test_iterator():
    r3 = CudaFn(3)
    y = [1, 2, 3]
    x = r3.element(y)

    assert all_equal([a for a in x], [b for b in y])