-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_multi_run.py
More file actions
160 lines (111 loc) · 4.45 KB
/
Copy pathplot_multi_run.py
File metadata and controls
160 lines (111 loc) · 4.45 KB
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
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
from config import multi_run_results_file_path, MAX_CASE
label_fontsize = 30
tick_fontsize = 25
legend_fontsize = 30
plt.rcParams['font.family'] = 'Times New Roman'
print(multi_run_results_file_path)
filename = multi_run_results_file_path
list_case = []
list_tau_fixed = []
list_tau_adaptive = []
list_loss = []
list_acc = []
keys = []
keys_adaptive = []
with open(filename) as f:
for line in f:
l = line.replace('\n', '').split(',')
type = l[0]
simulation = l[1]
case = l[2]
tau_fixed = l[3]
loss = l[4]
accuracy = l[5]
tau_adaptive = l[6]
if (simulation != 'Simulation') and (type != 'centralized'):
list_case.append(int(case))
list_tau_fixed.append(int(tau_fixed))
list_loss.append(float(loss))
list_acc.append(float(accuracy))
keys.append((int(case), int(tau_fixed)))
if tau_fixed == '-1':
list_tau_adaptive.append(float(tau_adaptive))
keys_adaptive.append((int(case), int(tau_fixed)))
if type == 'centralized':
list_case.append(case)
list_tau_fixed.append((tau_fixed))
list_loss.append(float(loss))
list_acc.append(float(accuracy))
keys.append((case, tau_fixed))
list_tau_fixed = list(set(list_tau_fixed))
try:
i = list_tau_fixed.index('nan')
del list_tau_fixed[i]
except: # Exception if no centralized result exists
pass
list_tau_fixed = sorted([i for i in list_tau_fixed])
def avg_over_simulations(keys, values, list_ref):
i = iter(keys)
j = iter(values)
k = list(zip(i, j))
intermediate = defaultdict(list)
d = []
for key, value in k:
intermediate[key].append(value)
for key, value in intermediate.items():
d.append((key, sum(value) / len(value)))
d = dict(d)
# Centralized
centralized = d.get(('None', 'nan'), None)
ncase = list(range(0, MAX_CASE))
case = []
for i in range(0, len(ncase)):
case.append([])
for i in range(0, len(ncase)):
for j in range(0, len(list_ref)):
a = d.get((ncase[i], list_ref[j]), '')
case[i].append(a)
return [centralized, case]
loss_centralized, avg_list_loss = avg_over_simulations(keys, list_loss, list_tau_fixed)
accuracy_centralized, avg_list_acc = avg_over_simulations(keys, list_acc, list_tau_fixed)
_, tauAvg = avg_over_simulations(keys_adaptive, list_tau_adaptive, list_tau_fixed)
N_CASES = 4
color_cases = ['blue', 'green', 'cyan', 'magenta']
fixed_local_it_indexes = [i for i, x in enumerate(list_tau_fixed) if x > 0]
xaxis = [list_tau_fixed[i] for i in fixed_local_it_indexes]
single_point = np.ones(len(xaxis))
plt.figure(1, figsize=(8, 6))
for c in range(0, N_CASES):
plt.semilogx(xaxis, [avg_list_loss[c][i] for i in fixed_local_it_indexes], label='Case' + str(c+1),
color=color_cases[c])
if loss_centralized is not None:
plt.semilogx(xaxis, loss_centralized * single_point, '--', label='Centralized case', color='black')
plt.legend(loc='upper right', fontsize=legend_fontsize)
plt.xlabel('Local Update Times $\\tau$', fontsize=label_fontsize)
plt.ylabel('Loss Value', fontsize=label_fontsize)
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.3f'))
plt.xticks(fontsize=tick_fontsize)
plt.yticks(fontsize=tick_fontsize)
plt.tight_layout()
plt.savefig("loss_plot.pdf", format='pdf', bbox_inches='tight')
plt.savefig("loss_plot.svg", format='svg', bbox_inches='tight')
plt.figure(2, figsize=(8, 6))
for c in range(0, N_CASES):
plt.semilogx(xaxis, [avg_list_acc[c][i] for i in fixed_local_it_indexes], label='Case' + str(c+1),
color=color_cases[c])
if accuracy_centralized is not None:
plt.semilogx(xaxis, accuracy_centralized * single_point, '--', label='Centralized case', color='black')
plt.legend(loc='upper right', fontsize=legend_fontsize)
plt.xlabel('Local Update Times $\\tau$', fontsize=label_fontsize)
plt.ylabel('Classification Accuracy', fontsize=label_fontsize)
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.3f'))
plt.xticks(fontsize=tick_fontsize)
plt.yticks(fontsize=tick_fontsize)
plt.tight_layout()
plt.savefig("accuracy_plot.pdf", format='pdf', bbox_inches='tight')
plt.savefig("accuracy_plot.svg", format='svg', bbox_inches='tight')
plt.show()