예제 #1
0
def test_resizing_op_adjoint(padding, fn_impl):
    pad_mode, pad_const = padding
    dtypes = [
        dt for dt in odl.fn_impl(fn_impl).available_dtypes()
        if is_real_floating_dtype(dt)
    ]

    for dtype in dtypes:
        space = odl.uniform_discr([0, -1], [1, 1], (4, 5),
                                  dtype=dtype,
                                  impl=fn_impl)
        res_space = odl.uniform_discr([0, -1.4], [1.5, 1.4], (6, 7),
                                      dtype=dtype,
                                      impl=fn_impl)
        res_op = odl.ResizingOperator(space,
                                      res_space,
                                      pad_mode=pad_mode,
                                      pad_const=pad_const)

        if pad_const != 0.0:
            with pytest.raises(NotImplementedError):
                res_op.adjoint
            return

        elem = noise_element(space)
        res_elem = noise_element(res_space)
        inner1 = res_op(elem).inner(res_elem)
        inner2 = elem.inner(res_op.adjoint(res_elem))
        assert almost_equal(inner1, inner2, places=dtype_places(dtype))
예제 #2
0
def test_resizing_op_call(fn_impl):
    dtypes = [
        dt for dt in odl.fn_impl(fn_impl).available_dtypes()
        if is_scalar_dtype(dt)
    ]

    for dtype in dtypes:
        # Minimal test since this operator only wraps resize_array
        space = odl.uniform_discr([0, -1], [1, 1], (4, 5), impl=fn_impl)
        res_space = odl.uniform_discr([0, -0.6], [2, 0.2], (8, 2),
                                      impl=fn_impl)
        res_op = odl.ResizingOperator(space, res_space)
        out = res_op(space.one())
        true_res = np.zeros((8, 2))
        true_res[:4, :] = 1
        assert np.array_equal(out, true_res)

        out = res_space.element()
        res_op(space.one(), out=out)
        assert np.array_equal(out, true_res)

        # Test also mapping to default impl for other 'fn_impl'
        if fn_impl != 'numpy':
            space = odl.uniform_discr([0, -1], [1, 1], (4, 5), impl=fn_impl)
            res_space = odl.uniform_discr([0, -0.6], [2, 0.2], (8, 2))
            res_op = odl.ResizingOperator(space, res_space)
            out = res_op(space.one())
            true_res = np.zeros((8, 2))
            true_res[:4, :] = 1
            assert np.array_equal(out, true_res)

            out = res_space.element()
            res_op(space.one(), out=out)
            assert np.array_equal(out, true_res)
예제 #3
0
def space(request, ndim, interp, dtype, fn_impl):
    """Example space.

    Generates example spaces with various implementations, dimensions, dtypes
    and interpolations.
    """
    if np.dtype(dtype) not in odl.fn_impl(fn_impl).available_dtypes():
        pytest.skip('dtype not available for this backend')

    return odl.uniform_discr([-1] * ndim, [1] * ndim, [20] * ndim,
                             interp=interp,
                             impl=fn_impl,
                             dtype=dtype)
예제 #4
0
def test_resizing_op_properties(fn_impl, padding):
    dtypes = [
        dt for dt in odl.fn_impl(fn_impl).available_dtypes()
        if is_scalar_dtype(dt)
    ]

    pad_mode, pad_const = padding

    for dtype in dtypes:
        # Explicit range
        space = odl.uniform_discr([0, -1], [1, 1], (10, 5), dtype=dtype)
        res_space = odl.uniform_discr([0, -3], [2, 3], (20, 15), dtype=dtype)
        res_op = odl.ResizingOperator(space,
                                      res_space,
                                      pad_mode=pad_mode,
                                      pad_const=pad_const)

        assert res_op.domain == space
        assert res_op.range == res_space
        assert res_op.offset == (0, 5)
        assert res_op.pad_mode == pad_mode
        assert res_op.pad_const == pad_const
        if pad_mode == 'constant' and pad_const != 0:
            assert not res_op.is_linear
        else:
            assert res_op.is_linear

        # Implicit range via ran_shp and offset
        res_op = odl.ResizingOperator(space,
                                      ran_shp=(20, 15),
                                      offset=[0, 5],
                                      pad_mode=pad_mode,
                                      pad_const=pad_const)
        assert np.allclose(res_op.range.min_pt, res_space.min_pt)
        assert np.allclose(res_op.range.max_pt, res_space.max_pt)
        assert np.allclose(res_op.range.cell_sides, res_space.cell_sides)
        assert res_op.range.dtype == res_space.dtype
        assert res_op.offset == (0, 5)
        assert res_op.pad_mode == pad_mode
        assert res_op.pad_const == pad_const
        if pad_mode == 'constant' and pad_const != 0:
            assert not res_op.is_linear
        else:
            assert res_op.is_linear
예제 #5
0
def test_resizing_op_inverse(padding, fn_impl):
    pad_mode, pad_const = padding
    dtypes = [
        dt for dt in odl.fn_impl(fn_impl).available_dtypes()
        if is_scalar_dtype(dt)
    ]

    for dtype in dtypes:
        space = odl.uniform_discr([0, -1], [1, 1], (4, 5),
                                  dtype=dtype,
                                  impl=fn_impl)
        res_space = odl.uniform_discr([0, -1.4], [1.5, 1.4], (6, 7),
                                      dtype=dtype,
                                      impl=fn_impl)
        res_op = odl.ResizingOperator(space,
                                      res_space,
                                      pad_mode=pad_mode,
                                      pad_const=pad_const)

        # Only left inverse if the operator extentds in all axes
        x = noise_element(space)
        assert res_op.inverse(res_op(x)) == x
예제 #6
0
def test_real_imag(fn_impl):
    # Get real and imag
    for dtype in filter(odl.util.is_complex_floating_dtype,
                        odl.fn_impl(fn_impl).available_dtypes()):
        cdiscr = odl.uniform_discr([0, 0], [1, 1], [2, 2], dtype=dtype,
                                   impl=fn_impl)
        rdiscr = cdiscr.real_space

        x = cdiscr.element([[1 - 1j, 2 - 2j], [3 - 3j, 4 - 4j]])
        assert x.real in rdiscr
        assert all_equal(x.real, [1, 2, 3, 4])
        assert x.imag in rdiscr
        assert all_equal(x.imag, [-1, -2, -3, -4])

        # Set with different data types and shapes
        for assigntype in (lambda x: x, list, tuple, rdiscr.element):
            # Without [:] assignment
            x = cdiscr.zero()
            x.real = assigntype([[2, 3], [4, 5]])
            assert all_equal(x.real, [2, 3, 4, 5])

            x = cdiscr.zero()
            x.real = assigntype([4, 5, 6, 7])
            assert all_equal(x.real, [4, 5, 6, 7])

            x = cdiscr.zero()
            x.imag = assigntype([[2, 3], [4, 5]])
            assert all_equal(x.imag, [2, 3, 4, 5])

            x = cdiscr.zero()
            x.imag = assigntype([4, 5, 6, 7])
            assert all_equal(x.imag, [4, 5, 6, 7])

            # With [:] assignment
            x = cdiscr.zero()
            x.real[:] = assigntype([[2, 3], [4, 5]])
            assert all_equal(x.real, [2, 3, 4, 5])

            x = cdiscr.zero()
            x.real[:] = assigntype([4, 5, 6, 7])
            assert all_equal(x.real, [4, 5, 6, 7])

            x = cdiscr.zero()
            x.imag[:] = assigntype([[2, 3], [4, 5]])
            assert all_equal(x.imag, [2, 3, 4, 5])

            x = cdiscr.zero()
            x.imag[:] = assigntype([4, 5, 6, 7])
            assert all_equal(x.imag, [4, 5, 6, 7])

        x = cdiscr.zero()
        x.real = 1
        assert all_equal(x.real, [1, 1, 1, 1])

        x = cdiscr.zero()
        x.imag = -1
        assert all_equal(x.imag, [-1, -1, -1, -1])

        # 'F' ordering
        cdiscr = odl.uniform_discr([0, 0], [1, 1], [2, 2], dtype=dtype,
                                   impl=fn_impl, order='F')

        x = cdiscr.element()
        newreal = [[3, 4], [5, 6]]
        x.real = newreal
        assert all_equal(x.real, [3, 5, 4, 6])  # flattened in 'F' order
        newreal = [4, 5, 6, 7]
        x.real = newreal
        assert all_equal(x.real, [4, 5, 6, 7])