GraphKit¶
Lightweight computation graphs for Python¶
GraphKit is a lightweight Python module for creating and running ordered graphs of computations, where the nodes of the graph correspond to computational operations, and the edges correspond to output –> input dependencies between those operations. Such graphs are useful in computer vision, machine learning, and many other domains.
- Operations
- Graph Composition
- API Reference
- Changelog
- v1.3.0 (Oct 2019): New DAG solver, better plotting & “sideffect”
- v1.2.4 (Mar 7, 2018)
- 1.2.2 (Mar 7, 2018, @huyng): Fixed versioning
- 1.2.1 (Feb 23, 2018, @huyng): Fixed multi-threading bug and faster compute through caching of find_necessary_steps
- 1.2.0 (Feb 13, 2018, @huyng)
- 1.1.0 (Nov 9, 2017, @huyng)
- 1.0.4 (Nov 3, 2017, @huyng): Networkx 2.0 compatibility
- 1.0.3 (Jan 31, 2017, @huyng): Make plotting dependencies optional
- 1.0.2 (Sep 29, 2016, @pumpikano): Merge pull request #5 from yahoo/remove-packaging-dep
- 1.0.1 (Aug 24, 2016)
- 1.0 (Aug 2, 2016, @robwhess)
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
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.