Revision c16015e2 plotterBCrealization.py

View differences:

plotterBCrealization.py
1
import code  # code.interact(local=dict(globals(), **locals()))
2
import operator
3
from scipy import stats
4
from collections import defaultdict
5
import os
6
import sys
7
from statsmodels.graphics.tsaplots import plot_acf, acf
8
from matplotlib.colors import LinearSegmentedColormap
1 9
import pandas as pd
2 10
from pprint import pprint
3 11
import numpy as np
4 12
import glob
5 13
import matplotlib
6
matplotlib.use('Agg')
14
# matplotlib.use('Agg')
7 15
import matplotlib.pyplot as plt
8
import seaborn as sns; sns.set()
9
from statsmodels.graphics.tsaplots import plot_acf, acf
10
import sys
11
import os
12
from collections import defaultdict
13
from scipy import stats
14
import operator
15
import code  # code.interact(local=dict(globals(), **locals()))
16
import seaborn as sns
17
sns.set()
16 18

  
17 19
folder = sys.argv[1]
18 20
interval = 100
19
if len(sys.argv)>2:
21
if len(sys.argv) > 2:
20 22
    interval = int(sys.argv[2])
21 23
nick = folder.split('/')[-2].split('_')[0]+"_"
22 24

  
23 25

  
24 26
os.chdir(folder)
25 27

  
26
dfn = pd.DataFrame() #rows=nodes columns=BC at column-index time-instant 
28
dfn = pd.DataFrame()  # columns=node, rows= BC at row-index time-instant
27 29
print "Loading data from", folder, "..."
28
for snap in sorted(glob.glob('./BC*')):
30
for snap in sorted(glob.glob('./stats*')):
29 31
    # print snap
30
    df = pd.read_csv(snap, names=['BC'], skiprows=1)
31
    dfn = pd.concat([dfn,df], axis=1)
32
    node_id = int(snap.strip('.csv').strip('./stats'))
33
    df = pd.read_csv(snap, names=['time', str(node_id)], skiprows=1)
34
    dfn = pd.concat([dfn, df[str(node_id)]], axis=1)
35

  
32 36

  
33 37
print "Processing and plotting..."
34 38
if not os.path.exists("plots"+nick):
35 39
    os.makedirs("plots"+nick)
36 40
os.chdir("plots"+nick)
37 41

  
38
nodes = dfn.index.tolist()
42
nodes = range(len(dfn.columns))
39 43
initialCentrality = {}
40 44
for n in nodes:
41
    initialCentrality[n] = dfn.iloc[n][0]
42
n0 = dfn.iloc[0]
45
    initialCentrality[n] = dfn.iloc[0][n]
46

  
47
n0 = dfn.iloc[:, 0]
43 48
y = n0.values
44 49

  
45 50
'''
......
64 69
    os.makedirs("BCreal")
65 70
os.chdir("BCreal")
66 71

  
72

  
67 73
for i in range(0, len(y)-interval, interval):
68 74
    plt.plot(range(i, i+interval, 1), y[i:i+interval])
69 75
    plt.ylim(min(y), max(y))
70 76
    plt.xlabel("Time [s]")
71 77
    plt.ylabel("Betweenness Centrality (NON-norm)")
72
    plt.savefig(nick+"BCrealization["+str(i)+"-"+str(i+interval)+"].pdf", format='pdf')
78
    plt.savefig(nick+"BCrealization["+str(i) +
79
                "-"+str(i+interval)+"].pdf", format='pdf')
73 80
    plt.clf()
74 81
os.chdir("./..")
75 82

  
83

  
84
'''
76 85
# BC Heatmaps for consecutive time-frames
77 86
print "BC Heatmaps for consecutive time-frames"
78 87
if not os.path.exists("TimeFramesHeatmaps"):
79 88
    os.makedirs("TimeFramesHeatmaps")
89

  
80 90
os.chdir("TimeFramesHeatmaps")
81 91
sns.set(font_scale=0.5)
92

  
82 93
for i in range(0, len(y)-interval, interval):
83
    xticks=range(i, i+interval)
94
    xticks = range(i, i+interval)
84 95
    #yticks=range(0, len(dfn),5)
85
    sns.heatmap(dfn.iloc[:,xticks],cmap="Spectral", xticklabels = xticks, cbar_kws={'label': 'BC'})
96
    sns.heatmap(dfn.iloc[xticks].T, cmap="Spectral",
97
                xticklabels=xticks, cbar_kws={'label': 'BC'})
86 98
    #ax.set_xticks(range(i, i+interval))
87 99
    plt.xlabel("Time [sec]")
88 100
    plt.ylabel("Nodes")
89 101
    plt.yticks(rotation=0)
90
    plt.savefig(nick+"BCrealization["+str(i)+"-"+str(i+interval)+"].pdf", format='pdf')
102
    plt.savefig(nick+"BCrealization["+str(i) +
103
                "-"+str(i+interval)+"].pdf", format='pdf')
91 104
    plt.clf()
92 105
os.chdir("./..")
93 106
sns.set(font_scale=1)
94

  
107
'''
95 108

  
96 109
def coreNodesAtTime(t, perc):
97
    BCd = dict(dfn.iloc[:, t])
110
    BCd = dict(dfn.iloc[t])
98 111
    srtd_BC = sorted(BCd.items(), key=operator.itemgetter(1), reverse=True)
99 112
    upto = int(len(srtd_BC) * (perc/100.0))
100
    coreNodes = [e[0] for e in srtd_BC[:upto]]
113
    coreNodes = [int(e[0]) for e in srtd_BC[:upto]]
101 114
    coreDict = {k: v for k, v in srtd_BC[:upto]}
102 115
    coreRank = {}
103 116
    for i in range(upto):
104 117
        coreRank[srtd_BC[i][0]] = i
105 118
    return coreDict, coreRank, coreNodes
106 119

  
120

  
107 121
print "CoreResistence..."
108 122
'''dfCoreResist = pd.DataFrame()
109 123
for t in range(len(dfn.iloc[0])):
......
118 132
    activeMap[n] = flag
119 133
    coreResistMap[0][n] = flag
120 134

  
121
for t in range(1, len(dfn.iloc[0])):
135
print "\tComputing ResistMap..."
136
for t in range(1, len(dfn.iloc[:,0])):
122 137
    coreNodes = coreNodesAtTime(t, 20)[2]
123 138
    old_Actives = [k for k, v in activeMap.items() if v]
139
    #code.interact(local=dict(globals(), **locals()))
124 140
    # rimuovi chi non e' piu' nella top20
125 141
    for n in old_Actives:
126 142
        if n not in coreNodes:
......
138 154
            resistings[n] = False
139 155
    coreResistMap.append(resistings)
140 156

  
141
from matplotlib.colors import LinearSegmentedColormap
142

  
157
print "\tPlotting ResistMap..."
143 158
cmap1 = LinearSegmentedColormap.from_list('mycmap1', ['white', 'blue'], 2)
144
resDF = pd.DataFrame(coreResistMap).transpose()
145
xticks = range(0, len(resDF.iloc[0]),1)
146
sns.heatmap(resDF, cmap=cmap1, xticklabels = xticks, cbar_kws={'label': '\"Core Or Not\" (Blue or White)'})#
159
resDF = pd.DataFrame(coreResistMap).T
147 160

  
148 161
plt.ylabel("Nodes")
149 162
plt.xlabel("Time")
163
#sns.heatmap(resDF, cmap=cmap1, xticklabels=range(10000), yticklabels=range(650), cbar_kws={
164
#            'label': '\"Core Or Not\" (Blue or White)'})
165

  
166
small=pd.DataFrame(resDF.iloc[:,0:1000])
167
sns.heatmap(small, cmap=cmap1, xticklabels=range(1000), yticklabels=range(len(small)), cbar_kws={
168
            'label': '\"Core Or Not\" (Blue or White)'})
169

  
150 170
plt.savefig(nick+"coreResistMap-EntryTOP10LeavingTOP20.pdf", format='pdf')
151 171
plt.clf()
152 172

  
173

  
153 174
def activeIntervals(v):
154 175
    retval = []
155 176
    current = 0
......
170 191
                prev = False
171 192
    return retval
172 193

  
173

  
194
#code.interact(local=dict(globals(), **locals()))
195
print "Distribuzione tempo permanenza nel core..."
174 196
nodes2interval = {}
175 197
for n in nodes:
176 198
    nodes2interval[n] = activeIntervals(resDF.iloc[n])
......
181 203
np.mean(allint)
182 204

  
183 205
#code.interact(local=dict(globals(), **locals()))
184
pd.DataFrame(allint).hist(bins=50, normed=True)
206
pd.DataFrame(allint).hist(bins=50,normed=True)
185 207
plt.xlabel("Intervals of Persistence in the core [sec]")
186 208
plt.ylabel("Normalized Frequency")
187
plt.savefig(nick+"PersistenceDistributionEntryTOP10LeavingTOP20.pdf", format='pdf')
209
plt.savefig(
210
    nick+"PersistenceDistributionEntryTOP10LeavingTOP20.pdf", format='pdf')
188 211
plt.clf()
189 212

  
190
f=open(nick +"stats.txt",'w')
213
f = open(nick + "stats.txt", 'w')
191 214
f.write(str(pd.DataFrame(allint).describe()))
192
f.close()
215
f.close()

Also available in: Unified diff