Statistics
| Branch: | Revision:

nepatest_popbabel / scripts / runsSynthetizer.py @ 60ba786f

History | View | Annotate | Download (6.89 KB)

1
import sys
2
import glob
3
from pprint import pprint
4
import code
5
import csv
6
import numpy as np
7
import matplotlib as mpl
8
from prettytable import PrettyTable
9
import numpy as np
10
import operator
11

    
12

    
13
def figsize(scale):
14
    fig_width_pt = 469.755                          # Get this from LaTeX using \the\textwidth
15
    inches_per_pt = 1.0/72.27                       # Convert pt to inch
16
    golden_mean = (np.sqrt(5.0)-1.0)/2.0            # Aesthetic ratio (you could change this)
17
    fig_width = fig_width_pt*inches_per_pt*scale    # width in inches
18
    fig_height = fig_width*golden_mean              # height in inches
19
    fig_size = [fig_width,fig_height]
20
    return fig_size
21

    
22
mpl.use('pgf')
23
pgf_with_latex = {                      # setup matplotlib to use latex for output
24
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
25
    "text.usetex": True,                # use LaTeX to write all text
26
    "font.family": "serif",
27
    "font.serif": [],                   # blank entries should cause plots to inherit fonts from the document
28
    "font.sans-serif": [],
29
    "font.monospace": [],
30
    "axes.labelsize": 14,               # LaTeX default is 10pt font.
31
    "text.fontsize": 12,
32
    "legend.fontsize": 11,               # Make the legend/label fonts a little smaller
33
    "xtick.labelsize": 12,
34
    "ytick.labelsize": 12,
35
    "figure.figsize": figsize(0.9),     # default fig size of 0.9 textwidth
36
    "pgf.preamble": [
37
        r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts becasue your computer can handle it :)
38
        r"\usepackage[T1]{fontenc}",        # plots will be generated using this preamble
39
        ]
40
    }
41
mpl.rcParams.update(pgf_with_latex)
42
import matplotlib.pyplot as plt
43
import networkx as nx
44

    
45

    
46

    
47
def readCSVlog(f):
48
    #Dealing with pop csv
49
    fres={} #timeToTuple
50
    elaps1=[]
51
    min1=0.0
52
    with open(f, 'r') as csvfile:
53
        reader = csv.reader(csvfile, delimiter=',')
54
        next(reader, None)
55
        for row in reader:
56
            time=row[0]
57
            loops=int(row[1])
58
            bh=int(row[2])
59
            total=int(row[3])
60
            
61
            #converting time string to comparable number
62
            splt=time.split('-')
63
            c=0.0
64
            if (splt[-1]=='high'):
65
                c=5
66
            splt=splt[0].split(':')
67
            hh=int(splt[0])
68
            mm=int(splt[1])
69
            ss=int(splt[2])
70
            numtime=hh*60*60 + mm*60 + ss + c*(10**-1)
71
            el=0.0
72
            if (not elaps1):
73
                min1=numtime
74
            else:
75
                el=numtime-min1
76
            elaps1.append(el)
77
            fres[el]=(loops,bh,total)
78
    return fres
79

    
80
def empiricalLoss(dres):
81
    tots=[]
82
    for k in sorted(dres.keys()):
83
        tots.append(dres[k][-1])
84
    area=0.0
85
    for i in range(0,len(dres)):
86
        area+=tots[i]*0.5 #0.5 is because sample were done every 1/2 seconds
87
    return area
88

    
89
def summary(v):
90
        vmin=min(v)
91
        vmax=max(v)
92
        tot=sum(v)
93
        avg=np.mean(v)
94
        std=np.std(v)
95
        t = PrettyTable(['min','max', 'mean','std','tot'])
96
        t.add_row([vmin,vmax,avg,std,tot])
97
        print t
98

    
99
def synth(folder1,folder2,graphfile):
100
    
101

    
102
    if(graphfile.endswith(".edges")):
103
        G = nx.read_weighted_edgelist(graphfile, nodetype=int, create_using=nx.Graph())
104
    loads = nx.load_centrality(G,normalized=False,weight='weight')
105

    
106
    dumps1=glob.glob(folder1+"*csv")
107
    dumps2=glob.glob(folder2+"*csv")
108

    
109
    #code.interact(local=dict(globals(), **locals())) 
110
    res1={} #node: emprical Loss
111
    res2={} #node: emprical Loss
112
    for d in dumps1:
113
        #print d
114
        dres=readCSVlog(d)
115
         
116
        exp=d.strip("-results.csv").split("/")[-1]
117
        failnodeid=""
118
        for p in exp.split("-"):
119
            if p.startswith("h"):
120
                p=int(p.split("_")[0].strip("h"))
121
                failnodeid=p
122

    
123
        eloss=empiricalLoss(dres)
124
        res1[failnodeid]=eloss
125

    
126
    for d in dumps2:
127
        #print d
128
        dres=readCSVlog(d)
129
         
130
        exp=d.strip("-results.csv").split("/")[-1]
131
        failnodeid=""
132
        for p in exp.split("-"):
133
            if p.startswith("h"):
134
                p=int(p.split("_")[0].strip("h"))
135
                failnodeid=p
136

    
137
        eloss=empiricalLoss(dres)
138
        res2[failnodeid]=eloss
139

    
140
    kset1=set(res1.keys())
141
    kset2=set(res2.keys())
142
    comkeys=list(set.intersection(kset1,kset2)) 
143

    
144
    popDir={k: res1[k] for k in comkeys}
145
    nopopDir={k: res2[k] for k in comkeys}
146
    loads={k: loads[k] for k in comkeys}
147

    
148
    srtloads = sorted(loads.items(), key=operator.itemgetter(1), reverse=True)
149
    #srtnopop = sorted(nopopDir.items(), key=operator.itemgetter(1))
150

    
151
    #pprint([x[0] for x in srtloads])
152
    x = []
153
    ypop = []
154
    ynopop = []
155
    relGain = []
156
    d={}
157
    for e in srtloads:
158
        popv=popDir[e[0]]
159
        nopopv=nopopDir[e[0]]
160
        if (popv!=0 and nopopv!=0):
161
            x.append(e[0])
162
            ypop.append(popv)
163
            ynopop.append(nopopv)
164
            d[e[0]]=(popv,nopopv)
165
        else:
166
            print >> sys.stderr , "popv = %.4f, nopopv = %.4f, host = %s" % (popv,nopopv,e[0])
167
            srtloads.remove(e)
168
        #guadagno relativo = 1 - frac{LossPop}{LossNOpop}
169
        if(nopopv != 0):
170
            relGain.append(1.0-(popv / nopopv))
171

    
172
    return (x,ypop,ynopop,relGain)
173
    
174
def lossReduction(ypop,ynopop):
175
    totpop=sum(ypop)*1.0
176
    totnopop=sum(ynopop)*1.0
177

    
178
    print "Overall comparison", 1.0 - (totpop / totnopop), "totpop =",totpop, "totnopop=",totnopop
179
    print "Overall gain: %.2f%%" % ((1.0 - (totpop / totnopop)) * 100)
180
    nf = len(ypop)
181
    return ((1.0 - (totpop / totnopop)) * 100, nf)
182

    
183
if __name__ == "__main__":
184
    folder1=sys.argv[1] #popFolder
185
    folder2=sys.argv[2] #NOpopFolder
186
    graphfile=sys.argv[3]
187
    x,ypop,ynopop,relGain = synth(folder1,folder2,graphfile)
188

    
189
    G = nx.read_weighted_edgelist(graphfile, nodetype=int, create_using=nx.Graph())
190
    loads = nx.load_centrality(G,normalized=False,weight='weight')
191
    
192
    out=open("tuned.csv","w")
193

    
194
    t = PrettyTable(['CENTR','POP', 'NOPOP','RELGAIN'])
195
    for i in range(1,len(x)):
196
        row=[loads[x[i]],ypop[i],ynopop[i],relGain[i]]
197
        t.add_row(row)
198
        #code.interact(local=dict(globals(), **locals()))
199
        row = [str(a) for a in row]
200
        out.write(",".join(row)+"\n")
201
    print t
202
    out.close()
203
    #code.interact(local=dict(globals(), **locals()))
204
    
205
    print "Relative Gain"
206
    summary(relGain)
207
    #pprint(relGain)
208
    
209
    lossReduction(ypop,ynopop)
210
    
211
    f = plt.figure(1)
212
    plt.plot(range(len(x)), ypop, '-r*', label="pop",linewidth=1.2)
213
    plt.plot(range(len(x)), ynopop, '-b^', label="NOpop",linewidth=1.2)
214
    plt.legend(loc='upper right', fontsize=10, numpoints=1)
215
    plt.grid(True)
216

    
217
    plt.xlabel('experimentID (from most central failed node to less one)')
218
    plt.ylabel('Integral Empirical Loss')
219
    plt.legend(loc='upper right', fontsize=10, numpoints=1)
220
    plt.savefig("popVSnopopPlot.pdf",bbox_inches='tight')
221
    #plt.savefig("popVSnopopPlot.pgf",bbox_inches='tight')