diff options
author | Pjotr Prins | 2015-03-18 11:05:39 +0300 |
---|---|---|
committer | Pjotr Prins | 2015-03-18 11:05:39 +0300 |
commit | 178cdbbd1a52cfcab975ab27b36e148009cc3577 (patch) | |
tree | 8804154ca6fc281e7f6895b48480b268fd93a0e2 | |
parent | e6e3b12eeb3fc57b9652468304c1fd14a0a816d0 (diff) | |
download | genenetwork2-178cdbbd1a52cfcab975ab27b36e148009cc3577.tar.gz |
Introducing callbacks
-rw-r--r-- | wqflask/wqflask/my_pylmm/pyLMM/gn2.py | 17 | ||||
-rw-r--r-- | wqflask/wqflask/my_pylmm/pyLMM/kinship.py | 17 | ||||
-rw-r--r-- | wqflask/wqflask/my_pylmm/pyLMM/lmm.py | 5 | ||||
-rw-r--r-- | wqflask/wqflask/my_pylmm/pyLMM/lmm2.py | 12 | ||||
-rw-r--r-- | wqflask/wqflask/my_pylmm/pyLMM/standalone.py | 34 |
5 files changed, 71 insertions, 14 deletions
diff --git a/wqflask/wqflask/my_pylmm/pyLMM/gn2.py b/wqflask/wqflask/my_pylmm/pyLMM/gn2.py index e0c6c8a7..4702c670 100644 --- a/wqflask/wqflask/my_pylmm/pyLMM/gn2.py +++ b/wqflask/wqflask/my_pylmm/pyLMM/gn2.py @@ -5,13 +5,25 @@ from __future__ import absolute_import, print_function, division +import numpy as np import sys import logging # logging.basicConfig(level=logging.DEBUG) +# np.set_printoptions() def progress(location, count, total): - print("Progress: %s %i %i @%d%%" % (location,count,total,round(count*100.0/total))) + """ + Progress update + """ + logging.info("Progress: %s %d%%" % (location,round(count*100.0/total))) + +def mprint(msg,data): + """ + Array/matrix print function + """ + m = np.array(data) + print(msg,m.shape,"=\n",m) def callbacks(): return dict( @@ -22,7 +34,8 @@ def callbacks(): warning = logging.warning, error = logging.error, critical = logging.critical, - progress = progress + progress = progress, + mprint = mprint ) # ----- Minor test cases: diff --git a/wqflask/wqflask/my_pylmm/pyLMM/kinship.py b/wqflask/wqflask/my_pylmm/pyLMM/kinship.py index 0c43587e..43e7fe36 100644 --- a/wqflask/wqflask/my_pylmm/pyLMM/kinship.py +++ b/wqflask/wqflask/my_pylmm/pyLMM/kinship.py @@ -155,20 +155,21 @@ def kinship(G,computeSize=1000,numThreads=None,useBLAS=False,verbose=True): # np.savetxt(outFile+".kve",Kve) return K -def kvakve(K, verbose=True): +def kvakve(K, callbacks): """ Obtain eigendecomposition for K and return Kva,Kve where Kva is cleaned of small values < 1e-6 (notably smaller than zero) """ - if verbose: sys.stderr.write("Obtaining eigendecomposition for %dx%d matrix\n" % (K.shape[0],K.shape[1]) ) - + info = callbacks()['info'] + mprint = callbacks()['mprint'] + + info("Obtaining eigendecomposition for %dx%d matrix" % (K.shape[0],K.shape[1]) ) Kva,Kve = linalg.eigh(K) - if verbose: - print("Kva is: ", Kva.shape, Kva) - print("Kve is: ", Kve.shape, Kve) + mprint("Kva",Kva) + mprint("Kve",Kve) - if sum(Kva < 1e-6): - if verbose: sys.stderr.write("Cleaning %d eigen values (Kva<0)\n" % (sum(Kva < 0))) + if sum(Kva < 0): + info("Cleaning %d eigen values (Kva<0)" % (sum(Kva < 0))) Kva[Kva < 1e-6] = 1e-6 return Kva,Kve diff --git a/wqflask/wqflask/my_pylmm/pyLMM/lmm.py b/wqflask/wqflask/my_pylmm/pyLMM/lmm.py index 8a24d98b..5ad644e2 100644 --- a/wqflask/wqflask/my_pylmm/pyLMM/lmm.py +++ b/wqflask/wqflask/my_pylmm/pyLMM/lmm.py @@ -54,11 +54,14 @@ import genotype import phenotype import gwas +# ---- A trick to decide on the environment: try: from wqflask.my_pylmm.pyLMM import chunks + from gn2 import callbacks except ImportError: print("WARNING: Standalone version missing the Genenetwork2 environment\n") has_gn2=False + from standalone import callbacks pass #np.seterr('raise') @@ -594,7 +597,7 @@ class LMM: # if self.verbose: sys.stderr.write("Obtaining eigendecomposition for %dx%d matrix\n" % (K.shape[0],K.shape[1]) ) begin = time.time() # Kva,Kve = linalg.eigh(K) - Kva,Kve = kvakve(K) + Kva,Kve = kvakve(K,callbacks) end = time.time() if self.verbose: sys.stderr.write("Total time: %0.3f\n" % (end - begin)) print("sum(Kva),sum(Kve)=",sum(Kva),sum(Kve)) diff --git a/wqflask/wqflask/my_pylmm/pyLMM/lmm2.py b/wqflask/wqflask/my_pylmm/pyLMM/lmm2.py index d4b3ac82..6aefb9d3 100644 --- a/wqflask/wqflask/my_pylmm/pyLMM/lmm2.py +++ b/wqflask/wqflask/my_pylmm/pyLMM/lmm2.py @@ -24,6 +24,16 @@ from scipy import optimize from optmatrix import matrixMult import kinship +# A trick to decide on the environment: +try: + from wqflask.my_pylmm.pyLMM import chunks + from gn2 import callbacks +except ImportError: + print("WARNING: Standalone version missing the Genenetwork2 environment\n") + has_gn2=False + from standalone import callbacks + pass + def calculateKinship(W,center=False): """ W is an n x m matrix encoding SNP minor alleles. @@ -184,7 +194,7 @@ class LMM2: # if self.verbose: sys.stderr.write("Obtaining eigendecomposition for %dx%d matrix\n" % (K.shape[0],K.shape[1]) ) begin = time.time() # Kva,Kve = linalg.eigh(K) - Kva,Kve = kinship.kvakve(K) + Kva,Kve = kinship.kvakve(K,callbacks) end = time.time() if self.verbose: sys.stderr.write("Total time: %0.3f\n" % (end - begin)) print("sum(Kva),sum(Kve)=",sum(Kva),sum(Kve)) diff --git a/wqflask/wqflask/my_pylmm/pyLMM/standalone.py b/wqflask/wqflask/my_pylmm/pyLMM/standalone.py index a806729e..bbee3cd7 100644 --- a/wqflask/wqflask/my_pylmm/pyLMM/standalone.py +++ b/wqflask/wqflask/my_pylmm/pyLMM/standalone.py @@ -8,13 +8,29 @@ from __future__ import absolute_import, print_function, division +import numpy as np import sys import logging logging.basicConfig(level=logging.DEBUG) +np.set_printoptions(precision=3,suppress=True) def progress(location, count, total): - logging.info("Progress: %s %i %i @%d%%" % (location,count,total,round(count*100.0/total))) + logging.info("Progress: %s %d%%" % (location,round(count*100.0/total))) + +def mprint(msg,data): + """ + Array/matrix print function + """ + m = np.array(data) + if m.ndim == 1: + print(msg,m.shape,"=\n",m[0:3]," ... ",m[-3:]) + if m.ndim == 2: + print(msg,m.shape,"=\n[", + m[0][0:3]," ... ",m[0][-3:],"\n ", + m[1][0:3]," ... ",m[1][-3:],"\n ...\n ", + m[-2][0:3]," ... ",m[-2][-3:],"\n ", + m[-1][0:3]," ... ",m[-1][-3:],"]") def callbacks(): return dict( @@ -25,7 +41,8 @@ def callbacks(): warning = logging.warning, error = logging.error, critical = logging.critical, - progress = progress + progress = progress, + mprint = mprint ) # ----- Minor test cases: @@ -39,3 +56,16 @@ if __name__ == '__main__': wrln("Hello %i" % 34) progress = callbacks()['progress'] progress("I am half way",50,100) + list = [0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15, + 0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15, + 0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15, + 0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15, + 0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15] + mprint("list",list) + matrix = [[1,0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15], + [2,0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15], + [3,0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15], + [4,0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15], + [5,0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15], + [6,0.5,0.6096595 , -0.31559815, -0.52793285, 1.16573418e-15]] + mprint("matrix",matrix) |