Skip to main content

A Python lib for solving & executing graphs of functions, with `pandas` in mind

Project description

Latest version in PyPI Latest release in GitHub (build-version: v10.2.1, build-date: 2020-09-18T11:46:20.332529) Supported Python versions of latest release in PyPi Development Status Travis continuous integration testing ok? (Linux) ReadTheDocs ok? cover-status Code Style Apache License, version 2.0

Github watchers Github stargazers Github forks Issues count

It’s a DAG all the way down!

sample graphtik plot

Lightweight computation graphs for Python

Graphtik is a library to compose, plot & execute graphs of python functions (a.k.a pipelines) that consume and populate (possibly nested) named data (a.k.a dependencies), based on whether values for those dependencies exist in the inputs or have been calculated earlier, with pandas in mind.

  • Its primary use case is building flexible algorithms for data science/machine learning projects.

  • It should be extendable to implement the following:

    • an IoC dependency resolver (e.g. Java Spring);

    • an executor of interdependent tasks based on files (e.g. GNU Make);

    • a custom ETL engine;

    • a spreadsheet calculation engine.

Graphtik sprang from Graphkit (summer 2019, v1.2.2) to experiment with Python 3.6+ features, but has diverged significantly with enhancements ever since.

Features

  • Deterministic pre-decided execution plan (excepting partial-outputs or endured operations, see below).

  • Can assemble existing functions without modifications into pipelines.

  • dependency resolution can bypass calculation cycles based on data given and asked.

  • Support functions with optional <optionals> input args and/or varargs <varargish>.

  • Support functions with partial outputs; keep working even if certain endured operations fail.

  • Facilitate trivial conveyor operations and alias on provides.

  • Support cycles, by annotating repeated updates of dependency values as sideffects, (e.g. to add columns into pandas.DataFrames).

  • Hierarchical dependencies <subdoc> may access data values deep in solution with json pointer path expressions.

  • Hierarchical dependencies annotated as implicit imply which subdoc dependency the function reads or writes in the parent-doc.

  • Merge <operation merging> or nest <operation nesting> sub-pipelines.

  • Early eviction of intermediate results from solution, to optimize memory footprint.

  • Solution tracks all intermediate overwritten <overwrite> values for the same dependency.

  • Parallel execution (but underdeveloped).

  • Elaborate Graphviz plotting with configurable plot themes.

  • Integration with Sphinx sites with the new graphtik directive.

  • Authored with debugging in mind.

  • Parallel execution (but underdeveloped & deprecated).

Anti-features

  • It’s not an orchestrator for long-running tasks, nor a calendar scheduler - Apache Airflow, Dagster or Luigi may help for that.

  • It’s not really a parallelizing optimizer, neither a map-reduce framework - look additionally at Dask, IpyParallel, Celery, Hive, Pig, Spark, Hadoop, etc.

Quick start

Here’s how to install:

pip install graphtik

OR with various “extras” dependencies, such as, for plotting:

pip install graphtik[plot]
. Tip::

Supported extras:

plot

for plotting with Graphviz,

matplot

for plotting in maplotlib windows

sphinx

for embedding plots in sphinx-generated sites,

test

for running pytests,

dill

may help for pickling parallel tasks - see marshalling term and set_marshal_tasks() configuration.

all

all of the above, plus development libraries, eg black formatter.

dev

like all

Let’s build a graphtik computation graph that produces x3 outputs out of 2 inputs α and β:

  • α x β

  • α - αxβ

  • |α - αxβ| ^ 3

>>> from graphtik import compose, operation
>>> from operator import mul, sub
>>> @operation(name="abs qubed",
...            needs=["α-α×β"],
...            provides=["|α-α×β|³"])
... def abs_qubed(a):
...     return abs(a) ** 3

Compose the abspow function along the mul & sub built-ins into a computation graph:

>>> graphop = compose("graphop",
...     operation(needs=["α", "β"], provides=["α×β"])(mul),
...     operation(needs=["α", "α×β"], provides=["α-α×β"])(sub),
...     abs_qubed,
... )
>>> graphop
Pipeline('graphop', needs=['α', 'β', 'α×β', 'α-α×β'],
                    provides=['α×β', 'α-α×β', '|α-α×β|³'],
                    x3 ops: mul, sub, abs qubed)

Run the graph and request all of the outputs (notice that unicode characters work also as Python identifiers):

>>> graphop(α=2, β=5)
{'α': 2, 'β': 5, 'α×β': 10, 'α-α×β': -8, '|α-α×β|³': 512}

… or request a subset of outputs:

>>> solution = graphop.compute({'α': 2, 'β': 5}, outputs=["α-α×β"])
>>> solution
{'α-α×β': -8}

… and plot the results (if in jupyter, no need to create the file):

>>> solution.plot('executed_3ops.svg')  # doctest: +SKIP

sample graphtik plot

graphtik legend

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphtik-10.2.1.tar.gz (173.2 kB view details)

Uploaded Source

Built Distribution

graphtik-10.2.1-py2.py3-none-any.whl (117.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file graphtik-10.2.1.tar.gz.

File metadata

  • Download URL: graphtik-10.2.1.tar.gz
  • Upload date:
  • Size: 173.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for graphtik-10.2.1.tar.gz
Algorithm Hash digest
SHA256 0114d7315a60cad3d0b49ee09001dac0d5f5d2c512545ff8c3fa148489d43ef1
MD5 d30c83071d7e1aba8c3caff0c4336271
BLAKE2b-256 40e5bf77fae5ebba48709008be2ef698565dbc3e7d03efbc0b78c79b7704ff1a

See more details on using hashes here.

File details

Details for the file graphtik-10.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: graphtik-10.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 117.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for graphtik-10.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8339ebccc1f3c0f1313e16dd74675659b056f63e16ef6dfe4e31445f8a3ca6d8
MD5 7c77eb1946faefa8a6926bcb76521b8e
BLAKE2b-256 d8986dcd8171abe3eebd34a7f6b7498c6a77858b529db448cc10c4009c326311

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page