Ejemplo n.º 1
0
def test_part_deriv_cpu():
    """Discretized partial derivative."""

    with pytest.raises(TypeError):
        PartialDerivative(odl.Rn(1))

    # discretized space
    space = odl.uniform_discr([0, 0], [2, 1], DATA_2D.shape)

    # operator
    partial_0 = PartialDerivative(space, axis=0, method='central',
                                  padding_method='constant')
    partial_1 = PartialDerivative(space, axis=1, method='central',
                                  padding_method='constant')

    # discretized space vector
    vec = partial_0.domain.element(DATA_2D)

    # partial derivative
    partial_vec_0 = partial_0(vec)
    partial_vec_1 = partial_1(vec)

    # explicit calculation of finite difference

    # axis: 0
    diff_0 = np.zeros_like(DATA_2D)
    # interior: second-order accurate differences
    diff_0[1:-1, :] = (DATA_2D[2:, :] - DATA_2D[:-2, :]) / 2.0
    # boundary: second-order accurate central differences with zero padding
    diff_0[0, :] = DATA_2D[1, :] / 2.0
    diff_0[-1, :] = -DATA_2D[-2, :] / 2.0
    diff_0 /= space.cell_sides[0]

    # axis: 1
    diff_1 = np.zeros_like(DATA_2D)
    # interior: second-order accurate differences
    diff_1[:, 1:-1] = (DATA_2D[:, 2:] - DATA_2D[:, :-2]) / 2.0
    # boundary: second-order accurate central differences with zero padding
    diff_1[:, 0] = DATA_2D[:, 1] / 2.0
    diff_1[:, -1] = -DATA_2D[:, -2] / 2.0
    diff_1 /= space.cell_sides[1]

    # assert `dfe0` and `dfe1` do differ
    assert (diff_0 != diff_1).any()

    assert partial_vec_0 != partial_vec_1
    assert all_equal(partial_vec_0.asarray(), diff_0)
    assert all_equal(partial_vec_1.asarray(), diff_1)

    # adjoint operator
    vec1 = space.element(DATA_2D)
    vec2 = space.element(DATA_2D + np.random.rand(*DATA_2D.shape))
    assert almost_equal(partial_0(vec1).inner(vec2),
                        vec1.inner(partial_0.adjoint(vec2)))
    assert almost_equal(partial_1(vec1).inner(vec2),
                        vec1.inner(partial_1.adjoint(vec2)))
Ejemplo n.º 2
0
def test_part_deriv(space, method, padding):
    """Discretized partial derivative."""

    with pytest.raises(TypeError):
        PartialDerivative(odl.rn(1))

    if isinstance(padding, tuple):
        pad_mode, pad_const = padding
    else:
        pad_mode, pad_const = padding, 0

    # discretized space
    dom_vec = noise_element(space)
    dom_vec_arr = dom_vec.asarray()

    # operator
    for axis in range(space.ndim):
        partial = PartialDerivative(space,
                                    axis=axis,
                                    method=method,
                                    pad_mode=pad_mode,
                                    pad_const=pad_const)

        # Compare to helper function
        dx = space.cell_sides[axis]
        diff = finite_diff(dom_vec_arr,
                           axis=axis,
                           dx=dx,
                           method=method,
                           pad_mode=pad_mode,
                           pad_const=pad_const)

        partial_vec = partial(dom_vec)
        assert all_almost_equal(partial_vec.asarray(), diff)

        # Test adjoint operator
        derivative = partial.derivative()
        ran_vec = noise_element(space)
        deriv_vec = derivative(dom_vec)
        adj_vec = derivative.adjoint(ran_vec)
        lhs = ran_vec.inner(deriv_vec)
        rhs = dom_vec.inner(adj_vec)

        # Check not to use trivial data
        assert lhs != 0
        assert rhs != 0
        assert almost_equal(lhs, rhs, places=4)
Ejemplo n.º 3
0
def test_part_deriv(impl, method, padding):
    """Discretized partial derivative."""

    with pytest.raises(TypeError):
        PartialDerivative(odl.Rn(1))

    if isinstance(padding, tuple):
        padding_method, padding_value = padding
    else:
        padding_method, padding_value = padding, None

    # discretized space
    space = odl.uniform_discr([0, 0], [2, 1], DATA_2D.shape, impl=impl)
    dom_vec = space.element(DATA_2D)

    # operator
    for axis in range(space.ndim):
        partial = PartialDerivative(space, axis=axis, method=method,
                                    padding_method=padding_method,
                                    padding_value=padding_value)

        # Compare to helper function
        dx = space.cell_sides[axis]
        diff = finite_diff(DATA_2D, axis=axis, dx=dx, method=method,
                           padding_method=padding_method,
                           padding_value=padding_value)

        partial_vec = partial(dom_vec)
        assert all_almost_equal(partial_vec.asarray(), diff)

        # Test adjoint operator
        derivative = partial.derivative()
        ran_vec = derivative.range.element(DATA_2D ** 2)
        deriv_vec = derivative(dom_vec)
        adj_vec = derivative.adjoint(ran_vec)
        lhs = ran_vec.inner(deriv_vec)
        rhs = dom_vec.inner(adj_vec)

        # Check not to use trivial data
        assert lhs != 0
        assert rhs != 0
        assert almost_equal(lhs, rhs)
Ejemplo n.º 4
0
def test_part_deriv(space, method, padding):
    """Discretized partial derivative."""

    with pytest.raises(TypeError):
        PartialDerivative(odl.rn(1))

    if isinstance(padding, tuple):
        pad_mode, pad_const = padding
    else:
        pad_mode, pad_const = padding, 0

    # discretized space
    dom_vec = noise_element(space)
    dom_vec_arr = dom_vec.asarray()

    # operator
    for axis in range(space.ndim):
        partial = PartialDerivative(space, axis=axis, method=method,
                                    pad_mode=pad_mode,
                                    pad_const=pad_const)

        # Compare to helper function
        dx = space.cell_sides[axis]
        diff = finite_diff(dom_vec_arr, axis=axis, dx=dx, method=method,
                           pad_mode=pad_mode,
                           pad_const=pad_const)

        partial_vec = partial(dom_vec)
        assert all_almost_equal(partial_vec.asarray(), diff)

        # Test adjoint operator
        derivative = partial.derivative()
        ran_vec = noise_element(space)
        deriv_vec = derivative(dom_vec)
        adj_vec = derivative.adjoint(ran_vec)
        lhs = ran_vec.inner(deriv_vec)
        rhs = dom_vec.inner(adj_vec)

        # Check not to use trivial data
        assert lhs != 0
        assert rhs != 0
        assert almost_equal(lhs, rhs, places=4)
Ejemplo n.º 5
0
def test_part_deriv(space, method, padding):
    """Discretized partial derivative."""
    if isinstance(padding, tuple):
        pad_mode, pad_const = padding
    else:
        pad_mode, pad_const = padding, 0

    dom_vec = noise_element(space)
    dom_vec_arr = dom_vec.asarray()

    for axis in range(space.ndim):
        partial = PartialDerivative(space,
                                    axis=axis,
                                    method=method,
                                    pad_mode=pad_mode,
                                    pad_const=pad_const)

        # Compare to helper function
        dx = space.cell_sides[axis]
        diff = finite_diff(dom_vec_arr,
                           axis=axis,
                           dx=dx,
                           method=method,
                           pad_mode=pad_mode,
                           pad_const=pad_const)

        partial_vec = partial(dom_vec)
        assert all_almost_equal(partial_vec, diff)

        # Test adjoint operator
        derivative = partial.derivative()
        ran_vec = noise_element(space)
        deriv_vec = derivative(dom_vec)
        adj_vec = derivative.adjoint(ran_vec)
        lhs = ran_vec.inner(deriv_vec)
        rhs = dom_vec.inner(adj_vec)

        # Check not to use trivial data
        assert lhs != 0
        assert rhs != 0
        assert lhs == pytest.approx(rhs, rel=dtype_tol(space.dtype))
Ejemplo n.º 6
0
def test_part_deriv_init():
    """Check initialization of ``PartialDerivative``."""
    space = odl.uniform_discr([0, 0], [1, 1], (4, 5))

    op = PartialDerivative(space, axis=0)
    assert repr(op) != ''
    op = PartialDerivative(space, axis=1)
    assert repr(op) != ''
    op = PartialDerivative(space, axis=0, range=space.astype('float32'))
    assert repr(op) != ''
    op = PartialDerivative(space, axis=0, method='central')
    assert repr(op) != ''
    op = PartialDerivative(space, axis=0, pad_const=1)
    assert repr(op) != ''
    op = PartialDerivative(space, axis=0, pad_mode='order1')
    assert repr(op) != ''

    with pytest.raises(TypeError):
        PartialDerivative(odl.rn(1))