Statistics
| Branch: | Revision:

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

 1 ```import os ``` ```import sys ``` ```import networkx as nx ``` ```from pprint import pprint ``` ```import code ``` ```from prettytable import PrettyTable ``` ```import matplotlib ``` ```matplotlib.use('Agg') ``` ```from matplotlib import pyplot as plt ``` ```import code ``` ```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): ``` ``` numcouples=0 ``` ``` numMultiPath=0 ``` ``` for s in G.nodes(): ``` ``` for t in G.nodes(): ``` ``` if(s==t): ``` ``` continue ``` ``` numcouples+=1 ``` ``` sps = list(nx.all_shortest_paths(G,s,t,'weight')) ``` ``` if(len(sps)>1): ``` ``` '''print s,t ``` ``` print sps ``` ``` print path_weight(G,sps[0]) ``` ``` print path_weight(G,sps[1]) ``` ``` print "\n\n"''' ``` ``` numMultiPath += len(sps)-1 ``` ``` return (float(numcouples),float(numMultiPath)) ``` ```folder=sys.argv[1] ``` ```graphFile=sys.argv[2] ``` ```#BASE TEORICA DI RIFERIMENTO ``` ```H = nx.read_edgelist(graphFile, delimiter=' ', data=(('weight',float),), create_using=nx.Graph(), nodetype=int) ``` ```G = H.to_directed() ``` ```print type(G) ``` ```#loadindexes = nx.load_centrality(G,normalized=False,weight='weight') ``` ```loadindexes = nx.load_centrality(G,normalized=False,weight='weight') ``` ```bcindexes = nx.betweenness_centrality(G,normalized=False,weight='weight',endpoints=False) ``` ```totb=sum(bcindexes.values()) ``` ```pprint(loadindexes) ``` ```tott = sum(loadindexes.values()) ``` ```print "tot =", tott ``` ```#VALORI DA PROTOCOLLO REALE ``` ```os.chdir(folder) ``` ```filenames=os.listdir(".") ``` ```res={} ``` ```totr=0 ``` ```for f in filenames: ``` ``` if(f.endswith("_cdump.csv")): ``` ``` text=os.popen("tail "+f).read() ``` ``` f=f.strip("_cdump.csv") ``` ``` nodename=(f) ``` ``` lines=text.splitlines() ``` ``` last=lines[-1] ``` ``` cent=int(last.split(",")[-1]) ``` ``` res[int(nodename.strip("h"))]=float(cent) ``` ```pprint(res) ``` ```totr = sum(res.values()) ``` ```print "tot =", totr ``` ```#controlla se res = loadindexes ``` ```import operator ``` ```sorted_x = sorted(res.items(), key=operator.itemgetter(1), reverse=True) ``` ```t = PrettyTable(['PROTO', 'TEORIC_LOAD','CHECK_L']) ``` ```for c in sorted_x: ``` ``` proto = c[1] ``` ``` teoricL = loadindexes[c[0]] ``` ``` teoricBC = bcindexes[c[0]] ``` ``` if (prototeoricL*1.01): ``` ``` strout='>' ``` ``` else: ``` ``` strout='=' ``` ``` #checkL = (proto<=teoricL*1.01 and proto>=teoricL*0.99) ``` ``` #checkBC = (proto<=teoricBC*1.01 and proto>=teoricBC*0.99) ``` ``` t.add_row([proto,teoricL,strout]) ``` ```print t ``` ```print "totProto=%f totTLoad=%f (totProto-totTLoad)=%f" %(totr, tott, totr - tott) ``` ```#numcouples, numMultiPath = foo(G) ``` ```#print numcouples, numMultiPath, numMultiPath/numcouples ``` ```x, y1 = zip(*sorted_x)#proto values ``` ```#code.interact(local=dict(globals(), **locals())) ``` ```plt.plot(tuple(range(len(x))), y1, 'ro', label="proto") ``` ```y2=[] ``` ```for n in x: ``` ``` #code.interact(local=dict(globals(), **locals())) ``` ``` y2.append(loadindexes[n]) ``` ```plt.plot(tuple(range(len(x))), y2, 'b*', label="teoric") ``` ```#plt.xticks(x, xticks, rotation='vertical', fontsize=8) ``` ```plt.xlabel('Nodes by descending centrality id') ``` ```plt.ylabel('Babel Centrality index') ``` ```plt.legend(loc='upper right', fontsize=10, numpoints=1) ``` ```# Pad margins so that markers don't get clipped by the axes ``` ```plt.margins(0.2) ``` ```# Tweak spacing to prevent clipping of tick-labels ``` ```plt.subplots_adjust(bottom=0.3) ``` ```#fig = plt.gcf() ``` ```#plt.show() ``` ```plt.savefig("plot.pdf") ```