Multiple lib changes

This commit is contained in:
2007-01-25 11:58:10 +00:00
parent a65d79697f
commit 1c2c2c8895
7 changed files with 519 additions and 152 deletions

View File

@@ -201,16 +201,16 @@ def node_weighted_adj_matrix(G, weights=None, ave_type='harmonic', with_labels=F
def weighted_adj_matrix(G, with_labels=False):
"""Adjacency matrix of an XGraph whos weights are given in edges.
"""
A,labels = NX.adj_matrix(G,with_labels=True)
A, labels = NX.adj_matrix(G, with_labels=True)
W = A.astype('<f8')
for orf,i in labels.items():
for orf2,j in labels.items():
if G.has_edge(orf,orf2):
edge_weight = G.get_edge(orf,orf2)
W[i,j]=edge_weight
W[j,i]=edge_weight
for orf, i in labels.items():
for orf2, j in labels.items():
if G.has_edge(orf, orf2):
edge_weight = G.get_edge(orf, orf2)
W[i,j] = edge_weight
W[j,i] = edge_weight
if with_labels==True:
return W,labels
return W, labels
else:
return W
@@ -418,6 +418,31 @@ def weighted_laplacian(G,with_labels=False):
else:
return L
def subnetworks(G, T2):
"""Return the highest scoring (T2-test) subgraph og G.
Use simulated annealing to identify highly scoring subgraphs.
ref: -- Ideker et.al (Bioinformatics 18, 2002)
-- Patil and Nielsen (PNAS 2006)
"""
N = 1000
states = [(node, False) for node in G.nodes()]
t2_last = 0.0
for i in xrange(N):
if i==0: #assign random states
states = [(state[0], True) for state in states if rand(1)>.5]
sub_nodes = [state[0] for state in states if state[1]]
Gsub = NX.subgraph(G, sub_nodes)
Gsub = NX.connected_components_subgraphs(Gsub)[0]
t2 = [T2[node] for node in Gsub]
if t2>t2_last:
pass
else:
p = numpy.exp()
"""Below are methods for calculating graph metrics
@@ -473,7 +498,7 @@ Ke = expm(A) .... expm(-A)?
# 13.09.2206: update for use in numpy
def K_expAdj(W, normalised=False, alpha=1.0):
def K_expAdj(W, normalised=True, alpha=1.0):
"""Matrix exponential of adjacency matrix, mentioned in Kandola as a general diffusion kernel.
"""
W = asarray(W)
@@ -499,7 +524,7 @@ def K_expAdj(W, normalised=False, alpha=1.0):
return dot(dot(vr,psigma),vri)
def K_vonNeumann(W,normalised=False,alpha=1.0):
def K_vonNeumann(W, normalised=True, alpha=1.0):
""" The geometric series of path lengths.
Returns matrix square root of pseudo inverse of the adjacency matrix.
"""
@@ -540,8 +565,8 @@ def K_laplacian(W, normalised=True, alpha=1.0):
D = diag(sum(W,0))
L = D - W
if normalised==True:
T = diag(sqrt(1./sum(W,0)))
L = dot(dot(T,L),T)
T = diag(sqrt(1./sum(W, 0)))
L = dot(dot(T, L), T)
e,vr = eig(L)
e = real(e)
vri = inv(vr)
@@ -551,7 +576,7 @@ def K_laplacian(W, normalised=True, alpha=1.0):
for i in range(len(e)):
if e[i] > cutoff:
psigma[i] = 1.0/e[i]
K = dot(dot(vr,diag(psigma)),vri).astype(t)
K = dot(dot(vr, diag(psigma)), vri).astype(t)
K = real(K)
I = eye(n)
K = (1-alpha)*I + alpha*K