Skip to content
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

Add to_dot method to PyGraph and PyDiGraph #103

Merged
merged 11 commits into from
Aug 12, 2020
Merged

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Jul 19, 2020

This commit adds a new method to PyGraph and PyDiGraph, to_dot(), which
is used to generate a graphviz dot file for a graph object. The
implementation of this is influenced by petgraph's built-in dot module
[1].

[1] https://github.com/petgraph/petgraph/blob/0.5.1/src/dot.rs

This commit adds a new method to PyGraph and PyDiGraph, to_dot(), which
is used to generate a graphviz dot file for a graph object. The
implementation of this is influenced by petgraph's built-in dot module
[1].

[1] https://github.com/petgraph/petgraph/blob/0.5.1/src/dot.rs
@coveralls
Copy link

coveralls commented Jul 19, 2020

Coverage Status

Coverage increased (+1.1%) to 90.27% when pulling aacdfb0 on mtreinish:add-dot into 7fd4bb4 on Qiskit:master.

src/digraph.rs Outdated Show resolved Hide resolved
src/digraph.rs Outdated Show resolved Hide resolved
src/dot_utils.rs Outdated
Comment on lines 25 to 41
pub fn build_dot<G, N, E, T>(
graph: G,
file: &mut T,
graph_attrs: Option<BTreeMap<String, String>>,
mut node_attrs: N,
mut edge_attrs: E,
) -> PyResult<()>
where
T: Write,
G: GraphBase
+ IntoEdgeReferences
+ IntoNodeReferences
+ NodeIndexable
+ GraphProp,
G: Data<NodeWeight = PyObject, EdgeWeight = PyObject>,
N: FnMut(&PyObject) -> PyResult<BTreeMap<String, String>>,
E: FnMut(&PyObject) -> PyResult<BTreeMap<String, String>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't follow what's going on with G N E T here

Copy link
Member Author

Choose a reason for hiding this comment

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

These are bounds for generic types on this function. Basically it's saying that the parameter graph is of generic type G where any object passed in via graph implements a bunch of traits. A trait defines a set of common methods and attributes that can be implemented for a type. The specific list of traits for G are from petgraph and basically are just shared functions for graph traversal and access (I got most of them from compiler errors and the similar function in petgraph that generates dot). The definitions of them are here: https://docs.rs/petgraph/0.5.1/petgraph/visit/index.html#traits but they basically just enable us to pass in either a PyGraph or PyDiGraph and have the function work as expected for both (the trait definitions for either type are at the top of both digraph.rs and graph.rs).

T, N, and E are easier to follow because they're much simpler. Parameter file is a mutable borrow of generic type T, where T is anything that implements the Write trait. This can be a file object, a buffer, etc. Parameter node_attrs is a generic type N whiere N is any function that takes in a &PyObject and returns a PyResult<BTreeMap<String, String> and parameter edge_attrs is a generic type E where it's also a function like that (E and N are different though because they are not necessarily going to be the same function).

src/dot_utils.rs Outdated Show resolved Hide resolved
tests/test_dot.py Show resolved Hide resolved
tests/test_dot.py Show resolved Hide resolved
mtreinish and others added 3 commits July 27, 2020 15:37
Co-authored-by: Lauren Capelluto <[email protected]>
This commit changes the node_attrs and edge_attrs callables to be opt-in
and default to None. This makes the user burden lower because if you
don't require extra formatting this simplifies the call signature.
The to_dot methods were documented as returning a string if filename was
not specified, but we were returing a bytes object not a string. This
commit corrects the return type to convert the bytes array to a string
primitive and returning that instead of the raw PyBytes object.
@mtreinish mtreinish merged commit 69961e4 into Qiskit:master Aug 12, 2020
@mtreinish mtreinish deleted the add-dot branch August 12, 2020 15:08
mtreinish added a commit that referenced this pull request Aug 13, 2020
Since the introduction of the to_dot() methods in #103 and the use of
jupyter-sphinx in the docs, the read the docs builds have been failing.
It looks like this is due to:
jupyter/jupyter-sphinx#126
This commit fixes this issue by pinning the jupyter sphinx version to
a version that is known working on read the docs.
@mtreinish mtreinish mentioned this pull request May 18, 2021
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.

3 participants