def testGetWaveletFeaturesTest(self): #See if we can reproduce the data from the wavelet X, X2, Xs, Xopls, YList, df = MetabolomicsUtils.loadData() waveletStr = 'db4' mode = "zpd" level = 10 C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode) X0 = pywt.waverec(C, waveletStr, mode) tol = 10**-6 self.assertTrue(numpy.linalg.norm(X0 - X[0, :]) < tol) def reconstructSignal(X, Xw, waveletStr, level, mode, C): Xrecstr = numpy.zeros(X.shape) for i in range(Xw.shape[0]): C2 = [] colIndex = 0 for j in range(len(list(C))): C2.append(Xw[i, colIndex:colIndex+len(C[j])]) colIndex += len(C[j]) Xrecstr[i, :] = pywt.waverec(tuple(C2), waveletStr, mode) return Xrecstr #Now do the same for the whole X C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode) Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode) Xrecstr = reconstructSignal(X, Xw, waveletStr, level, mode, C) self.assertTrue(numpy.linalg.norm(X - Xrecstr) < tol) waveletStr = 'db8' C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode) Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode) Xrecstr = reconstructSignal(X, Xw, waveletStr, level, mode, C) self.assertTrue(numpy.linalg.norm(X - Xrecstr) < tol) waveletStr = 'haar' C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode) Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode) Xrecstr = reconstructSignal(X, Xw, waveletStr, level, mode, C) self.assertTrue(numpy.linalg.norm(X - Xrecstr) < tol)
def testCreateIndicatorLabels(self): metaUtils = MetabolomicsUtils() X, XStd, X2, (XoplsCortisol, XoplsTesto, XoplsIgf1), YCortisol, YTesto, YIgf1, ages = metaUtils.loadData() YCortisol = YCortisol[numpy.logical_not(numpy.isnan(YCortisol))] YCortisolIndicators = metaUtils.createIndicatorLabel(YCortisol, metaUtils.boundsDict["Cortisol"]) YTesto = YTesto[numpy.logical_not(numpy.isnan(YTesto))] YTestoIndicators = metaUtils.createIndicatorLabel(YTesto, metaUtils.boundsDict["Testosterone"]) YIgf1 = YIgf1[numpy.logical_not(numpy.isnan(YIgf1))] YIgf1Indicators = metaUtils.createIndicatorLabel(YIgf1, metaUtils.boundsDict["IGF1"]) s = numpy.sum(YCortisolIndicators, 1) nptst.assert_array_equal(s, numpy.ones(s.shape[0])) s = numpy.sum(YTestoIndicators, 1) nptst.assert_array_equal(s, numpy.ones(s.shape[0])) s = numpy.sum(YIgf1Indicators, 1) nptst.assert_array_equal(s, numpy.ones(s.shape[0])) #Now compare to those labels in the file X, X2, (XoplsCortisol, XoplsTesto, XoplsIgf1), YCortisol, YTesto, YIgf1, ages = metaUtils.loadData() dataDir = PathDefaults.getDataDir() + "metabolomic/" fileName = dataDir + "data.RMN.total.6.txt" data = pandas.read_csv(fileName, delimiter=",") YCortisolIndicators = metaUtils.createIndicatorLabel(YCortisol, metaUtils.boundsDict["Cortisol"]) YCortisolIndicators2 = numpy.array(data[["Ind.Cortisol.1", "Ind.Cortisol.2", "Ind.Cortisol.3"]]) for i in range(YCortisolIndicators.shape[0]): if not numpy.isnan(YCortisol[i]) and not numpy.isnan(YCortisolIndicators2[i, :]).any(): #nptst.assert_almost_equal(YCortisolIndicators2[i, :], YCortisolIndicators[i, :]) pass YTestoIndicators = metaUtils.createIndicatorLabel(YTesto, metaUtils.boundsDict["Testosterone"]) YTestoIndicators2 = numpy.array(data[["Ind.Testo.1", "Ind.Testo.2", "Ind.Testo.3"]]) for i in range(YTestoIndicators.shape[0]): if not numpy.isnan(YTesto[i]) and not numpy.isnan(YTestoIndicators2[i, :]).any(): #print(i, YTesto[i]) nptst.assert_almost_equal(YTestoIndicators2[i, :], YTestoIndicators[i, :]) YIgf1Indicators = metaUtils.createIndicatorLabel(YIgf1, metaUtils.boundsDict["IGF1"]) YIgf1Indicators2 = numpy.array(data[["Ind.IGF1.1", "Ind.IGF1.2", "Ind.IGF1.3"]]) for i in range(YIgf1Indicators.shape[0]): if not numpy.isnan(YIgf1[i]) and not numpy.isnan(YIgf1Indicators2[i, :]).any(): #print(i, YIgf1[i]) #nptst.assert_almost_equal(YIgf1Indicators2[i, :], YIgf1Indicators[i, :]) pass
from socket import gethostname from sklearn.decomposition import PCA """ Run a variety of bipartite ranking on the metabolomics data """ logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) logging.debug("Running from machine " + str(gethostname())) numpy.random.seed(21) os.system('taskset -p 0xffffffff %d' % os.getpid()) dataDir = PathDefaults.getDataDir() + "metabolomic/" metaUtils = MetabolomicsUtils() X, XStd, X2, (XoplsCortisol, XoplsTesto, XoplsIgf1), YCortisol, YTesto, YIgf1, ages = metaUtils.loadData() #We model 99.1% of the spectrum with 100 eigenvectors pca = PCA(n_components=100) XPca = pca.fit_transform(X) 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) dataDict = {} dataDict["raw"] = X dataDict["pca"] = XPca #dataDict["log"] = X2
def testLoadData(self): metaUtils = MetabolomicsUtils() X, XStd, X2, (XoplsCortisol, XoplsTesto, XoplsIgf1), YCortisol, YTesto, YIgf1, ages = metaUtils.loadData()
import numpy from wallhack.metabolomics.MetabolomicsUtils import MetabolomicsUtils X, X2, df = MetabolomicsUtils.loadData() #Just figure out the boundaries of the levels numpy.set_printoptions(threshold=3000) labelNames = ["IGF1.val", "Cortisol.val", "Testosterone.val"] labelNames2 = ["Ind.IGF1.1", "Ind.IGF1.2", "Ind.IGF1.3"] YList = MetabolomicsUtils.createLabelList(df, labelNames) YList2 = MetabolomicsUtils.createLabelList(df, labelNames2) Y, inds = YList[0] Y1 = numpy.array(df.rx(labelNames2[0])).ravel()[inds] Y2 = numpy.array(df.rx(labelNames2[1])).ravel()[inds] Y3 = numpy.array(df.rx(labelNames2[2])).ravel()[inds] inds = numpy.argsort(Y) YY = numpy.c_[Y[inds], Y1[inds]] YY = numpy.c_[YY, Y2[inds]] YY = numpy.c_[YY, Y3[inds]] print(YY) labelNames2 = ["Ind.Cortisol.1", "Ind.Cortisol.2", "Ind.Cortisol.3"] YList2 = MetabolomicsUtils.createLabelList(df, labelNames2) Y, inds = YList[1] Y1 = numpy.array(df.rx(labelNames2[0])).ravel()[inds] Y2 = numpy.array(df.rx(labelNames2[1])).ravel()[inds] Y3 = numpy.array(df.rx(labelNames2[2])).ravel()[inds]