Statistics
| Branch: | Revision:

mobicen / timeAnalysis.py @ cf03b931

History | View | Annotate | Download (2.29 KB)

1 fa4a0a42 LoreBz
import pandas as pd
2
from pprint import pprint
3
import numpy as np
4
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()))
12
13
folder = sys.argv[1]
14
lags = int(sys.argv[2])
15 ffc2b265 LoreBz
nick = sys.argv[3]
16 fa4a0a42 LoreBz
os.chdir(folder)
17
18 cf03b931 LoreBz
dfn = pd.DataFrame() #rows=nodes columns=BC at column-index time-instant 
19 fa4a0a42 LoreBz
print "Loading data from", folder, "..."
20
for snap in sorted(glob.glob('./BC*')):
21
    # print snap
22 cf03b931 LoreBz
    df = pd.read_csv(snap, names=['BC'], skiprows=1)
23
    dfn = pd.concat([dfn,df], axis=1)
24
25
nodes = dfn.index.tolist()
26 fa4a0a42 LoreBz
27 d1ed760e LoreBz
initialCentrality = {}
28 cf03b931 LoreBz
for n in nodes:
29
    initialCentrality[n] = dfn.iloc[n][0]
30 d1ed760e LoreBz
31
from mpl_toolkits import mplot3d
32
import operator
33
34
sorted_x = sorted(initialCentrality.items(), key=operator.itemgetter(1), reverse=True)
35
srtNodes = [e[0] for e in sorted_x]
36
37 cf03b931 LoreBz
dfACF = pd.DataFrame() # rows=Time-Lags, columns = nodes
38 fa4a0a42 LoreBz
print "Processing data..."
39 cf03b931 LoreBz
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()))
42
    nodeACF = pd.DataFrame(nodeACF)
43
    dfACF = pd.concat([dfACF, nodeACF], axis=1)
44
45 fa4a0a42 LoreBz
46 d1ed760e LoreBz
'''
47
X ==> time-lag
48
Y ==> i nodi in ordine di centralita a tempo_0
49
Z ==> l'acf del nodo y al time-lag x
50
'''
51 fa4a0a42 LoreBz
52
os.chdir("./..")
53
# Plotting
54
55
#lags=20
56
x = range(0, lags)
57
y = []
58
for i in x:
59 cf03b931 LoreBz
    y.append(np.mean(dfACF.iloc[i])) #[:,i] ==> all values of column i, where a column
60 fa4a0a42 LoreBz
61 d1ed760e LoreBz
plt.plot(x, y, lw="1.5")
62 fa4a0a42 LoreBz
plt.ylabel('Mean Autocorrelation')
63
plt.xlabel('Time-lags')
64
#plt.ylim(-1.0,1.0)
65
plt.xlim(0,lags)
66 ffc2b265 LoreBz
plt.savefig(nick+"autoCorrMean.pdf", format='pdf')
67 fa4a0a42 LoreBz
plt.clf()
68 d1ed760e LoreBz
69 cf03b931 LoreBz
#code.interact(local=dict(globals(), **locals()))
70
71
X,Y,Z = [], [], []
72
for node in srtNodes:
73
    for lag in range(lags):
74
        X.append(lag)
75
        Y.append(node)
76
        Z.append(list(dfACF.iloc[lag])[node])
77
        
78 d1ed760e LoreBz
79
fig = plt.figure()
80
ax = plt.axes(projection='3d')
81
ax.set_xlabel('Time-Lag')
82
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)
85
ax.set_xlim(0, lags)
86
ax.set_ylim(0, len(srtNodes))
87
#ax.set_zlim(-1.0, 1.0)
88 ffc2b265 LoreBz
plt.savefig(nick+"autoBC-3d.pdf", format="pdf")
89 d1ed760e LoreBz
90 fa4a0a42 LoreBz
print "Plot saved in", folder+".."
91
print "THE END"