-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
116 lines (81 loc) · 3.19 KB
/
utils.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
#!/usr/bin/python
# -*- coding: UTF8 -*-
import csv
import json
from objectpath import *
from os import path
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import multiprocessing as mp
class ExperimentResults:
def __init__(self, configs, stats, props):
self.configs = configs
self.stats = stats
self.props = props
class ExperimentConfigs:
def __init__(self, raw_configs):
self.raw_configs = raw_configs
def __getitem__(self, index):
return self.raw_configs.execute('$.' + index)
class ExperimentStats:
def __init__(self, raw_stats):
self.raw_stats = raw_stats
def __getitem__(self, index):
return self.raw_stats.execute('$.' + index)
def read_configs(result_dir, config_json_file_name):
try:
with open(path.join(result_dir, config_json_file_name)) as config_json_file:
configs = Tree(json.load(config_json_file))
except Exception as e:
print(e)
return None
else:
return configs
def read_stats(result_dir, stats_file_name):
try:
with open(path.join(result_dir, stats_file_name)) as stats_json_file:
configs = Tree(json.load(stats_json_file))
except Exception as e:
print(e)
return None
else:
return configs
def parse_result(result_dir, config_json_file_name='config.noc.json', stats_json_file_name='stats.json', **props):
return ExperimentResults(ExperimentConfigs(read_configs(result_dir, config_json_file_name)),
ExperimentStats(read_stats(result_dir, stats_json_file_name)), props)
def to_csv(output_file_name, results, fields):
with open(output_file_name, 'w') as output_file:
writer = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow([field[0] for field in fields])
for result in results:
writer.writerow([field[1](result) for field in fields])
def generate_plot(csv_file_name, plot_file_name, x, y, hue, y_title, xticklabels_rotation=90):
sns.set(font_scale=1.5)
sns.set_style("white", {"legend.frameon": True})
df = pd.read_csv(csv_file_name)
ax = sns.barplot(data=df, x=x, y=y, hue=hue, palette=sns.color_palette("Paired"))
ax.set_xlabel('')
ax.set_ylabel(y_title)
labels = ax.get_xticklabels()
ax.set_xticklabels(labels, rotation=xticklabels_rotation)
fig = ax.get_figure()
if hue:
legend = ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
legend.set_label('')
fig.savefig(plot_file_name + '.pdf', bbox_extra_artists=(legend,), bbox_inches='tight')
fig.savefig(plot_file_name + '.jpg', bbox_extra_artists=(legend,), bbox_inches='tight')
else:
fig.tight_layout()
fig.savefig(plot_file_name) + '.pdf'
fig.savefig(plot_file_name + '.jpg')
plt.clf()
plt.close('all')
def run_experiments(experiments, run_experiment):
num_processes = (mp.cpu_count() - 1) if mp.cpu_count() > 1 else 1
pool = mp.Pool(num_processes)
pool.map(run_experiment, experiments)
pool.close()
pool.join()
def add_experiment(experiments, *args):
experiments.append(args)