forked from tbekolay/quantities-comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_comparison.py
118 lines (89 loc) · 3.23 KB
/
run_comparison.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
import benchmarks.bench_astropy
import benchmarks.bench_pint
import benchmarks.bench_physipy
import benchmarks.bench_forallpeople
import benchmarks as bm
from pprint import pprint
import json
import warnings
import numpy as np
import pandas as pd
warnings.simplefilter('ignore')
np.seterr(all='ignore')
CLASSES = (bm.bench_astropy.BenchAstropy,
bm.bench_pint.BenchPint,
bm.bench_physipy.BenchPhysipy,
bm.bench_forallpeople.BenchForallpeople,
)
def run_comparisons(classes=CLASSES):
"""
Return generator for benchmarking the packages.
"""
for cls in classes:
print(cls.__name__)
yield bm.base.bench(cls)
def save_comparisons(results, fname=None):
if fname is None:
fname = 'results.json'
with open(fname, 'w') as outfile:
json.dump(list(results), outfile, indent=2, separators=(',', ': '))
def get_comparisons(classes=CLASSES, fname=None):
results = run_comparisons(classes)
save_comparisons(results, fname)
def process_pandas(res):
facts = {}
syntax = {}
speed = {}
compatibility = {}
for ires in res:
name = ires['name']
facts[name] = ires['facts']
syntax[name] = ires['syntax']
# We want to transpose the speed dict so it is organized
# by operation type rather than measurement.
# With pandas 1.4 we should be ablle to add error bars as well.
for key, value in ires['speed'].items():
for key1, value1 in value.items():
if key1 not in speed:
speed[key1] = {}
if key not in speed[key1]:
speed[key1][key] = {}
speed[key1][key][name] = value1
for key, value in ires['compatibility'].items():
if key not in compatibility:
compatibility[key] = {}
compatibility[key][name] = value
facts = pd.DataFrame.from_dict(facts, orient='index')
syntax = pd.DataFrame.from_dict(syntax, orient='index')
def color_green_true_red_false(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'green' if val else 'red'
return 'color: %s' % color
#facts = facts.sort_values(axis=0).sort(axis=1)
#syntax = syntax.sort_values(axis=0).sort(axis=1)
# speed and compatibility are dicts of DataFrames
for key, value in speed.items():
value = pd.DataFrame.from_dict(value, orient='columns')
speed[key] = value#.sort(axis=0).sort(axis=1)
for key, value in compatibility.items():
value = pd.DataFrame.from_dict(value, orient='index')
value = value.style.applymap(color_green_true_red_false)
compatibility[key] = value#.sort(axis=0).sort(axis=1)
resdict = {'facts': facts,
'syntax': syntax,
'speed': speed, # dict of df
'compatibility': compatibility, # dict of df
}
return resdict
def get_pandas(classes=CLASSES):
"""
Main interface : use only this.
"""
res = run_comparisons(classes)
return process_pandas(res)
if __name__ == "__main__":
get_comparisons()