Statistics
| Branch: | Revision:

nepatest_popbabel / scripts / lossred.py @ 60ba786f

History | View | Annotate | Download (3.65 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 networkx as nx
10

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

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

    
44
folder = sys.argv[1]
45

    
46
exp2couple = {}
47

    
48
exps = glob.glob(folder+"*POP")
49
exps = [e.split("/")[-1] for e in exps]
50

    
51
for e1 in exps:
52
    ntwname = e1.replace("NOPOP","").replace("POP","").replace("Unw","")
53
    #print ntwname
54
    for e2 in exps:
55
        if e1==e2: continue
56
        if e2.startswith(ntwname):
57
            #print e1,e2
58
            if e2.endswith("NOPOP"):
59
                pope = folder+e1
60
                nopope = folder+e2
61
            else:
62
                pope = folder+e2
63
                nopope = folder+e1
64
            #print "\t\t", pope, nopope
65
            exp2couple[ntwname]=(pope,nopope)
66

    
67
#pprint(exp2couple)
68
#code.interact(local=dict(globals(), **locals()))
69

    
70
graphs1 = set(glob.glob("./data/*edges"))
71
#pprint(graphs1)
72
graphs2 = set(glob.glob("./data/*/*edges"))
73
#pprint(graphs2)
74
graphs = list(set().union(graphs1,graphs2))
75

    
76
for ntw in exp2couple.keys():
77
    for g in graphs:
78
        gn = g.split("/")[-1]
79
        #print ntw.lower(), gn.lower()
80
        if ntw.lower() in gn.lower():
81
            print ntw, g
82
            exp2couple[ntw]+=(g,)
83

    
84
#pprint(exp2couple)
85
#code.interact(local=dict(globals(), **locals())) 
86

    
87
out=open("allNTWlossReductions.csv", "w")
88
out.write("Network,NTWsize,|Edges|,nf,Loss Reduction\n")
89
t = PrettyTable(['Network','NTWsize','|Edges|','nf','Loss Reduction'])
90
for ntw in exp2couple:
91
    if (len(exp2couple[ntw])>2):
92
        f1,f2,gn=exp2couple[ntw]
93
        G = nx.read_weighted_edgelist(gn, nodetype=int, create_using=nx.Graph())
94
        ntwsize = len(G.nodes())
95
        numedges = len(G.edges())
96
        #code.interact(local=dict(globals(), **locals()))
97
        x,ypop,ynopop,relGain = synth.synth(f1+"/",f2+"/",gn)
98
        #code.interact(local=dict(globals(), **locals()))
99
        lossred, nf=synth.lossReduction(ypop,ynopop)
100
        #print "Lossred in ",ntw,"=",lossred
101
        t.add_row([ntw,ntwsize,numedges,nf,lossred])
102
        out.write(ntw+","+str(ntwsize)+","+str(numedges)+","+str(nf)+","+str(lossred)+"\n")        
103
print t