Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualizations as SVG with links and tooltips in jupyter notebook >=7. #1373

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ jobs:
- name: Fix tutorial dependencies
if: ${{ matrix.dir == 'examples/kdd22' }}
run: |
pip install "pandas<2.0.0" "xgboost<=1.5.1"
pip install "numpy<2.0.0" "pandas<2.0.0" "xgboost<=1.5.1"
- name: pip list packages
run: pip list
- name: show pip dependencies
Expand Down
3 changes: 1 addition & 2 deletions lale/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ def to_graphviz(
lale_operator: "lale.operators.Operator",
ipython_display: bool = True,
call_depth: int = 1,
**dot_graph_attr,
):
import lale.json_operator
import lale.operators
Expand All @@ -403,7 +402,7 @@ def to_graphviz(
if not isinstance(lale_operator, lale.operators.Operator):
raise TypeError("The input to to_graphviz needs to be a valid LALE operator.")
jsn = lale.json_operator.to_json(lale_operator, call_depth=call_depth + 1)
dot = lale.visualize.json_to_graphviz(jsn, ipython_display, dot_graph_attr)
dot = lale.visualize.json_to_graphviz(jsn, ipython_display)
return dot


Expand Down
24 changes: 18 additions & 6 deletions lale/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _indiv_op_tooltip(uid, jsn) -> str:
return tooltip


def _json_to_graphviz_rec(uid, jsn, cluster2reps, is_root, dot_graph_attr):
def _json_to_graphviz_rec(uid, jsn, cluster2reps, is_root):
kind = lale.json_operator.json_op_kind(jsn)
dot: graphviz.Digraph
if kind in ["Pipeline", "OperatorChoice"] or "steps" in jsn:
Expand All @@ -135,7 +135,9 @@ def _json_to_graphviz_rec(uid, jsn, cluster2reps, is_root, dot_graph_attr):
if is_root:
dot.attr(
"graph",
{**dot_graph_attr, "rankdir": "LR", "compound": "true", "nodesep": "0.1"},
rankdir="LR",
compound="true",
nodesep="0.1",
)
dot.attr("node", fontsize="11", margin="0.06,0.03")
if kind == "Pipeline":
Expand Down Expand Up @@ -183,7 +185,7 @@ def _json_to_graphviz_rec(uid, jsn, cluster2reps, is_root, dot_graph_attr):
for step_uid, step_jsn in nodes.items():
node_kind = lale.json_operator.json_op_kind(step_jsn)
if node_kind in ["Pipeline", "OperatorChoice"] or "steps" in step_jsn:
sub_dot = _json_to_graphviz_rec(step_uid, step_jsn, cluster2reps, False, {})
sub_dot = _json_to_graphviz_rec(step_uid, step_jsn, cluster2reps, False)
dot.subgraph(sub_dot)
else:
assert node_kind == "IndividualOp"
Expand Down Expand Up @@ -226,12 +228,22 @@ def _json_to_graphviz_rec(uid, jsn, cluster2reps, is_root, dot_graph_attr):
return dot


def json_to_graphviz(jsn, ipython_display, dot_graph_attr):
class _HTML4Jupyter:
def __init__(self, html):
self.html = html

def _repr_html_(self):
return self.html


def json_to_graphviz(jsn, ipython_display):
cluster2reps = _get_cluster2reps(jsn)
dot = _json_to_graphviz_rec("(root)", jsn, cluster2reps, True, dot_graph_attr)
dot = _json_to_graphviz_rec("(root)", jsn, cluster2reps, True)
if ipython_display:
import IPython.display

IPython.display.display(dot)
svg = dot.pipe(format="svg", encoding="utf-8")
for_jupyter = _HTML4Jupyter(svg)
IPython.display.display(for_jupyter)
return None
return dot
Loading