-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotting.py
More file actions
356 lines (313 loc) · 13.3 KB
/
Copy pathplotting.py
File metadata and controls
356 lines (313 loc) · 13.3 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import numpy as np
from scipy import stats
import pandas as pd
import wandb
from math import isnan
import pickle
import os
import pprint
from copy import deepcopy
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import rc
from utils import dict_hash
import warnings
warnings.filterwarnings("ignore")
rc("mathtext", default="regular")
result_path_parent = "results/"
dataset = "mnist"
print(dataset)
if dataset in ["fmnist","mnist"]:
select_fraction_main = 0.01 # 0.03
T_main = 400
T_array = [150, 250, 350]
# For mnist and fmnist
select_fraction_main = 0.01
if dataset in ["fmnist"]:
GLOBAL_PROJECT_NAME = "FL-FMNIST-Final"
else:
GLOBAL_PROJECT_NAME = "FL-MNIST-Final"
elif dataset in ["cifar10"]:
select_fraction_main = 0.1
T_main = 200
T_array = [100, 150, 200]
select_fraction_main = 0.1
GLOBAL_PROJECT_NAME = "FL-CIFAR10-Final"
project_name = GLOBAL_PROJECT_NAME
download_again = False
print(f"download status = {download_again}")
# entity = "your_entity" # Optional if the project is in your default entity
# Fetch run data from wandb
result_path = os.path.join(result_path_parent, f"{dataset}-runs.pkl")
if not os.path.exists(result_path_parent):
os.makedirs(result_path_parent)
if os.path.exists(result_path) and download_again == False:
with open(result_path, "rb") as f:
runs = pickle.load(f)
else:
wandb.login()
runs = wandb.Api().runs(
f"{project_name}",
# filters={
# "config.noise_level": {"$in": [0, 0.05, 0.1]},
# "config.select_fraction": {"$in": [0.01]},
# "config.dataset_alpha": {"$in": [1e-4, 1e-1, 1e1]},
# "config.systems_heterogenity": {"$in": [0, 0.5, 0.9]},
# },
)
with open(result_path, "wb") as f:
pickle.dump(runs, f)
result_df_path = os.path.join(result_path_parent, f"{dataset}-runs-df.pkl")
if os.path.exists(result_df_path) and download_again == False:
with open(result_df_path, "rb") as f:
result_df_original = pickle.load(f)
else:
completed_runs = []
print(len(runs))
for run in runs:
config = run.config
if (
len(config) > 0
and config["select_fraction"] == select_fraction_main
):
run_data = run.history(
keys=[
"test_accuracy",
"train_accuracy",
"train_loss",
"val_loss",
"test_loss",
"_timestamp"
]
)
if len(run_data) < T_main:
continue
run_config = pd.DataFrame.from_dict([config])
merged_df = (
run_data.assign(key=1)
.merge(run_config.assign(key=1), on="key")
.drop("key", axis=1)
)
completed_runs.append(merged_df)
print(len(completed_runs))
result_df_original = pd.concat(completed_runs, ignore_index=True)
with open(result_df_path, "wb") as f:
pickle.dump(result_df_original, f)
algo_list = ["greedyshap","ucb","sfedavg","fedavg","fedprox","poc","centralised"]
algo_list_formal = ["GreedyFed","UCB","S-FedAvg","FedAvg","FedProx","Power-Of-Choice","Centralized"]
def format_results(T_results, smoothed_df):
T_results = T_results - 1
resultsgroup = smoothed_df[smoothed_df["_step"] == T_results].groupby(["algorithm", "memory","mu"], dropna = False)
accuracy_mean = resultsgroup.mean()["test_accuracy"]
accuracy_std = resultsgroup.std()["test_accuracy"]
best_idxs = accuracy_mean.groupby("algorithm").idxmax()
accuracy_mean = accuracy_mean[best_idxs]
accuracy_std = accuracy_std[best_idxs]
# Create a custom categorical data type
algo_cat = pd.CategoricalDtype(categories=algo_list, ordered=True)
df = accuracy_mean
# Apply the custom data type to the "algorithm" column
df.index = df.index.set_levels(df.index.levels[0].astype(algo_cat), level='algorithm')
accuracy_mean = list(accuracy_mean.sort_index(level="algorithm"))
df = accuracy_std
# Apply the custom data type to the "algorithm" column
df.index = df.index.set_levels(df.index.levels[0].astype(algo_cat), level='algorithm')
accuracy_std = list(accuracy_std.sort_index(level="algorithm"))
result_list = []
for idx, algorithm in enumerate(algo_list):
# print(f"{algorithm} ${100*accuracy_mean[idx]:.2f}"+" \pm "+f"{100*accuracy_std[idx]:.2f}$")
result_list.append(f"${100*accuracy_mean[idx]:.2f} \pm {100*accuracy_std[idx]:.2f}$")
return result_list
def smoothen_df(alpha, result_df):
smoothed_df = pd.DataFrame()
# Smooth the data for each unique combination of 'algorithm' and 'algo_seed' separately using EWMA
for (algorithm, seed, memory, mu, dataset_alpha, noise, systems_heterogenity), group in result_df.groupby(
["algorithm", "seed", "memory","mu","dataset_alpha", "noise", "systems_heterogenity"], dropna = False
):
# keep only the latest run if all parameters are same
if (group["_timestamp"].head(T_main).values > group["_timestamp"].tail(T_main).values)[0]:
algorithm_df = group.head(T_main)
else:
algorithm_df = group.tail(T_main)
# Sort the unique step values for interpolation
sorted_steps = np.sort(algorithm_df["_step"].unique())
# Perform EWMA smoothing on the accuracy
smoothed_accuracy = algorithm_df["test_accuracy"].ewm(alpha=alpha).mean()
algorithm_smoothed_df = pd.DataFrame(
{
"_step": sorted_steps,
"test_accuracy": smoothed_accuracy,
"algorithm": algorithm,
"seed": seed,
"memory": memory,
"mu":mu,
"dataset_alpha":dataset_alpha,
"noise":noise,
"systems_heterogenity":systems_heterogenity,
}
)
smoothed_df = pd.concat([smoothed_df, algorithm_smoothed_df])
return smoothed_df
def generate_timing_results(result_df_original, smoothing=0, T = [150, 250, 350]):
noise = 0
dataset_alpha = 1e-4
systems_heterogenity = 0
df_filter_global = (
(result_df_original["noise"] == noise)
& (result_df_original["dataset_alpha"] == dataset_alpha)
& (result_df_original["systems_heterogenity"] == systems_heterogenity)
& (result_df_original["algo_beta"] != 0.01)
)
result_df = result_df_original.copy(deep=True)
result_df = result_df[df_filter_global]
smoothed_df = smoothen_df(1 - smoothing, result_df)
# Timing constraints data MNIST
results_1 = format_results(T_results=T[0], smoothed_df=smoothed_df)
results_2 = format_results(T_results=T[1], smoothed_df=smoothed_df)
results_3 = format_results(T_results=T[2], smoothed_df=smoothed_df)
for algo, a, b ,c in zip(algo_list_formal, results_1, results_2, results_3):
print(f"& {algo} & {a} & {b} & {c} \\\\")
def checknanequality(x, y):
if x == y:
return True
elif isinstance(x, float) and isnan(x) and isinstance(y, float) and isnan(y):
return True
else:
return False
def generate_timing_plots(result_df_original, smoothing=0, T = [150, 250, 350]):
noise = 0
dataset_alpha = 1e-4
systems_heterogenity = 0
df_filter_global = (
(result_df_original["noise"] == noise)
& (result_df_original["dataset_alpha"] == dataset_alpha)
& (result_df_original["systems_heterogenity"] == systems_heterogenity)
& (result_df_original["algo_beta"] != 0.01)
)
result_df = result_df_original.copy(deep=True)
result_df = result_df[df_filter_global]
smoothed_df = smoothen_df(1, result_df)
T_results = T_main - 1
resultsgroup = smoothed_df[smoothed_df["_step"]==T_results].groupby(["algorithm", "memory","mu"], dropna = False)
accuracy_mean = resultsgroup.mean()["test_accuracy"]
best_idxs = accuracy_mean.groupby("algorithm").idxmax()
smoothed_df_new = pd.DataFrame()
# Smooth the data for each unique combination of 'algorithm' and 'algo_seed' separately using EWMA
for (algorithm, seed, memory, mu, dataset_alpha, noise, systems_heterogenity), group in smoothed_df.groupby(
["algorithm", "seed", "memory","mu","dataset_alpha", "noise", "systems_heterogenity"], dropna = False
):
algorithm_df = group
# Sort the unique step values for interpolation
sorted_steps = np.sort(algorithm_df["_step"].unique())
# Perform EWMA smoothing on the accuracy
smoothed_accuracy = algorithm_df["test_accuracy"].ewm(alpha=1-smoothing).mean()
algorithm_smoothed_df = pd.DataFrame(
{
"_step": sorted_steps,
"test_accuracy": smoothed_accuracy,
"algorithm": algorithm,
"seed": seed,
"memory": memory,
"mu":mu,
"dataset_alpha":dataset_alpha,
"noise":noise,
"systems_heterogenity":systems_heterogenity,
}
)
if (checknanequality(memory, best_idxs[algorithm][1])) and checknanequality(mu,best_idxs[algorithm][2]):
smoothed_df_new = pd.concat([smoothed_df_new, algorithm_smoothed_df])
g = sns.lineplot(
data=smoothed_df_new, x="_step", y="test_accuracy", hue="algorithm", palette="tab10", errorbar="sd"
)
sns.despine()
plt.ylabel("Test Accuracy")
plt.xlabel("Communication Rounds")
# plt.ylim([0, 1])
fedprox_text = "FedProx"
fedavg_text = "FedAvg"
ucb_text = "UCB"
poc_text = "Power-Of-Choice"
sfedavg_text = "S-FedAvg"
centralised_text = "Centralized"
greedyshap_text = "GreedyFed"
g.legend_.set_title("Algorithm")
# ensure labels are in correct order
new_labels = [
centralised_text,
fedavg_text,
fedprox_text,
greedyshap_text,
poc_text,
sfedavg_text,
ucb_text,
]
for t, l in zip(g.legend_.texts, new_labels):
t.set_text(l)
# plt.grid(True)
plt.savefig(f'plots/{dataset}-timing.pdf', format="pdf", bbox_inches="tight")
# plt.savefig('testplot.png')
def generate_datahet_results(result_df_original, smoothing=0):
noise = 0
systems_heterogenity = 0
T = T_main
df_filter_global = (
(result_df_original["noise"] == noise)
& (result_df_original["systems_heterogenity"] == systems_heterogenity)
& (result_df_original["algo_beta"] != 0.01)
)
result_df = result_df_original.copy(deep=True)
result_df = result_df[df_filter_global]
smoothed_df = smoothen_df(1 - smoothing, result_df)
# Timing constraints data MNIST
results_1 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["dataset_alpha"]==1e-4])
results_2 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["dataset_alpha"]==1e-1])
results_3 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["dataset_alpha"]==1e1])
for algo, a, b ,c in zip(algo_list_formal, results_1, results_2, results_3):
print(f"& {algo} & {a} & {b} & {c} \\\\")
def generate_systems_results(result_df_original, smoothing=0):
noise = 0
dataset_alpha = 1e-4
T = T_main
df_filter_global = (
(result_df_original["noise"] == noise)
& (result_df_original["dataset_alpha"] == dataset_alpha)
& (result_df_original["algo_beta"] != 0.01)
)
result_df = result_df_original.copy(deep=True)
result_df = result_df[df_filter_global]
smoothed_df = smoothen_df(1 - smoothing, result_df)
# Timing constraints data MNIST
results_1 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["systems_heterogenity"]==0])
results_2 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["systems_heterogenity"]==0.5])
results_3 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["systems_heterogenity"]==0.9])
for algo, a, b ,c in zip(algo_list_formal, results_1, results_2, results_3):
print(f"& {algo} & {a} & {b} & {c} \\\\")
def generate_noise_results(result_df_original, smoothing=0):
systems_heterogenity = 0
dataset_alpha = 1e-4
T = T_main
df_filter_global = (
(result_df_original["systems_heterogenity"] == systems_heterogenity)
& (result_df_original["dataset_alpha"] == dataset_alpha)
& (result_df_original["algo_beta"] != 0.01)
)
result_df = result_df_original.copy(deep=True)
result_df = result_df[df_filter_global]
smoothed_df = smoothen_df(1 - smoothing, result_df)
# Timing constraints data MNIST
results_1 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["noise"]==0])
results_2 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["noise"]==0.05])
results_3 = format_results(T_results=T, smoothed_df=smoothed_df[smoothed_df["noise"]==0.1])
for algo, a, b ,c in zip(algo_list_formal, results_1, results_2, results_3):
print(f"& {algo} & {a} & {b} & {c} \\\\")
# generate LaTeX tables from raw run data
print("data")
generate_datahet_results(result_df_original)
print("timing")
generate_timing_results(result_df_original, T=T_array)
print("systems")
generate_systems_results(result_df_original)
print("noise")
generate_noise_results(result_df_original)
print("plots")
generate_timing_plots(result_df_original, T=T_array)