GraphKit

Travis continuous integration testing ok? (Linux) cover-status Latest version in GitHub Latest version in PyPI Supported Python versions of latest release in PyPi Development Status PyPi downloads Code Style Apache License, version 2.0

Github watchers Github stargazers Github forks Issues count

It’s a DAG all the way down!

sample graphkit plot

Quick start

Here’s how to install:

pip install graphkit

OR with dependencies for plotting support (and you need to install Graphviz program separately with your OS tools):

pip install graphkit[plot]

Here’s a Python script with an example GraphKit computation graph that produces multiple outputs (a * b, a - a * b, and abs(a - a * b) ** 3):

from operator import mul, sub
from graphkit import compose, operation

# Computes |a|^p.
def abspow(a, p):
   c = abs(a) ** p
   return c

# Compose the mul, sub, and abspow operations into a computation graph.
graphop = compose(name="graphop")(
   operation(name="mul1", needs=["a", "b"], provides=["ab"])(mul),
   operation(name="sub1", needs=["a", "ab"], provides=["a_minus_ab"])(sub),
   operation(name="abspow1", needs=["a_minus_ab"], provides=["abs_a_minus_ab_cubed"], params={"p": 3})(abspow)
)

# Run the graph-operation and request all of the outputs.
out = graphop({'a': 2, 'b': 5})

# Prints "{'a': 2, 'a_minus_ab': -8, 'b': 5, 'ab': 10, 'abs_a_minus_ab_cubed': 512}".
print(out)

# Run the graph-operation and request a subset of the outputs.
out = graphop({'a': 2, 'b': 5}, outputs=["a_minus_ab"])

# Prints "{'a_minus_ab': -8}".
print(out)

As you can see, any function can be used as an operation in GraphKit, even ones imported from system modules!

Plotting

For debugging the above graph-operation you may plot the execution plan of the last computation it using these methods:

graphop.plot(show=True)                # open a matplotlib window
graphop.plot("intro.svg")              # other supported formats: png, jpg, pdf, ...
graphop.plot()                         # without arguments return a pydot.DOT object
graphop.plot(solution=out)             # annotate graph with solution values
Intro graph
Graphkit Legend

The legend for all graphkit diagrams, generated by graphkit.plot.legend().

Tip

The pydot.Dot instances returned by plot() are rendered directly in Jupyter/IPython notebooks as SVG images.

Note

For plots, Graphviz program must be in your PATH, and pydot & matplotlib python packages installed. You may install both when installing graphkit with its plot extras:

pip install graphkit[plot]

License

Code licensed under the Apache License, Version 2.0 license. See LICENSE file for terms.