Statistics
| Branch: | Revision:

mobicen / timeAnalysis.py @ 1ef4948a

History | View | Annotate | Download (3.56 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 column-index time-instant
26
print "Loading data from", folder, "..."
27
for snap in sorted(glob.glob('./BC*')):
28
    # print snap
29
    df = pd.read_csv(snap, names=['BC'], skiprows=1)
30
    dfn = pd.concat([dfn, df], axis=1)
31

    
32
nodes = dfn.index.tolist()
33

    
34
initialCentrality = {}
35
for n in nodes:
36
    initialCentrality[n] = dfn.iloc[n][0]
37

    
38

    
39
sorted_x = sorted(initialCentrality.items(),
40
                  key=operator.itemgetter(1), reverse=True)
41
srtNodes = [e[0] for e in sorted_x]
42

    
43
dfACF = pd.DataFrame()  # rows=Time-Lags, columns = nodes
44
print "Processing data..."
45
for node in nodes:
46
    nodeACF = [pd.Series(dfn.iloc[node]).autocorr(lag) for lag in range(lags)]
47
    nodeACF = pd.DataFrame(nodeACF)
48
    dfACF = pd.concat([dfACF, nodeACF], axis=1)
49

    
50

    
51
'''
52
X ==> time-lag
53
Y ==> i nodi in ordine di centralita a tempo_0
54
Z ==> l'acf del nodo y al time-lag x
55
'''
56

    
57
if not os.path.exists("plots"+nick):
58
    os.makedirs("plots"+nick)
59
os.chdir("plots"+nick)
60
# Plotting
61
# Mean AutoCorrelation and Rank-Correlation
62
# lags=20
63
firstRank = dfn.iloc[:, 0]
64
x = range(0, lags)
65
meanACF = []
66
rankCorr = []
67
weightedRankCorr = []
68
for i in x:
69
    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])
72
plt.plot(x, meanACF, lw="1.5", label='Mean Autocorrelation')
73
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)')
76
plt.ylabel('Corr coeff: [ACF, Spearman rho]')
77
plt.xlabel('Time-lags / Time')
78
plt.grid()
79
plt.legend()
80
# plt.ylim(-1.0,1.0)
81
plt.xlim(0, lags)
82
plt.savefig(nick+"autoCorrMean-RankSpearman.pdf", format='pdf')
83
plt.clf()
84
'''
85

86

87

88
nodes2coreInst = defaultdict(list)
89
#nodes2rankInst = defaultdict(list)
90

91
for t in range(len(dfn.iloc[0])):
92
    coreT, coreRankT = coreNodesAtTime(dfn, t, 5)
93
    for n in coreT:
94
        nodes2coreInst[n].append((t,coreT[n],coreRankT[n]))
95

96

97

98
for n in [5,38,59,92]:
99
    points = nodes2coreInst[n]
100
    x = [p[0] for p in points]
101
    y = [len(nodes2coreInst)-p[2] for p in points]
102
    color = n / float(len(nodes2coreInst.keys()))
103
    #rgba = cmap(color)
104
    #plt.scatter(x,y, rgba)
105
    plt.plot(x,y, 'o')
106
plt.ylim(0, len(nodes2coreInst))
107
plt.show()
108
code.interact(local=dict(globals(), **locals()))
109
plt.show()
110
exit()
111
# Core Persitence
112
plags=100
113
x = range(0, plags)
114
y = []
115

116
for i in x:
117
    print "cacca"
118

119
plt.clf()
120

121
code.interact(local=dict(globals(), **locals()))'''
122

    
123
X, Y, Z = [], [], []
124
for node in srtNodes:
125
    for lag in range(lags):
126
        X.append(lag)
127
        Y.append(node)
128
        Z.append(list(dfACF.iloc[lag])[node])
129

    
130

    
131
fig = plt.figure()
132
ax = plt.axes(projection='3d')
133
ax.set_xlabel('Time-Lag')
134
ax.set_ylabel('Nodes sorted by BC at t_0')
135
ax.set_zlabel('ACF at time-lag x of node y')
136
ax.plot_trisurf(X, Y, Z, linewidth=0.2, antialiased=True)
137
ax.set_xlim(0, lags)
138
ax.set_ylim(0, len(srtNodes))
139
#ax.set_zlim(-1.0, 1.0)
140
plt.savefig(nick+"autoBC-3d.pdf", format="pdf")
141

    
142
print "THE END"