|
| 1 | +from .utils import make_button, stage_register, VBoxTyped, TypedBase, amend_last_record, replay_next |
| 2 | +import ipywidgets as ipw |
| 3 | +import pandas as pd |
| 4 | +from progressivis.table.aggregate import Aggregate |
| 5 | +from progressivis.core import Sink, Module |
| 6 | + |
| 7 | +from typing import Any as AnyType, Optional, List, Tuple, Dict, Callable |
| 8 | + |
| 9 | +WidgetType = AnyType |
| 10 | + |
| 11 | + |
| 12 | +def get_flag_status(dt: str, op: str) -> bool: |
| 13 | + return dt in ("string", "datetime64") # op in type_op_mismatches.get(dt, set()) |
| 14 | + |
| 15 | + |
| 16 | +class AggregateW(VBoxTyped): |
| 17 | + class Typed(TypedBase): |
| 18 | + hidden_sel: ipw.SelectMultiple |
| 19 | + grid: ipw.GridBox |
| 20 | + freeze_ck: ipw.Checkbox |
| 21 | + start_btn: ipw.Button |
| 22 | + |
| 23 | + def init(self) -> None: |
| 24 | + self.hidden_cols: List[str] = [] |
| 25 | + fncs = ["hide"] + list(Aggregate.registry.keys()) |
| 26 | + self.all_functions = dict(zip(fncs, fncs)) |
| 27 | + self.child.hidden_sel = ipw.SelectMultiple( |
| 28 | + options=self.hidden_cols, |
| 29 | + value=[], |
| 30 | + rows=5, |
| 31 | + description="❎", |
| 32 | + disabled=False, |
| 33 | + ) |
| 34 | + self.child.hidden_sel.observe(self._selm_obs_cb, "value") |
| 35 | + self.visible_cols: List[str] = list(self.dtypes.keys()) |
| 36 | + self.obs_flag = False |
| 37 | + self.info_cbx: Dict[Tuple[str, str], ipw.Checkbox] = {} |
| 38 | + self.child.grid = self.draw_matrix() |
| 39 | + self.child.freeze_ck = ipw.Checkbox(description="Freeze") |
| 40 | + self.child.start_btn = make_button( |
| 41 | + "Activate", cb=self._start_btn_cb, disabled=True |
| 42 | + ) |
| 43 | + |
| 44 | + def init_aggregate(self, compute: AnyType) -> Aggregate: |
| 45 | + s = self.input_module.scheduler() |
| 46 | + with s: |
| 47 | + aggr = Aggregate(compute=compute, scheduler=s) |
| 48 | + aggr.input.table = self.input_module.output[self.input_slot] |
| 49 | + sink = Sink(scheduler=s) |
| 50 | + sink.input.inp = aggr.output.result |
| 51 | + return aggr |
| 52 | + |
| 53 | + def draw_matrix(self, ext_df: Optional[pd.DataFrame] = None) -> ipw.GridBox: |
| 54 | + lst: List[WidgetType] = [ipw.Label("")] + [ |
| 55 | + ipw.Label(s) for s in self.all_functions.values() |
| 56 | + ] |
| 57 | + width_ = len(lst) |
| 58 | + for col in sorted(self.visible_cols): |
| 59 | + col_type = self.dtypes[col] |
| 60 | + lst.append(ipw.Label(f"{col}:{col_type}")) |
| 61 | + for k in self.all_functions.keys(): |
| 62 | + lst.append(self._info_checkbox(col, k, get_flag_status(col_type, k))) |
| 63 | + gb = ipw.GridBox( |
| 64 | + lst, |
| 65 | + layout=ipw.Layout(grid_template_columns=f"200px repeat({width_-1}, 70px)"), |
| 66 | + ) |
| 67 | + return gb |
| 68 | + |
| 69 | + def _info_checkbox(self, col: str, func: str, dis: bool) -> ipw.Checkbox: |
| 70 | + wgt = ipw.Checkbox(value=False, description="", disabled=dis, indent=False) |
| 71 | + self.info_cbx[(col, func)] = wgt |
| 72 | + wgt.observe(self._make_cbx_obs(col, func), "value") |
| 73 | + return wgt |
| 74 | + |
| 75 | + def _start_btn_cb(self, btn: ipw.Button) -> None: |
| 76 | + compute = [ |
| 77 | + (col, fnc) |
| 78 | + for ((col, fnc), ck) in self.info_cbx.items() |
| 79 | + if fnc != "hide" and ck.value |
| 80 | + ] |
| 81 | + if self.child.freeze_ck.value: |
| 82 | + amend_last_record({'frozen': dict(compute=compute)}) |
| 83 | + self.output_module = self.init_aggregate(compute) |
| 84 | + self.output_slot = "result" |
| 85 | + btn.disabled = True |
| 86 | + self.make_chaining_box() |
| 87 | + self.dag_running() |
| 88 | + |
| 89 | + def run(self) -> None: |
| 90 | + content = self.frozen_kw |
| 91 | + self.output_module = self.init_aggregate(**content) |
| 92 | + self.output_slot = "result" |
| 93 | + self.dag_running() |
| 94 | + replay_next(self.carrier) |
| 95 | + |
| 96 | + def _selm_obs_cb(self, change: AnyType) -> None: |
| 97 | + self.obs_flag = True |
| 98 | + cols = change["new"] |
| 99 | + for col in cols: |
| 100 | + self.hidden_cols.remove(col) |
| 101 | + self.visible_cols.append(col) |
| 102 | + self.child.hidden_sel.options = sorted(self.hidden_cols) |
| 103 | + self.child.grid = self.draw_matrix() |
| 104 | + |
| 105 | + def _make_cbx_obs(self, col: str, func: str) -> Callable[[AnyType], None]: |
| 106 | + def _cbk(change: AnyType) -> None: |
| 107 | + if func == "hide": |
| 108 | + self.child.start_btn.disabled = True |
| 109 | + self.hidden_cols.append(col) |
| 110 | + self.visible_cols.remove(col) |
| 111 | + self.child.hidden_sel.options = sorted(self.hidden_cols) |
| 112 | + self.child.grid = self.draw_matrix() |
| 113 | + else: |
| 114 | + self.child.start_btn.disabled = False |
| 115 | + |
| 116 | + return _cbk |
| 117 | + |
| 118 | + def get_underlying_modules(self) -> List[Module]: |
| 119 | + return [self.output_module] |
| 120 | + |
| 121 | + |
| 122 | +stage_register["Aggregate"] = AggregateW |
0 commit comments