-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbenchmark.py
219 lines (183 loc) · 7.02 KB
/
benchmark.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
from itertools import product, repeat
from pathlib import Path
import time
from tabulate import tabulate
import numpy as np
from joblib import Memory
from tqdm import tqdm
from mtscomp import compress, decompress, load_raw_data
dtype = np.uint16
def mtscomp_perf(**kwargs):
ds = kwargs.pop('ds', None)
assert ds
name, n_channels, sample_rate, duration = ds
# Compress the file.
path = Path('data/' + name)
out = path.parent / 'data.cbin'
outmeta = path.parent / 'data.ch'
t0 = time.perf_counter()
compress(
path, out, outmeta, sample_rate=sample_rate, n_channels=n_channels, dtype=dtype,
check_after_compress=False, **kwargs)
t1 = time.perf_counter()
wt = t1 - t0
# Decompress the file and write it to disk.
out2 = path.with_suffix('.decomp.bin')
t0 = time.perf_counter()
decompress(out, outmeta, out2, check_after_decompress=False)
t1 = time.perf_counter()
rtc = t1 - t0
# Read the uncompressed file.
t0 = time.perf_counter()
x = load_raw_data(path, n_channels=n_channels, dtype=dtype, mmap=False)
assert x.size
t1 = time.perf_counter()
rtdec = t1 - t0
orig_size = path.stat().st_size
compressed_size = out.stat().st_size
return {
'read_time_compressed': rtc,
'read_time_decompressed': rtdec,
'write_time': wt,
'ratio': 100 - 100 * compressed_size / orig_size,
}
params = {
'ds': {
'title': 'dataset',
'values': [
# ('imec_385_1s.bin', 385, 3e4, 1.),
# ('imec_385_10s.bin', 385, 3e4, 10.),
('imec_385_100s.bin', 385, 3e4, 100.),
# ('pierre_10s.bin', 256, 2e4, 10.),
],
},
'n_threads': {
'title': 'n_threads',
'values': [1, 4, 8]
},
# 'do_time_diff': {
# 'title': 'time diff',
# 'values': [False, True],
# },
# 'do_spatial_diff': {
# 'title': 'spatial diff',
# 'values': [False, True],
# },
# 'compression_level': {
# 'title': 'Compression level',
# 'values': [-1],
# },
# 'chunk_duration': {
# 'title': 'Chunk duration (s)',
# 'values': [.1, 1, 10],
# },
}
targets = {
'values': ['write_time', 'read_time_compressed', 'read_time_decompressed', 'ratio'],
}
def _iter_param_set(params):
"""Iterate over all combinations of parameters as dictionaries {param: value}."""
yield from map(
dict, product(*(zip(repeat(param), info['values']) for param, info in params.items())))
class PlotParams:
def __init__(self, fun, params, targets):
self.fun = fun
self.params = params
self.targets = targets
self.plot_param = self._get_param_for_plotdim('plot')
self.row_param = self._get_param_for_plotdim('row')
self.column_param = self._get_param_for_plotdim('column')
self.group_param = self._get_param_for_plotdim('group')
self.bar_param = self._get_param_for_plotdim('bar')
self.target_plotdim = self.targets.get('plotdim', '')
self.target_values = self.targets.get('values', [])
self.plot_values = self._get_param_values(self.plot_param)
self.row_values = self._get_param_values(self.row_param)
self.column_values = self._get_param_values(self.column_param)
self.group_values = self._get_param_values(self.group_param)
self.bar_values = self._get_param_values(self.bar_param)
self.n_plots = len(self.plot_values) or 1
self.n_rows = len(self.row_values) or 1
self.n_columns = len(self.column_values) or 1
self.n_groups = len(self.group_values) or 1
self.n_bars = len(self.bar_values) or 1
def _get_param_values(self, param):
if param == 'target':
return self.target_values
return self.params.get(param, {}).get('values', [])
def _get_param_for_plotdim(self, plotdim):
if self.targets.get('plotdim', '') == plotdim:
return 'target'
try:
return next(p for p, i in self.params.items() if i.get('plotdim', '') == plotdim)
except StopIteration:
return
def _get_target(self, plot_idx=0, row=0, column=0, group_idx=0, bar_idx=0):
if self.target_plotdim == 'plot':
target_idx = plot_idx
elif self.target_plotdim == 'row':
target_idx = row
elif self.target_plotdim == 'column':
target_idx = column
elif self.target_plotdim == 'group':
target_idx = group_idx
return target_idx
def get_plot_value(self, plot_idx=0, row=0, column=0, group_idx=0, bar_idx=0):
target_idx = self._get_target(
plot_idx=plot_idx, row=row, column=column, group_idx=group_idx, bar_idx=bar_idx)
params = {
self.row_param: self.row_values[row],
self.column_param: self.column_values[column],
self.group_param: self.group_values[group_idx],
self.plot_param: self.plot_values[plot_idx],
self.bar_param: self.bar_values[bar_idx],
}
params.pop('target', None)
return self.fun(**params).get(self.target_values[target_idx], 0)
def make(self):
import matplotlib.pyplot as plt
fig, axes = plt.subplots(
self.n_rows, self.n_columns,
)
index = np.arange(self.n_groups)
bar_width = .75 / self.n_bars
for row in range(self.n_rows):
for column in range(self.n_columns):
if self.n_columns == self.n_rows == 1:
ax = axes
elif self.n_columns == 1:
ax = axes[row]
else:
ax = axes[row, column]
target_idx = self._get_target(
plot_idx=0, row=row, column=column)
for bar in range(self.n_bars):
values = [
self.get_plot_value(
row=row, column=column, group_idx=group, bar_idx=bar)
for group in range(self.n_groups)
]
label = self.bar_values[bar]
ax.bar(index + bar_width * bar, values, bar_width, label=label)
ax.set_xlabel(self._get_param_for_plotdim('group'))
ax.set_ylabel(self.target_values[target_idx])
ax.set_xticks(index + bar_width, map(str, self.group_values))
return fig
def benchmark_plots(fun, params=None, targets=None, output_dir=None):
params = params or {}
pp = PlotParams(fun, params, targets)
fig = pp.make()
fig.tight_layout()
return fig
if __name__ == '__main__':
location = '.cache'
memory = Memory(location, verbose=0)
fun = memory.cache(mtscomp_perf)
N = len(list(_iter_param_set(params)))
table = []
for param_set in tqdm(_iter_param_set(params), total=N):
output = fun(**param_set)
d = {k: v for k, v in param_set.items() if len(params[k]['values']) > 1}
d.update(output)
table.append(d)
print(tabulate(table, headers='keys'))