mobicen / plotterBCrealization.py @ master
History | View | Annotate | Download (6.18 KB)
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 |
9 |
import pandas as pd |
10 |
from pprint import pprint |
11 |
import numpy as np |
12 |
import glob |
13 |
import matplotlib |
14 |
# matplotlib.use('Agg')
|
15 |
import matplotlib.pyplot as plt |
16 |
import seaborn as sns |
17 |
sns.set() |
18 |
|
19 |
|
20 |
|
21 |
folder = sys.argv[1]
|
22 |
interval = 100
|
23 |
if len(sys.argv) > 2: |
24 |
interval = int(sys.argv[2]) |
25 |
nick = folder.split('/')[-2].split('_')[0]+"_" |
26 |
|
27 |
|
28 |
os.chdir(folder) |
29 |
|
30 |
dfn = pd.DataFrame() # columns=node, rows= BC at row-index time-instant
|
31 |
print "Loading data from", folder, "..." |
32 |
for snap in sorted(glob.glob('./stats*')): |
33 |
# print snap
|
34 |
node_id = int(snap.strip('.csv').strip('./stats')) |
35 |
df = pd.read_csv(snap, names=['time', str(node_id)], skiprows=1) |
36 |
dfn = pd.concat([dfn, df[str(node_id)]], axis=1) |
37 |
|
38 |
#code.interact(local=dict(globals(), **locals()))
|
39 |
|
40 |
print "Processing and plotting..." |
41 |
if not os.path.exists("plots"+nick): |
42 |
os.makedirs("plots"+nick)
|
43 |
os.chdir("plots"+nick)
|
44 |
|
45 |
nodes = range(len(dfn.columns)) |
46 |
initialCentrality = {} |
47 |
for n in nodes: |
48 |
initialCentrality[n] = dfn.iloc[0][n]
|
49 |
|
50 |
n0 = dfn.iloc[:, 0]
|
51 |
y = n0.values |
52 |
|
53 |
'''
|
54 |
#Batch Means of ACF
|
55 |
print "Bacth Means of ACF..."
|
56 |
nlg=15
|
57 |
memo=50
|
58 |
batMeans = []
|
59 |
for i in range(0, len(y)-memo, memo):
|
60 |
bacf = acf(y[i:i+memo], nlags=nlg)
|
61 |
batMeans.append(np.mean(bacf))
|
62 |
|
63 |
pd.Series(batMeans).plot()
|
64 |
plt.ylabel("Mean ACF for lags [0...15]")
|
65 |
plt.xlabel("Batches of 50 samples")
|
66 |
plt.savefig(nick+"batchMeansACF.pdf", format='pdf')
|
67 |
plt.clf()'''
|
68 |
|
69 |
# BC realization of a random node
|
70 |
print "BC realization of a random node..." |
71 |
if not os.path.exists("BCreal"): |
72 |
os.makedirs("BCreal")
|
73 |
os.chdir("BCreal")
|
74 |
|
75 |
|
76 |
for i in range(0, len(y)-interval, interval): |
77 |
plt.plot(range(i, i+interval, 1), y[i:i+interval]) |
78 |
plt.ylim(min(y), max(y)) |
79 |
plt.xlabel("Time [s]")
|
80 |
plt.ylabel("Betweenness Centrality (NON-norm)")
|
81 |
plt.savefig(nick+"BCrealization["+str(i) + |
82 |
"-"+str(i+interval)+"].pdf", format='pdf') |
83 |
plt.clf() |
84 |
os.chdir("./..")
|
85 |
|
86 |
|
87 |
'''
|
88 |
# BC Heatmaps for consecutive time-frames
|
89 |
print "BC Heatmaps for consecutive time-frames"
|
90 |
if not os.path.exists("TimeFramesHeatmaps"):
|
91 |
os.makedirs("TimeFramesHeatmaps")
|
92 |
|
93 |
os.chdir("TimeFramesHeatmaps")
|
94 |
sns.set(font_scale=0.5)
|
95 |
|
96 |
for i in range(0, len(y)-interval, interval):
|
97 |
xticks = range(i, i+interval)
|
98 |
#yticks=range(0, len(dfn),5)
|
99 |
sns.heatmap(dfn.iloc[xticks].T, cmap="Spectral",
|
100 |
xticklabels=xticks, cbar_kws={'label': 'BC'})
|
101 |
#ax.set_xticks(range(i, i+interval))
|
102 |
plt.xlabel("Time [sec]")
|
103 |
plt.ylabel("Nodes")
|
104 |
plt.yticks(rotation=0)
|
105 |
plt.savefig(nick+"BCrealization["+str(i) +
|
106 |
"-"+str(i+interval)+"].pdf", format='pdf')
|
107 |
plt.clf()
|
108 |
os.chdir("./..")
|
109 |
sns.set(font_scale=1)
|
110 |
'''
|
111 |
|
112 |
def coreNodesAtTime(t, perc): |
113 |
BCd = dict(dfn.iloc[t])
|
114 |
srtd_BC = sorted(BCd.items(), key=operator.itemgetter(1), reverse=True) |
115 |
upto = int(len(srtd_BC) * (perc/100.0)) |
116 |
coreNodes = [int(e[0]) for e in srtd_BC[:upto]] |
117 |
coreDict = {k: v for k, v in srtd_BC[:upto]} |
118 |
coreRank = {} |
119 |
for i in range(upto): |
120 |
coreRank[srtd_BC[i][0]] = i
|
121 |
return coreDict, coreRank, coreNodes
|
122 |
|
123 |
|
124 |
print "CoreResistence..." |
125 |
'''dfCoreResist = pd.DataFrame()
|
126 |
for t in range(len(dfn.iloc[0])):
|
127 |
coreT, coreRankT, coreNodes = coreNodesAtTime(t, 20)
|
128 |
corePD = pd.DataFrame(coreNodes)
|
129 |
dfCoreResist = pd.concat([dfCoreResist, corePD], axis=1)'''
|
130 |
activeMap = defaultdict(bool)
|
131 |
coreResistMap = [{}] |
132 |
firstCore = coreNodesAtTime(0, 20)[2] |
133 |
for n in nodes: |
134 |
flag = n in firstCore
|
135 |
activeMap[n] = flag |
136 |
coreResistMap[0][n] = flag
|
137 |
|
138 |
print "\tComputing ResistMap..." |
139 |
for t in range(1, len(dfn.iloc[:,0])): |
140 |
coreNodes = coreNodesAtTime(t, 20)[2] |
141 |
old_Actives = [k for k, v in activeMap.items() if v] |
142 |
#code.interact(local=dict(globals(), **locals()))
|
143 |
# rimuovi chi non e' piu' nella top20
|
144 |
for n in old_Actives: |
145 |
if n not in coreNodes: |
146 |
activeMap[n] = False
|
147 |
# aggiungi i nuovi arrivatim chi si trova nella meta' alta
|
148 |
for n in coreNodes[:len(coreNodes)/2]: |
149 |
activeMap[n] = True
|
150 |
# aggiorna la coreResistMap
|
151 |
resistings = {} |
152 |
for n in nodes: |
153 |
if activeMap[n]:
|
154 |
if n in coreNodes: |
155 |
resistings[n] = True
|
156 |
else:
|
157 |
resistings[n] = False
|
158 |
coreResistMap.append(resistings) |
159 |
|
160 |
print "\tPlotting ResistMap..." |
161 |
cmap1 = LinearSegmentedColormap.from_list('mycmap1', ['white', 'blue'], 2) |
162 |
resDF = pd.DataFrame(coreResistMap).T |
163 |
|
164 |
plt.ylabel("Nodes")
|
165 |
plt.xlabel("Time")
|
166 |
#sns.heatmap(resDF, cmap=cmap1, xticklabels=range(10000), yticklabels=range(650), cbar_kws={
|
167 |
# 'label': '\"Core Or Not\" (Blue or White)'})
|
168 |
|
169 |
small=pd.DataFrame(resDF.iloc[:,0:1000]) |
170 |
#code.interact(local=dict(globals(), **locals()))
|
171 |
sns.heatmap(small.applymap(int), cmap=cmap1, xticklabels=range(1000), yticklabels=range(len(small)), cbar_kws={ |
172 |
'label': '\"Core Or Not\" (Blue or White)'}) |
173 |
|
174 |
plt.savefig(nick+"coreResistMap-EntryTOP10LeavingTOP20.pdf", format='pdf') |
175 |
plt.clf() |
176 |
|
177 |
|
178 |
def activeIntervals(v): |
179 |
retval = [] |
180 |
current = 0
|
181 |
prev = False
|
182 |
for i in range(0, len(v)): |
183 |
if v[i]:
|
184 |
if prev == False: |
185 |
current += 1
|
186 |
prev = True
|
187 |
elif prev == True: |
188 |
current += 1
|
189 |
elif v[i] == False: |
190 |
if prev == False: |
191 |
continue
|
192 |
elif prev == True: |
193 |
retval.append(current) |
194 |
current = 0
|
195 |
prev = False
|
196 |
return retval
|
197 |
|
198 |
#code.interact(local=dict(globals(), **locals()))
|
199 |
print "Distribuzione tempo permanenza nel core..." |
200 |
nodes2interval = {} |
201 |
for n in nodes: |
202 |
nodes2interval[n] = activeIntervals(resDF.iloc[n]) |
203 |
|
204 |
allint = [] |
205 |
for e in nodes2interval.values(): |
206 |
allint = allint+e |
207 |
np.mean(allint) |
208 |
|
209 |
#code.interact(local=dict(globals(), **locals()))
|
210 |
pd.DataFrame(allint).hist(bins=50,normed=True) |
211 |
plt.xlabel("Intervals of Persistence in the core [sec]")
|
212 |
plt.ylabel("Normalized Frequency")
|
213 |
plt.savefig( |
214 |
nick+"PersistenceDistributionEntryTOP10LeavingTOP20.pdf", format='pdf') |
215 |
plt.clf() |
216 |
|
217 |
f = open(nick + "stats.txt", 'w') |
218 |
f.write(str(pd.DataFrame(allint).describe()))
|
219 |
f.close() |