It's a DAG all the way down
GraphKit is an an understandable and lightweight Python module for building and running ordered graphs of computations. The API posits a fair compromise between features and complexity without precluding any. It might be of use in computer vision, machine learning and other data science domains, or become the core of a custom ETL pipelne.
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 and request all of the outputs.
>>> out = graphop({'a': 2, 'b': 5})
>>> print(out)
{'a': 2, 'b': 5, 'ab': 10, 'a_minus_ab': -8, 'abs_a_minus_ab_cubed': 512}
>>> # Run the graph and request a subset of the outputs.
>>> out = graphop({'a': 2, 'b': 5}, outputs=["a_minus_ab"])
>>> print(out)
{'a_minus_ab': -8}
As you can see, any function can be used as an operation in GraphKit, even ones imported from system modules!
For debugging the above graph-operation you may plot either the newly omposed graph or the execution plan of the last computation executed, using these methods:
graphop.plot(show=True) # open a matplotlib window
graphop.plot("graphop.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
TIP: The
pydot.Dot
instances returned byplot()
are rendered as SVG in Jupyter/IPython.
Code licensed under the Apache License, Version 2.0 license. See LICENSE file for terms.