mobicen / plotterBCrealization.py @ c16015e2
History  View  Annotate  Download (6.07 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 
folder = sys.argv[1]

20 
interval = 100

21 
if len(sys.argv) > 2: 
22 
interval = int(sys.argv[2]) 
23 
nick = folder.split('/')[2].split('_')[0]+"_" 
24  
25  
26 
os.chdir(folder) 
27  
28 
dfn = pd.DataFrame() # columns=node, rows= BC at rowindex timeinstant

29 
print "Loading data from", folder, "..." 
30 
for snap in sorted(glob.glob('./stats*')): 
31 
# print snap

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  
36  
37 
print "Processing and plotting..." 
38 
if not os.path.exists("plots"+nick): 
39 
os.makedirs("plots"+nick)

40 
os.chdir("plots"+nick)

41  
42 
nodes = range(len(dfn.columns)) 
43 
initialCentrality = {} 
44 
for n in nodes: 
45 
initialCentrality[n] = dfn.iloc[0][n]

46  
47 
n0 = dfn.iloc[:, 0]

48 
y = n0.values 
49  
50 
'''

51 
#Batch Means of ACF

52 
print "Bacth Means of ACF..."

53 
nlg=15

54 
memo=50

55 
batMeans = []

56 
for i in range(0, len(y)memo, memo):

57 
bacf = acf(y[i:i+memo], nlags=nlg)

58 
batMeans.append(np.mean(bacf))

59 

60 
pd.Series(batMeans).plot()

61 
plt.ylabel("Mean ACF for lags [0...15]")

62 
plt.xlabel("Batches of 50 samples")

63 
plt.savefig(nick+"batchMeansACF.pdf", format='pdf')

64 
plt.clf()'''

65  
66 
# BC realization of a random node

67 
print "BC realization of a random node..." 
68 
if not os.path.exists("BCreal"): 
69 
os.makedirs("BCreal")

70 
os.chdir("BCreal")

71  
72  
73 
for i in range(0, len(y)interval, interval): 
74 
plt.plot(range(i, i+interval, 1), y[i:i+interval]) 
75 
plt.ylim(min(y), max(y)) 
76 
plt.xlabel("Time [s]")

77 
plt.ylabel("Betweenness Centrality (NONnorm)")

78 
plt.savefig(nick+"BCrealization["+str(i) + 
79 
""+str(i+interval)+"].pdf", format='pdf') 
80 
plt.clf() 
81 
os.chdir("./..")

82  
83  
84 
'''

85 
# BC Heatmaps for consecutive timeframes

86 
print "BC Heatmaps for consecutive timeframes"

87 
if not os.path.exists("TimeFramesHeatmaps"):

88 
os.makedirs("TimeFramesHeatmaps")

89 

90 
os.chdir("TimeFramesHeatmaps")

91 
sns.set(font_scale=0.5)

92 

93 
for i in range(0, len(y)interval, interval):

94 
xticks = range(i, i+interval)

95 
#yticks=range(0, len(dfn),5)

96 
sns.heatmap(dfn.iloc[xticks].T, cmap="Spectral",

97 
xticklabels=xticks, cbar_kws={'label': 'BC'})

98 
#ax.set_xticks(range(i, i+interval))

99 
plt.xlabel("Time [sec]")

100 
plt.ylabel("Nodes")

101 
plt.yticks(rotation=0)

102 
plt.savefig(nick+"BCrealization["+str(i) +

103 
""+str(i+interval)+"].pdf", format='pdf')

104 
plt.clf()

105 
os.chdir("./..")

106 
sns.set(font_scale=1)

107 
'''

108  
109 
def coreNodesAtTime(t, perc): 
110 
BCd = dict(dfn.iloc[t])

111 
srtd_BC = sorted(BCd.items(), key=operator.itemgetter(1), reverse=True) 
112 
upto = int(len(srtd_BC) * (perc/100.0)) 
113 
coreNodes = [int(e[0]) for e in srtd_BC[:upto]] 
114 
coreDict = {k: v for k, v in srtd_BC[:upto]} 
115 
coreRank = {} 
116 
for i in range(upto): 
117 
coreRank[srtd_BC[i][0]] = i

118 
return coreDict, coreRank, coreNodes

119  
120  
121 
print "CoreResistence..." 
122 
'''dfCoreResist = pd.DataFrame()

123 
for t in range(len(dfn.iloc[0])):

124 
coreT, coreRankT, coreNodes = coreNodesAtTime(t, 20)

125 
corePD = pd.DataFrame(coreNodes)

126 
dfCoreResist = pd.concat([dfCoreResist, corePD], axis=1)'''

127 
activeMap = defaultdict(bool)

128 
coreResistMap = [{}] 
129 
firstCore = coreNodesAtTime(0, 20)[2] 
130 
for n in nodes: 
131 
flag = n in firstCore

132 
activeMap[n] = flag 
133 
coreResistMap[0][n] = flag

134  
135 
print "\tComputing ResistMap..." 
136 
for t in range(1, len(dfn.iloc[:,0])): 
137 
coreNodes = coreNodesAtTime(t, 20)[2] 
138 
old_Actives = [k for k, v in activeMap.items() if v] 
139 
#code.interact(local=dict(globals(), **locals()))

140 
# rimuovi chi non e' piu' nella top20

141 
for n in old_Actives: 
142 
if n not in coreNodes: 
143 
activeMap[n] = False

144 
# aggiungi i nuovi arrivatim chi si trova nella meta' alta

145 
for n in coreNodes[:len(coreNodes)/2]: 
146 
activeMap[n] = True

147 
# aggiorna la coreResistMap

148 
resistings = {} 
149 
for n in nodes: 
150 
if activeMap[n]:

151 
if n in coreNodes: 
152 
resistings[n] = True

153 
else:

154 
resistings[n] = False

155 
coreResistMap.append(resistings) 
156  
157 
print "\tPlotting ResistMap..." 
158 
cmap1 = LinearSegmentedColormap.from_list('mycmap1', ['white', 'blue'], 2) 
159 
resDF = pd.DataFrame(coreResistMap).T 
160  
161 
plt.ylabel("Nodes")

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  
170 
plt.savefig(nick+"coreResistMapEntryTOP10LeavingTOP20.pdf", format='pdf') 
171 
plt.clf() 
172  
173  
174 
def activeIntervals(v): 
175 
retval = [] 
176 
current = 0

177 
prev = False

178 
for i in range(0, len(v)): 
179 
if v[i]:

180 
if prev == False: 
181 
current += 1

182 
prev = True

183 
elif prev == True: 
184 
current += 1

185 
elif v[i] == False: 
186 
if prev == False: 
187 
continue

188 
elif prev == True: 
189 
retval.append(current) 
190 
current = 0

191 
prev = False

192 
return retval

193  
194 
#code.interact(local=dict(globals(), **locals()))

195 
print "Distribuzione tempo permanenza nel core..." 
196 
nodes2interval = {} 
197 
for n in nodes: 
198 
nodes2interval[n] = activeIntervals(resDF.iloc[n]) 
199  
200 
allint = [] 
201 
for e in nodes2interval.values(): 
202 
allint = allint+e 
203 
np.mean(allint) 
204  
205 
#code.interact(local=dict(globals(), **locals()))

206 
pd.DataFrame(allint).hist(bins=50,normed=True) 
207 
plt.xlabel("Intervals of Persistence in the core [sec]")

208 
plt.ylabel("Normalized Frequency")

209 
plt.savefig( 
210 
nick+"PersistenceDistributionEntryTOP10LeavingTOP20.pdf", format='pdf') 
211 
plt.clf() 
212  
213 
f = open(nick + "stats.txt", 'w') 
214 
f.write(str(pd.DataFrame(allint).describe()))

215 
f.close() 