Changeset 1955:9b187b15f253 in orangebioinformatics
 Timestamp:
 02/17/14 09:42:57 (2 months ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orangecontrib/bio/obiGeneSetSig.py
r1952 r1955 13 13 14 14 from . import obiExpression, obiGene, obiGeneSets, obiGsea, stats 15 16 15 17 16 def corgs_activity_score(ex, corg): … … 78 77 79 78 80 def pca( data, snapshot=0):79 def pca(M, snapshot=None): 81 80 "Perform PCA on M, return eigenvectors and eigenvalues, sorted." 82 M = data.toNumpy("a")[0]83 81 XMean = numpy.mean(M, axis = 0) 84 82 M = M  XMean 85 86 T, N = numpy.shape(M)87 # if there are less rows T than columns N, use snapshot method88 if (T < N) or snapshot: 89 C = numpy.dot(M, numpy.transpose(M))90 evals, evecsC = numpy.linalg.eigh( C) #columns of evecsC are eigenvectors91 evecs = numpy.dot(M.T,evecsC)/numpy.sqrt(numpy.abs(evals))83 84 if snapshot == None: 85 snapshot = M.shape[0] < M.shape[1] 86 87 if snapshot: #less columns than rows 88 evals, evecsC = numpy.linalg.eigh(M.dot(M.T)) #columns of evecsC are eigenvectors 89 evecs = M.T.dot(evecsC)/numpy.sqrt(numpy.abs(evals)) 92 90 else: 93 K = numpy.dot(numpy.transpose(M), M) 94 evals, evecs = numpy.linalg.eigh(K) 91 evals, evecs = numpy.linalg.eigh(M.T.dot(M)) 95 92 96 evecs = numpy.transpose(evecs)93 evecs = evecs.T 97 94 98 95 # sort the eigenvalues and eigenvectors, decending order … … 100 97 evecs = numpy.take(evecs, order, 0) 101 98 evals = numpy.take(evals, order) 99 102 100 return evals, evecs, XMean 103 101 104 102 def pca2(M): 103 """ Perform PCA on M, return eigenvectors and eigenvalues, sorted. 104 Mostly euivalent to pca(), slightly slower but more stable.""" 105 XMean = numpy.mean(M, axis = 0) 106 M = M  XMean 107 U,s,Vt = numpy.linalg.svd(M, full_matrices=False) 108 return s*s, Vt, XMean 109 105 110 class GeneSetTrans(object): 106 111 … … 440 445 441 446 #create distances to all learning data  save or other class 447 442 448 for c in data: 443 449 p = pearson(c, ex) … … 565 571 566 572 return TR[0][0] 567 573 574 def eigvturn(A): 575 """ It multiplies rows (vectors of unit lengths) where 576 sum < 0 with 1. """ 577 turn = (numpy.sum(A, axis=1, keepdims=True) > 0)*2  1 578 return A*turn 579 568 580 class PCA(ParametrizedTransformation): 569 581 582 def __init__(self, **kwargs): 583 self.turn = kwargs.pop("turn", False) #turn eigenvetors 584 if self.turn == True: 585 self.turn = eigvturn 586 super(PCA, self).__init__(**kwargs) 587 570 588 def _get_par(self, datao): 571 return pca(datao) 589 M = datao.toNumpy("a")[0] 590 evals, evect, xmean = pca(M) 591 if self.turn: 592 evect = self.turn(evect) 593 return evals, evect, xmean 572 594 573 595 def _use_par(self, arr, constructt): … … 897 919 return select, None 898 920 else: 899 return set(select), pca(datas )921 return set(select), pca(datas.toNumpy("a")[0]) 900 922 901 923 def _use_par(self, arr, constructt): … … 994 1016 #ass = LLR(data, matcher=matcher, gene_sets=gsets, class_values=choosen_cv, min_part=0.0, normalize=True) 995 1017 #ass = LLR_slow(data, matcher=matcher, gene_sets=gsets, class_values=choosen_cv, min_part=0.0) 996 ass = CORGs(data, matcher=matcher, gene_sets=gsets, class_values=choosen_cv, min_part=0.0, cv=True)1018 ass = PCA(data, matcher=matcher, gene_sets=gsets, class_values=choosen_cv, min_part=0.0, cv=True) 997 1019 ar = to_old_dic(ass.domain, data[:5]) 998 1020 pp2(ar)
Note: See TracChangeset
for help on using the changeset viewer.