forked from gem5/gem5
/
gem5_utils.py
124 lines (89 loc) · 3.53 KB
/
gem5_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
117
118
119
120
121
122
123
124
#!/usr/bin/python
# -*- coding: UTF8 -*-
import csv
import json
import collections
from objectpath import *
from os import path
from pyparsing import Word, Optional, ParseException, printables, nums, restOfLine
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
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[index] if index in self.raw_stats else None
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):
stat_rule = Word(printables) + Word('nan.%' + nums) + Optional(restOfLine)
stats = []
try:
with open(path.join(result_dir, stats_file_name)) as stats_file:
i = 0
for stat_line in stats_file:
if len(stats) <= i:
stats.append(collections.OrderedDict())
try:
stat = stat_rule.parseString(stat_line)
key = stat[0]
value = stat[1]
stats[i][key] = value
except ParseException as e:
# print(e)
pass
if 'End Simulation Statistics' in stat_line:
i += 1
except Exception as e:
print(e)
return None
else:
return stats
def parse_result(result_dir, config_json_file_name='config.json', stats_file_name='stats.txt', **props):
return ExperimentResults(ExperimentConfigs(read_configs(result_dir, config_json_file_name)),
[ExperimentStats(stat) for stat in read_stats(result_dir, stats_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, 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)
fig.savefig(plot_file_name + '.jpg')
plt.clf()
plt.close('all')