Skip to content

DAG.from_dot_digraph #46

@thorwhalen

Description

@thorwhalen

We can make a dot digraph from a DAG which is very useful to view it.

We also have some convenient ways to make a DAG, such as the code_to_dag function.

Yet I still find myself sometimes building a graph using the dot language (or rather a mini-dot language (I use from qo import dgdisp)). This may indicate the utility of building a DAG from a dot string.

I tried to write a function that takes a graphviz.Digraph instance and returns the edges of the graph as pairs of node IDs, so that I could then use this to make my DAG (using a similar approach as code_to_dag). But I failed. It seems like those graphviz objects are for writing, not reading -- at least not reading into some python-ready object.

I then decided to go through networkx, which has a lot of conversion tools.
This code looked promising.

My slightly modified version:

import networkx as nx

G = nx.complete_graph(5)
A = nx.nx_agraph.to_agraph(G)  # convert to a graphviz graph
X1 = nx.nx_agraph.from_agraph(A)  # convert back to networkx (but as Graph)
X2 = nx.Graph(A)  # fancy way to do conversion
G1 = nx.Graph(X1)  # now make it a Graph

A.write("k5.dot")  # write to dot file
X3 = nx.nx_agraph.read_dot("k5.dot")  # read from dotfile

# You can also create .png directly with the AGraph.draw method
A.draw("k5.png", prog="neato")

# This is what I added, to be able to see the graph:
from IPython.display import Image
Image('k5.png')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions