-
Notifications
You must be signed in to change notification settings - Fork 47
/
generate_report.py
254 lines (184 loc) · 7.07 KB
/
generate_report.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
# coding=utf-8
import argparse
import json
import numpy as np
import pandas as pd
from log_parser import read_results
def calculate_statistics(tracks, niter):
niter -= 1
best_track = None
best_quality = np.inf
best_iter = -1
median = []
low = []
high = []
total = []
for track in tracks:
cur_quality = track.get_best_score()
time_per_iter = track.get_time_per_iter()
if not len(time_per_iter) or time_per_iter.shape[0] < niter:
continue
median.append(np.median(time_per_iter))
low.append(np.quantile(time_per_iter, 0.25))
high.append(np.quantile(time_per_iter, 0.75))
time_series = track.time_series
total.append(time_series[niter] - time_series[0])
if best_quality > cur_quality:
best_quality = cur_quality
best_iter = np.argmin(track.get_series()[1])
best_track = track
if best_track is None:
return {}
print(best_track)
median = sum(median) / len(median)
low = sum(low) / len(low)
high = sum(high) / len(high)
dev = max(median - low, high - median)
total = np.median(total)
return {
"Best": best_quality,
"Iter": best_iter,
"MedianTime": median,
"Deviation": dev,
"TotalTime": total
}
def get_experiment_stats(results_file, gpu, niter):
stats = {}
tracks = read_results(results_file)
for experiment_name in tracks:
stats[experiment_name] = {}
experiment_tracks = tracks[experiment_name]
experiment_tracks = dict(filter(lambda track: gpu == ('GPU' in track[0]), experiment_tracks.items()))
for algorithm_name in experiment_tracks:
stats[experiment_name][algorithm_name] = {}
table_tracks = split_tracks(experiment_tracks[algorithm_name])
for params, cur_tracks in table_tracks.items():
stat = calculate_statistics(cur_tracks, niter)
if stat == {}:
continue
stats[experiment_name][algorithm_name][params] = stat
return stats
def get_table_header(experiment_stats):
parameter_set = None
for algorithm_name in experiment_stats:
alg_parameter_set = set(experiment_stats[algorithm_name].keys())
if parameter_set is None:
parameter_set = alg_parameter_set
else:
parameter_set &= alg_parameter_set
return sorted(list(parameter_set))
def get_median_str(stat):
median = np.round(stat["MedianTime"], 3)
dev = np.round(stat["Deviation"], 3)
median_str = str(median)
if abs(dev) > 0:
median_str += u' +/- ' + str(dev)
return median_str
def print_all_in_one_table(stats, gpu, params, output):
median_table = []
total_table = []
index = ["catboost", "xgboost", "lightgbm"]
for algorithm_name in index:
median_row = []
total_row = []
if gpu:
algorithm_name += "-GPU"
else:
algorithm_name += "-CPU"
for experiment_name in stats:
experiment_stats = stats[experiment_name]
if algorithm_name not in experiment_stats:
continue
if params not in experiment_stats[algorithm_name]:
median_row.append(0.)
total_row.append(0.)
continue
cur_stat = experiment_stats[algorithm_name][params]
median_row.append(get_median_str(cur_stat))
total_row.append(np.round(cur_stat["TotalTime"], 3))
median_table.append(median_row)
total_table.append(total_row)
median_table = pd.DataFrame(median_table, index=index, columns=stats.keys())
total_table = pd.DataFrame(total_table, index=index, columns=stats.keys())
with open(output, 'w') as f:
f.write('Median time per iter, sec')
f.write('\n')
f.write(median_table.to_string())
f.write('\n')
f.write('Total time, sec')
f.write('\n')
f.write(total_table.to_string())
f.write('\n')
def print_experiment_table(stats, output):
for experiment_name in stats:
experiment_stats = stats[experiment_name]
header = get_table_header(experiment_stats)
median_table = []
total_table = []
for algorithm_name in experiment_stats:
algorithm_stats = experiment_stats[algorithm_name]
median_row = []
total_row = []
for parameter in header:
cur_stat = algorithm_stats[parameter]
total = np.round(cur_stat["TotalTime"], 3)
median_row.append(get_median_str(cur_stat))
total_row.append(total)
median_table.append(median_row)
total_table.append(total_row)
index = experiment_stats.keys()
median_table = pd.DataFrame(median_table, index=index, columns=header)
total_table = pd.DataFrame(total_table, index=index, columns=header)
with open(experiment_name + output, 'w') as f:
f.write('Median time per iter, sec')
f.write('\n')
f.write(median_table.to_string())
f.write('\n')
f.write('Total time, sec')
f.write('\n')
f.write(total_table.to_string())
f.write('\n')
def split_tracks(tracks):
depths = []
samples = []
for track in tracks:
depths.append(track.params.max_depth)
if "subsample" not in track.params_dict.keys():
samples.append(1.0)
continue
samples.append(track.params.subsample)
depths = set(depths)
samples = set(samples)
table_tracks = {(depth, subsample): [] for depth in depths for subsample in samples}
for track in tracks:
subsample = track.params.subsample if "subsample" in track.params_dict.keys() else 1.
table_tracks[(track.params.max_depth, subsample)].append(track)
return table_tracks
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--result', default='./results.json')
parser.add_argument('-o', '--output')
parser.add_argument('-t', '--type', choices=['common-table', 'by-depth-table', 'json'], default='common-table')
parser.add_argument('-f', '--filter', choices=['only-gpu', 'only-cpu'], default='only-gpu')
parser.add_argument('-p', '--params', default=(6.0, 1.0))
parser.add_argument('-niter', type=int, default=999)
args = parser.parse_args()
on_gpu = args.filter == 'only-gpu'
stats = get_experiment_stats(args.result, on_gpu, niter=args.niter)
output = args.output
if args.output is None:
output = args.type + '.txt'
if args.type == 'common-table':
print_all_in_one_table(stats, on_gpu, params=args.params, output=output)
return
if args.type == 'by-depth-table':
print_experiment_table(stats, output)
return
if args.type == 'json':
while len(stats.keys()) == 1:
stats = stats.values()[0]
with open(output, 'w') as f:
json.dump(stats, f)
return
if __name__ == "__main__":
main()