Revision c16015e2
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() |
simulator.py | ||
---|---|---|
44 | 44 |
NN = self.params['nodes_number'] |
45 | 45 |
self.statsFiles = {} |
46 | 46 |
for i in range(NN): |
47 |
f = open(outPath+"/stats"+str(i)+".csv", 'a+')
|
|
47 |
f = open(outPath+"/stats"+'%08d' % i+".csv", 'a+')
|
|
48 | 48 |
self.statsFiles[i] = f |
49 | 49 |
f.write("Time,BC\n") |
50 | 50 |
|
timeAnalysis.py | ||
---|---|---|
24 | 24 |
|
25 | 25 |
dfn = pd.DataFrame() # rows=nodes columns=BC at column-index time-instant |
26 | 26 |
print "Loading data from", folder, "..." |
27 |
for snap in sorted(glob.glob('./BC*')):
|
|
27 |
for snap in sorted(glob.glob('./stats*')):
|
|
28 | 28 |
# print snap |
29 |
df = pd.read_csv(snap, names=['BC'], skiprows=1) |
|
30 |
dfn = pd.concat([dfn, df], axis=1) |
|
29 |
node_id = int(snap.strip('.csv').strip('./stats')) |
|
30 |
df = pd.read_csv(snap, names=['time', str(node_id)], skiprows=1) |
|
31 |
dfn = pd.concat([dfn, df[str(node_id)]], axis=1) |
|
31 | 32 |
|
32 |
nodes = dfn.index.tolist() |
|
33 |
|
|
34 |
nodes = dfn.columns.tolist() |
|
33 | 35 |
|
34 | 36 |
initialCentrality = {} |
35 | 37 |
for n in nodes: |
36 |
initialCentrality[n] = dfn.iloc[n][0]
|
|
38 |
initialCentrality[int(n)] = dfn.iloc[0][n]
|
|
37 | 39 |
|
38 | 40 |
|
39 | 41 |
sorted_x = sorted(initialCentrality.items(), |
... | ... | |
43 | 45 |
dfACF = pd.DataFrame() # rows=Time-Lags, columns = nodes |
44 | 46 |
print "Processing data..." |
45 | 47 |
for node in nodes: |
46 |
nodeACF = [pd.Series(dfn.iloc[node]).autocorr(lag) for lag in range(lags)]
|
|
47 |
nodeACF = pd.DataFrame(nodeACF)
|
|
48 |
print "Autocorr of node", node
|
|
49 |
nodeACF = pd.DataFrame([dfn[node].autocorr(lag) for lag in range(lags)])
|
|
48 | 50 |
dfACF = pd.concat([dfACF, nodeACF], axis=1) |
49 | 51 |
|
50 | 52 |
|
... | ... | |
56 | 58 |
|
57 | 59 |
if not os.path.exists("plots"+nick): |
58 | 60 |
os.makedirs("plots"+nick) |
61 |
|
|
59 | 62 |
os.chdir("plots"+nick) |
60 | 63 |
# Plotting |
61 | 64 |
# Mean AutoCorrelation and Rank-Correlation |
62 | 65 |
# lags=20 |
63 |
firstRank = dfn.iloc[:, 0]
|
|
66 |
firstRank = dfn.iloc[0,:]
|
|
64 | 67 |
x = range(0, lags) |
65 | 68 |
meanACF = [] |
66 | 69 |
rankCorr = [] |
67 | 70 |
weightedRankCorr = [] |
68 | 71 |
for i in x: |
72 |
#code.interact(local=dict(globals(), **locals())) |
|
69 | 73 |
meanACF.append(np.mean(dfACF.iloc[i])) |
70 |
rankCorr.append(stats.spearmanr(firstRank, dfn.iloc[:, i])[0]) |
|
71 |
weightedRankCorr.append(stats.weightedtau(firstRank, dfn.iloc[:, i])[0]) |
|
74 |
rankCorr.append(stats.spearmanr(firstRank, dfn.iloc[i,:])[0]) |
|
75 |
#weightedRankCorr.append(stats.weightedtau(firstRank, dfn.iloc[i,:])[0]) |
|
76 |
|
|
72 | 77 |
plt.plot(x, meanACF, lw="1.5", label='Mean Autocorrelation') |
73 | 78 |
plt.plot(x, rankCorr, lw="1.5", label='Rank-Correlation (with rank at t_0)') |
74 |
plt.plot(x, weightedRankCorr, lw="1.5", |
|
75 |
label='Weighted-Rank-Correlation (with rank at t_0)') |
|
79 |
#plt.plot(x, weightedRankCorr, lw="1.5",
|
|
80 |
# label='Weighted-Rank-Correlation (with rank at t_0)')
|
|
76 | 81 |
plt.ylabel('Corr coeff: [ACF, Spearman rho]') |
77 | 82 |
plt.xlabel('Time-lags / Time') |
78 | 83 |
plt.grid() |
... | ... | |
81 | 86 |
plt.xlim(0, lags) |
82 | 87 |
plt.savefig(nick+"autoCorrMean-RankSpearman.pdf", format='pdf') |
83 | 88 |
plt.clf() |
89 |
|
|
90 |
|
|
84 | 91 |
''' |
85 | 92 |
|
86 | 93 |
|
... | ... | |
118 | 125 |
|
119 | 126 |
plt.clf() |
120 | 127 |
|
121 |
code.interact(local=dict(globals(), **locals()))'''
|
|
128 |
''' |
|
122 | 129 |
|
123 | 130 |
X, Y, Z = [], [], [] |
124 | 131 |
for node in srtNodes: |
132 |
print "n:", node |
|
125 | 133 |
for lag in range(lags): |
134 |
print "\tn:%d lag:%d" % (node,lag) |
|
135 |
#code.interact(local=dict(globals(), **locals())) |
|
126 | 136 |
X.append(lag) |
127 | 137 |
Y.append(node) |
128 | 138 |
Z.append(list(dfACF.iloc[lag])[node]) |
util/milanoMob.py | ||
---|---|---|
23 | 23 |
MAX_WT = 0. |
24 | 24 |
|
25 | 25 |
# Random Waypoint model |
26 |
rwp = random_waypoint(nr_nodes=500, dimensions=(
|
|
26 |
rwp = random_waypoint(nr_nodes=200, dimensions=(
|
|
27 | 27 |
MAX_X, MAX_Y), velocity=(MIN_V, MAX_V), wt_max=MAX_WT) |
28 | 28 |
|
29 | 29 |
# Reference Point Group model |
30 | 30 |
# In tutto 150 nodi = 10x(5+4+3) + 15x(2) |
31 | 31 |
# 10 gruppi X 5 |
32 |
groups = [5 for _ in range(10)]
|
|
32 |
groups = [5 for _ in range(3)]
|
|
33 | 33 |
nr_nodes = sum(groups) |
34 | 34 |
rpg5 = reference_point_group( |
35 | 35 |
groups, dimensions=(MAX_X, MAX_Y), aggregation=0.5) |
36 | 36 |
|
37 | 37 |
# 10 gruppi X 4 |
38 |
groups = [4 for _ in range(10)]
|
|
38 |
groups = [4 for _ in range(3)]
|
|
39 | 39 |
nr_nodes = sum(groups) |
40 | 40 |
rpg4 = reference_point_group( |
41 | 41 |
groups, dimensions=(MAX_X, MAX_Y), aggregation=0.5) |
42 | 42 |
|
43 | 43 |
# 10 gruppi X 3 |
44 |
groups = [3 for _ in range(10)]
|
|
44 |
groups = [3 for _ in range(3)]
|
|
45 | 45 |
nr_nodes = sum(groups) |
46 | 46 |
rpg3 = reference_point_group( |
47 | 47 |
groups, dimensions=(MAX_X, MAX_Y), aggregation=0.5) |
48 | 48 |
|
49 | 49 |
# 15 gruppi X 2 |
50 |
groups = [2 for _ in range(15)]
|
|
50 |
groups = [2 for _ in range(10)]
|
|
51 | 51 |
nr_nodes = sum(groups) |
52 | 52 |
rpg2 = reference_point_group( |
53 | 53 |
groups, dimensions=(MAX_X, MAX_Y), aggregation=0.5) |
Also available in: Unified diff