-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4sf.py
378 lines (304 loc) · 9.51 KB
/
m4sf.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import csv
import networkx as nx
def ng(G):
"""Computes n_G(u,v) for every ordered pair of vertices u,v of G.
For every ordered pair of vertices u,v of graph G, computes n_G(u,v), the
number of vertices of G that are closer to u than to v.
Args:
G: a graph.
Returns:
A dict mapping keys (u,v) to n_G(u,v).
"""
distance = dict(nx.all_pairs_shortest_path_length(G))
ngraph = {}
vertices = G.nodes
for x in vertices:
for y in vertices:
ngraph[(x,y)] = 0
for z in vertices:
if distance[x][z] < distance[y][z]:
ngraph[(x,y)] += 1
return ngraph
def peripherality(G):
"""Computes peri(u) for each vertex u of G.
For every vertex u of graph G, computes peri(u), the number of vertices in
G that has more vertices of G closer to itself than u.
Args:
G: a graph.
Returns:
A dict mapping keys u to peri(u).
"""
ngraph = ng(G)
peri = {}
vertices = G.nodes
for x in vertices:
peri[x] = 0
for y in vertices:
if ngraph[(y,x)] > ngraph[(x,y)]:
peri[x] += 1
return peri
def spr(G):
"""Computes spr(u) for each vertex u of G.
For every vertex u of graph G, computes spr(u), the sum of n_G(x,u) where
x runs over all vertices of G.
Args:
G: a graph.
Returns:
A dict mapping keys u to spr(u).
"""
ngraph = ng(G)
sprt = {}
vertices = G.nodes
for x in vertices:
sprt[x] = 0
for y in vertices:
sprt[x] += ngraph[(y,x)]
return sprt
def eperi(G):
"""Computes eperi(e) for each edge e of G.
For every edge e = (u,v) of graph G, computes eperi(e), the number of
vertices x of G such that n_G(u,x) < n_G(x,u) and n_G(v,x) < n_G(x,v).
Args:
G: a graph.
Returns:
A dict mapping keys e to eperi(e).
"""
ngraph = ng(G)
eper = {}
vertices = G.nodes
edges = G.edges
for e in edges:
eper[e] = 0
for x in vertices:
if x != e[0] and x != e[1] and ngraph[(x,e[0])] > ngraph[(e[0],x)] and ngraph[(x,e[1])] > ngraph[(e[1],x)]:
eper[e] += 1
return eper
def eperi1(G):
ngraph = ng(G)
eper = {}
vertices = G.nodes
edges = G.edges
for e in edges:
eper[e] = 0
for x in vertices:
if x != e[0] and x != e[1] and (ngraph[(x,e[0])] > ngraph[(e[0],x)] or ngraph[(x,e[1])] > ngraph[(e[1],x)]):
eper[e] += 1
return eper
def espr(G):
"""Computes espr(e) for each edge e of G.
For every edge e = (u,v) of graph G, computes espr(e), the sum of
n_G(x,u) + n_G(x,v) with x running over all vertices of G except for u and
v.
Args:
G: a graph.
Returns:
A dict mapping keys e to espr(e).
"""
ngraph = ng(G)
esprt = {}
vertices = G.nodes
edges = G.edges
for e in edges:
esprt[e] = 0
for x in vertices:
if x != e[0] and x != e[1]:
esprt[e] += (ngraph[(x,e[0])]+ngraph[(x,e[1])])
return esprt
def irr(G):
"""Computes irr(e) for each edge e of G.
For every edge e = (u,v) of graph G, computes irr(e), the difference of
degrees of u and v.
Args:
G: a graph.
Returns:
A dict mapping keys e to irr(e).
"""
edges = G.edges
degree = G.degree
irre = {}
for x in edges:
irre[x] = abs(degree[x[0]]-degree[x[1]])
return irre
def mostar(G):
"""Computes mostar(G) for graph G.
For graph G, computes mostar(G), the sum of difference between n_G(u,v) and
n_G(v,u) with (u,v) running over all edges of G.
Args:
G: a graph.
Returns:
mostar(G).
"""
edges = G.edges
ngraph = ng(G)
most = {}
for x in edges:
most[x] = abs(ngraph[(x[0],x[1])]-ngraph[(x[1],x[0])])
return most
def edeg(G):
"""Computes edeg(e) for every edge of graph G.
For every edge e = (u,v) of graph G, computes edeg(G), the number of
vertices of G not equal to u or v which are adjacent to u, v, or both.
Args:
G: a graph.
Returns:
A dict mapping keys e to edge(e).
"""
edges = G.edges
vertices = G.nodes
edegree = {}
for x in edges:
counter = 0
for y in vertices:
if y != x[0] and y != x[1] and ((x[0],y) in edges or (x[1],y) in edges):
counter += 1
edegree[x] = counter
return edegree
def edge_ecc(G):
"""Computes edeg_ecc(e) for every edge of connected graph G.
For every edge e = (u,v) of connected graph G, computes edeg_ecc(e), the
longest distance from e to any vertex of G.
Args:
G: a graph.
Returns:
A dict mapping keys e to edeg_ecc(e).
"""
edges = G.edges
vertices = G.nodes
distance = dict(nx.all_pairs_shortest_path_length(G))
edge_ecc = {}
for x in edges:
maxdist = 0
for y in vertices:
maxdist = max(maxdist,min(distance[x[0]][y],distance[x[1]][y]))
edge_ecc[x] = maxdist
return edge_ecc
def listrank(index_list):
"""Computes the list rank of a dict.
For every key x of a dict, computes the number of other keys with larger
value.
Args:
index_list: a dict.
Returns:
A dict mapping a key x to the number of other keys with larger value in
the input dict.
"""
list_rank = {}
for z in index_list:
counter = 1
for y in index_list:
if index_list[z] < index_list[y]:
counter += 1
list_rank[z] = counter
return list_rank
def revlistrank(index_list):
"""Computes the reverse list rank of a dict.
For every key x of a dict, computes the number of other keys with smaller
value.
Args:
index_list: a dict.
Returns:
A dict mapping a key x to the number of other keys with smaller value in
the input dict.
"""
list_rank = {}
for z in index_list:
counter = 1
for y in index_list:
if index_list[z] > index_list[y]:
counter += 1
list_rank[z] = counter
return list_rank
reaction = {}
counter = 0
with open('downloads/super_fast_reactions.csv', mode = 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
counter += 1
reactants = []
products = []
reactant_check = 0
for j in range(len(row)):
if row[j] != '.' and reactant_check == 0:
reactants.append(row[j])
elif row[j] == '.':
reactant_check = 1
elif row[j] != '.' and reactant_check == 1:
products.append(row[j])
reaction[counter] = [reactants, products]
reaction_graph_sf = nx.Graph()
for i in reaction:
new_edge = []
for j in reaction[i][0]:
if j != 'M':
reaction_graph_sf.add_node(j)
new_edge.append(j)
for s in range(len(new_edge)):
for t in range(s+1,len(new_edge)):
if new_edge[s] != new_edge[t]:
reaction_graph_sf.add_edge(new_edge[s],new_edge[t])
reaction = {}
counter = 0
with open('downloads/mozart4.csv', mode = 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in reader:
counter += 1
if row[0] != row[2] and row[0] != 'M' and row[2] != 'M':
reaction[counter] = [row[0],row[2]]
reaction_graph_m4 = nx.Graph()
for i in reaction:
new_edge = []
for j in reaction[i]:
if j != 'M':
reaction_graph_m4.add_node(j)
new_edge.append(j)
for s in range(len(new_edge)):
for t in range(s+1,len(new_edge)):
if new_edge[s] != new_edge[t]:
reaction_graph_m4.add_edge(new_edge[s],new_edge[t])
# superfast calculations
def central_calc(G):
peri_list = peripherality(G)
spr_list = spr(G)
dc = nx.degree_centrality(G)
dc_rank = listrank(dc)
cc = nx.closeness_centrality(G)
cc_rank = listrank(cc)
bc = nx.betweenness_centrality(G)
bc_rank = listrank(bc)
ec = nx.eigenvector_centrality(G)
ec_rank = listrank(ec)
ecc = nx.eccentricity(G)
for z in sorted(peri_list):
print z, '&', revlistrank(peri_list)[z], '&', revlistrank(spr_list)[z], '&', dc_rank[z], '&', cc_rank[z], '&', bc_rank[z], '&', ec_rank[z], '&', revlistrank(ecc)[z], ' \\\ '
print '\hline'
print(G.order())
print(G.number_of_edges())
print(nx.diameter(G))
print(G.degree())
eecc = edge_ecc(G)
edegree = edeg(G)
eperx = eperi(G)
espr1 = espr(G)
mst = mostar(G)
erank = []
for z in edegree:
erankrow = []
if z[0] < z[1]:
erankrow.append(z[0])
erankrow.append(z[1])
else:
erankrow.append(z[1])
erankrow.append(z[0])
erankrow.append(listrank(edegree)[z])
erankrow.append(revlistrank(eecc)[z])
erankrow.append(revlistrank(eperx)[z])
erankrow.append(revlistrank(espr1)[z])
erankrow.append(revlistrank(mst)[z])
erank.append(erankrow)
for z in sorted(erank):
print z[0],',',z[1], '&', z[2],'&', z[3], '&', z[4], '&', z[5], '&', z[6], ' \\\ '
print '\hline'
print ''
return ''
print(central_calc(reaction_graph_sf))
print(central_calc(reaction_graph_m4))