forked from TL-System/plato
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
94 lines (73 loc) · 2.95 KB
/
Copy pathplot.py
File metadata and controls
94 lines (73 loc) · 2.95 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
"""
A simple utility that plot figures of results as PDF files, stored in results/.
"""
import csv
from typing import Dict, List, Any
import matplotlib.pyplot as plt
from plato.config import Config
def read_csv_to_dict(result_csv_file: str) -> Dict[str, List]:
"""Read a CSV file and write the values that need to be plotted
into a dictionary."""
result_dict: Dict[str, List] = {}
plot_pairs = Config().results.plot
plot_pairs = [x.strip() for x in plot_pairs.split(',')]
for pairs in plot_pairs:
pair = [x.strip() for x in pairs.split('&')]
for item in pair:
if item not in result_dict:
result_dict[item] = []
with open(result_csv_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
for item in result_dict:
if item == 'gloabl_round':
result_dict[item].append(int(row[item]))
else:
result_dict[item].append(float(row[item]))
return result_dict
def plot(x_label: str, x_value: List[Any], y_label: str, y_value: List[Any],
figure_file_name: str):
"""Plot a figure."""
fig, ax = plt.subplots()
ax.plot(x_value, y_value)
ax.set(xlabel=x_label, ylabel=y_label)
fig.savefig(figure_file_name)
def plot_figures_from_dict(result_csv_file: str, result_dir: str):
"""Plot figures with dictionary of results."""
result_dict = read_csv_to_dict(result_csv_file)
plot_pairs = Config().results.plot
plot_pairs = [x.strip() for x in plot_pairs.split(',')]
for pairs in plot_pairs:
figure_file_name = result_dir + pairs + '.pdf'
pair = [x.strip() for x in pairs.split('&')]
x_y_labels: List = []
x_y_values: Dict[str, List] = {}
for item in pair:
label = {
'global_round': 'Global training round',
'accuracy': 'Accuracy (%)',
'training_time': 'Training time (s)',
'edge_agg_num': 'Aggregation rounds on edge servers'
}[item]
x_y_labels.append(label)
x_y_values[label] = result_dict[item]
x_label = x_y_labels[0]
y_label = x_y_labels[1]
x_value = x_y_values[x_label]
y_value = x_y_values[y_label]
plot(x_label, x_value, y_label, y_value, figure_file_name)
def main():
"""Plotting figures from the run-time results."""
__ = Config()
if hasattr(Config(), 'results'):
datasource = Config().data.datasource
model = Config().trainer.model_name
server_type = Config().algorithm.type
result_dir = f'./results/{datasource}/{model}/{server_type}/'
result_csv_file = result_dir + 'result.csv'
print(f"Plotting results located at {result_csv_file}.")
plot_figures_from_dict(result_csv_file, result_dir)
else:
print("No results to be plotted according to the configuration file.")
if __name__ == "__main__":
main()