forked from cubewise-code/optimus-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.py
194 lines (150 loc) · 7.51 KB
/
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
import itertools
import logging
import os
import statistics
from pathlib import WindowsPath
from typing import List, Union
import matplotlib.lines as mlines
from matplotlib import pyplot as plt
SEPARATOR = ","
class PermutationResult:
counter = 1
current_ram = None
def __init__(self, mode: str, cube_name: str, view_names: list, dimension_order: list,
query_times_by_view: dict, ram_usage: float = None, ram_percentage_change: float = None,
reset_counter: bool = False):
from optimuspy import ExecutionMode
self.mode = ExecutionMode(mode)
self.cube_name = cube_name
self.view_names = view_names
self.dimension_order = dimension_order
self.query_times_by_view = query_times_by_view
self.is_best = False
# from original dimension order
if ram_usage:
self.ram_usage = ram_usage
# from all other dimension orders
elif ram_percentage_change is not None:
self.ram_usage = PermutationResult.current_ram + (
PermutationResult.current_ram * ram_percentage_change / 100)
else:
raise RuntimeError("Either 'ram_usage' or 'ram_percentage_change' must be provided")
PermutationResult.current_ram = self.ram_usage
self.ram_percentage_change = ram_percentage_change or 0
if reset_counter:
PermutationResult.counter = 1
self.permutation_id = PermutationResult.counter
PermutationResult.counter += 1
def median_query_time(self, view_name: str = None) -> float:
view_name = view_name or self.view_names[0]
median = statistics.median(self.query_times_by_view[view_name])
if not median:
raise RuntimeError(f"view '{view_name}' in cube '{self.cube_name}' is too small")
return median
def build_csv_header(self) -> str:
return SEPARATOR.join(
["ID", "Mode", "Is Best", "Mean Query Time", "RAM"] +
["Dimension" + str(d) for d in range(1, len(self.dimension_order) + 1)]) + "\n"
def to_row(self, view_name: str) -> List[str]:
from optimuspy import LABEL_MAP
return [str(self.permutation_id),
LABEL_MAP[self.mode],
str(self.is_best),
"{0:.8f}".format(self.median_query_time(view_name)),
"{0:.0f}".format(self.ram_usage)] + list(self.dimension_order)
def to_csv_row(self, view_name: str) -> str:
return SEPARATOR.join(self.to_row(view_name)) + "\n"
class OptimusResult:
TEXT_FONT_SIZE = 5
def __init__(self, cube_name: str, permutation_results: List[PermutationResult]):
self.cube_name = cube_name
self.permutation_results = permutation_results
self.best_result = self.determine_best_result()
if self.best_result:
for permutation_result in permutation_results:
if permutation_result.permutation_id == self.best_result.permutation_id:
permutation_result.is_best = True
def to_lines(self, view_name) -> List[str]:
lines = itertools.chain(
[self.permutation_results[0].build_csv_header()],
[result.to_csv_row(view_name) for result in self.permutation_results])
return list(lines)
def to_csv(self, view_name: str, file_name: 'WindowsPath'):
lines = self.to_lines(view_name)
os.makedirs(os.path.dirname(str(file_name)), exist_ok=True)
with open(str(file_name), "w") as file:
file.writelines(lines)
def to_xlsx(self, view_name: str, file_name: 'WindowsPath'):
try:
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook(file_name)
worksheet = workbook.add_worksheet()
# Iterate over the data and write it out row by row.
for row, line in enumerate(self.to_lines(view_name)):
for col, item in enumerate(line.split(SEPARATOR)):
worksheet.write(row, col, item)
workbook.close()
except ImportError:
logging.warning("Failed to import xlsxwriter. Writing to csv instead")
file_name = file_name.with_suffix(".csv")
return self.to_csv(view_name, file_name)
# create scatter plot ram vs. performance
def to_png(self, view_name: str, file_name: str):
from optimuspy import LABEL_MAP, COLOR_MAP
for result in self.permutation_results:
query_time_ratio = float(result.median_query_time(view_name)) / float(
self.original_order_result.median_query_time(view_name)) - 1
ram_in_gb = float(result.ram_usage) / (1024 ** 3)
plt.scatter(
query_time_ratio,
ram_in_gb,
color=COLOR_MAP.get(result.mode),
label=LABEL_MAP.get(result.mode),
marker="x" if result.is_best else ".")
plt.text(query_time_ratio, ram_in_gb, result.permutation_id, fontsize=self.TEXT_FONT_SIZE)
mean_query_time = statistics.mean(
[result.median_query_time(view_name)
for result
in self.permutation_results]) / float(self.original_order_result.median_query_time(view_name)) - 1
mean_ram = statistics.mean(
[result.ram_usage
for result
in self.permutation_results]) / (1024 ** 3)
plt.scatter(mean_query_time, mean_ram, color=COLOR_MAP.get("Mean"), label=LABEL_MAP.get("Mean"), marker=".")
plt.text(mean_query_time, mean_ram, "", fontsize=self.TEXT_FONT_SIZE)
# prettify axes
plt.xlabel('Query Time Compared to Original Order')
plt.ylabel('RAM in GB')
plt.gca().set_xticklabels(['{:.0f}%'.format(x * 100) for x in plt.gca().get_xticks()])
# plot legend
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
# add custom best order marker: x
by_label["Best"] = mlines.Line2D([], [], color=COLOR_MAP.get("Iterations"), marker='x', linestyle='None',
label='Best Order')
plt.legend(by_label.values(), by_label.keys())
plt.grid(True)
plt.savefig(file_name, dpi=400)
plt.clf()
@property
def original_order_result(self) -> PermutationResult:
from optimuspy import ExecutionMode
for result in self.permutation_results:
if result.mode == ExecutionMode.ORIGINAL_ORDER:
return result
def determine_best_result(self) -> Union[PermutationResult, None]:
ram_range = [result.ram_usage for result in self.permutation_results]
min_ram, max_ram = min(ram_range), max(ram_range)
query_speed_range = [result.median_query_time() for result in self.permutation_results]
min_query_speed, max_query_speed = min(query_speed_range), max(query_speed_range)
# find a good balance between speed and ram
for value in (0.01, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.2, 0.25):
ram_threshold = min_ram + value * (max_ram - min_ram)
query_speed_threshold = min_query_speed + value * (max_query_speed - min_query_speed)
for permutation_result in self.permutation_results:
if all([permutation_result.ram_usage <= ram_threshold,
permutation_result.median_query_time() <= query_speed_threshold]):
return permutation_result
# no dimension order falls in sweet spot
return None