-
Notifications
You must be signed in to change notification settings - Fork 341
Add ipycytoscape plotting for InterDependencies_ objects #7305
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Adds InterDependencies visualisation capability via ipywidgets and ipycytoscape |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """ | ||
| Utils for visualizing InterDependencies_. Note that these only work in | ||
| Jupyter and require you to have ipycytoscape installed. This can be | ||
| installed with ``pip install qcodes[live_plotting]`` | ||
| """ | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import ipycytoscape | ||
|
Check failure on line 9 in src/qcodes/dataset/descriptions/dependencies_viz.py
|
||
| import ipywidgets | ||
|
Check failure on line 10 in src/qcodes/dataset/descriptions/dependencies_viz.py
|
||
| import networkx as nx | ||
|
|
||
| if TYPE_CHECKING: | ||
| from qcodes.dataset.descriptions.dependencies import InterDependencies_ | ||
|
|
||
| INTERDEPENDENCIES_STYLE = [ | ||
| { | ||
| "selector": "node", | ||
| "css": { | ||
| "content": "data(id)", | ||
| "text-valign": "center", | ||
| "font-weight": 10, | ||
| "color": "white", | ||
| "text-outline-width": 1, | ||
| "text-outline-color": "#11479e", | ||
| "background-color": "#8aa8d8", | ||
| "border-color": "#11479e", | ||
| "border-width": 1.5, | ||
| "border-style": "solid", | ||
| }, | ||
| }, | ||
| { | ||
| "selector": "node[node_type='dependency']", | ||
| "css": { | ||
| "text-outline-color": "#089222", | ||
| "background-color": "#90C79A", | ||
| "border-color": "#089222", | ||
| }, | ||
| }, | ||
| { | ||
| "selector": "node[node_type='inference']", | ||
| "css": { | ||
| "text-outline-color": "#920808", | ||
| "background-color": "#D37B7B", | ||
| "border-color": "#920808", | ||
| }, | ||
| }, | ||
| { | ||
| "selector": "edge", | ||
| "style": { | ||
| "width": 4, | ||
| "curve-style": "bezier", | ||
| "target-arrow-shape": "triangle", | ||
| }, | ||
| }, | ||
| { | ||
| "selector": "edge[interdep_type = 'depends_on'], ", | ||
| "style": { | ||
| "line-color": "#089222", | ||
| "target-arrow-color": "#089222", | ||
| }, | ||
| }, | ||
| { | ||
| "selector": "edge[interdep_type = 'inferred_from']", | ||
| "style": { | ||
| "line-color": "#920808", | ||
| "target-arrow-color": "#920808", | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def draw_interdepenencies(interdeps: "InterDependencies_") -> ipywidgets.HBox: | ||
| graphwidget = ipycytoscape.CytoscapeWidget() | ||
| graphwidget.graph.add_graph_from_json(interdeps_to_ipycytoscape_json(interdeps)) | ||
| graphwidget.set_style(INTERDEPENDENCIES_STYLE) | ||
| graphwidget.user_zooming_enabled = True | ||
| graphwidget.min_zoom = 0.2 | ||
| graphwidget.max_zoom = 5 | ||
| graphwidget.set_layout(name="cola", animate=False, avoidOverlap=True) | ||
| graphwidget.wheel_sensitivity = 0.1 | ||
| return ipywidgets.HBox([graphwidget]) | ||
|
|
||
|
|
||
| def interdeps_to_ipycytoscape_json( | ||
| interdeps: "InterDependencies_", | ||
| ) -> dict[str, list[dict[str, Any]]]: | ||
| graph_json: dict[str, list[dict[str, Any]]] = nx.cytoscape_data(interdeps.graph)[ | ||
| "elements" | ||
| ] | ||
| interdeps_dict = interdeps._to_dict() | ||
| dependencies = list(interdeps_dict["dependencies"].keys()) | ||
| inferences = list(interdeps_dict["inferences"].keys()) | ||
|
|
||
| for node_dict in graph_json["nodes"]: | ||
| if node_dict["data"]["id"] in dependencies: | ||
| node_dict["data"]["node_type"] = "dependency" | ||
| elif node_dict["data"]["id"] in inferences: | ||
| node_dict["data"]["node_type"] = "inference" | ||
| return graph_json | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should call this something like
jupyerand then move the dependency on ipykernel and ipywidgets down here. The latter should probably not be done in this pr. That way we can give users the option to pull in the juypyter deps if they want to