Пример #1
0
def _grouped_and_flat_arrays(shapes, dtype):
    """Return a grouped and flat list of arrays with specified shapes.

    The lists are constructed as if they were used in a wavelet transform,
    i.e. the array with shape ``shapes[0]`` appears once, while the
    others appear ``2 ** ndim - 1`` times each.
    """
    space = odl.discr_sequence_space(shape=shapes[0], dtype=dtype)
    array = noise_array(space).reshape(space.shape)
    grouped_list = [array]
    flat_list = [array.ravel()]
    ndim = space.ndim

    for shape in shapes[1:]:
        space = odl.discr_sequence_space(shape=shape, dtype=dtype)
        arrays = [noise_array(space).reshape(shape)
                  for _ in range(2 ** ndim - 1)]
        grouped_list.append(tuple(arrays))
        flat_list.extend([arr.ravel() for arr in arrays])

    return grouped_list, flat_list
Пример #2
0
def _grouped_and_flat_arrays(shapes, dtype):
    """Return a grouped and flat list of arrays with specified shapes.

    The lists are constructed as if they were used in a wavelet transform,
    i.e. the array with shape ``shapes[0]`` appears once, while the
    others appear ``2 ** ndim - 1`` times each.
    """
    space = odl.discr_sequence_space(shape=shapes[0], dtype=dtype)
    array = noise_array(space).reshape(space.shape)
    grouped_list = [array]
    flat_list = [array.ravel()]
    ndim = space.ndim

    for shape in shapes[1:]:
        space = odl.discr_sequence_space(shape=shape, dtype=dtype)
        arrays = [noise_array(space).reshape(shape)
                  for _ in range(2 ** ndim - 1)]
        grouped_list.append(tuple(arrays))
        flat_list.extend([arr.ravel() for arr in arrays])

    return grouped_list, flat_list
Пример #3
0
def test_explicit_example(odl_floating_dtype):
    """Comparison with hand-calculated wavelet transform."""
    dtype = odl_floating_dtype

    space = odl.uniform_discr([0, 0], [1, 1], (16, 15), dtype=dtype)
    x = noise_array(space).reshape(space.shape)

    # We use a Daubechies-2 wavelet
    wavelet = pywt.Wavelet('db2')
    filter_l = np.array(wavelet.dec_lo)
    filter_h = np.array(wavelet.dec_hi)

    # Build the 2D filters
    filter_ll = filter_l[:, None] * filter_l[None, :]
    filter_lh = filter_l[:, None] * filter_h[None, :]
    filter_hl = filter_h[:, None] * filter_l[None, :]
    filter_hh = filter_h[:, None] * filter_h[None, :]

    # Convolve x with 2D filters (implicitly uses zero-padding)
    conv_ll = convolve(x, filter_ll)
    conv_lh = convolve(x, filter_lh)
    conv_hl = convolve(x, filter_hl)
    conv_hh = convolve(x, filter_hh)

    # Downsampling by factor 2, taking the odd indices, gives the wavelet
    # coefficients
    coeff_aa = conv_ll[1::2, 1::2]
    coeff_ad = conv_lh[1::2, 1::2]
    coeff_da = conv_hl[1::2, 1::2]
    coeff_dd = conv_hh[1::2, 1::2]

    # Compare with single-level wavelet trafo (zero padding)
    coeffs = pywt_single_level_decomp(x, wavelet='db2', mode='zero')
    approx, details = coeffs

    assert all_almost_equal(approx, coeff_aa)
    assert all_almost_equal(details, [coeff_ad, coeff_da, coeff_dd])

    # Second level, continuing with the level 1 approximation coefficient
    coeff_2_aa = convolve(coeff_aa, filter_ll)[1::2, 1::2]
    coeff_2_ad = convolve(coeff_aa, filter_lh)[1::2, 1::2]
    coeff_2_da = convolve(coeff_aa, filter_hl)[1::2, 1::2]
    coeff_2_dd = convolve(coeff_aa, filter_hh)[1::2, 1::2]

    # Compare with multi-level wavelet trafo (zero padding)
    coeffs = pywt_multi_level_decomp(x, wavelet='db2', mode='zero', nlevels=2)
    approx_2, details_2, details_1 = coeffs

    assert all_almost_equal(approx_2, coeff_2_aa)
    assert all_almost_equal(details_1, [coeff_ad, coeff_da, coeff_dd])
    assert all_almost_equal(details_2, [coeff_2_ad, coeff_2_da, coeff_2_dd])
Пример #4
0
def test_explicit_example(floating_dtype):
    """Comparison with hand-calculated wavelet transform."""

    space = odl.uniform_discr([0, 0], [1, 1], (16, 15), dtype=floating_dtype)
    x = noise_array(space).reshape(space.shape)

    # We use a Daubechies-2 wavelet
    wavelet = pywt.Wavelet('db2')
    filter_l = np.array(wavelet.dec_lo)
    filter_h = np.array(wavelet.dec_hi)

    # Build the 2D filters
    filter_ll = filter_l[:, None] * filter_l[None, :]
    filter_lh = filter_l[:, None] * filter_h[None, :]
    filter_hl = filter_h[:, None] * filter_l[None, :]
    filter_hh = filter_h[:, None] * filter_h[None, :]

    # Convolve x with 2D filters (implicitly uses zero-padding)
    conv_ll = convolve(x, filter_ll)
    conv_lh = convolve(x, filter_lh)
    conv_hl = convolve(x, filter_hl)
    conv_hh = convolve(x, filter_hh)

    # Downsampling by factor 2, taking the odd indices, gives the wavelet
    # coefficients
    coeff_aa = conv_ll[1::2, 1::2]
    coeff_ad = conv_lh[1::2, 1::2]
    coeff_da = conv_hl[1::2, 1::2]
    coeff_dd = conv_hh[1::2, 1::2]

    # Compare with single-level wavelet trafo (zero padding)
    coeffs = pywt_single_level_decomp(x, wavelet='db2', mode='zero')
    approx, details = coeffs

    assert all_almost_equal(approx, coeff_aa)
    assert all_almost_equal(details, [coeff_ad, coeff_da, coeff_dd])

    # Second level, continuing with the level 1 approximation coefficient
    coeff_2_aa = convolve(coeff_aa, filter_ll)[1::2, 1::2]
    coeff_2_ad = convolve(coeff_aa, filter_lh)[1::2, 1::2]
    coeff_2_da = convolve(coeff_aa, filter_hl)[1::2, 1::2]
    coeff_2_dd = convolve(coeff_aa, filter_hh)[1::2, 1::2]

    # Compare with multi-level wavelet trafo (zero padding)
    coeffs = pywt_multi_level_decomp(x, wavelet='db2', mode='zero', nlevels=2)
    approx_2, details_2, details_1 = coeffs

    assert all_almost_equal(approx_2, coeff_2_aa)
    assert all_almost_equal(details_1, [coeff_ad, coeff_da, coeff_dd])
    assert all_almost_equal(details_2, [coeff_2_ad, coeff_2_da, coeff_2_dd])
Пример #5
0
def test_vector_class_init(fn):
    # Test that code runs
    arr = noise_array(fn)

    NumpyFnVector(fn, arr)
    # Space has to be an actual space
    for non_space in [1, complex, np.array([1, 2])]:
        with pytest.raises(TypeError):
            NumpyFnVector(non_space, arr)

    # Data has to be a numpy array
    with pytest.raises(TypeError):
        NumpyFnVector(fn, list(arr))

    # Data has to be a numpy array or correct dtype
    with pytest.raises(TypeError):
        NumpyFnVector(fn, arr.astype(int))
Пример #6
0
def _pos_array(fn):
    """Create an array with positive real entries as weight in `fn`."""
    return np.abs(noise_array(fn)) + 0.1
Пример #7
0
def test_vector_class_init(fn):
    # Test that code runs
    arr = noise_array(fn)

    NumpyFnVector(fn, arr)