-
Notifications
You must be signed in to change notification settings - Fork 135
Add function to mermaid diagram #1490
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
Open
williambdean
wants to merge
5
commits into
pymc-devs:main
Choose a base branch
from
williambdean:mermaid-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
31acc6f
add function_to_mermaid
williambdean 09ecb2d
add space before edges section
williambdean df5a4c9
add test for diagram
williambdean b0f7ce2
install pydot for tests
williambdean 8004aef
take scalar op name if Elemwise
williambdean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from pytensor.d3viz.formatting import PyDotFormatter | ||
|
||
|
||
def function_to_mermaid(fn): | ||
formatter = PyDotFormatter() | ||
dot = formatter(fn) | ||
|
||
nodes = dot.get_nodes() | ||
edges = dot.get_edges() | ||
|
||
mermaid_lines = ["graph TD"] | ||
mermaid_lines.append("%% Nodes:") | ||
for node in nodes: | ||
name = node.get_name() | ||
label = node.get_label() | ||
shape = node.get_shape() | ||
|
||
if label.endswith("."): | ||
label = f"{label}0" | ||
|
||
if shape == "box": | ||
shape = "rect" | ||
else: | ||
shape = "rounded" | ||
|
||
mermaid_lines.extend( | ||
[ | ||
f'{name}["{label}"]', | ||
f"{name}@{{ shape: {shape} }}", | ||
] | ||
) | ||
|
||
fillcolor = node.get_fillcolor() | ||
if fillcolor is not None and not fillcolor.startswith("#"): | ||
fillcolor = _color_to_hex(fillcolor) | ||
mermaid_lines.append(f"style {name} fill:{fillcolor}") | ||
|
||
mermaid_lines.append("\n%% Edges:") | ||
for edge in edges: | ||
source = edge.get_source() | ||
target = edge.get_destination() | ||
|
||
mermaid_lines.append(f"{source} --> {target}") | ||
|
||
return "\n".join(mermaid_lines) | ||
|
||
|
||
def _color_to_hex(color_name): | ||
"""Based on the colors in d3viz module.""" | ||
return { | ||
"limegreen": "#32CD32", | ||
"SpringGreen": "#00FF7F", | ||
"YellowGreen": "#9ACD32", | ||
"dodgerblue": "#1E90FF", | ||
"lightgrey": "#D3D3D3", | ||
"yellow": "#FFFF00", | ||
"cyan": "#00FFFF", | ||
"magenta": "#FF00FF", | ||
"red": "#FF0000", | ||
"blue": "#0000FF", | ||
"green": "#008000", | ||
"grey": "#808080", | ||
}.get(color_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
from pytensor import function | ||
from pytensor import tensor as pt | ||
from pytensor.mermaid import function_to_mermaid | ||
|
||
|
||
@pytest.fixture | ||
def sample_function(): | ||
x = pt.dmatrix("x") | ||
y = pt.dvector("y") | ||
z = pt.dot(x, y) | ||
z.name = "z" | ||
return function([x, y], z) | ||
|
||
|
||
def test_function_to_mermaid(sample_function): | ||
diagram = function_to_mermaid(sample_function) | ||
|
||
assert ( | ||
diagram | ||
== dedent(""" | ||
graph TD | ||
%% Nodes: | ||
n1["Shape_i"] | ||
n1@{ shape: rounded } | ||
style n1 fill:#00FFFF | ||
n2["x"] | ||
n2@{ shape: rect } | ||
style n2 fill:#32CD32 | ||
n2["x"] | ||
n2@{ shape: rect } | ||
style n2 fill:#32CD32 | ||
n4["AllocEmpty"] | ||
n4@{ shape: rounded } | ||
n6["CGemv"] | ||
n6@{ shape: rounded } | ||
n7["1.0"] | ||
n7@{ shape: rect } | ||
style n7 fill:#00FF7F | ||
n8["y"] | ||
n8@{ shape: rect } | ||
style n8 fill:#32CD32 | ||
n9["0.0"] | ||
n9@{ shape: rect } | ||
style n9 fill:#00FF7F | ||
n10["z"] | ||
n10@{ shape: rect } | ||
style n10 fill:#1E90FF | ||
|
||
%% Edges: | ||
n2 --> n1 | ||
n1 --> n4 | ||
n4 --> n6 | ||
n7 --> n6 | ||
n2 --> n6 | ||
n8 --> n6 | ||
n9 --> n6 | ||
n6 --> n10 | ||
""").strip() | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this a function?
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.
mermaid needs hexcolors. the pydotformatter has strings names for colors