Statistics
| Branch: | Revision:

## nepatest_popbabel / scripts / sc.py @ 60ba786f

 1 ```import networkx as nx ``` ```import sys ``` ```from pylab import show, hist, figure ``` ```from matplotlib.pyplot import plot, ion, show, draw ``` ```from pprint import pprint ``` ```import code ``` ```""" returns a copy of G with ``` ``` the most important nodes ``` ``` according to the pagerank """ ``` ```def most_important(G): ``` ``` ranking = nx.betweenness_centrality(G).items() ``` ``` r = [x[1] for x in ranking] ``` ``` m = sum(r)/len(r) # mean centrality ``` ``` t = m*3 # threshold, we keep only the nodes with 3 times the mean ``` ``` Gt = G.copy() ``` ``` for k, v in ranking: ``` ``` if v < t: ``` ``` Gt.remove_node(k) ``` ``` return Gt ``` ```def path_weight(G,path): ``` ``` tot = 0.0 ``` ``` for i in range(0,len(path)-1): ``` ``` n=path[i] ``` ``` m=path[i+1] ``` ``` w=float(G.get_edge_data(n,m)['weight']) ``` ``` print"\t", w ``` ``` tot = tot + w ``` ``` return tot ``` ```def foo(G): ``` ``` count=0 ``` ``` ends2paths={} ``` ``` for s in G.nodes(): ``` ``` for t in G.nodes(): ``` ``` if(s==t): ``` ``` continue ``` ``` sps = list(nx.all_shortest_paths(G,s,t,'weight')) ``` ``` if(len(sps)>1): ``` ``` count+=len(sps) ``` ``` code.interact(local=dict(globals(), **locals())) ``` ``` print "between %d ans %d there are #%d equiv SPS" % (s,t,len(sps)) ``` ``` ends2paths[(s,t)]=sps ``` ``` #print sps ``` ``` #print path_weight(G,sps[0]) ``` ``` #print path_weight(G,sps[1]) ``` ``` #print "\n\n" ``` ``` return ends2paths ``` ```def draw_Graph(G): ``` ``` Gt = most_important(G) # trimming ``` ``` # create the layout ``` ``` pos = nx.spring_layout(G) ``` ``` nx.draw_networkx_nodes(G,pos,node_color='w') #nodelist = list(set(G.nodes()) - set(Gt.nodes())) ``` ``` nx.draw_networkx_nodes(Gt,pos,node_color='r',node_size=600) ``` ``` nx.draw_networkx_labels(G,pos,font_size=12,font_color='k') ``` ``` nx.draw_networkx_labels(Gt,pos,font_size=16,font_color='w') ``` ``` nx.draw_networkx_edges(G,pos,arrows=True) ``` ``` #ion() ``` ``` draw() ``` ``` show() ``` ```#first argument should be graphFile ``` ```graphFile=sys.argv[1] ``` ```print graphFile ``` ```#read the graph (edgelist format) ``` ```G = nx.read_edgelist(graphFile, delimiter=' ',data=(('weight',float),), create_using=nx.Graph(), nodetype=int) ``` ```#G = H.to_directed() ``` ```'''e2sps=foo(G) ``` ``` ``` ```from collections import defaultdict ``` ```d=defaultdict(int) ``` ```for k in e2sps: ``` ``` for sp in e2sps[k]: ``` ``` for i in range(len(sp)-1): ``` ``` d[(sp[i], sp[i+1])] += 1 ``` ``` ``` ```import operator ``` ```sorted_x = sorted(d.items(), key=operator.itemgetter(1), reverse=True)''' ``` ```cindexes = nx.load_centrality(G,normalized=False,weight='weight') ``` ```#pprint(cindexes) ``` ```for k in sorted(cindexes.items(), key=lambda x:x[1], reverse=True): ``` ``` print "Node",k[0],":",k[1] ``` ```print "tot =", sum(cindexes.values()) ``` ```print graphFile, "size:",len(G.nodes()) ``` ```draw_Graph(G) ```