Python pywt.waverecn() Examples

The following are 18 code examples of pywt.waverecn(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pywt , or try the search function .
Example #1
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def test_waverecn_invalid_coeffs():
    # approximation coeffs as None and no valid detail oeffs
    coeffs = [None, {}]
    assert_raises(ValueError, pywt.waverecn, coeffs, 'db1')

    # use of None for a coefficient value
    coeffs = [np.ones((2, 2, 2)), {}, {'daa': None}, ]
    assert_raises(ValueError, pywt.waverecn, coeffs, 'db1')

    # invalid key names in coefficient list
    coeffs = [np.ones((4, 4, 4)), {'daa': np.ones((4, 4, 4)),
                                   'foo': np.ones((4, 4, 4))}]
    assert_raises(ValueError, pywt.waverecn, coeffs, 'db1')

    # mismatched key name lengths
    coeffs = [np.ones((4, 4, 4)), {'daa': np.ones((4, 4, 4)),
                                   'da': np.ones((4, 4, 4))}]
    assert_raises(ValueError, pywt.waverecn, coeffs, 'db1')

    # key name lengths don't match the array dimensions
    coeffs = [[[[1.0]]], {'ad': [[[0.0]]], 'da': [[[0.0]]], 'dd': [[[0.0]]]}]
    assert_raises(ValueError, pywt.waverecn, coeffs, 'db1')

    # input list cannot be empty
    assert_raises(ValueError, pywt.waverecn, [], 'haar') 
Example #2
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def test_waverecn_coeff_reshape_odd():
    # verify round trip is correct:
    #   wavedecn - >coeffs_to_array-> array_to_coeffs -> waverecn
    rng = np.random.RandomState(1234)
    x1 = rng.randn(35, 33)
    for mode in pywt.Modes.modes:
        for wave in ['haar', ]:
            w = pywt.Wavelet(wave)
            maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len)
            if maxlevel == 0:
                continue
            coeffs = pywt.wavedecn(x1, w, mode=mode)
            coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs)
            coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices)
            x1r = pywt.waverecn(coeffs2, w, mode=mode)
            # truncate reconstructed values to original shape
            x1r = x1r[[slice(s) for s in x1.shape]]
            assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) 
Example #3
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def test_wavedecn_coeff_reshape_even():
    # verify round trip is correct:
    #   wavedecn - >coeffs_to_array-> array_to_coeffs -> waverecn
    # This is done for wavedec{1, 2, n}
    rng = np.random.RandomState(1234)
    params = {'wavedec': {'d': 1, 'dec': pywt.wavedec, 'rec': pywt.waverec},
              'wavedec2': {'d': 2, 'dec': pywt.wavedec2, 'rec': pywt.waverec2},
              'wavedecn': {'d': 3, 'dec': pywt.wavedecn, 'rec': pywt.waverecn}}
    N = 28
    for f in params:
        x1 = rng.randn(*([N] * params[f]['d']))
        for mode in pywt.Modes.modes:
            for wave in wavelist:
                w = pywt.Wavelet(wave)
                maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len)
                if maxlevel == 0:
                    continue

                coeffs = params[f]['dec'](x1, w, mode=mode)
                coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs)
                coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices,
                                               output_format=f)
                x1r = params[f]['rec'](coeffs2, w, mode=mode)

                assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) 
Example #4
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_dtypes():
    x = np.ones((4, 4, 4))
    for dt, tol in dtypes_and_tolerances:
        coeffs = pywt.wavedecn(x.astype(dt), 'db1')
        assert_allclose(pywt.waverecn(coeffs, 'db1'), x, atol=tol, rtol=tol) 
Example #5
Source File: wavelet.py    From odl with Mozilla Public License 2.0 5 votes vote down vote up
def _call(self, coeffs):
        """Return the inverse wavelet transform of ``coeffs``."""
        if self.impl == 'pywt':
            coeffs = pywt.unravel_coeffs(coeffs,
                                         coeff_slices=self._coeff_slices,
                                         coeff_shapes=self._coeff_shapes,
                                         output_format='wavedecn')
            recon = pywt.waverecn(
                coeffs, wavelet=self.pywt_wavelet, mode=self.pywt_pad_mode,
                axes=self.axes)
            recon_shape = self.range.shape
            if recon.shape != recon_shape:
                # If the original shape was odd along any transformed axes it
                # will have been rounded up to the next even size after the
                # reconstruction. The extra sample should be discarded.
                # The underlying reason is decimation by two in reconstruction
                # must keep ceil(N/2) samples in each band for perfect
                # reconstruction. Reconstruction then upsamples by two.
                # When N is odd, (2 * np.ceil(N/2)) != N.
                recon_slc = []
                for i, (n_recon, n_intended) in enumerate(zip(recon.shape,
                                                              recon_shape)):
                    if n_recon == n_intended + 1:
                        # Upsampling added one entry too much in this axis,
                        # drop last one
                        recon_slc.append(slice(-1))
                    elif n_recon == n_intended:
                        recon_slc.append(slice(None))
                    else:
                        raise ValueError(
                            'in axis {}: expected size {} or {} in '
                            '`recon_shape`, got {}'
                            ''.format(i, n_recon - 1, n_recon,
                                      n_intended))
                recon = recon[tuple(recon_slc)]
            return recon
        else:
            raise RuntimeError("bad `impl` '{}'".format(self.impl)) 
Example #6
Source File: solver_l1.py    From OneNet with GNU General Public License v3.0 5 votes vote down vote up
def inverse_wavelet_transform(w_coeffs_rgb, coeff_slices, x_shape):
    x_hat = np.zeros(x_shape)
    for i in range(w_coeffs_rgb.shape[0]):
        w_coeffs_list = pywt.array_to_coeffs(w_coeffs_rgb[i,:,:], coeff_slices)
        x_hat[0,:,:,i] = pywt.waverecn(w_coeffs_list, wavelet='db4', mode='periodization')
    return x_hat 
Example #7
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_axes_errors():
    data = np.ones((8, 8, 8))
    c = pywt.wavedecn(data, 'haar')
    # repeated axes not allowed
    assert_raises(ValueError, pywt.waverecn, c, 'haar', axes=(1, 1))
    # out of range axis not allowed
    assert_raises(ValueError, pywt.waverecn, c, 'haar', axes=(0, 1, 3)) 
Example #8
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_int_axis():
    # waverecn should also work for axes as an integer
    rstate = np.random.RandomState(0)
    data = rstate.standard_normal((8, 8))
    for axis in [0, 1]:
        coefs = pywt.wavedecn(data, 'haar', axes=axis)
        rec = pywt.waverecn(coefs, 'haar', axes=axis)
        assert_allclose(rec, data, atol=1e-14) 
Example #9
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_axes_subsets():
    rstate = np.random.RandomState(0)
    data = rstate.standard_normal((8, 8, 8, 8))
    # test all combinations of 3 out of 4 axes transformed
    for axes in combinations((0, 1, 2, 3), 3):
        coefs = pywt.wavedecn(data, 'haar', axes=axes)
        rec = pywt.waverecn(coefs, 'haar', axes=axes)
        assert_allclose(rec, data, atol=1e-14) 
Example #10
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_all_wavelets_modes():
    # test 2D case using all wavelets and modes
    rstate = np.random.RandomState(1234)
    r = rstate.randn(80, 96)
    for wavelet in wavelist:
        for mode in pywt.Modes.modes:
            coeffs = pywt.wavedecn(r, wavelet, mode=mode)
            assert_allclose(pywt.waverecn(coeffs, wavelet, mode=mode),
                            r, rtol=tol_single, atol=tol_single) 
Example #11
Source File: DWT.py    From pylops with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _rmatvec(self, x):
        if self.reshape:
            x = np.reshape(x, self.dimsd)
        x = pywt.array_to_coeffs(x, self.sl, output_format='wavedecn')
        y = pywt.waverecn(x, wavelet=self.waveletadj, mode='periodization',
                          axes=(self.dir, ))
        y = self.pad.rmatvec(y.ravel())
        return y 
Example #12
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_wavedecn_complex():
    data = np.ones((4, 4, 4)) + 1j
    coeffs = pywt.wavedecn(data, 'db1')
    assert_allclose(pywt.waverecn(coeffs, 'db1'), data, rtol=1e-12) 
Example #13
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_multilevel_dtypes_nd():
    wavelet = pywt.Wavelet('haar')
    for dt_in, dt_out in zip(dtypes_in, dtypes_out):
        # wavedecn, waverecn
        x = np.ones((8, 8), dtype=dt_in)
        errmsg = "wrong dtype returned for {0} input".format(dt_in)
        cA, coeffsD2, coeffsD1 = pywt.wavedecn(x, wavelet, level=2)
        assert_(cA.dtype == dt_out, "wavedecn: " + errmsg)
        for key, c in coeffsD1.items():
            assert_(c.dtype == dt_out, "wavedecn: " + errmsg)
        for key, c in coeffsD2.items():
            assert_(c.dtype == dt_out, "wavedecn: " + errmsg)
        x_roundtrip = pywt.waverecn([cA, coeffsD2, coeffsD1], wavelet)
        assert_(x_roundtrip.dtype == dt_out, "waverecn: " + errmsg) 
Example #14
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_invalid_coeffs2():
    # shape mismatch should raise an error
    coeffs = [np.ones((4, 4, 4)), {'ada': np.ones((4, 4))}]
    assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') 
Example #15
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_lists():
    # support coefficient arrays specified as lists instead of arrays
    coeffs = [[[1.0]], {'ad': [[0.0]], 'da': [[0.0]], 'dd': [[0.0]]}]
    assert_equal(pywt.waverecn(coeffs, 'db1').shape, (2, 2)) 
Example #16
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn_empty_coeff():
    coeffs = [np.ones((2, 2, 2)), {}, {}]
    assert_equal(pywt.waverecn(coeffs, 'db1').shape, (8, 8, 8))

    assert_equal(pywt.waverecn(coeffs, 'db1').shape, (8, 8, 8))
    coeffs = [np.ones((2, 2, 2)), {}, {'daa': np.ones((4, 4, 4))}]

    coeffs = [np.ones((2, 2, 2)), {}, {}, {'daa': np.ones((8, 8, 8))}]
    assert_equal(pywt.waverecn(coeffs, 'db1').shape, (16, 16, 16)) 
Example #17
Source File: test_multilevel.py    From DeepLearning_Wavelet-LSTM with MIT License 5 votes vote down vote up
def test_waverecn():
    rstate = np.random.RandomState(1234)
    # test 1D through 4D cases
    for nd in range(1, 5):
        x = rstate.randn(*(4, )*nd)
        coeffs = pywt.wavedecn(x, 'db1')
        assert_(len(coeffs) == 3)
        assert_allclose(pywt.waverecn(coeffs, 'db1'), x, rtol=tol_double) 
Example #18
Source File: solver_l1.py    From OneNet with GNU General Public License v3.0 5 votes vote down vote up
def inverse_wavelet_transform(w_coeffs_rgb, coeff_slices, x_shape):
    x_hat = np.zeros(x_shape)
    for i in range(w_coeffs_rgb.shape[0]):
        w_coeffs_list = pywt.array_to_coeffs(w_coeffs_rgb[i,:,:], coeff_slices)
        x_hat[0,:,:,i] = pywt.waverecn(w_coeffs_list, wavelet='db4', mode='periodization')
    return x_hat