Python pywt.wavedec2() Examples
The following are 25
code examples of pywt.wavedec2().
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: wavelet_basis.py From sparse_gen with MIT License | 6 votes |
def generate_basis(): """generate the basis""" x = np.zeros((56, 56)) coefs = pywt.wavedec2(x, 'db1') n_levels = len(coefs) basis = [] for i in range(n_levels): coefs[i] = list(coefs[i]) n_filters = len(coefs[i]) for j in range(n_filters): for m in range(coefs[i][j].shape[0]): try: for n in range(coefs[i][j].shape[1]): coefs[i][j][m][n] = 1 temp_basis = pywt.waverec2(coefs, 'db1') basis.append(temp_basis) coefs[i][j][m][n] = 0 except IndexError: coefs[i][j][m] = 1 temp_basis = pywt.waverec2(coefs, 'db1') basis.append(temp_basis) coefs[i][j][m] = 0 basis = np.array(basis) return basis
Example #2
Source File: fusion_dwb.py From ImageFusion with MIT License | 6 votes |
def fusion(self): self._load_images() coeffss = [] for image in self._images: coeffss.append(pywt.wavedec2(image, 'db1', level=self._zt)) # low pass if self._mp == 0: cAF = coeffss[0][0] for coeffs in coeffss[1:]: cAF += coeffs[0] cAF = cAF/len(coeffs) # high pass if self._ap == 2: hipassF = coeffss[0][1:] for coeffs in coeffss[1:]: # every image for idxLevel, HVDs in enumerate(coeffs[1:]): # every level for idxDirec, HVD in enumerate(HVDs): maxMap = hipassF[idxLevel][idxDirec] < HVD hipassF[idxLevel][idxDirec][maxMap] = HVD[maxMap] coeffsFusion = [cAF,] + hipassF self._fusionImage = pywt.waverec2(coeffsFusion, 'db1') return self._fusionImage
Example #3
Source File: wavelets.py From opensurfaces with MIT License | 6 votes |
def compute_wavelet_feature_vector(image, wavelet='db6'): image_np = np.array(image) rgb = [image_np[:, :, i] for i in (0, 1, 2)] if isinstance(wavelet, basestring): wavelet = pywt.Wavelet(wavelet) feature_vector = [] for c in rgb: level = pywt.dwt_max_level(min(c.shape[0], c.shape[1]), wavelet.dec_len) levels = pywt.wavedec2(c, wavelet, mode='sym', level=level) for coeffs in levels: if not isinstance(coeffs, tuple): coeffs = (coeffs,) for w in coeffs: w_flat = w.flatten() feature_vector += [float(np.mean(w_flat)), float(np.std(w_flat))] return feature_vector
Example #4
Source File: test_concurrent.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def _assert_all_coeffs_equal(coefs1, coefs2): # return True only if all coefficients of SWT or DWT match over all levels if len(coefs1) != len(coefs2): return False for (c1, c2) in zip(coefs1, coefs2): if isinstance(c1, tuple): # for swt, swt2, dwt, dwt2, wavedec, wavedec2 for a1, a2 in zip(c1, c2): assert_array_equal(a1, a2) elif isinstance(c1, dict): # for swtn, dwtn, wavedecn for k, v in c1.items(): assert_array_equal(v, c2[k]) else: return False return True
Example #5
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
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 #6
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_coeffs_to_array(): # single element list returns the first element a_coeffs = [np.arange(8).reshape(2, 4), ] arr, arr_slices = pywt.coeffs_to_array(a_coeffs) assert_allclose(arr, a_coeffs[0]) assert_allclose(arr, arr[arr_slices[0]]) assert_raises(ValueError, pywt.coeffs_to_array, []) # invalid second element: array as in wavedec, but not 1D assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs[0], ] * 2) # invalid second element: tuple as in wavedec2, but not a 3-tuple assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs[0], (a_coeffs[0], )]) # coefficients as None is not supported assert_raises(ValueError, pywt.coeffs_to_array, [None, ]) assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs, (None, None, None)]) # invalid type for second coefficient list element assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs, None]) # use an invalid key name in the coef dictionary coeffs = [np.array([0]), dict(d=np.array([0]), c=np.array([0]))] assert_raises(ValueError, pywt.coeffs_to_array, coeffs)
Example #7
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_waverec2_odd_length(): x = np.ones((10, 6)) coeffs = pywt.wavedec2(x, 'db1') assert_allclose(pywt.waverec2(coeffs, 'db1'), x, rtol=1e-12)
Example #8
Source File: spatialscores.py From pysteps with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _wavelet_decomp(X, w): c = pywt.wavedec2(X, w) X_out = [] for k in range(len(c)): c_ = c[:] for k_ in set(range(len(c))).difference([k]): c_[k_] = tuple([np.zeros_like(v) for v in c[k_]]) X_k = pywt.waverec2(c_, w) X_out.append(X_k) return X_out
Example #9
Source File: analyze.py From rep0st with MIT License | 5 votes |
def analyze(self, images): image = images['gray'] if self.image_scale is not None: assert self.image_scale & (self.image_scale - 1) == 0, "image_scale is not power of 2" else: image_natural_scale = 2 ** int(numpy.log2(min(image.shape[0:2]))) image_scale = max(image_natural_scale, self.hash_size) ll_max_level = int(numpy.log2(image_scale)) level = int(numpy.log2(self.hash_size)) assert self.hash_size & (self.hash_size - 1) == 0, "hash_size is not power of 2" assert level <= ll_max_level, "hash_size in a wrong range" dwt_level = ll_max_level - level scaled = cv2.resize(image, (image_scale, image_scale), interpolation=cv2.INTER_AREA) pixels = numpy.float32(scaled) pixels /= 255 # Remove low level frequency LL(max_ll) if @remove_max_haar_ll using haar filter if self.remove_max_haar_ll: coeffs = pywt.wavedec2(pixels, 'haar', level=ll_max_level) coeffs = list(coeffs) coeffs[0] *= 0 pixels = pywt.waverec2(coeffs, 'haar') # Use LL(K) as freq, where K is log2(@hash_size) coeffs = pywt.wavedec2(pixels, self.mode, level=dwt_level) dwt_low = coeffs[0] # Substract median and compute hash med = numpy.median(dwt_low) diff = dwt_low > med return numpy.packbits(numpy.uint8(diff.reshape(-1, 1)))
Example #10
Source File: solver_l1.py From OneNet with GNU General Public License v3.0 | 5 votes |
def wavelet_transform(x): w_coeffs_rgb = [] # np.zeros(x.shape[3], np.prod(x.shape)) for i in range(x.shape[3]): w_coeffs_list = pywt.wavedec2(x[0,:,:,i], 'db4', level=None, mode='periodization') w_coeffs, coeff_slices = pywt.coeffs_to_array(w_coeffs_list) w_coeffs_rgb.append(w_coeffs) w_coeffs_rgb = np.array(w_coeffs_rgb) return w_coeffs_rgb, coeff_slices
Example #11
Source File: test_concurrent.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_concurrent_wavedec(): # wavedec on 1D data calls the Cython dwt_single # other cases call dwt_axis for wavedec_func, x in zip([pywt.wavedec, pywt.wavedec2, pywt.wavedecn], [np.ones(8), np.eye(16), np.eye(16)]): transform = partial(wavedec_func, wavelet='haar', level=1) for _ in range(10): arrs = [x.copy() for _ in range(100)] with futures.ThreadPoolExecutor(max_workers=max_workers) as ex: results = list(ex.map(transform, arrs)) # validate result from one of the concurrent runs expected_result = transform(x) _assert_all_coeffs_equal(expected_result, results[-1])
Example #12
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_waverec2_axes_errors(): data = np.ones((4, 4)) c = pywt.wavedec2(data, 'haar') # integer axes not allowed assert_raises(TypeError, pywt.waverec2, c, 'haar', axes=1) # non-unique axes not allowed assert_raises(ValueError, pywt.waverec2, c, 'haar', axes=(0, 0)) # out of range axis not allowed assert_raises(ValueError, pywt.waverec2, c, 'haar', axes=(0, 2))
Example #13
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_waverec2_axes_subsets(): rstate = np.random.RandomState(0) data = rstate.standard_normal((8, 8, 8)) # test all combinations of 2 out of 3 axes transformed for axes in combinations((0, 1, 2), 2): coefs = pywt.wavedec2(data, 'haar', axes=axes) rec = pywt.waverec2(coefs, 'haar', axes=axes) assert_allclose(rec, data, atol=1e-14)
Example #14
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_waverec2_none_coeffs(): x = np.arange(24).reshape(6, 4) coeffs = pywt.wavedec2(x, 'db1') coeffs[1] = (None, None, None) assert_(x.shape == pywt.waverec2(coeffs, 'db1').shape) #### # nd multilevel dwt function tests ####
Example #15
Source File: wavelet.py From vampyre with MIT License | 5 votes |
def __init__(self,nrow=256,ncol=256,wavelet='db4',level=3,fwd_mode='recon',\ dtype=np.float64,name=None): # Save parameters self.wavelet = wavelet self.level = level shape0 = (nrow,ncol) shape1 = (nrow,ncol) dtype0 = dtype dtype1 = dtype if pywt.Wavelet(wavelet).orthogonal: svd_avail = True #SVD calculation assumes an orthogonal wavelet else: svd_avail = False BaseLinTrans.__init__(self, shape0, shape1, dtype0, dtype1,\ svd_avail=svd_avail,name=name) # Set the mode to periodic to make the wavelet orthogonal self.mode = 'periodization' # Send a zero image to get the coefficient slices im = np.zeros((nrow,ncol)) coeffs = pywt.wavedec2(im, wavelet=self.wavelet, level=self.level, \ mode=self.mode) _, self.coeff_slices = pywt.coeffs_to_array(coeffs) # Confirm that fwd_mode is valid if (fwd_mode != 'recon') and (fwd_mode != 'analysis'): raise common.VpException('fwd_mode must be recon or analysis') self.fwd_mode = fwd_mode
Example #16
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_wavedec2_invalid_inputs(): # input array has too few dimensions data = np.ones(4) assert_raises(ValueError, pywt.wavedec2, data, 'haar')
Example #17
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_wavedec2_complex(): data = np.ones((4, 4)) + 1j coeffs = pywt.wavedec2(data, 'db1') assert_(len(coeffs) == 3) assert_allclose(pywt.waverec2(coeffs, 'db1'), data, rtol=1e-12)
Example #18
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_waverec2_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.wavedec2(r, wavelet, mode=mode) assert_allclose(pywt.waverec2(coeffs, wavelet, mode=mode), r, rtol=tol_single, atol=tol_single)
Example #19
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_multilevel_dtypes_2d(): wavelet = pywt.Wavelet('haar') for dt_in, dt_out in zip(dtypes_in, dtypes_out): # wavedec2, waverec2 x = np.ones((8, 8), dtype=dt_in) errmsg = "wrong dtype returned for {0} input".format(dt_in) cA, coeffsD2, coeffsD1 = pywt.wavedec2(x, wavelet, level=2) assert_(cA.dtype == dt_out, "wavedec2: " + errmsg) for c in coeffsD1: assert_(c.dtype == dt_out, "wavedec2: " + errmsg) for c in coeffsD2: assert_(c.dtype == dt_out, "wavedec2: " + errmsg) x_roundtrip = pywt.waverec2([cA, coeffsD2, coeffsD1], wavelet) assert_(x_roundtrip.dtype == dt_out, "waverec2: " + errmsg)
Example #20
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_waverec2_accuracies(): rstate = np.random.RandomState(1234) x0 = rstate.randn(4, 4) for dt, tol in dtypes_and_tolerances: x = x0.astype(dt) if np.iscomplexobj(x): x += 1j*rstate.randn(4, 4).astype(x.real.dtype) coeffs = pywt.wavedec2(x, 'db1') assert_(len(coeffs) == 3) assert_allclose(pywt.waverec2(coeffs, 'db1'), x, atol=tol, rtol=tol)
Example #21
Source File: solver_l1.py From OneNet with GNU General Public License v3.0 | 5 votes |
def wavelet_transform(x): w_coeffs_rgb = [] # np.zeros(x.shape[3], np.prod(x.shape)) for i in range(x.shape[3]): w_coeffs_list = pywt.wavedec2(x[0,:,:,i], 'db4', level=None, mode='periodization') w_coeffs, coeff_slices = pywt.coeffs_to_array(w_coeffs_list) w_coeffs_rgb.append(w_coeffs) w_coeffs_rgb = np.array(w_coeffs_rgb) return w_coeffs_rgb, coeff_slices
Example #22
Source File: wavelet_basis.py From csgm with MIT License | 5 votes |
def generate_basis(): """generate the basis""" x = np.zeros((64, 64)) coefs = pywt.wavedec2(x, 'db1') n_levels = len(coefs) basis = [] for i in range(n_levels): coefs[i] = list(coefs[i]) n_filters = len(coefs[i]) for j in range(n_filters): for m in range(coefs[i][j].shape[0]): try: for n in range(coefs[i][j].shape[1]): coefs[i][j][m][n] = 1 temp_basis = pywt.waverec2(coefs, 'db1') basis.append(temp_basis) coefs[i][j][m][n] = 0 except IndexError: coefs[i][j][m] = 1 temp_basis = pywt.waverec2(coefs, 'db1') basis.append(temp_basis) coefs[i][j][m] = 0 basis = np.array(basis) return basis
Example #23
Source File: celebA_estimators.py From csgm with MIT License | 5 votes |
def get_wavelet(x): coefs_list = [] for i in range(3): coefs_list.append(pywt.wavedec2(x[:, :, i], 'db1')) return coefs_list
Example #24
Source File: wavelet.py From vampyre with MIT License | 5 votes |
def recon(self,z1): """ Wavelet reconstruction: coefficients -> image """ coeffs = pywt.array_to_coeffs(z1, self.coeff_slices, \ output_format='wavedec2') z0 = pywt.waverec2(coeffs, wavelet=self.wavelet, mode=self.mode) return z0
Example #25
Source File: wavelet.py From vampyre with MIT License | 5 votes |
def analysis(self,z0): """ Analysis: image -> coefficients """ coeffs = pywt.wavedec2(z0, wavelet=self.wavelet, level=self.level, \ mode=self.mode) z1, _ = pywt.coeffs_to_array(coeffs) return z1