def test_swt_max_level(): # odd sized signal will warn about no levels of decomposition possible assert_warns(UserWarning, pywt.swt_max_level, 11) with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) assert_equal(pywt.swt_max_level(11), 0) # no warnings when >= 1 level of decomposition possible assert_equal(pywt.swt_max_level(2), 1) # divisible by 2**1 assert_equal(pywt.swt_max_level(4*3), 2) # divisible by 2**2 assert_equal(pywt.swt_max_level(16), 4) # divisible by 2**4 assert_equal(pywt.swt_max_level(16*3), 4) # divisible by 2**4
def test_swt_decomposition(): x = [3, 7, 1, 3, -2, 6, 4, 6] db1 = pywt.Wavelet('db1') (cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=2) assert_allclose(cA1, [ 7.07106781, 5.65685425, 2.82842712, 0.70710678, 2.82842712, 7.07106781, 7.07106781, 6.36396103 ]) assert_allclose(cD1, [ -2.82842712, 4.24264069, -1.41421356, 3.53553391, -5.65685425, 1.41421356, -1.41421356, 2.12132034 ]) expected_cA2 = [7, 4.5, 4, 5.5, 7, 9.5, 10, 8.5] assert_allclose(cA2, expected_cA2, rtol=1e-12) expected_cD2 = [3, 3.5, 0, -4.5, -3, 0.5, 0, 0.5] assert_allclose(cD2, expected_cD2, rtol=1e-12, atol=1e-14) # level=1, start_level=1 decomposition should match level=2 res = pywt.swt(cA1, db1, level=1, start_level=1) cA2, cD2 = res[0] assert_allclose(cA2, expected_cA2, rtol=1e-12) assert_allclose(cD2, expected_cD2, rtol=1e-12, atol=1e-14) coeffs = pywt.swt(x, db1) assert_(len(coeffs) == 3) assert_(pywt.swt_max_level(len(x)) == 3)
def c_dists(Y, use_swt=True, level_weights=False): w = pywt.Wavelet('sym2') if use_swt: L = pywt.swt_max_level(Y.shape[0]) C = [pywt.swt(Y[:, i], w, level=L) for i in range(Y.shape[1])] C = [[list(reshape(l[0], -1)) + list(reshape(l[1], -1)) for l in c] for c in C] else: L = pywt.dwt_max_level(Y.shape[0], w) C = [pywt.wavedec(Y[:, i], w, level=L) for i in range(Y.shape[1])] if level_weights: if use_swt: raise NameError('No level weights with SWT') Wc = [1. for x in range(1, L + 1)] D = zeros((len(C), len(C))) for i in range(len(C)): for j in range(i + 1, len(C)): d = sum([ distance.cosine(C[i][x], C[j][x]) * Wc[x] for x in range(L) ]) / sum(Wc) D[i, j] = d D[j, i] = d return D else: Cn = [] for c in C: cn = [] for l in c: cn += list(l) Cn.append(cn) return abs(pdist(Cn, 'cosine'))
def test_swt_default_level_by_axis(): # make sure default number of levels matches the max level along the axis wav = 'db2' x = np.ones((2**3, 2**4, 2**5)) for axis in (0, 1, 2): sdec = pywt.swt(x, wav, level=None, start_level=0, axis=axis) assert_equal(len(sdec), pywt.swt_max_level(x.shape[axis]))
def test_swt_decomposition(): x = [3, 7, 1, 3, -2, 6, 4, 6] db1 = pywt.Wavelet('db1') atol = tol_double (cA3, cD3), (cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=3) expected_cA1 = [7.07106781, 5.65685425, 2.82842712, 0.70710678, 2.82842712, 7.07106781, 7.07106781, 6.36396103] assert_allclose(cA1, expected_cA1, rtol=1e-8, atol=atol) expected_cD1 = [-2.82842712, 4.24264069, -1.41421356, 3.53553391, -5.65685425, 1.41421356, -1.41421356, 2.12132034] assert_allclose(cD1, expected_cD1, rtol=1e-8, atol=atol) expected_cA2 = [7, 4.5, 4, 5.5, 7, 9.5, 10, 8.5] assert_allclose(cA2, expected_cA2, rtol=tol_double, atol=atol) expected_cD2 = [3, 3.5, 0, -4.5, -3, 0.5, 0, 0.5] assert_allclose(cD2, expected_cD2, rtol=tol_double, atol=atol) expected_cA3 = [9.89949494, ] * 8 assert_allclose(cA3, expected_cA3, rtol=1e-8, atol=atol) expected_cD3 = [0.00000000, -3.53553391, -4.24264069, -2.12132034, 0.00000000, 3.53553391, 4.24264069, 2.12132034] assert_allclose(cD3, expected_cD3, rtol=1e-8, atol=atol) # level=1, start_level=1 decomposition should match level=2 res = pywt.swt(cA1, db1, level=1, start_level=1) cA2, cD2 = res[0] assert_allclose(cA2, expected_cA2, rtol=tol_double, atol=atol) assert_allclose(cD2, expected_cD2, rtol=tol_double, atol=atol) coeffs = pywt.swt(x, db1) assert_(len(coeffs) == 3) assert_(pywt.swt_max_level(len(x)), 3)
def __init__(self, signal, wavelet): super(SWTDistribution, self).__init__() self.level = pywt.swt_max_level(signal.shape[0]) self.swt = np.asarray( [D**2 for _, D in pywt.swt(signal, wavelet, level=self.level)]) self.value = np.flip(np.sum(self.swt, axis=1) / np.sum(self.swt), axis=-1)
def get_swt_coeffs(self, img, wavelet='db1', downsample="True"): p = self.get_image(img, gray=True) # downsampling if downsample: p = pywt.dwt2(p, wavelet)[0] # calculate maximum decomposition level l_max = pywt.swt_max_level(min(np.shape(p))) self.max_level = l_max # Multilevel 2D stationary wavelet transform with wavelet filters. coefs = pywt.swt2(p, wavelet, l_max, start_level=0, trim_approx=True, norm=True) # converting from float64 to uint8 to save memory coefs[0] = coefs[0].astype(np.uint8) for level in range(1, l_max): coefs[level] = list(coefs[level]) for band in range(3): picture = np.asarray(coefs[level][band]) picture -= np.min(picture) picture *= 255 / np.max(picture) picture = picture.astype(np.uint8) coefs[level][band] = picture return coefs
def get_wdc(signal): num_dwt_scales = int(ConfigParams['NUM_WDC_SCALES']) dwt_segment = pow(2, num_dwt_scales) dwt_shift = len(signal) % dwt_segment if dwt_shift != 0: signal = signal[0:len(signal) - dwt_shift] max_decomposition_level = pywt.swt_max_level(len(signal)) custom_wavelet = CustomWavelet # custom_wavelet = pywt.Wavelet('bior1.5') coeffs = pywt.swt(signal, custom_wavelet, level=num_dwt_scales, start_level=0) wdc = [] wdc_shifts = [0, 1, 3, 7, 15, 31, 65] for scale_id in range(0, num_dwt_scales): wdc_current_scale = coeffs[num_dwt_scales - 1 - scale_id][1] if scale_id > 0: list_of_zeros = np.asarray([0] * wdc_shifts[scale_id]) wdc_current_scale = wdc_current_scale[0:-wdc_shifts[scale_id]] wdc_current_scale = np.concatenate((list_of_zeros, wdc_current_scale)) wdc.append(wdc_current_scale) return wdc
def get_wavelet_feature(lightcurve): """ Returns wavelet coefficients for a given lightcurve object. Param ------ lightcurve: Lightcurve object An instance of the Lightcurve class """ interp_flux = lightcurve.interp_flux feats = [] for i in range(len(interp_flux)): flux = interp_flux[i] mlev = pywt.swt_max_level(len(flux)) coeffs = np.array(pywt.swt(flux, 'sym2', level=mlev)) npoints = len(coeffs[0, 0, :]) c = coeffs.reshape(mlev * 2, npoints).T wavout = c.flatten('F') feats.append(wavout) lc_logger.info( "Func get_wavelet_feature() extracted features from lightcurve " + lightcurve.filename) feats = np.array(feats) return feats
def c_dists(Y,use_swt=True,level_weights=False): w = pywt.Wavelet('sym2') if use_swt: L = pywt.swt_max_level(Y.shape[0]) C = [pywt.swt(Y[:,i],w,level=L) for i in range(Y.shape[1])] C = [[list(reshape(l[0],-1)) + list(reshape(l[1],-1)) for l in c] for c in C] else: L = pywt.dwt_max_level(Y.shape[0],w) C = [pywt.wavedec(Y[:,i],w,level=L) for i in range(Y.shape[1])] if level_weights: if use_swt: raise NameError('No level weights with SWT') Wc = [1. for x in range(1,L+1)] D = zeros((len(C),len(C))) for i in range(len(C)): for j in range(i+1,len(C)): d = sum([distance.cosine(C[i][x],C[j][x])*Wc[x] for x in range(L)])/sum(Wc) D[i,j] = d D[j,i] = d return D else: Cn = [] for c in C: cn = [] for l in c: cn += list(l) Cn.append(cn) return abs(pdist(Cn,'cosine'))
def setup(self, n, wavelet): try: from pywt import swt2 except ImportError: raise NotImplementedError("swt2 not available") self.data = np.ones((n, n), dtype='float') self.level = pywt.swt_max_level(n)
def test_swt_decomposition(): x = [3, 7, 1, 3, -2, 6, 4, 6] db1 = pywt.Wavelet('db1') (cA3, cD3), (cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=3) expected_cA1 = [7.07106781, 5.65685425, 2.82842712, 0.70710678, 2.82842712, 7.07106781, 7.07106781, 6.36396103] assert_allclose(cA1, expected_cA1) expected_cD1 = [-2.82842712, 4.24264069, -1.41421356, 3.53553391, -5.65685425, 1.41421356, -1.41421356, 2.12132034] assert_allclose(cD1, expected_cD1) expected_cA2 = [7, 4.5, 4, 5.5, 7, 9.5, 10, 8.5] assert_allclose(cA2, expected_cA2, rtol=tol_double) expected_cD2 = [3, 3.5, 0, -4.5, -3, 0.5, 0, 0.5] assert_allclose(cD2, expected_cD2, rtol=tol_double, atol=1e-14) expected_cA3 = [9.89949494, ] * 8 assert_allclose(cA3, expected_cA3) expected_cD3 = [0.00000000, -3.53553391, -4.24264069, -2.12132034, 0.00000000, 3.53553391, 4.24264069, 2.12132034] assert_allclose(cD3, expected_cD3) # level=1, start_level=1 decomposition should match level=2 res = pywt.swt(cA1, db1, level=1, start_level=1) cA2, cD2 = res[0] assert_allclose(cA2, expected_cA2, rtol=tol_double) assert_allclose(cD2, expected_cD2, rtol=tol_double, atol=1e-14) coeffs = pywt.swt(x, db1) assert_(len(coeffs) == 3) assert_(pywt.swt_max_level(len(x)) == 3)
def multilevel_dwt_decomposition_example(): x = [3, 7, 1, 1, -2, 5, 4, 6] # Multilevel DWT decomposition. db1 = pywt.Wavelet('db1') cA3, cD3, cD2, cD1 = pywt.wavedec(x, db1) print('Approximation coefficients (level 3) =', cA3) print('Detail coefficients (level 3) =', cD3) print('Detail coefficients (level 2) =', cD2) print('Detail coefficients (level 1) =', cD1) # Multilevel IDWT reconstruction. coeffs = pywt.wavedec(x, db1) print('Signal reconstruction =', pywt.waverec(coeffs, db1)) # Multilevel stationary wavelet transform (SWT) decomposition. x = [3, 7, 1, 3, -2, 6, 4, 6] (cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=2) print('Approximation coefficients (level 1) =', cA1) print('Detail coefficients (level 1) =', cD1) print('Approximation coefficients (level 2) =', cA2) print('Detail coefficients (level 2) =', cD2) [(cA2, cD2)] = pywt.swt(cA1, db1, level=1, start_level=1) print('Approximation coefficients (level 2) =', cA2) print('Detail coefficients (level 2) =', cD2) coeffs = pywt.swt(x, db1) print('Coefficients =', coeffs) print('Length of coefficients =', len(coeffs)) print('Max level =', pywt.swt_max_level(len(x)))
def swt(data, wavelet, level=None): """ Stationary Wavelet Transform This version is 2 orders of magnitude faster than the one in pywt even though it uses pywt for all the calculations. Input parameters: data One-dimensional data to transform wavelet Either the name of a wavelet or a Wavelet object level Number of levels """ if level is None: level = pywt.swt_max_level(len(data)) num_levels = level idata = data.copy() res = [] for j in range(1,num_levels+1): step_size = int(math.pow(2, j-1)) last_index = step_size # allocate cA = np.empty_like(data) cD = np.empty_like(data) for first in xrange(last_index): # 0 to last_index - 1 # Getting the indices that we will transform indices = np.arange(first, len(cD), step_size) # select the even indices even_indices = indices[0::2] # select the odd indices odd_indices = indices[1::2] # get the even (cA1,cD1) = pywt.dwt(idata[indices], wavelet, 'per') cA[even_indices] = cA1 cD[even_indices] = cD1 # then the odd (cA1,cD1) = pywt.dwt(np.roll(idata[indices],-1), wavelet, 'per') cA[odd_indices] = cA1 cD[odd_indices] = cD1 # set the data for the next loop idata = cA # prepend the result res.insert(0,(cA,cD)) return res
def getSWTcoeflist(self,MaxLevel = 9): # crop two leads self.rawsig = self.crop_data_for_swt(self.rawsig) print '-'*3 print 'len(rawsig)= ',len(self.rawsig) print 'SWT maximum level =',pywt.swt_max_level(len(self.rawsig)) coeflist = pywt.swt(self.rawsig,'db6',MaxLevel) cAlist,cDlist = zip(*coeflist) self.cAlist = cAlist self.cDlist = cDlist
def main(): noise_sigma = 2 wavelet = 'bior1.3' images = list(io.load_fits_images_in_folder('./resources')) with PdfPages('multipage.pdf') as pdf: for i, (calibrated_image, npe_truth) in tqdm(enumerate(images)): fig = plt.figure() plt.text(1, 1, 'Event Number: {}'.format(i)) plt.xlim(0, 2) plt.ylim(0, 2) plt.axis('off') pdf.savefig(fig) plt.close() # transform the image level = pywt.swt_max_level(len(calibrated_image)) # print('maximum level of decomposition: {}'.format(level)) coeff_list = pywt.swt2(calibrated_image, wavelet, level) fig = plot.coefficients(coeff_list) pdf.savefig(figure=fig) plt.close() levels = [0.889, 0.7, 0.586] coeff_list = denoise.thresholding(coeff_list, sigma_d=noise_sigma, kind='hard', sigma_levels=levels) # coeff_list = denoise.wiener(coeff_list) reconstructed_image = pywt.iswt2(coeff_list, wavelet) fig = plot.results(calibrated_image, reconstructed_image, npe_truth) pdf.savefig(figure=fig) plt.close() fig = plot.pixel_histogram( reconstructed_image, calibrated_image, npe_truth, labels=['reconstructed', 'calibrated', 'npe mc truth'], bins=60) pdf.savefig(figure=fig) plt.close()
def denoise(self): """denoise the data using the 2stage kurtosis denoising""" #make sure the data has a len dividible by 2^2 self.len_swt = self.len while not (self.len_swt / 4).is_integer(): self.len_swt -= 1 inp = self.input_nobase[:self.len_swt] self.wave = pywt.Wavelet(self.wave_type) nLevel = pywt.swt_max_level(self.len_swt) self.coeffs = pywt.swt(inp, self.wave, level=2) print(" \t Denoise STW coefficients \t %1.2f %1.2f" % (self.TK, self.TT)) (cA2, cD2), (cA1, cD1) = self.coeffs # rolling kurtosis k2 = self._rolling_kts(cD2, self.nwin) k1 = self._rolling_kts(cD1, self.nwin) # thresholding cD2[k2 < self.TK] = 0 cD1[k1 < self.TK] = 0 cA2[k2 < self.TK] = 0 cA1[k1 < self.TK] = 0 # universal threshold sigma_roll_1 = mad(cD1[cD1 != 0]) * np.ones(self.len_swt) uthresh_roll_1 = self.TT * sigma_roll_1 * np.sqrt( 2 * np.log(self.len_swt)) cD1[abs(cD1) < uthresh_roll_1] = 0 # universal threshold sigma_roll_2 = mad(cD2[cD2 != 0]) * np.ones(self.len_swt) uthresh_roll_2 = self.TT * sigma_roll_2 * np.sqrt( 2 * np.log(self.len_swt)) cD2[abs(cD2) < uthresh_roll_2] = 0 # final threshold cA1[cD1 == 0] = 0 cA2[cD2 == 0] = 0 self.denoised_coeffs = [(cA1, cD1), (cA2, cD2)] # denoise the data #self.input_denoised = self._iswt(self.denoised_coeffs,self.wave) self.input_denoised = pywt.iswt(self.denoised_coeffs, self.wave)
def _swt(self, sequence): """ Use stationary wavelet transform method. """ details_filename = self.options.out_filename + '_details.txt' details_handle = open(details_filename, 'w') padded_sequence = self._pad_sequence(sequence) sequence_encoding = self.encode_sequence(padded_sequence) level = pywt.swt_max_level(len(sequence_encoding)) coeffs = pywt.swt(sequence_encoding, 'haar', level) for (cA, cD) in coeffs: approx_handle.write('\t'.join(str(a) for a in cA) + '\n') details_handle.write('\t'.join(str(d) for d in cD) + '\n') details_handle.close() return details_filename
def w2decomp(img, mode='bior3.5', _compute=True): # Run all available wavelet decomp # return an image cube imArray = fits.getdata(img) # DataPrep conversions imArray[imArray < 0.1] = 0.1 imArray = np.float32(np.sqrt(imArray)) normal = imArray.max() imArray /= imArray.max(); max_lvl = pywt.swt_max_level(len(imArray)) out_imgs = np.zeros((imArray.shape[0], imArray.shape[1], max_lvl - 1)) for level in range(1, max_lvl): if _compute: lw = pywt.swt2(imArray, mode, level=level)[0][0] out_imgs[:, :, level - 1] = lw return out_imgs
def calculate_suitable_lvl(data, wv, r, swt=True): # stationary if swt: max_lvl = pywt.swt_max_level(len(data)) lvl = 1 ent = [] pre_e = entropy(pywt.swt(data, wv, lvl), r) ent.append(pre_e) lvl += 1 while True: new_e = entropy(pywt.swt(data, wv, lvl), r) ent.append(new_e) if lvl < max_lvl: lvl += 1 else: break e_sorted = sorted(ent[:]) median = e_sorted[len(e_sorted) / 2] lvl = ent.index(median) + 1 # discrete else: lvl = 1 data_e = entropy(data, r, True) max_lvl = pywt.dwt_max_level(len(data), wv) while True: new_e = entropy(pywt.dwt(data, wv, lvl)[lvl - 1], r, True) if new_e > data_e: break elif lvl == max_lvl: break else: lvl += 1 if lvl == max_lvl: pass return lvl
def test_swt_decomposition(): x = [3, 7, 1, 3, -2, 6, 4, 6] db1 = pywt.Wavelet("db1") (cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=2) assert_allclose( cA1, [7.07106781, 5.65685425, 2.82842712, 0.70710678, 2.82842712, 7.07106781, 7.07106781, 6.36396103] ) assert_allclose( cD1, [-2.82842712, 4.24264069, -1.41421356, 3.53553391, -5.65685425, 1.41421356, -1.41421356, 2.12132034] ) expected_cA2 = [7, 4.5, 4, 5.5, 7, 9.5, 10, 8.5] assert_allclose(cA2, expected_cA2, rtol=1e-12) expected_cD2 = [3, 3.5, 0, -4.5, -3, 0.5, 0, 0.5] assert_allclose(cD2, expected_cD2, rtol=1e-12, atol=1e-14) # level=1, start_level=1 decomposition should match level=2 res = pywt.swt(cA1, db1, level=1, start_level=1) cA2, cD2 = res[0] assert_allclose(cA2, expected_cA2, rtol=1e-12) assert_allclose(cD2, expected_cD2, rtol=1e-12, atol=1e-14) coeffs = pywt.swt(x, db1) assert_(len(coeffs) == 3) assert_(pywt.swt_max_level(len(x)) == 3)
def maxdeclevel(self): maxlev = pywt.swt_max_level(len(self.data)) return maxlev
samprate, start_wf1, num_samp1 = gy.ReadWfm(wf1, wfdisc, start_e, end_e, station1, verbose) # # Pad with zeroes until length reaches the next power of two # w = wlt.Wavelet('db1') N=1 n=1 while (len(wf1) > N): N = N*2 if(len(wf1) < N): for i in range(N-len(wf1)): wf1.append(0.) len1 = wlt.swt_max_level(N) n1 = len(wf1) len3 = wlt.swt_max_level(n1) len2 = wlt.dwt_max_level(N, w.dec_len) print(N, 'swt:',len1,'dwt:',len2,len3,n1) n += 1 for i in range(N): add.append(0.) start_e += time_interval for i in range(N) : tt.append((float(i)/samprate)) if (test_case): d1,d2 = gy.ReadSite(sfile, triad, 0) theta1 = (theta1+90.)*np.pi/180. dt1,dt2 = gy.CompDt(d1,d2,theta1,1.48) for i in xrange (len(wf1)):
>>> print cA2 [ 7. 4.5 4. 5.5 7. 9.5 10. 8.5] >>> print cD2 [ 3. 3.5 0. -4.5 -3. 0.5 0. 0.5] >>> coeffs = pywt.swt(x, db1) >>> len(coeffs) 3 >>> pywt.swt_max_level(len(x)) 3 4 2D DWT和IDWT 4.1 2D一阶变换dwt2 dwt2(data, wavelet, mode=’sym’) 返回 (cA, (cH, cV, cD)), 分别是逼近、水平细节、垂直细节和对角线细节 >>> import pywt, numpy >>> data = numpy.ones((4,4), dtype=numpy.float64)
def run(self): self.list = [] # separate background from foreground if int(self.FG_MEDIAN) % 2 == 0: raise FeatureException('FG_MEDIAN must be an odd integer.') if int(self.BG_MEDIAN) % 2 == 0: raise FeatureException('BG_MEDIAN must be an odd integer.') image_fg = cv2.medianBlur(self.image, int(self.FG_MEDIAN)).astype(np.float32) image_bg = cv2.medianBlur(self.image, int(self.BG_MEDIAN)).astype(np.float32) image_nobg = image_fg - image_bg image_nobg = self._feature_scale(image_nobg, 0, 255, np.uint8) self._image_debug_save('image_nobg', image_nobg) # decompose the image into detail levels input_len = self.image.shape[0] maxl = pywt.swt_max_level(input_len) coeffs = pywt.swt2(image_nobg, 'haar', level=maxl, start_level=0) # horizontal detail mix l0 + l1 lh0 = coeffs[0][1][0] lh1 = coeffs[1][1][0] lh01 = cv2.addWeighted(lh0, self.L0MUL, lh1, self.L1MUL, 0) # vertical detail mix l0 + l1 hl0 = coeffs[0][1][1] hl1 = coeffs[1][1][1] hl01 = cv2.addWeighted(hl0, self.L0MUL, hl1, self.L1MUL, 0) # mix both together img_mixed = cv2.addWeighted(lh01, 0.5, hl01, 0.5, 0) # feature scale and cast to 8 bit unsigned img_mixed = self._feature_scale(img_mixed, 0, 255, np.uint8) # apply histogram equalisation and blur img_mixed = cv2.medianBlur(img_mixed, 5).astype(np.uint8) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) img_mixed = clahe.apply(img_mixed) img_mixed = cv2.GaussianBlur(img_mixed, (9, 9), 0) self._image_debug_save('Vert. + Horiz. L0+1 coefficients', img_mixed) # calculate brightness histogram and thresholding values img_ravel = img_mixed.ravel() hist, bin_edges = np.histogram(img_ravel, 256, [0, 256], density=True) loc, scale = norm.fit(img_ravel) thresh_end = int(norm.ppf(0.5, loc=loc, scale=scale)) thresh_st1 = int(norm.ppf(0.005, loc=loc, scale=scale)) thresh_st2 = int(norm.ppf(0.001, loc=loc, scale=scale)) #print(thresh_end, thresh_st1, thresh_st2) """# attempt at removing image defects lh4 = coeffs[3][1][0] hl4 = coeffs[3][1][1] l4 = cv2.addWeighted(lh4, 0.5, hl4, 0.5, 0) l4 = coeffs[2][1][2] l4 = self._feature_scale(l4, 0, 255, np.uint8) #l4 = cv2.GaussianBlur(l4, (3, 3), 0) l4 = cv2.medianBlur(l4, 5).astype(np.uint8) t, l4thresh = cv2.threshold(l4, int(norm.ppf(0.002, loc=loc, scale=scale)), thresh_end, cv2.THRESH_TOZERO_INV) #t, l4thresh2 = cv2.threshold(l4thresh, int(norm.ppf(0.001, loc=loc, scale=scale)), thresh_end, cv2.THRESH_BINARY_INV) self._image_debug_save('Vert. + Horiz. L3 coefficients', l4thresh)""" """# debug histogram plot fig, ax = plt.subplots(1, 1) #ax.plot(x, pdf) plt.hist(img_ravel, 256, [0, 256]) plt.show()""" # prominent features t, img_thresh1 = cv2.threshold(img_mixed, thresh_st1, thresh_end, cv2.THRESH_TRUNC) self._image_debug_save('After first threshold', img_thresh1) t, img_thresh2 = cv2.threshold(img_mixed, thresh_st2, thresh_end, cv2.THRESH_BINARY_INV) self._image_debug_save('After second threshold', img_thresh2) labels = self._thresh_and_label(img_thresh2) # discriminate based on expected properties labels_work = labels labels_large = [] self.MIN_FEATURE_AREA = int(self.MIN_FEATURE_AREA) self.MAX_BBOX_OVERLAP = float(self.MAX_BBOX_OVERLAP) self.MAX_OVERLAP = float(self.MAX_OVERLAP) for region1 in labels: # remove very small features if region1.area < self.MIN_FEATURE_AREA: labels_work.remove(region1) continue # remove obvious artifacts (features with too low width / height ratio) dim = region1.dim ratio = min(dim[0], dim[1]) / max(dim[0], dim[1]) if ratio < 0.1: labels_work.remove(region1) continue # remove overlapping features for region2 in labels: if region1 == region2: continue if region1.bboxarea > region2.bboxarea: continue overlap = region1.overlap(region2) if (overlap[3] >= self.MAX_OVERLAP or overlap[2] >= self.MAX_BBOX_OVERLAP) and region1 in labels_work: labels_work.remove(region1) break # if the feature is large, save it for the next pass if region1.area >= self.LARGE_FEATURE_AREA: region1.is_unclear = True labels_large.append(region1) labels = labels_work # pass to break up "large" features for region in labels_large: img_slice = img_mixed[region.br1:region.br2, region.bc1:region.bc2] t, img_thresh = cv2.threshold(img_slice, thresh_st2, thresh_end, cv2.THRESH_BINARY_INV) labels_new = self._thresh_and_label(img_thresh) # translate the newly found features with respect to their slice coordinates for f in labels_new: f.move(region.bx1 + f.x, region.by1 + f.y) labels = labels + labels_new # final pass to remove overlapping features labels_work = labels for region1 in labels: if region1.is_unclear == True: continue for region2 in labels: if region1 == region2: continue if region1.bboxarea > region2.bboxarea: continue if region2.is_unclear == True: continue overlap = region1.overlap(region2) if (overlap[2] >= self.MAX_BBOX_OVERLAP) and region1 in labels_work: #labels_work.remove(region1) break labels = labels_work #print('len(labels)', len(labels)) self.list = labels
def modelling_cycle(): #--------------- initialization -------------------# # initial_data = test_data initial_data = test_data_one # fig_init = plt.figure() # fig_init.canvas.manager.set_window_title('Initial data') # plt.plot(initial_data, color='g') wavelet_families = pywt.families() print 'Wavelet families:', ', '.join(wavelet_families) wavelet_family = wavelet_families[4] selected_wavelet = pywt.wavelist(wavelet_family)[0] wavelet = pywt.Wavelet(selected_wavelet) print 'Selected wavelet:', selected_wavelet max_level = pywt.swt_max_level(len(initial_data)) # decomposition_level = max_level / 2 decomposition_level = 3 print 'Max level:', max_level, '\t Decomposition level:', decomposition_level #--------------- decomposition -------------------# w_initial_coefficients = pywt.swt(initial_data, wavelet, level=decomposition_level) w_selected_coefficiets = select_levels_from_swt(w_initial_coefficients) w_node_coefficients = select_node_levels_from_swt(w_initial_coefficients) #something terribly wrong here, yet the rest works! #------------------ threshold --------------------# threshold = measure_threshold(w_initial_coefficients) w_threshold_coeff = w_initial_coefficients[:] apply_threshold(w_threshold_coeff) plot_initial_updated(w_initial_coefficients, w_threshold_coeff) # plt.figure() # for coeff in w_selected_coefficiets: # plt.plot(coeff) # plt.figure() # for coeff in w_node_coefficients: # plt.plot(coeff) # plt.show() #--------------- modification -------------------# r = R() w_new_coefficients = [0] * len(w_selected_coefficiets) for index in range(0, len(w_selected_coefficiets)): r.i_data = w_selected_coefficiets[index] r('hw <- HoltWinters( ts(i_data, frequency = 12), gamma = TRUE )') r('pred <- predict(hw, 50, prediction.interval = TRUE)') w_new_coefficients[index] = append(w_selected_coefficiets[index], r.pred[:,0]) index += 1 w_new_node_coefficients = [0] * len(w_node_coefficients) for index in range(0, len(w_node_coefficients)): r.i_data = w_node_coefficients[index] r('hw <- HoltWinters( ts(i_data, frequency = 12), gamma = TRUE )') r('pred <- predict(hw, 50, prediction.interval = TRUE)') w_new_node_coefficients[index] = append(w_node_coefficients[index], r.pred[:,0]) index += 1 #---- # plt.figure() # for coeff in w_new_coefficients: # plt.plot(coeff) # plt.figure() # for coeff in w_new_node_coefficients: # plt.plot(coeff) # plt.show() #--------------- reconstruction -------------------# # wInitialwithUpdated_Nodes = update_node_levels_swt(w_initial_coefficients, w_new_node_coefficients) # plot_initial_updated(w_initial_coefficients, w_new_node_coefficients, True) # plot_initial_updated(w_initial_coefficients, wInitialwithUpdated_Nodes) (!) # plt.figure() # for dyad in wInitialwithUpdated_Nodes: # plt.plot(dyad[0]) # plt.plot(dyad[1]) # # plt.figure() # for dyad in w_initial_coefficients: # plt.plot(dyad[0]) # plt.plot(dyad[1]) # # plt.show() # w_updated_coefficients = update_selected_levels_swt(w_initial_coefficients, w_selected_coefficiets) # w_updated_coefficients = update_selected_levels_swt(w_initial_coefficients, w_new_coefficients) #---- # w_updated_coefficients = update_swt(w_initial_coefficients, w_selected_coefficiets, w_node_coefficients) w_updated_coefficients_nodes = update_swt(w_initial_coefficients, w_new_coefficients, w_new_node_coefficients) w_updated_coefficients = update_selected_levels_swt(w_initial_coefficients, w_new_coefficients) plot_initial_updated(w_initial_coefficients, w_updated_coefficients_nodes) plot_initial_updated(w_initial_coefficients, w_updated_coefficients) reconstructed_Stationary_nodes = iswt(w_updated_coefficients_nodes, selected_wavelet) reconstructed_Stationary = iswt(w_updated_coefficients, selected_wavelet) fig_sta_r = plt.figure() fig_sta_r.canvas.manager.set_window_title('SWT reconstruction') plt.plot(reconstructed_Stationary) fig_sta_r_n = plt.figure() fig_sta_r_n.canvas.manager.set_window_title('SWT reconstruction (nodes)') plt.plot(reconstructed_Stationary_nodes) plt.show()
print ("Filtered 2, length:", len(wf2)) fltwf3 = scipy.signal.lfilter(b, a, wf3) print ("Filtered 3, length:", len(wf3)) n1 = len(wf1) elif(wavelet_transform): w = wlt.Wavelet(wlt_type) N=1 n=1 while (len(wf1) > N): N = N*2 if(len(wf1) < N): for i in range(N-len(wf1)): wf1.append(0.) wf2.append(0.) wf3.append(0.) len1 = wlt.swt_max_level(N) n1 = len(wf1) len3 = wlt.swt_max_level(n1) len2 = wlt.dwt_max_level(N, w.dec_len) print(N, 'swt:',len1,'dwt:',len2,len3,n1) n += 1 for i in range(N): add1.append(0.) add2.append(0.) add3.append(0.) [(cA7, cD7),(cA6, cD6),(cA5, cD5),(cA4, cD4),(cA3, cD3),(cA2, cD2), (cA1, cD1)] = wlt.swt(wf1, wlt_type, 7, start_level=start_level) if(len(add_levels) > 0): # figadd = plt.figure(figsize=(10,6)) # ax1=plt.subplot(511) # plt.title(label)