Skip to content

Adds function to plot onnx model as graphs #61

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

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
improve versatility
  • Loading branch information
xadupre committed Jan 8, 2024
commit 8436ea447246d57e782866e4f099d93313db263e
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build/*
*egg-info/*
onnxruntime_profile*
prof
test*.png
_doc/sg_execution_times.rst
_doc/auto_examples/*
_doc/examples/_cache/*
Expand Down
6 changes: 6 additions & 0 deletions _unittests/ut_plotting/test_graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_draw_graph_graphviz(self):
draw_graph_graphviz(dot, image=fout)
self.assertExists(os.path.exists(fout))

def test_draw_graph_graphviz_proto(self):
fout = "test_draw_graph_graphviz_proto.png"
dot = self._get_graph()
draw_graph_graphviz(dot, image=fout)
self.assertExists(os.path.exists(fout))

def test_plot_dot(self):
dot = to_dot(self._get_graph())
ax = plot_dot(dot)
Expand Down
20 changes: 13 additions & 7 deletions onnx_array_api/plotting/graphviz_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import subprocess
import sys
import tempfile
from typing import List, Optional, Tuple
from typing import List, Optional, Tuple, Union
import numpy as np
from onnx import ModelProto


def _find_in_PATH(prog: str) -> Optional[str]:
Expand Down Expand Up @@ -139,24 +140,29 @@ def _run_graphviz(filename: str, image: str, engine: str = "dot") -> str:


def draw_graph_graphviz(
dot: str,
dot: Union[str, ModelProto],
image: str,
engine: str = "dot",
) -> str:
"""
Draws a graph using :epkg:`Graphviz`.

:param dot: dot graph
:param dot: dot graph or ModelProto
:param image: output image, None, just returns the output
:param engine: *dot* or *neato*
:return: :epkg:`Graphviz` output or
the dot text if *image* is None

The function creates a temporary file to store the dot file if *image* is not None.
"""
if isinstance(dot, ModelProto):
from .dot_plot import to_dot

sdot = to_dot(dot)
else:
sdot = dot
with tempfile.NamedTemporaryFile(delete=False) as fp:
fp.write(dot.encode("utf-8"))
fp.seek(0)
fp.write(sdot.encode("utf-8"))
fp.close()

filename = fp.name
Expand All @@ -172,15 +178,15 @@ def draw_graph_graphviz(


def plot_dot(
dot: str,
dot: Union[str, ModelProto],
ax: Optional["matplotlib.axis.Axis"] = None, # noqa: F821
engine: str = "dot",
figsize: Optional[Tuple[int, int]] = None,
) -> "matplotlib.axis.Axis": # noqa: F821
"""
Draws a dot graph into a matplotlib graph.

:param dot: dot graph
:param dot: dot graph or ModelProto
:param image: output image, None, just returns the output
:param engine: *dot* or *neato*
:param figsize: figsize of ax is None
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy