mobicen / timeAnalysis.py @ 23c7ab1e
History  View  Annotate  Download (3.78 KB)
1 
import code # code.interact(local=dict(globals(), **locals())) 

2 
from collections import deque 
3 
from scipy import stats 
4 
import matplotlib.pyplot as plt 
5 
from collections import defaultdict 
6 
import os 
7 
import sys 
8 
from statsmodels.graphics.tsaplots import plot_acf, acf 
9 
import operator 
10 
from mpl_toolkits import mplot3d 
11 
import pandas as pd 
12 
from pprint import pprint 
13 
import numpy as np 
14 
import glob 
15 
import matplotlib 
16 
import seaborn as sns 
17 
sns.set() 
18  
19  
20 
folder = sys.argv[1]

21 
lags = int(sys.argv[2]) 
22 
nick = folder.split('/')[2].split('_')[0]+"_" 
23 
os.chdir(folder) 
24  
25 
dfn = pd.DataFrame() # rows=nodes columns=BC at columnindex timeinstant

26 
print "Loading data from", folder, "..." 
27 
for snap in sorted(glob.glob('./stats*')): 
28 
# print snap

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) 
32  
33  
34 
nodes = dfn.columns.tolist() 
35  
36 
initialCentrality = {} 
37 
for n in nodes: 
38 
initialCentrality[int(n)] = dfn.iloc[0][n] 
39  
40  
41 
sorted_x = sorted(initialCentrality.items(),

42 
key=operator.itemgetter(1), reverse=True) 
43 
srtNodes = [e[0] for e in sorted_x] 
44  
45 
dfACF = pd.DataFrame() # rows=TimeLags, columns = nodes

46 
print "Processing data..." 
47 
for node in nodes: 
48 
#print "Autocorr of node", node

49 
nodeACF = pd.DataFrame([dfn[node].autocorr(lag) for lag in range(lags)]) 
50 
dfACF = pd.concat([dfACF, nodeACF], axis=1)

51  
52  
53 
'''

54 
X ==> timelag

55 
Y ==> i nodi in ordine di centralita a tempo_0

56 
Z ==> l'acf del nodo y al timelag x

57 
'''

58  
59 
if not os.path.exists("plots"+nick): 
60 
os.makedirs("plots"+nick)

61  
62 
os.chdir("plots"+nick)

63 
# Plotting

64 
# Mean AutoCorrelation and RankCorrelation

65 
# lags=20

66 
firstRank = dfn.iloc[0,:]

67 
x = range(0, lags) 
68 
meanACF = [] 
69 
rankCorr = [] 
70 
weightedRankCorr = [] 
71 
for i in x: 
72 
#code.interact(local=dict(globals(), **locals()))

73 
meanACF.append(np.mean(dfACF.iloc[i])) 
74 
rankCorr.append(stats.spearmanr(firstRank, dfn.iloc[i,:])[0])

75 
#weightedRankCorr.append(stats.weightedtau(firstRank, dfn.iloc[i,:])[0])

76  
77 
plt.plot(x, meanACF, lw="1.5", label='Mean Autocorrelation') 
78 
plt.plot(x, rankCorr, lw="1.5", label='RankCorrelation (with rank at t_0)') 
79 
#plt.plot(x, weightedRankCorr, lw="1.5",

80 
# label='WeightedRankCorrelation (with rank at t_0)')

81 
plt.ylabel('Corr coeff: [ACF, Spearman rho]')

82 
plt.xlabel('Timelags / Time')

83 
plt.grid() 
84 
plt.legend() 
85 
# plt.ylim(1.0,1.0)

86 
plt.xlim(0, lags)

87 
plt.savefig(nick+"autoCorrMeanRankSpearman.pdf", format='pdf') 
88 
plt.clf() 
89  
90  
91 
'''

92 

93 

94 

95 
nodes2coreInst = defaultdict(list)

96 
#nodes2rankInst = defaultdict(list)

97 

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

99 
coreT, coreRankT = coreNodesAtTime(dfn, t, 5)

100 
for n in coreT:

101 
nodes2coreInst[n].append((t,coreT[n],coreRankT[n]))

102 

103 

104 

105 
for n in [5,38,59,92]:

106 
points = nodes2coreInst[n]

107 
x = [p[0] for p in points]

108 
y = [len(nodes2coreInst)p[2] for p in points]

109 
color = n / float(len(nodes2coreInst.keys()))

110 
#rgba = cmap(color)

111 
#plt.scatter(x,y, rgba)

112 
plt.plot(x,y, 'o')

113 
plt.ylim(0, len(nodes2coreInst))

114 
plt.show()

115 
code.interact(local=dict(globals(), **locals()))

116 
plt.show()

117 
exit()

118 
# Core Persitence

119 
plags=100

120 
x = range(0, plags)

121 
y = []

122 

123 
for i in x:

124 
print "cacca"

125 

126 
plt.clf()

127 

128 
'''

129  
130 
X, Y, Z = [], [], [] 
131 
for node in srtNodes: 
132 
#print "n:", node

133 
for lag in range(lags): 
134 
#print "\tn:%d lag:%d" % (node,lag)

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

136 
X.append(lag) 
137 
Y.append(node) 
138 
Z.append(list(dfACF.iloc[lag])[node])

139  
140  
141 
fig = plt.figure() 
142 
ax = plt.axes(projection='3d')

143 
ax.set_xlabel('TimeLag')

144 
ax.set_ylabel('Nodes sorted by BC at t_0')

145 
ax.set_zlabel('ACF at timelag x of node y')

146 
ax.plot_trisurf(X, Y, Z, linewidth=0.2, antialiased=True) 
147 
ax.set_xlim(0, lags)

148 
ax.set_ylim(0, len(srtNodes)) 
149 
#ax.set_zlim(1.0, 1.0)

150 
plt.savefig(nick+"autoBC3d.pdf", format="pdf") 
151  
152 
print "THE END" 