Statistics
| Branch: | Revision:

mobicen / plotterBCrealization.py @ c16015e2

History | View | Annotate | Download (6.07 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
folder = sys.argv[1]
20
interval = 100
21 c16015e2 LoreBz
if len(sys.argv) > 2:
22 1ef4948a LoreBz
    interval = int(sys.argv[2])
23
nick = folder.split('/')[-2].split('_')[0]+"_"
24
25
26
os.chdir(folder)
27
28 c16015e2 LoreBz
dfn = pd.DataFrame()  # columns=node, rows= BC at row-index time-instant
29 1ef4948a LoreBz
print "Loading data from", folder, "..."
30 c16015e2 LoreBz
for snap in sorted(glob.glob('./stats*')):
31 1ef4948a LoreBz
    # print snap
32 c16015e2 LoreBz
    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 1ef4948a LoreBz
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 c16015e2 LoreBz
nodes = range(len(dfn.columns))
43 1ef4948a LoreBz
initialCentrality = {}
44
for n in nodes:
45 c16015e2 LoreBz
    initialCentrality[n] = dfn.iloc[0][n]
46
47
n0 = dfn.iloc[:, 0]
48 1ef4948a LoreBz
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 c16015e2 LoreBz
73 1ef4948a LoreBz
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 (NON-norm)")
78 c16015e2 LoreBz
    plt.savefig(nick+"BCrealization["+str(i) +
79
                "-"+str(i+interval)+"].pdf", format='pdf')
80 1ef4948a LoreBz
    plt.clf()
81
os.chdir("./..")
82
83 c16015e2 LoreBz
84
'''
85 1ef4948a LoreBz
# BC Heatmaps for consecutive time-frames
86
print "BC Heatmaps for consecutive time-frames"
87
if not os.path.exists("TimeFramesHeatmaps"):
88
    os.makedirs("TimeFramesHeatmaps")
89 c16015e2 LoreBz

90 1ef4948a LoreBz
os.chdir("TimeFramesHeatmaps")
91
sns.set(font_scale=0.5)
92 c16015e2 LoreBz

93 1ef4948a LoreBz
for i in range(0, len(y)-interval, interval):
94 c16015e2 LoreBz
    xticks = range(i, i+interval)
95 1ef4948a LoreBz
    #yticks=range(0, len(dfn),5)
96 c16015e2 LoreBz
    sns.heatmap(dfn.iloc[xticks].T, cmap="Spectral",
97
                xticklabels=xticks, cbar_kws={'label': 'BC'})
98 1ef4948a LoreBz
    #ax.set_xticks(range(i, i+interval))
99
    plt.xlabel("Time [sec]")
100
    plt.ylabel("Nodes")
101
    plt.yticks(rotation=0)
102 c16015e2 LoreBz
    plt.savefig(nick+"BCrealization["+str(i) +
103
                "-"+str(i+interval)+"].pdf", format='pdf')
104 1ef4948a LoreBz
    plt.clf()
105
os.chdir("./..")
106
sns.set(font_scale=1)
107 c16015e2 LoreBz
'''
108 1ef4948a LoreBz
109
def coreNodesAtTime(t, perc):
110 c16015e2 LoreBz
    BCd = dict(dfn.iloc[t])
111 1ef4948a LoreBz
    srtd_BC = sorted(BCd.items(), key=operator.itemgetter(1), reverse=True)
112
    upto = int(len(srtd_BC) * (perc/100.0))
113 c16015e2 LoreBz
    coreNodes = [int(e[0]) for e in srtd_BC[:upto]]
114 1ef4948a LoreBz
    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 c16015e2 LoreBz
121 1ef4948a LoreBz
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 c16015e2 LoreBz
print "\tComputing ResistMap..."
136
for t in range(1, len(dfn.iloc[:,0])):
137 1ef4948a LoreBz
    coreNodes = coreNodesAtTime(t, 20)[2]
138
    old_Actives = [k for k, v in activeMap.items() if v]
139 c16015e2 LoreBz
    #code.interact(local=dict(globals(), **locals()))
140 1ef4948a LoreBz
    # 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 c16015e2 LoreBz
print "\tPlotting ResistMap..."
158 1ef4948a LoreBz
cmap1 = LinearSegmentedColormap.from_list('mycmap1', ['white', 'blue'], 2)
159 c16015e2 LoreBz
resDF = pd.DataFrame(coreResistMap).T
160 1ef4948a LoreBz
161
plt.ylabel("Nodes")
162
plt.xlabel("Time")
163 c16015e2 LoreBz
#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 1ef4948a LoreBz
plt.savefig(nick+"coreResistMap-EntryTOP10LeavingTOP20.pdf", format='pdf')
171
plt.clf()
172
173 c16015e2 LoreBz
174 1ef4948a LoreBz
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 c16015e2 LoreBz
#code.interact(local=dict(globals(), **locals()))
195
print "Distribuzione tempo permanenza nel core..."
196 1ef4948a LoreBz
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 c16015e2 LoreBz
pd.DataFrame(allint).hist(bins=50,normed=True)
207 1ef4948a LoreBz
plt.xlabel("Intervals of Persistence in the core [sec]")
208
plt.ylabel("Normalized Frequency")
209 c16015e2 LoreBz
plt.savefig(
210
    nick+"PersistenceDistributionEntryTOP10LeavingTOP20.pdf", format='pdf')
211 1ef4948a LoreBz
plt.clf()
212
213 c16015e2 LoreBz
f = open(nick + "stats.txt", 'w')
214 1ef4948a LoreBz
f.write(str(pd.DataFrame(allint).describe()))
215 c16015e2 LoreBz
f.close()