Skip to content

Add graphviz model graphs #3049

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 4 commits into from
Jun 27, 2018
Merged

Conversation

ColCarroll
Copy link
Member

This follows #1683 and #876 to create model representations for graphs. I also reran the multilevel modeling notebook, updating a bit of the code, and adding the graphs for each of the models.

Installation of graphviz is now supported on conda-forge, and is used in dask, so it seems like a reasonable choice. I've also isolated the code, so it is easy to remove if it causes headaches.

The function takes only one (optional) argument - the model object, which it can graph from the context manager if necessary. It returns a graphviz.Digraph, so if someone wanted to customize the object for publication or any other reason, that is surprisingly easy. My favorite part is that the graphs display inline in a notebook.

I've included two examples of the graphs from the Radon example, since they look interesting, and show all of the styling I added:

  • Rectangles, ovals, shaded ovals for deterministic, RV, and observed variables
  • Plate detection (I just group by variable shape, so it can definitely be wrong)
  • A subtle rounded corner on the plates for a touch of class

image

image

for more information.
"""
model = pm.modelcontext(model)
return ModelGraph(model).make_graph()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need newline

if parents != upstream:
det_map = {}
for d in deterministics:
d_set = set([j for j in inputs([func], blockers=[d])])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just use a set comprehension: {j for j in inputs([func], blockers=[d])}

@fonnesbeck
Copy link
Member

This is great! Can we just make this the repr for Model?

@rlouf
Copy link
Contributor

rlouf commented Jun 22, 2018

This looks very cool! Have you considered daft?

@junpenglao
Copy link
Member

This is awesome! Is there still any problem of installation of graphviz on Mac and Windows? Could somebody with a winOS test this?

@ColCarroll
Copy link
Member Author

  1. can we make this the repr for Model? Probably! I would wrap it in a try/except and fall back to the latex repr. graphviz is easier to install now, but the binaries are still a bit annoying, and I would not want to support that as an installation requirement.
  2. Have you considered daft? I thought I did, and remembered thinking it was a little ugly, but I looked again and it seems awesome! The implementation here is not tied closely to graphviz, so I (or someone) could drop in another graphing engine reasonably easily.
  3. Is there an installation problem on Mac or Windows? I've run this on OSX and on Ubuntu. I actually installed it on OSX using brew install graphviz and then pip install graphviz, but then verified that it worked in a conda environment, too. No idea about windows 😬

@twiecki
Copy link
Member

twiecki commented Jun 22, 2018

This is awesome, the radon example is much improved due to this.

@fonnesbeck
Copy link
Member

@ColCarroll fair enough on not wanting to have graphviz as a hard dependency. Would be cool, but probably not worth the pain.

@ColCarroll
Copy link
Member Author

I took a small look at making this the repr, but it looks confusing (ipython/ipython#11202).

daft looks great, but requires manually specifying the position of each node. If someone picked up daft-dev/daft#81 then I think it would definitely be the right choice.

Anyways, I think this is ready to merge.

@ColCarroll ColCarroll merged commit 09014b9 into pymc-devs:master Jun 27, 2018
@ColCarroll
Copy link
Member Author

(merged to avoid conflicts with README.md with a bunch of other PRs coming... feel free to revert if there are more issues!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants