-
Notifications
You must be signed in to change notification settings - Fork 29
/
plot_results.py
304 lines (269 loc) · 10.4 KB
/
plot_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
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
import argparse
import math
import pandas as pd
import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
from scipy.stats.mstats import gmean
from npbench.infrastructure import utilities as util
def my_round(x, width):
float_format = "{:." + f"{width}" + "f}"
return float_format.format(x)
# geomean which ignores NA values
def my_geomean(x):
x = x.dropna()
res = gmean(x)
return res
# make nice/short numbers with up/down indicator
def my_speedup_abbr(x):
prefix = ""
label = ""
if math.isnan(x):
return ""
if x < 1:
prefix = u"\u2191"
x = 1 / x
elif x > 1:
prefix = u"\u2193"
if x > 100:
x = int(x)
if x > 1000:
label = prefix + str(my_round(x / 1000, 1)) + "k"
else:
label = prefix + str(my_round(x, 1))
return str(label)
# make nice/short runtime numbers with seconds / milliseconds
def my_runtime_abbr(x):
suffix = " s"
if math.isnan(x):
return ""
if x < 0.1:
x = x * 1000
suffix = " ms"
return str(my_round(x, 2)) + suffix
def bootstrap_ci(data, statfunction=np.median, alpha=0.05, n_samples=300):
"""inspired by https://github.com/cgevans/scikits-bootstrap"""
# import warnings
def bootstrap_ids(data, n_samples=100):
for _ in range(n_samples):
yield np.random.randint(data.shape[0], size=(data.shape[0], ))
alphas = np.array([alpha / 2, 1 - alpha / 2])
nvals = np.round((n_samples - 1) * alphas).astype(int)
# if np.any(nvals < 10) or np.any(nvals >= n_samples - 10):
# warnings.warn(
# "Some values used extremal samples; results are probably unstable. "
# "Try to increase n_samples")
data = np.array(data)
if np.prod(data.shape) != max(data.shape):
raise ValueError("Data must be 1D")
data = data.ravel()
boot_indexes = bootstrap_ids(data, n_samples)
stat = np.asarray([statfunction(data[_ids]) for _ids in boot_indexes])
stat.sort(axis=0)
return stat[nvals][1] - stat[nvals][0]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p",
"--preset",
choices=['S', 'M', 'L', 'paper'],
nargs="?",
default='S')
args = vars(parser.parse_args())
# create a database connection
database = r"npbench.db"
conn = util.create_connection(database)
data = pd.read_sql_query("SELECT * FROM results", conn)
# get rid of kind and dwarf, we don't use them
data = data.drop(['timestamp', 'kind', 'dwarf', 'version'],
axis=1).reset_index(drop=True)
# Remove everything that does not have a domain
data = data[data["domain"] != ""]
# remove everything that does not validate, then get rid of validated column
data = data[data['validated'] == True]
data = data.drop(['validated'], axis=1).reset_index(drop=True)
# Filter by preset
data = data[data['preset'] == args['preset']]
data = data.drop(['preset'], axis=1).reset_index(drop=True)
# for each framework and benchmark, choose only the best details,mode (based on median runtime), then get rid of those
aggdata = data.groupby(["benchmark", "domain", "framework", "mode", "details"],
dropna=False).agg({
"time": np.median
}).reset_index()
best = aggdata.sort_values("time").groupby(
["benchmark", "domain", "framework", "mode"],
dropna=False).first().reset_index()
bestgroup = best.drop(
["time"],
axis=1) # remove time, we don't need it and it is actually a median
data = pd.merge(left=bestgroup,
right=data,
on=["benchmark", "domain", "framework", "mode", "details"],
how="inner") # do a join on data and best
data = data.drop(['mode', 'details'], axis=1).reset_index(drop=True)
frmwrks = list(data['framework'].unique())
print(frmwrks)
assert ('numpy' in frmwrks)
frmwrks.remove('numpy')
frmwrks.append('numpy')
lfilter = ['benchmark', 'domain'] + frmwrks
# get improvement over numpy (keep times in best_wide_time for numpy column), reorder columns
best_wide = best.pivot_table(index=["benchmark", "domain"],
columns="framework",
values="time").reset_index() # pivot to wide form
best_wide = best_wide[lfilter].reset_index(drop=True)
best_wide_time = best_wide.copy(deep=True)
for f in frmwrks:
best_wide[f] = best_wide[f] / best_wide_time['numpy']
# compute ci-size for each
cidata = data.groupby(["benchmark", "domain", "framework"], dropna=False).agg({
"time": [bootstrap_ci, np.median]
}).reset_index()
cidata.columns = ['_'.join(col).strip() for col in cidata.columns.values]
cidata['perc'] = (cidata['time_bootstrap_ci'] / cidata['time_median']) * 100
overall = best_wide.drop(['domain'], axis=1)
overall = pd.melt(overall, [
'benchmark',
])
overall = overall.groupby(['framework']).value.apply(my_geomean).reset_index(
) #this throws warnings if NA is found, which is ok
overall_wide = overall.pivot_table(columns="framework",
values="value",
dropna=False).reset_index(drop=True)
overall_wide = overall_wide[frmwrks]
overall_time = best_wide_time.drop(['domain'], axis=1)
overall_time = pd.melt(overall_time, ['benchmark'])
overall_time = overall_time.groupby(
['framework']).value.apply(my_geomean).reset_index(
) #this throws warnings if NA is found, which is ok
overall_time_wide = overall_time.pivot_table(
columns="framework", values="value", dropna=False).reset_index(drop=True)
overall_time_wide = overall_wide[frmwrks]
plt.style.use('classic')
figsz = (len(frmwrks) + 1, 12)
fig, (ax2, ax1) = plt.subplots(2,
1,
figsize=figsz,
sharex=True,
gridspec_kw={'height_ratios': [0.1, 5.7]})
hm_data_all = overall_wide
im0 = ax2.imshow(hm_data_all.to_numpy(),
cmap='RdYlGn_r',
interpolation='nearest',
vmin=0,
vmax=2,
aspect="auto")
ax2.set_yticks(np.arange(1))
ax2.set_yticklabels(["Total"])
for j in range(len(overall_wide.columns)):
if j < len(overall_wide.columns) - 1:
label = hm_data_all.to_numpy()[0, j]
t = label
if t < 1:
t = 1 / t
if t < 1.3:
text = ax2.text(j,
0,
my_speedup_abbr(label),
ha="center",
va="center",
color="grey",
fontsize=8)
else:
text = ax2.text(j,
0,
my_speedup_abbr(label),
ha="center",
va="center",
color="white",
fontsize=8)
else:
label = overall_time_wide['numpy'].to_numpy()[0]
# plot benchmark heatmap
hm_data = best_wide.drop(['benchmark', 'domain'], axis=1)
im = ax1.imshow(hm_data.to_numpy(),
cmap='RdYlGn_r',
interpolation='nearest',
vmin=0,
vmax=2,
aspect="auto")
# We want to show all ticks...
ticks = ax1.set_xticks(np.arange(len(hm_data.columns)))
ticks = ax1.set_yticks(np.arange(len(best_wide['benchmark'])))
# ... and label them with the respective list entries
ticks = ax1.set_xticklabels(hm_data.columns)
ticks = ax1.set_yticklabels(best_wide['benchmark'])
# Rotate the tick labels and set their alignment.
plt.setp(ax1.get_xticklabels(),
rotation=90,
ha="right",
rotation_mode="anchor")
for i in range(len(best_wide['benchmark'])):
# annotate with improvement over numpy
for j in range(len(hm_data.columns)):
b = best_wide['benchmark'][i]
f = hm_data.columns[j]
if j < len(hm_data.columns) - 1:
label = hm_data.to_numpy()[i, j]
if math.isnan(label):
r = ""
if len(r) > 0:
text = ax1.text(j,
i,
str(r.to_numpy()[0]),
ha="center",
va="center",
color="red",
fontsize=7)
else:
p = cidata[(cidata['framework_'] == f)
& (cidata['benchmark_'] == b)]['perc']
ci = int(p.to_numpy()[0])
if ci > 0:
ci = "$^{(" + str(ci) + ")}$"
else:
ci = ""
t = label
if t < 1:
t = 1 / t
if t < 1.3:
text = ax1.text(j,
i,
my_speedup_abbr(label) + ci,
ha="center",
va="center",
color="grey",
fontsize=8)
else:
text = ax1.text(j,
i,
my_speedup_abbr(label) + ci,
ha="center",
va="center",
color="white",
fontsize=8)
else:
label = best_wide_time['numpy'].to_numpy()[i]
p = cidata[(cidata['framework_'] == f)
& (cidata['benchmark_'] == b)]['perc']
try:
ci = int(p.to_numpy()[0])
if ci > 0:
ci = "$^{(" + str(ci) + ")}$"
else:
ci = ""
except:
pass
finally:
ci = ""
text = ax1.text(j,
i,
my_runtime_abbr(label) + ci,
ha="center",
va="center",
color="black",
fontsize=8)
ax1.set_ylabel("Benchmarks", labelpad=0)
plt.tight_layout()
plt.savefig("heatmap.pdf", dpi=600)
plt.show()