Statistics
| Branch: | Revision:

mobicen / plotterBCrealization.py @ master

History | View | Annotate | Download (6.18 KB)

1 c16015e2 LoreBz
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 1ef4948a LoreBz
import pandas as pd
10
from pprint import pprint
11
import numpy as np
12
import glob
13 9c01cdd6 LoreBz
import matplotlib
14 c16015e2 LoreBz
# matplotlib.use('Agg')
15 9c01cdd6 LoreBz
import matplotlib.pyplot as plt
16 c16015e2 LoreBz
import seaborn as sns
17
sns.set()
18 1ef4948a LoreBz
19 23c7ab1e LoreBz
20
21 1ef4948a LoreBz
folder = sys.argv[1]
22
interval = 100
23 c16015e2 LoreBz
if len(sys.argv) > 2:
24 1ef4948a LoreBz
    interval = int(sys.argv[2])
25
nick = folder.split('/')[-2].split('_')[0]+"_"
26
27
28
os.chdir(folder)
29
30 c16015e2 LoreBz
dfn = pd.DataFrame()  # columns=node, rows= BC at row-index time-instant
31 1ef4948a LoreBz
print "Loading data from", folder, "..."
32 c16015e2 LoreBz
for snap in sorted(glob.glob('./stats*')):
33 1ef4948a LoreBz
    # print snap
34 c16015e2 LoreBz
    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 0a4aa24d LoreBz
#code.interact(local=dict(globals(), **locals()))
39 1ef4948a LoreBz
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 c16015e2 LoreBz
nodes = range(len(dfn.columns))
46 1ef4948a LoreBz
initialCentrality = {}
47
for n in nodes:
48 c16015e2 LoreBz
    initialCentrality[n] = dfn.iloc[0][n]
49
50
n0 = dfn.iloc[:, 0]
51 1ef4948a LoreBz
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 c16015e2 LoreBz
76 1ef4948a LoreBz
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 c16015e2 LoreBz
    plt.savefig(nick+"BCrealization["+str(i) +
82
                "-"+str(i+interval)+"].pdf", format='pdf')
83 1ef4948a LoreBz
    plt.clf()
84
os.chdir("./..")
85
86 c16015e2 LoreBz
87
'''
88 1ef4948a LoreBz
# 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 c16015e2 LoreBz

93 1ef4948a LoreBz
os.chdir("TimeFramesHeatmaps")
94
sns.set(font_scale=0.5)
95 c16015e2 LoreBz

96 1ef4948a LoreBz
for i in range(0, len(y)-interval, interval):
97 c16015e2 LoreBz
    xticks = range(i, i+interval)
98 1ef4948a LoreBz
    #yticks=range(0, len(dfn),5)
99 c16015e2 LoreBz
    sns.heatmap(dfn.iloc[xticks].T, cmap="Spectral",
100
                xticklabels=xticks, cbar_kws={'label': 'BC'})
101 1ef4948a LoreBz
    #ax.set_xticks(range(i, i+interval))
102
    plt.xlabel("Time [sec]")
103
    plt.ylabel("Nodes")
104
    plt.yticks(rotation=0)
105 c16015e2 LoreBz
    plt.savefig(nick+"BCrealization["+str(i) +
106
                "-"+str(i+interval)+"].pdf", format='pdf')
107 1ef4948a LoreBz
    plt.clf()
108
os.chdir("./..")
109
sns.set(font_scale=1)
110 c16015e2 LoreBz
'''
111 1ef4948a LoreBz
112
def coreNodesAtTime(t, perc):
113 c16015e2 LoreBz
    BCd = dict(dfn.iloc[t])
114 1ef4948a LoreBz
    srtd_BC = sorted(BCd.items(), key=operator.itemgetter(1), reverse=True)
115
    upto = int(len(srtd_BC) * (perc/100.0))
116 c16015e2 LoreBz
    coreNodes = [int(e[0]) for e in srtd_BC[:upto]]
117 1ef4948a LoreBz
    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 c16015e2 LoreBz
124 1ef4948a LoreBz
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 c16015e2 LoreBz
print "\tComputing ResistMap..."
139
for t in range(1, len(dfn.iloc[:,0])):
140 1ef4948a LoreBz
    coreNodes = coreNodesAtTime(t, 20)[2]
141
    old_Actives = [k for k, v in activeMap.items() if v]
142 c16015e2 LoreBz
    #code.interact(local=dict(globals(), **locals()))
143 1ef4948a LoreBz
    # 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 c16015e2 LoreBz
print "\tPlotting ResistMap..."
161 1ef4948a LoreBz
cmap1 = LinearSegmentedColormap.from_list('mycmap1', ['white', 'blue'], 2)
162 c16015e2 LoreBz
resDF = pd.DataFrame(coreResistMap).T
163 1ef4948a LoreBz
164
plt.ylabel("Nodes")
165
plt.xlabel("Time")
166 c16015e2 LoreBz
#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 23c7ab1e LoreBz
#code.interact(local=dict(globals(), **locals()))
171
sns.heatmap(small.applymap(int), cmap=cmap1, xticklabels=range(1000), yticklabels=range(len(small)), cbar_kws={
172 c16015e2 LoreBz
            'label': '\"Core Or Not\" (Blue or White)'})
173
174 1ef4948a LoreBz
plt.savefig(nick+"coreResistMap-EntryTOP10LeavingTOP20.pdf", format='pdf')
175
plt.clf()
176
177 c16015e2 LoreBz
178 1ef4948a LoreBz
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 c16015e2 LoreBz
#code.interact(local=dict(globals(), **locals()))
199
print "Distribuzione tempo permanenza nel core..."
200 1ef4948a LoreBz
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 c16015e2 LoreBz
pd.DataFrame(allint).hist(bins=50,normed=True)
211 1ef4948a LoreBz
plt.xlabel("Intervals of Persistence in the core [sec]")
212
plt.ylabel("Normalized Frequency")
213 c16015e2 LoreBz
plt.savefig(
214
    nick+"PersistenceDistributionEntryTOP10LeavingTOP20.pdf", format='pdf')
215 1ef4948a LoreBz
plt.clf()
216
217 c16015e2 LoreBz
f = open(nick + "stats.txt", 'w')
218 1ef4948a LoreBz
f.write(str(pd.DataFrame(allint).describe()))
219 23c7ab1e LoreBz
f.close()