-
Notifications
You must be signed in to change notification settings - Fork 8
/
backbone.py
139 lines (115 loc) · 5.8 KB
/
backbone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'''
This module implements the disparity filter to compute a significance score of edge weights in networks
'''
import networkx as nx
import numpy as np
from scipy import integrate
def disparity_filter(G, weight='weight'):
''' Compute significance scores (alpha) for weighted edges in G as defined in Serrano et al. 2009
Args
G: Weighted NetworkX graph
Returns
Weighted graph with a significance score (alpha) assigned to each edge
References
M. A. Serrano et al. (2009) Extracting the Multiscale backbone of complex weighted networks. PNAS, 106:16, pp. 6483-6488.
'''
if nx.is_directed(G): #directed case
N = nx.DiGraph()
for u in G:
k_out = G.out_degree(u)
k_in = G.in_degree(u)
if k_out > 1:
sum_w_out = sum(np.absolute(G[u][v][weight]) for v in G.successors(u))
for v in G.successors(u):
w = G[u][v][weight]
p_ij_out = float(np.absolute(w))/sum_w_out
alpha_ij_out = 1 - (k_out-1) * integrate.quad(lambda x: (1-x)**(k_out-2), 0, p_ij_out)[0]
N.add_edge(u, v, weight = w, alpha_out=float('%.4f' % alpha_ij_out))
elif k_out == 1 and G.in_degree(G.successors(u)[0]) == 1:
#we need to keep the connection as it is the only way to maintain the connectivity of the network
v = G.successors(u)[0]
w = G[u][v][weight]
N.add_edge(u, v, weight = w, alpha_out=0., alpha_in=0.)
#there is no need to do the same for the k_in, since the link is built already from the tail
if k_in > 1:
sum_w_in = sum(np.absolute(G[v][u][weight]) for v in G.predecessors(u))
for v in G.predecessors(u):
w = G[v][u][weight]
p_ij_in = float(np.absolute(w))/sum_w_in
alpha_ij_in = 1 - (k_in-1) * integrate.quad(lambda x: (1-x)**(k_in-2), 0, p_ij_in)[0]
N.add_edge(v, u, weight = w, alpha_in=float('%.4f' % alpha_ij_in))
return N
else: #undirected case
B = nx.Graph()
for u in G:
k = len(G[u])
if k > 1:
sum_w = sum(np.absolute(G[u][v][weight]) for v in G[u])
for v in G[u]:
w = G[u][v][weight]
p_ij = float(np.absolute(w))/sum_w
alpha_ij = 1 - (k-1) * integrate.quad(lambda x: (1-x)**(k-2), 0, p_ij)[0]
B.add_edge(u, v, weight = w, alpha=float('%.4f' % alpha_ij))
return B
def disparity_filter_alpha_cut(G,weight='weight',alpha_t=0.4, cut_mode='or'):
''' Performs a cut of the graph previously filtered through the disparity_filter function.
Args
----
G: Weighted NetworkX graph
weight: string (default='weight')
Key for edge data used as the edge weight w_ij.
alpha_t: double (default='0.4')
The threshold for the alpha parameter that is used to select the surviving edges.
It has to be a number between 0 and 1.
cut_mode: string (default='or')
Possible strings: 'or', 'and'.
It works only for directed graphs. It represents the logic operation to filter out edges
that do not pass the threshold value, combining the alpha_in and alpha_out attributes
resulting from the disparity_filter function.
Returns
-------
B: Weighted NetworkX graph
The resulting graph contains only edges that survived from the filtering with the alpha_t threshold
References
---------
.. M. A. Serrano et al. (2009) Extracting the Multiscale backbone of complex weighted networks. PNAS, 106:16, pp. 6483-6488.
'''
if nx.is_directed(G):#Directed case:
B = nx.DiGraph()
for u, v, w in G.edges(data=True):
try:
alpha_in = w['alpha_in']
except KeyError: #there is no alpha_in, so we assign 1. It will never pass the cut
alpha_in = 1
try:
alpha_out = w['alpha_out']
except KeyError: #there is no alpha_out, so we assign 1. It will never pass the cut
alpha_out = 1
if cut_mode == 'or':
if alpha_in<alpha_t or alpha_out<alpha_t:
B.add_edge(u,v, weight=w[weight])
elif cut_mode == 'and':
if alpha_in<alpha_t and alpha_out<alpha_t:
B.add_edge(u,v, weight=w[weight])
return B
else:
B = nx.Graph()#Undirected case:
for u, v, w in G.edges(data=True):
try:
alpha = w['alpha']
except KeyError: #there is no alpha, so we assign 1. It will never pass the cut
alpha = 1
if alpha<alpha_t:
B.add_edge(u,v, weight=w[weight])
return B
if __name__ == '__main__':
G = nx.barabasi_albert_graph(1000, 5)
for u, v in G.edges():
G[u][v]['weight'] = np.random.randint(1,100)
alpha = 0.05
G = disparity_filter(G)
G2 = nx.Graph([(u, v, d) for u, v, d in G.edges(data=True) if d['alpha'] < alpha])
print 'alpha = %s' % alpha
print 'original: nodes = %s, edges = %s' % (G.number_of_nodes(), G.number_of_edges())
print 'backbone: nodes = %s, edges = %s' % (G2.number_of_nodes(), G2.number_of_edges())
print G2.edges(data=True)