-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_results.py
54 lines (39 loc) · 1.48 KB
/
save_results.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
import os
import csv
import argparse
from datetime import datetime
def results_setting(
args: argparse.ArgumentParser
):
results_dir = args.results_dir
dataset_name = args.dataset_type
model_name = args.model_type
approach_name = args.app_name
current_date = datetime.now().strftime('%Y-%m-%d')
results_dir = os.path.join(results_dir, dataset_name, model_name, approach_name, current_date)
if not os.path.exists(results_dir):
os.makedirs(results_dir)
print(f'Directory {results_dir} is created.')
rounds = 'R' + str(args.num_rounds)
epochs = 'E' + str(args.num_epochs)
batch = 'B' + str(args.batch_size)
if args.data_dist_type == 'iid':
dist_type = 'iid'
elif args.data_dist_type == 'non-iid':
dist_type = 'A' + str(args.alpha)
if approach_name == 'P_SFL' or approach_name == 'PKL_SFL':
dist_type += '_U' + str(args.mu) + '_L' + str(args.lam)
filename = '_'.join([rounds, epochs, batch, f'C{str(args.num_clients)}', dist_type]) + '.csv'
results_dir = os.path.join(results_dir, filename)
header = ['round', 'train_loss', 'test_loss', 'train_accuracy', 'test_accuracy']
with open(results_dir, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(header)
return results_dir
def save_data(
path: str,
write_data: list
):
with open(path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(write_data)