Graphical visualization for pymc model

Hi, I am trying to visualize my pymc model using graph and I am not able to execute pm.model_to_graphviz()

I have tried the following -

from pymc import model_to_graphviz
import graphviz
os.environ["PATH"] += os.pathsep + r'/opt/conda/lib/python3.9/site-packages/graphviz'

with model_factory(df):
    graphviz = pm.model_to_graphviz()

I could not find any .exe file in the graphviz package folder though.
I am getting the following error -

FileNotFoundError                         Traceback (most recent call last)
/opt/conda/lib/python3.9/site-packages/graphviz/backend/ in run_check(cmd, input_lines, encoding, quiet, **kwargs)
     78                 kwargs['stdout'] = kwargs['stderr'] = subprocess.PIPE
---> 79             proc = _run_input_lines(cmd, input_lines, kwargs=kwargs)
     80         else:

/opt/conda/lib/python3.9/site-packages/graphviz/backend/ in _run_input_lines(cmd, input_lines, kwargs)
     98 def _run_input_lines(cmd, input_lines, *, kwargs):
---> 99     popen = subprocess.Popen(cmd, stdin=subprocess.PIPE, **kwargs)

/opt/conda/lib/python3.9/ in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)
--> 947             self._execute_child(args, executable, preexec_fn, close_fds,
    948                                 pass_fds, cwd, env,

/opt/conda/lib/python3.9/ in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, gid, gids, uid, umask, start_new_session)
   1818                         err_msg = os.strerror(errno_num)
-> 1819                     raise child_exception_type(errno_num, err_msg, err_filename)
   1820                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: PosixPath('dot')

The above exception was the direct cause of the following exception:

ExecutableNotFound                        Traceback (most recent call last)
/opt/conda/lib/python3.9/site-packages/IPython/core/ in __call__(self, obj, include, exclude)
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

/opt/conda/lib/python3.9/site-packages/graphviz/ in _repr_mimebundle_(self, include, exclude, **_)
     96         include = set(include) if include is not None else {self._jupyter_mimetype}
     97         include -= set(exclude or [])
---> 98         return {mimetype: getattr(self, method_name)()
     99                 for mimetype, method_name in MIME_TYPES.items()
    100                 if mimetype in include}

/opt/conda/lib/python3.9/site-packages/graphviz/ in <dictcomp>(.0)
     96         include = set(include) if include is not None else {self._jupyter_mimetype}
     97         include -= set(exclude or [])
---> 98         return {mimetype: getattr(self, method_name)()
     99                 for mimetype, method_name in MIME_TYPES.items()
    100                 if mimetype in include}

/opt/conda/lib/python3.9/site-packages/graphviz/ in _repr_image_svg_xml(self)
    110     def _repr_image_svg_xml(self) -> str:
    111         """Return the rendered graph as SVG string."""
--> 112         return self.pipe(format='svg', encoding=SVG_ENCODING)

/opt/conda/lib/python3.9/site-packages/graphviz/ in pipe(self, format, renderer, formatter, neato_no_op, quiet, engine, encoding)
    102             '<?xml version='
    103         """
--> 104         return self._pipe_legacy(format,
    105                                  renderer=renderer,
    106                                  formatter=formatter,

/opt/conda/lib/python3.9/site-packages/graphviz/ in wrapper(*args, **kwargs)
    169                               category=category)
--> 171             return func(*args, **kwargs)
    173         return wrapper

/opt/conda/lib/python3.9/site-packages/graphviz/ in _pipe_legacy(self, format, renderer, formatter, neato_no_op, quiet, engine, encoding)
    119                      engine: typing.Optional[str] = None,
    120                      encoding: typing.Optional[str] = None) -> typing.Union[bytes, str]:
--> 121         return self._pipe_future(format,
    122                                  renderer=renderer,
    123                                  formatter=formatter,

/opt/conda/lib/python3.9/site-packages/graphviz/ in _pipe_future(self, format, renderer, formatter, neato_no_op, quiet, engine, encoding)
    147             if codecs.lookup(encoding) is codecs.lookup(self.encoding):
    148                 # common case: both stdin and stdout need the same encoding
--> 149                 return self._pipe_lines_string(*args, encoding=encoding, **kwargs)
    150             try:
    151                 raw = self._pipe_lines(*args, input_encoding=self.encoding, **kwargs)

/opt/conda/lib/python3.9/site-packages/graphviz/backend/ in pipe_lines_string(engine, format, input_lines, encoding, renderer, formatter, neato_no_op, quiet)
    210     kwargs = {'input_lines': input_lines, 'encoding': encoding}
--> 212     proc = execute.run_check(cmd, capture_output=True, quiet=quiet, **kwargs)
    213     return proc.stdout

/opt/conda/lib/python3.9/site-packages/graphviz/backend/ in run_check(cmd, input_lines, encoding, quiet, **kwargs)
     82     except OSError as e:
     83         if e.errno == errno.ENOENT:
---> 84             raise ExecutableNotFound(cmd) from e
     85         raise

ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

See here for more info on saving graphviz plots.

Thanks for the reply. I guess the issue is with the system’s PATH variable settings. If I run the following code -

with model_factory(df):
    g = pm.model_to_graphviz()
    g.render("graphname", format="png")

It throws the same error as above and creates a β€œgraphname” file like this. How to visualize this?

Hi - I have been trying to follow the example in this notebook -

It says - Alternatively, you may install the graphviz binaries yourself, and then pip install graphviz to get the python bindings

Can anyone help me here in installing graphviz binaries? I am using a jupyter notebook from terminal for this. Thanks

A couple of questions. How did you install graphviz? What platform are you on? And can you share the output of conda list graphviz?

I am using ubuntu and launching a jupyter notebook from there. I installed graphviz like this -

pip install graphviz
from pymc import model_to_graphviz

And here is the output of conda list graphviz -


Looks like you don’t have graphviz itself installed. Check out here and here if you’re on windows.

I did install graphviz using pip. See below. Sorry, but is this not the right way?

You installed python-graphviz. But if you read the installation instructions found here or here, you will see that one of the dependencies of python-graphviz is graphviz. python-graphviz is just connects python with graphviz.

The link that you shared has this command -

pip install graphviz

I did the same but it is still downloading the python interface package -

ubuntu@run-65165e55753e5058124cd5e8-pvd6d:/mnt/code$ pip install --user graphviz
Collecting graphviz
     |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 47 kB 1.5 MB/s             
Installing collected packages: graphviz
Successfully installed graphviz-0.20.1
ubuntu@run-65165e55753e5058124cd5e8-pvd6d:/mnt/code$ pip show graphviz
Name: graphviz
Version: 0.20.1
Summary: Simple Python interface for Graphviz
Author: Sebastian Bank
License: MIT
Location: /home/ubuntu/.local/lib/python3.9/site-packages

Yes. And the python interface is required. But that link also states:

So you need to install graphviz itself (in addition to the python interface) using the instructions linked to in that screenshot (which I also linked to above).

Thanks @cluhmann for the help. It works now!

1 Like