-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate_table.py
More file actions
358 lines (296 loc) · 15.9 KB
/
aggregate_table.py
File metadata and controls
358 lines (296 loc) · 15.9 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from bokeh.models.widgets.tables import HTMLTemplateFormatter
import logging
import param
import pandas as pd
import panel as pn
from scipy import stats
from custom_tabulator import CustomTabulator
from problem_table import ProblemTable
from report import Report
logger = logging.getLogger("visualizer.aggregate_table")
# TODO: current problems
# 1) style still causes a lot of jumping around
# 2) when forcing a redraw by triggering the data_view value, the table might jump to the top (e.g. when changing min_wins)
# 3) TODOs in code (queued and precedence on watchers / manual triggering attributes & domains in updated exp_data)
class AggregateTable(Report):
stylesheet = """
.tabulator-row.tabulator-selected .tabulator-cell{
background-color: #9abcea !important;
}
.tabulator-row:hover {
background-color: #bbbbbb !important;
}
.tabulator .tabulator-row.tabulator-selectable:hover .tabulator-cell{
background-color: #769bcc !important;
}
"""
# widget parameters
algorithms = param.ListSelector(default=[])
attributes = param.ListSelector(default=[])
domains = param.ListSelector(default=[])
precision = param.Integer(default=3, bounds=(0,15), label="Floating point precision")
# internal parameters
# stores which attributes are unfolded (the keys, and for each
# unfolded attribute which domains are unfolded (the values)
unfolded = param.Dict(precedence=-1, default={})
# for these attributes, their overall aggregates are current
aggregated_attributes = param.List(default=[]) # TODO: Ideally we would have a set...
# these domain aggregates (=value, set of domains) of attribute key are current
aggregated_attribute_domains = param.Dict(default={})
recompute_aggregate_needed = param.Event()
def __init__(self, experiment_data, **params):
super().__init__(experiment_data, **params)
# ajaxLoader false is set to reduce blinking (https://github.com/olifolkerd/tabulator/issues/1027)
self.data_view = CustomTabulator(
value=pd.DataFrame(), disabled=True, show_index=False,
pagination="remote", page_size=10000, frozen_columns=['Index'],
sizing_mode='stretch_both', configuration={"ajaxLoader": "False"},
sortable=False, stylesheets=[AggregateTable.stylesheet]
)
self.used_aggregators = dict()
def filter(df, unfolded, attributes, domains):
if df.empty:
return df
indices = [(a, "--", "--") for a in attributes]
for a, doms in unfolded.items():
if a not in attributes:
continue
indices += [(a, d, "--") for d in domains]
for d in doms:
if d not in domains:
continue
indices += [(a, d, p) for p in
self.experiment_data.problems_by_domain[d]]
indices.sort()
return df.loc[indices]
self.data_view.add_filter(pn.bind(
filter, unfolded=self.param.unfolded, attributes=self.param.attributes,
domains=self.param.domains
))
self.data_view.on_click(self.on_click_callback)
self.param_view.extend([
pn.pane.HTML("<label>Attributes</label>", margin=(5, 0, -5, 0)),
pn.widgets.CrossSelector.from_param(
self.param.attributes,
name="",
options = self.experiment_data.param.attributes,
margin=(5, 0, 5, 0),
width=400
# TODO: can we have a min_width with stretching? (Could not get it to work so far)
),
pn.pane.HTML("<label>Domains</label>", margin=(5, 0, -5, 0)),
pn.widgets.CrossSelector.from_param(
self.param.domains,
name="",
options = self.experiment_data.param.domains,
margin=(5, 0, 5, 0),
width=400
# TODO: can we have a min_width with stretching? (Could not get it to work so far)
),
pn.widgets.IntSlider.from_param(
self.param.precision,
margin=(5, 0, 5, 0),
min_width=100,
sizing_mode="stretch_width"
),
])
# TODO: rethink if the queued and precendence is really how we want it
self.param.watch(self.algorithms_updated, ["algorithms"], precedence=1)
self.param.watch(self.attributes_updated, ["attributes"], precedence=2)
self.param.watch(self.domains_updated, ["domains"], precedence=3)
self.experiment_data.param.watch(self.aggregators_changed, ["custom_aggregators"], precedence=4)
self.param.watch(self.compute_needed_aggregates, ["recompute_aggregate_needed"], precedence=5)
self.experiment_data.param.watch(self.algorithm_aliases_changed, ["custom_algorithm_aliases"])
self.experiment_data.param.watch(self.min_wins_changed, ["custom_min_wins"])
def style_table_by_row(self, row):
# Give aggregates a different style, and indent Index col text if it's a domain or problem.
style = [""] * len(row)
if row.name[1] == "--":
style = [x + "font-weight: bold; background-color: #E6E6E6;" for x in style]
elif row.name[2] == "--":
style = [x + "font-weight: bold; background-color: #F6F6F6;" for x in style]
style[0] = style[0] + "text-indent:25px;"
else:
style[0] = "text-indent:50px;"
return style
def on_click_callback(self, e):
row = self.data_view.value.iloc[e.row]
attribute, domain, problem = row.name[0:3]
# clicked on concrete problem -> open problem wise report
if problem != "--":
problem_report = ProblemTable(
self.experiment_data, sizing_mode="stretch_width",
domain=domain, problem=problem, algorithms=self.algorithms)
self.add_popup(problem_report, name=f"{domain} - {problem}")
return
if domain != "--": # clicked on domain aggregate
if domain in self.unfolded[attribute]:
self.unfolded[attribute].remove(domain)
else:
self.unfolded[attribute].append(domain)
else: # clicked on attribute aggregate
if attribute in self.unfolded:
self.unfolded.pop(attribute)
else:
self.unfolded[attribute] = []
self.param.trigger("recompute_aggregate_needed")
def update_data_view_table(self, patch_df):
raise NotImplementedError
def compute_needed_aggregates(self, *events):
logger.debug("computing needed aggregates")
if not isinstance(self.experiment_data.data, pd.DataFrame):
return
mi = pd.MultiIndex.from_tuples([], names=self.data_view.value.index.names)
cols = {"Index": pd.Series(dtype='string')} | {a.name: pd.Series(dtype='object') for a in self.algorithms}
patch_df = pd.DataFrame(cols, index = mi)
def update_patch_dict(df, row, index_string, aggregator):
# Since gmean is not a built-in function we need to set the variable
# to the actual function here. Furthermore, since gmean cannot deal
# with 0, we replace it with a very small positive value.
if aggregator == "gmean":
aggregator = stats.gmean
df = df.replace(0, 0.000001)
res = df.agg(aggregator)
res["Index"] = index_string % str(len(df))
patch_df.loc[row] = res
def get_rows_with_index_value(df, value):
return (df.loc[value] if value in df.index
else pd.DataFrame({}, columns=df.columns))
# we want to consider all rows where all selected columns have a value
base_data = self.experiment_data.data[[a.name for a in self.algorithms]].dropna()
for attribute in self.experiment_data.numeric_attributes.values():
if attribute.name not in self.attributes:
# If the attribute is currently not shown, don't compute anything
continue
attribute_data = get_rows_with_index_value(base_data, attribute.name)
if len(attribute_data) > 0:
# remove domains that are currently not considered
attribute_data = attribute_data.loc[attribute_data.index.get_level_values('domain').isin(self.domains)]
attribute_data = attribute_data.apply(pd.to_numeric, errors='coerce')
# Compute the overall attribute aggregate if it is not current.
if attribute.name not in self.aggregated_attributes:
row_index = (attribute.name, "--", "--")
num_probs = sum([len(self.experiment_data.problems_by_domain[d]) for d in self.domains])
index_string = f"{attribute.name} ({attribute.aggregator}, %s/{num_probs})"
update_patch_dict(
attribute_data, row_index, index_string, attribute.aggregator)
self.aggregated_attributes.append(attribute.name)
# For unfolded attributes we might need to update domain aggregates.
if attribute.name in self.unfolded:
relevant_domains = [d for d in self.domains if d not in self.aggregated_attribute_domains[attribute.name]]
for domain in relevant_domains:
domain_data = get_rows_with_index_value(attribute_data, domain)
row_index = (attribute.name, domain, "--")
index_string = f"{domain} (%s/{len(self.experiment_data.problems_by_domain[domain])})"
update_patch_dict(
domain_data, row_index, index_string, attribute.aggregator)
self.aggregated_attribute_domains[attribute.name].update(relevant_domains)
self.update_data_view_table(patch_df)
def get_algorithms_on_new_experiment_data(self):
raise NotImplementedError("get_algorithms_on_new_experiment_data not implemented")
@param.depends("experiment_data.data", watch=True)
def experiment_data_updated(self):
logger.debug("experiment data was updated")
if not hasattr(self, "data_view") or isinstance(self.data_view, pn.pane.Str):
return
self.param.algorithms.default = list(self.experiment_data.algorithms.values())
self.param.attributes.default = self.experiment_data.attributes
self.param.domains.default = self.experiment_data.domains
# Build the rows for the aggregated values such that we later just overwrite values rather than concatenate.
mi = pd.MultiIndex.from_product([self.experiment_data.attributes, ["--", *self.experiment_data.domains], ["--"]],
names = ["attribute", "domain", "problem"])
aggregated_data_skeleton = pd.DataFrame(data = "", index = mi, columns = [])
# Combine experiment data and aggregated data skeleton indices into an empty dataframe.
new_df = pd.concat([self.experiment_data.data[[]], aggregated_data_skeleton]).sort_index()
# Add Index column (solely used in the visualization).
pseudoindex = [x[0] if x[1]=="--" else (x[1] if x[2] == "--" else x[2]) for x in new_df.index]
new_df.insert(0, "Index", pseudoindex)
self.data_view.value = new_df
self.used_aggregators = { a.name: a.aggregator for a in self.experiment_data.numeric_attributes.values()}
self.param.update({
"algorithms": self.get_algorithms_on_new_experiment_data(),
"attributes": self.param.attributes.default,
"domains": self.param.domains.default,
"unfolded": {}
})
# TODO: we need to trigger attributes and domains because otherwise the widgets shows them as none selected - why?
self.param.trigger("attributes")
self.param.trigger("domains")
def algorithms_updated(self, event):
logger.debug("algorithms were updated")
self.param.update({
"aggregated_attributes": [],
"aggregated_attribute_domains": {a.name: set() for a in self.experiment_data.numeric_attributes.values()},
"recompute_aggregate_needed" : True
})
index_width = 17 + max([len(x) for x in self.data_view.value['Index']])*7
col_width = 17 + max([len(x) for x in self.data_view.value.columns[1:]] + [20])*7
widths_dict = {'Index': index_width} | {x : col_width for x in self.data_view.value.columns[1:]}
self.data_view.widths = widths_dict
def attributes_updated(self, event):
logger.debug("attributes were updated")
# Updating attributes does not invalidate aggregates, but we might need
# to (re-)compute new ones.
self.param.trigger("recompute_aggregate_needed")
def domains_updated(self, event):
logger.debug("domains were updated")
self.aggregated_attributes = []
self.param.trigger("recompute_aggregate_needed")
def aggregators_changed(self, event):
logger.debug("custom aggregators were updated")
for attribute in self.experiment_data.numeric_attributes.values():
if self.used_aggregators.get(attribute.name) != attribute.aggregator:
self.used_aggregators[attribute.name] = attribute.aggregator
self.aggregated_attributes.remove(attribute.name)
self.aggregated_attribute_domains[attribute.name] = set()
self.param.trigger("recompute_aggregate_needed")
def algorithm_aliases_changed(self, event):
logger.debug("custom algorithm aliases changed")
self.data_view.value.columns= ["Index"] + [x.get_name() for x in self.algorithms]
self.data_view.param.trigger("value")
def min_wins_changed(self, event):
logger.debug("custom min wins changed")
self.data_view.param.trigger("value")
@param.depends("precision", "algorithms", "experiment_data.custom_aggregators", watch=True)
def set_formatter_for_precision(self):
logger.debug("setting formatter for precision")
template = f"""
<%= function formatnumber() {{
f_val = parseFloat(value);
if (!isNaN(f_val)) {{
if (Number.isInteger(f_val)) {{
return '<div style="text-align:right">' + f_val + "</div>";
}} else {{
return '<div style="text-align:right">' + f_val.toFixed({self.precision}) + "</div>";
}}
}} else {{
return value;
}}
}}() %>
"""
if hasattr(self, "data_view"):
self.data_view.formatters = {x : HTMLTemplateFormatter(template=template) for x in self.data_view.value.columns}
def get_watchers_for_param_config(self):
return [
"attributes",
"domains",
"precision"
]
def get_param_config_dict(self):
d = {}
if set(self.attributes) != set(self.param.attributes.default):
d["attrs"] = [self.experiment_data.get_attribute_position(a) for a in self.attributes]
if set(self.domains) != set(self.param.domains.default):
d["doms"] = [self.experiment_data.get_domain_id(d) for d in self.domains]
if self.precision != self.param.precision.default:
d["prec"] = self.precision
return d
def set_params_from_param_config_dict(self, param_config_dict):
update = {}
if "attrs" in param_config_dict:
update["attributes"] = [self.experiment_data.get_attribute_by_position(id) for id in param_config_dict["attrs"]]
if "doms" in param_config_dict:
update["domains"] = [self.experiment_data.get_domain_by_id(id) for id in param_config_dict["doms"]]
if "prec" in param_config_dict:
update["precision"] = param_config_dict["prec"]
self.param.update(update)