nepatest_popbabel / scripts / comparator4validation.py @ 60ba786f
History  View  Annotate  Download (3.34 KB)
1 
import os 

2 
import sys 
3 
import networkx as nx 
4 
from pprint import pprint 
5 
import code 
6 
from prettytable import PrettyTable 
7 
import matplotlib 
8 
matplotlib.use('Agg')

9 
from matplotlib import pyplot as plt 
10 
import code 
11  
12 
def path_weight(G,path): 
13 
tot = 0.0

14 
for i in range(0,len(path)1): 
15 
n=path[i] 
16 
m=path[i+1]

17 
w=float(G.get_edge_data(n,m)['weight']) 
18 
print"\t", w 
19 
tot = tot + w 
20 
return tot

21  
22 
def foo(G): 
23 
numcouples=0

24 
numMultiPath=0

25 
for s in G.nodes(): 
26 
for t in G.nodes(): 
27 
if(s==t):

28 
continue

29 
numcouples+=1

30 
sps = list(nx.all_shortest_paths(G,s,t,'weight')) 
31 
if(len(sps)>1): 
32 
'''print s,t

33 
print sps

34 
print path_weight(G,sps[0])

35 
print path_weight(G,sps[1])

36 
print "\n\n"'''

37 
numMultiPath += len(sps)1 
38 
return (float(numcouples),float(numMultiPath)) 
39  
40  
41 
folder=sys.argv[1]

42 
graphFile=sys.argv[2]

43  
44 
#BASE TEORICA DI RIFERIMENTO

45 
H = nx.read_edgelist(graphFile, delimiter=' ', data=(('weight',float),), create_using=nx.Graph(), nodetype=int) 
46 
G = H.to_directed() 
47 
print type(G) 
48 
#loadindexes = nx.load_centrality(G,normalized=False,weight='weight')

49 
loadindexes = nx.load_centrality(G,normalized=False,weight='weight') 
50 
bcindexes = nx.betweenness_centrality(G,normalized=False,weight='weight',endpoints=False) 
51 
totb=sum(bcindexes.values())

52 
pprint(loadindexes) 
53 
tott = sum(loadindexes.values())

54 
print "tot =", tott 
55  
56  
57 
#VALORI DA PROTOCOLLO REALE

58 
os.chdir(folder) 
59  
60 
filenames=os.listdir(".")

61  
62 
res={} 
63 
totr=0

64 
for f in filenames: 
65 
if(f.endswith("_cdump.csv")): 
66 
text=os.popen("tail "+f).read()

67 
f=f.strip("_cdump.csv")

68 
nodename=(f) 
69 
lines=text.splitlines() 
70 
last=lines[1]

71 
cent=int(last.split(",")[1]) 
72 
res[int(nodename.strip("h"))]=float(cent) 
73 
pprint(res) 
74 
totr = sum(res.values())

75 
print "tot =", totr 
76  
77 
#controlla se res = loadindexes

78  
79 
import operator 
80  
81 
sorted_x = sorted(res.items(), key=operator.itemgetter(1), reverse=True) 
82  
83 
t = PrettyTable(['PROTO', 'TEORIC_LOAD','CHECK_L']) 
84 
for c in sorted_x: 
85 
proto = c[1]

86 
teoricL = loadindexes[c[0]]

87 
teoricBC = bcindexes[c[0]]

88 
if (proto<teoricL*0.99): 
89 
strout='<'

90 
elif (proto>teoricL*1.01): 
91 
strout='>'

92 
else:

93 
strout='='

94 
#checkL = (proto<=teoricL*1.01 and proto>=teoricL*0.99)

95 
#checkBC = (proto<=teoricBC*1.01 and proto>=teoricBC*0.99)

96 
t.add_row([proto,teoricL,strout]) 
97 
print t

98 
print "totProto=%f totTLoad=%f (totProtototTLoad)=%f" %(totr, tott, totr  tott) 
99  
100 
#numcouples, numMultiPath = foo(G)

101 
#print numcouples, numMultiPath, numMultiPath/numcouples

102  
103 
x, y1 = zip(*sorted_x)#proto values 
104 
#code.interact(local=dict(globals(), **locals()))

105 
plt.plot(tuple(range(len(x))), y1, 'ro', label="proto") 
106  
107 
y2=[] 
108 
for n in x: 
109 
#code.interact(local=dict(globals(), **locals()))

110 
y2.append(loadindexes[n]) 
111 
plt.plot(tuple(range(len(x))), y2, 'b*', label="teoric") 
112  
113 
#plt.xticks(x, xticks, rotation='vertical', fontsize=8)

114 
plt.xlabel('Nodes by descending centrality id')

115 
plt.ylabel('Babel Centrality index')

116 
plt.legend(loc='upper right', fontsize=10, numpoints=1) 
117 
# Pad margins so that markers don't get clipped by the axes

118 
plt.margins(0.2)

119 
# Tweak spacing to prevent clipping of ticklabels

120 
plt.subplots_adjust(bottom=0.3)

121 
#fig = plt.gcf()

122 
#plt.show()

123  
124 
plt.savefig("plot.pdf")

125  
126 