def testFilterWavelet(self): numExamples = 100 numFeatures = 16 X = numpy.random.rand(numExamples, numFeatures) level = 10 mode = "cpd" waveletStr = "db4" C = pywt.wavedec(X[0, :], waveletStr, mode, level=10) Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode) N = 10 Xw2, inds = MetabolomicsUtils.filterWavelet(Xw, N) tol = 10**-6 self.assertEquals(inds.shape[0], N) self.assertTrue(numpy.linalg.norm( Xw[:, inds] - Xw2[:, inds] ) < tol) zeroInds = numpy.setdiff1d(numpy.arange(Xw.shape[1]), inds) self.assertTrue(numpy.linalg.norm(Xw2[:, zeroInds]) < tol)
dataDir = PathDefaults.getDataDir() + "metabolomic/" X, X2, Xs, XOpls, YList, ages, df = MetabolomicsUtils.loadData() mode = "cpd" level = 10 XwDb4 = MetabolomicsUtils.getWaveletFeatures(X, 'db4', level, mode) XwDb8 = MetabolomicsUtils.getWaveletFeatures(X, 'db8', level, mode) XwHaar = MetabolomicsUtils.getWaveletFeatures(X, 'haar', level, mode) #Filter the wavelets Ns = [10, 25, 50, 75, 100] dataList = [] for i in range(len(Ns)): N = Ns[i] XwDb4F, inds = MetabolomicsUtils.filterWavelet(XwDb4, N) dataList.append((XwDb4F[:, inds], "Db4-" + str(N))) XwDb8F, inds = MetabolomicsUtils.filterWavelet(XwDb8, N) dataList.append((XwDb8F[:, inds], "Db8-" + str(N))) XwHaarF, inds = MetabolomicsUtils.filterWavelet(XwHaar, N) dataList.append((XwHaarF[:, inds], "Haar-" + str(N))) dataList.extend([(Xs, "raw_std"), (XwDb4, "Db4"), (XwDb8, "Db8"), (XwHaar, "Haar"), (X2, "log"), (XOpls, "opls")]) #Data for functional TreeRank dataListF = [(XwDb4, "Db4"), (XwDb8, "Db8"), (XwHaar, "Haar")] dataListPCA = ([(Xs, "raw_std"), (XwDb4, "Db4"), (XwDb8, "Db8"), (XwHaar, "Haar"), (X2, "log"), (XOpls, "opls")]) lock = multiprocessing.Lock()
Ns = range(0, 700, 50) waveletStrs = ['haar', 'db4', 'db8'] errors = numpy.zeros((len(waveletStrs), len(Ns))) mode = "cpd" standardiser = Standardiser() #X = standardiser.centreArray(X) for i in range(len(waveletStrs)): waveletStr = waveletStrs[i] Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode) C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode) for j in range(len(Ns)): N = Ns[j] Xw2, inds = MetabolomicsUtils.filterWavelet(Xw, N) X2 = MetabolomicsUtils.reconstructSignal(X, Xw2, waveletStr, mode, C) errors[i, j] = numpy.linalg.norm(X - X2) #Plot example wavelet after filtering waveletStr = "haar" N = 100 Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode) C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode) Xw2, inds = MetabolomicsUtils.filterWavelet(Xw, N) X2 = MetabolomicsUtils.reconstructSignal(X, Xw2, waveletStr, mode, C) plt.figure(3) plt.plot(range(X.shape[1]), X[0, :]) plt.plot(range(X.shape[1]), X2[0, :])