Revision 1ef4948a timeAnalysis.py

View differences:

timeAnalysis.py
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
1 11
import pandas as pd
2 12
from pprint import pprint
3 13
import numpy as np
4 14
import glob
5
from matplotlib import pyplot
6
from statsmodels.graphics.tsaplots import plot_acf, acf
7
import sys
8
import os
9
from collections import defaultdict
10
import matplotlib.pyplot as plt
11
import code  # code.interact(local=dict(globals(), **locals()))
15
import matplotlib
16
import seaborn as sns
17
sns.set()
18

  
12 19

  
13 20
folder = sys.argv[1]
14 21
lags = int(sys.argv[2])
15
nick = sys.argv[3]
22
nick = folder.split('/')[-2].split('_')[0]+"_"
16 23
os.chdir(folder)
17 24

  
18
dfn = pd.DataFrame() #rows=nodes columns=BC at column-index time-instant 
25
dfn = pd.DataFrame()  # rows=nodes columns=BC at column-index time-instant
19 26
print "Loading data from", folder, "..."
20 27
for snap in sorted(glob.glob('./BC*')):
21 28
    # print snap
22 29
    df = pd.read_csv(snap, names=['BC'], skiprows=1)
23
    dfn = pd.concat([dfn,df], axis=1)
30
    dfn = pd.concat([dfn, df], axis=1)
24 31

  
25 32
nodes = dfn.index.tolist()
26 33

  
......
28 35
for n in nodes:
29 36
    initialCentrality[n] = dfn.iloc[n][0]
30 37

  
31
from mpl_toolkits import mplot3d
32
import operator
33 38

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

  
37
dfACF = pd.DataFrame() # rows=Time-Lags, columns = nodes
43
dfACF = pd.DataFrame()  # rows=Time-Lags, columns = nodes
38 44
print "Processing data..."
39 45
for node in nodes:
40
    nodeACF =[pd.Series(dfn.iloc[node]).autocorr(lag) for lag in range(lags)]
41
    #code.interact(local=dict(globals(), **locals()))
46
    nodeACF = [pd.Series(dfn.iloc[node]).autocorr(lag) for lag in range(lags)]
42 47
    nodeACF = pd.DataFrame(nodeACF)
43 48
    dfACF = pd.concat([dfACF, nodeACF], axis=1)
44 49

  
......
49 54
Z ==> l'acf del nodo y al time-lag x
50 55
'''
51 56

  
52
os.chdir("./..")
57
if not os.path.exists("plots"+nick):
58
    os.makedirs("plots"+nick)
59
os.chdir("plots"+nick)
53 60
# Plotting
54

  
55
#lags=20
61
# Mean AutoCorrelation and Rank-Correlation
62
# lags=20
63
firstRank = dfn.iloc[:, 0]
56 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)
57 114
y = []
115

  
58 116
for i in x:
59
    y.append(np.mean(dfACF.iloc[i])) #[:,i] ==> all values of column i, where a column
60

  
61
plt.plot(x, y, lw="1.5")
62
plt.ylabel('Mean Autocorrelation')
63
plt.xlabel('Time-lags')
64
#plt.ylim(-1.0,1.0)
65
plt.xlim(0,lags)
66
plt.savefig(nick+"autoCorrMean.pdf", format='pdf')
117
    print "cacca"
118

  
67 119
plt.clf()
68 120

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

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

  
78 130

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

  
90
print "Plot saved in", folder+".."
91 142
print "THE END"
92

  

Also available in: Unified diff