nepatest_popbabel / scripts / sc.py @ 60ba786f
History | View | Annotate | Download (2.64 KB)
1 |
import networkx as nx |
---|---|
2 |
import sys |
3 |
from pylab import show, hist, figure |
4 |
from matplotlib.pyplot import plot, ion, show, draw |
5 |
from pprint import pprint |
6 |
import code |
7 |
|
8 |
""" returns a copy of G with
|
9 |
the most important nodes
|
10 |
according to the pagerank """
|
11 |
def most_important(G): |
12 |
ranking = nx.betweenness_centrality(G).items() |
13 |
r = [x[1] for x in ranking] |
14 |
m = sum(r)/len(r) # mean centrality |
15 |
t = m*3 # threshold, we keep only the nodes with 3 times the mean |
16 |
Gt = G.copy() |
17 |
for k, v in ranking: |
18 |
if v < t:
|
19 |
Gt.remove_node(k) |
20 |
return Gt
|
21 |
|
22 |
def path_weight(G,path): |
23 |
tot = 0.0
|
24 |
for i in range(0,len(path)-1): |
25 |
n=path[i] |
26 |
m=path[i+1]
|
27 |
w=float(G.get_edge_data(n,m)['weight']) |
28 |
print"\t", w |
29 |
tot = tot + w |
30 |
return tot
|
31 |
|
32 |
def foo(G): |
33 |
count=0
|
34 |
ends2paths={} |
35 |
for s in G.nodes(): |
36 |
for t in G.nodes(): |
37 |
if(s==t):
|
38 |
continue
|
39 |
sps = list(nx.all_shortest_paths(G,s,t,'weight')) |
40 |
if(len(sps)>1): |
41 |
count+=len(sps)
|
42 |
code.interact(local=dict(globals(), **locals())) |
43 |
print "between %d ans %d there are #%d equiv SPS" % (s,t,len(sps)) |
44 |
ends2paths[(s,t)]=sps |
45 |
#print sps
|
46 |
#print path_weight(G,sps[0])
|
47 |
#print path_weight(G,sps[1])
|
48 |
#print "\n\n"
|
49 |
return ends2paths
|
50 |
|
51 |
def draw_Graph(G): |
52 |
Gt = most_important(G) # trimming
|
53 |
# create the layout
|
54 |
pos = nx.spring_layout(G) |
55 |
nx.draw_networkx_nodes(G,pos,node_color='w') #nodelist = list(set(G.nodes()) - set(Gt.nodes())) |
56 |
nx.draw_networkx_nodes(Gt,pos,node_color='r',node_size=600) |
57 |
nx.draw_networkx_labels(G,pos,font_size=12,font_color='k') |
58 |
nx.draw_networkx_labels(Gt,pos,font_size=16,font_color='w') |
59 |
nx.draw_networkx_edges(G,pos,arrows=True)
|
60 |
#ion()
|
61 |
draw() |
62 |
show() |
63 |
|
64 |
|
65 |
|
66 |
#first argument should be graphFile
|
67 |
graphFile=sys.argv[1]
|
68 |
print graphFile
|
69 |
#read the graph (edgelist format)
|
70 |
G = nx.read_edgelist(graphFile, delimiter=' ',data=(('weight',float),), create_using=nx.Graph(), nodetype=int) |
71 |
#G = H.to_directed()
|
72 |
'''e2sps=foo(G)
|
73 |
|
74 |
from collections import defaultdict
|
75 |
d=defaultdict(int)
|
76 |
for k in e2sps:
|
77 |
for sp in e2sps[k]:
|
78 |
for i in range(len(sp)-1):
|
79 |
d[(sp[i], sp[i+1])] += 1
|
80 |
|
81 |
import operator
|
82 |
sorted_x = sorted(d.items(), key=operator.itemgetter(1), reverse=True)'''
|
83 |
|
84 |
cindexes = nx.load_centrality(G,normalized=False,weight='weight') |
85 |
#pprint(cindexes)
|
86 |
for k in sorted(cindexes.items(), key=lambda x:x[1], reverse=True): |
87 |
print "Node",k[0],":",k[1] |
88 |
|
89 |
print "tot =", sum(cindexes.values()) |
90 |
print graphFile, "size:",len(G.nodes()) |
91 |
draw_Graph(G) |