Skip to content

Commit

Permalink
replay mode reimplemented
Browse files Browse the repository at this point in the history
  • Loading branch information
xtianpoli committed Oct 23, 2024
1 parent 4b3396d commit ee074f1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: flake8
args: [--max-line-length=100]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.1.1
rev: v1.12.0
hooks:
- id: mypy
args: [--strict, --explicit-package-bases]
Expand Down
9 changes: 6 additions & 3 deletions ipyprogressivis/widgets/chaining/code_cell.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from .utils import VBoxTyped, TypedBase, stage_register
from .utils import (VBoxTyped, TypedBase, stage_register,
make_replay_next_btn, is_replay_only)

import ipywidgets as ipw


class CodeCellW(VBoxTyped):
class Typed(TypedBase):
dongle: ipw.Label
dongle: ipw.Label | ipw.Button

def initialize(self) -> None:
self.c_.dongle = ipw.Label("Chaining ...")
self.c_.dongle = (make_replay_next_btn() if is_replay_only()
else ipw.Label("Chaining ..."))


stage_register["Python"] = CodeCellW
2 changes: 2 additions & 0 deletions ipyprogressivis/widgets/chaining/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
TypedBase,
get_recording_state,
amend_last_record,
runner
)
import ipywidgets as ipw
import numpy as np
Expand Down Expand Up @@ -334,6 +335,7 @@ def _btn_apply_cb(self, btn: AnyType) -> None:
self.make_chaining_box()
self.dag_running()

@runner
def run(self) -> None:
content = self.frozen_kw
self.output_module = self.init_module(**content)
Expand Down
7 changes: 4 additions & 3 deletions ipyprogressivis/widgets/chaining/custom_loader.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .utils import VBoxTyped, TypedBase
from .utils import VBoxTyped, TypedBase, make_replay_next_btn, is_replay_only
import ipywidgets as ipw


class CustomLoaderW(VBoxTyped):
class Typed(TypedBase):
dongle: ipw.Label
dongle: ipw.Label | ipw.Button

def initialize(self) -> None:
self.c_.dongle = ipw.Label("Custom loader")
self.c_.dongle = (make_replay_next_btn() if is_replay_only()
else ipw.Label("Custom loader"))
69 changes: 41 additions & 28 deletions ipyprogressivis/widgets/chaining/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
assert HOME is not None

replay_list: List[Dict[str, AnyType]] = []
# chaining_boxes_to_make = []


def runner(func: Callable[..., AnyType]) -> Callable[..., AnyType]:
Expand Down Expand Up @@ -84,7 +83,6 @@ def _delete_cb(b: Any) -> "NodeCarrier":
PARAMS["deleted_stages"].add(
(self_.carrier.label, self_.carrier.number)
)
print("deleted stages", PARAMS["deleted_stages"])
assert self_.carrier.parent is not None
parent_title = self_.carrier.parent.title
title = self_.carrier.title
Expand Down Expand Up @@ -123,6 +121,7 @@ class Header:
class Proxy:
def __init__(self, carrier: "NodeCarrier") -> None:
self.__carrier = carrier
self.guest = carrier.children[0]
self.output_module: ModuleOrFacade | None = None
self.output_slot: str = "result"
self.output_dtypes: dict[str, str] | None = None
Expand Down Expand Up @@ -156,16 +155,20 @@ def scheduler(self) -> Scheduler:
return self.input_module.scheduler()

def resume(self) -> "NodeCarrier":
global parent_widget
self.__carrier._output_module = self.output_module # type: ignore
self.__carrier._output_slot = self.output_slot
self.__carrier._output_dtypes = self.output_dtypes
if self.freeze and not is_replay():
amend_last_record({"frozen": dict(cell=self.cell_content)})
if self.freeze:
frozen = {"frozen": dict(cell=self.cell_content)}
self.guest.frozen_kw = frozen # type: ignore
amend_last_record(frozen)
self.__carrier.dag_running()
if is_replay():
replay_next()
return self.__carrier
self.__carrier.make_chaining_box()
if PARAMS["replay_before_resume"] or not is_replay():
self.__carrier.make_chaining_box()
else:
self.__carrier.make_progress_bar()
parent_widget = self.__carrier
return self.__carrier


Expand Down Expand Up @@ -290,17 +293,18 @@ def restore_recorder() -> None:
def replay_next(obj: Optional[Union["Constructor", "NodeVBox"]] = None) -> None:
if not is_replay():
return
if not replay_list:
return
assert replay_list
stage = replay_list.pop(0)
print("STAGE:", stage)
parent = stage.get("parent", None)
if (
parent is not None and tuple(parent) in PARAMS["deleted_stages"]
): # skipping deleted
return replay_next(obj)
return replay_next()
if "deleted" in stage:
PARAMS["deleted_stages"].add((stage["title"], stage["number"]))
return replay_next(obj)
return replay_next()
if obj is None and stage and "ftype" not in stage: # not a loader => has a parent
assert parent is not None
t0, t1 = parent
Expand Down Expand Up @@ -397,9 +401,7 @@ def remove_tab(self, title: str) -> None:

def get_selected_title(self) -> Optional[str]:
if self.selected_index is None or self.selected_index >= len(self.titles):
# logger.warning("no selected title")
return None
# logger.warning(f"selected title {self.selected_index} {self.titles}")
return self.get_title(self.selected_index)

def get_selected_child(self) -> ipw.DOMWidget:
Expand Down Expand Up @@ -439,8 +441,6 @@ def _ds(col: str, dt: str) -> str:
assert sniffer._df is not None
norm_cols = dict(zip(sniffer._df.columns, normalize_columns(sniffer._df.columns)))
dtypes = {col: _ds(col, dt) for (col, dt) in sniffer._df.dtypes.to_dict().items()}
print("norm_cols", norm_cols)
print("dtypes", dtypes, list(sniffer._df.dtypes.to_dict().items()))
if usecols is not None:
dtypes = {norm_cols[col]: dtypes[col] for col in usecols}
else:
Expand Down Expand Up @@ -475,6 +475,16 @@ def make_button(
return btn


def make_replay_next_btn() -> ipw.Button:
def _fnc(btn: ipw.Button) -> None:
replay_next()
btn.disabled = True

return make_button(
"Next", cb=_fnc, disabled=False
)


stage_register: Dict[str, AnyType] = {}
parent_widget: Optional["NodeCarrier"] = None
parent_dtypes: Optional[Dict[str, str]] = None
Expand Down Expand Up @@ -610,7 +620,6 @@ def replay_start_loader(
) -> None:
global parent_widget
parent_widget = obj
print("kw stage", kw)
assert parent_widget
add_new_loader(obj, ftype, alias, frozen=frozen, number=number)

Expand All @@ -624,8 +633,6 @@ def replay_new_stage(
) -> None:
class _FakeSel:
value: str

print("kw stage", kw)
sel = _FakeSel()
sel.value = title
global parent_widget
Expand Down Expand Up @@ -732,7 +739,8 @@ def _progress_bar(self) -> ipw.FloatProgress:
description="Progress", min=0.0, max=1.0, layout={"width": "100%"}
)
mod_ = self._output_module
assert isinstance(mod_, Module)
if not isinstance(mod_, Module):
mod_ = mod_.module # i.e. mod_ is a Facade

def _proc(m: Module, r: int) -> None:
n, d = m.get_progress()
Expand Down Expand Up @@ -847,8 +855,9 @@ def get_previous(obj: "ChainingWidget") -> "ChainingWidget":
return get_previous(obj.subwidgets[-1])


new_stage_cell_0 = "Constructor.widget('{key}'){end}"
new_stage_cell_0 = "Constructor.widget('{key}'){end}\n"
new_stage_cell = "Constructor.widget('{key}', {num}){end}"

new_stage_cell_code = (
"%%pv_run_cell -p {key},{num}\n"
"# The 'proxy' name is present in this context"
Expand Down Expand Up @@ -899,6 +908,10 @@ def is_replay() -> bool:
return cast(bool, PARAMS.get("is_replay", False))


def is_replay_only() -> bool:
return is_replay() and not PARAMS["step_by_step"]


def get_stage_cell(
key: str, num: int, end: str, frozen: AnyType = None
) -> tuple[str, bool, bool]:
Expand Down Expand Up @@ -926,7 +939,6 @@ def add_new_stage(
title: str,
frozen: AnyType = None,
number: int | None = None,
no_record: bool = False,
) -> None:
stage = create_stage_widget(title, frozen, number=number)
parent_key = key_by_id[id(parent)]
Expand All @@ -940,8 +952,7 @@ def add_new_stage(
labcommand(
"progressivis:create_stage_cells", tag=tag, md=md, code=code, rw=rw, run=run
)
if not no_record:
add_to_record(dict(title=title, parent=parent_key, number=stage.number))
add_to_record(dict(title=title, parent=parent_key, number=stage.number))


def add_new_loader(
Expand Down Expand Up @@ -1140,6 +1151,9 @@ def compute_dtypes_then_call(
args: Iterable[Any] = (),
kw: Dict[str, Any] = {},
) -> None:
if is_replay_only():
self.output_dtypes = {}
return
s = self.output_module.scheduler()
with s:
ds = DataShape(scheduler=s)
Expand Down Expand Up @@ -1171,23 +1185,22 @@ def widget_dir(self) -> str:

def post_run(self) -> "NodeCarrier":
self.dag_running()
self.carrier.children = (ipw.Label("..."),)
self.carrier.children = (make_replay_next_btn()
if is_replay_only() else ipw.Label("..."),)
if PARAMS["replay_before_resume"]:
self.make_chaining_box()
# chaining_boxes_to_make.append(self)
else:
self.carrier.make_progress_bar()
replay_next()
return self.carrier

def post_delete(self) -> "NodeCarrier":
self.carrier.children = (ipw.Label("deleted"),)
replay_next()
return self.carrier

def manage_replay(self) -> None:
if self._do_replay_next:
replay_next()
pass
# replay_next() ?


class VBox(ipw.VBox, GuestWidget):
Expand Down

0 comments on commit ee074f1

Please sign in to comment.