-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplots_paper.py
177 lines (144 loc) · 5.8 KB
/
plots_paper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
import pickle, pprint, os, time, glob
import seaborn as sns
from matplotlib import pyplot as plt
import pulp
from graph_estimators import error_between_scalars, error_between_matrices, error_between_groups
#Style
plt.style.use('fivethirtyeight')
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 30
plt.rcParams['axes.labelsize'] = 30
plt.rcParams['axes.titlesize'] = 30
plt.rcParams['xtick.labelsize'] = 20
plt.rcParams['ytick.labelsize'] = 20
plt.rcParams['legend.fontsize'] = 30
plt.rcParams['figure.titlesize']= 30
def get_permutation_from_LP(Q1,Qt):
coeff = np.dot(np.transpose(Q1),Qt)
tau = {}
for i in range(Q1.shape[1]):
for j in range(Q1.shape[1]):# Why both Q1.shape with the [1]?
tau[(i,j)] = pulp.LpVariable("tau"+str(i)+str(j), 0, 1)
lp_prob = pulp.LpProblem("Unify LP", pulp.LpMaximize)
dot_cx = tau[(0,0)]*0
for i in range(Q1.shape[1]):
for j in range(Q1.shape[1]):
dot_cx += tau[(i,j)]*coeff[i,j]
lp_prob += dot_cx
for i in range(Q1.shape[1]):
constr = tau[(0,0)]*0
for j in range(Q1.shape[1]):
constr += tau[(i,j)]
lp_prob += constr == 1
for j in range(Q1.shape[1]):
constr = tau[(0,0)]*0
for i in range(Q1.shape[1]):
constr += tau[(i,j)]
lp_prob += constr == 1
# lp_prob.writeLP('temp.lp')
lp_prob.solve()
tau = []
for v in lp_prob.variables():
# print "\t",v.name, "=", v.varValue
tau.append(v.varValue)
# print "\t Obj =", pulp.value(lp_prob.objective)
return np.array(tau).reshape((Q1.shape[1],Q1.shape[1]))
def plot_error_vs_time(error,estimation_indices,error_std=None,attribute='',flag_write=False):
error = np.array([error[x] for x in error])
error_std = np.array([error_std[x] for x in error_std])
# print(estimation_indices)
# print(error)
# print(error_std)
fig, ax = plt.subplots()
ax.plot(estimation_indices,error)
if error_std is not None:
ax.fill_between(estimation_indices, error+error_std, error-error_std, color='yellow', alpha=0.5)
plt.xlabel('Number of snapshots')
plt.title(attribute)
plt.show()
if flag_write:
fig.savefig('./output/'+'_'.join([str(x) for x in time.localtime()])+'.png', bbox_inches='tight', pad_inches=0.2)
def plot_fixed_group(fname,flag_write=False):
rawdata = pickle.load(open(fname,'rb'))
log, glog, params = rawdata['log'], rawdata['glog'], rawdata['params']
attributes = {'wfinal':{'true_name':'Wtrue'},'gfinal':{'true_name':'gtrue'}}
if params['dynamic']=='bernoulli':
attributes['mufinal'] = {'true_name':'Mutrue'}
elif params['dynamic']=='lazy':
attributes['xifinal'] = {'true_name':'xitrue'}
else:
return NotImplementedError
if params['only_unify'] is True:
attributes = {'gfinal':{'true_name':'gtrue'}}
def get_title(attribute,params):
return attribute+' '+params['dynamic']+' n='+str(params['n'])+' k='+str(params['k'])+' '+params['unify_method']
def get_tau(gtrue,gfinal):
temp_nodes = gtrue.keys()
k = max(gtrue.values())
#Find permutation matrices tau
Qtrue = np.zeros((len(temp_nodes),k))
Qfinal = np.zeros((len(temp_nodes),k))
for i in temp_nodes: #every node index from 1 to n
Qtrue[i-1,gtrue[i]-1] = 1
Qfinal[i-1,gfinal[i]-1] = 1
tau = get_permutation_from_LP(Qtrue,Qfinal)
return {'tau':tau, 'Qtrue':Qtrue, 'Qfinal':Qfinal}
error = {}
error_std = {}
for attribute in attributes:
error[attribute] = {}
error_std[attribute] = {}
for t in params['estimation_indices']:
print('\n\n\n----------------------------\n\nt',t,'\n')
temp = []
for idx,x in enumerate(log):
tau_info = get_tau(glog[idx]['gtrue'],x[t]['gfinal'])
if attribute=='xifinal':
temp.append(error_between_scalars(params[attributes[attribute]['true_name']],x[t][attribute]))
elif attribute in ['wfinal','mufinal']:
temp.append(error_between_matrices(params[attributes[attribute]['true_name']],x[t][attribute],attribute,tau_info))
elif attribute=='gfinal':
temp.append(error_between_groups(glog[idx]['gtrue'],x[t][attribute],tau_info))
else:
return NotImplementedError
error[attribute][t] = np.mean(temp)
error_std[attribute][t] = np.std(temp)
for attribute in attributes:
plot_error_vs_time(error[attribute],params['estimation_indices'],error_std[attribute],get_title(attribute,params),flag_write)
# def plot_individual(fname,flag_write=False):
# rawdata = pickle.load(open(fname,'rb'))
# log, glog, params = rawdata['log'], rawdata['glog'], rawdata['params']
# attributes = {'gfinal':{'true_name':'gtrue'},'wfinal':{'true_name':'Wtrue'}}
# if params['dynamic']=='bernoulli':
# attributes['mufinal'] = {'true_name':'Mutrue'}
# elif params['dynamic']=='lazy':
# attributes['xifinal'] = {'true_name':'xitrue'}
# else:
# return NotImplementedError
# if params['only_unify'] is True:
# attributes = {'gfinal':{'true_name':'gtrue'}}
# error = {}
# error_std = {}
# attribute = 'wfinal'
# for t in params['estimation_indices']:
# temp = []
# for idx,x in enumerate(log):
# tau_info = get_tau(glog[idx]['gtrue'],x[t]['gfinal'])
# if attribute=='xifinal':
# temp.append(error_between_scalars(params[attributes[attribute]['true_name']],x[t][attribute]))
# elif attribute in ['wfinal','mufinal']:
# temp.append(error_between_matrices(params[attributes[attribute]['true_name']],x[t][attribute],attribute,tau_info))
# elif attribute=='gfinal':
# temp.append(error_between_groups(glog[idx]['gtrue'],x[t][attribute],tau_info))
# else:
# return NotImplementedError
# error[[t] = np.mean(temp)
# error_std[t] = np.std(temp)
# plot_error_vs_time(error[attribute],params['estimation_indices'],error_std[attribute],get_title(attribute,params),flag_write)
if __name__ == '__main__':
assert len(os.listdir('./output/pickles/')) is not None
for fname in glob.glob('./output/pickles/*pkl*'):
plot_fixed_group(fname,flag_write=True)