Skip to main content

Cached lazy evaluation of computational trees

Project description

Tests Documentation Status

Progressive evaluation

Documentation | Installation | Examples | Related

This package lets the user define a computation such that:

  1. Depending on the requested output, only the required (partial) computations are executed.
  2. Intermediate quantities can be overriden by the user such that dependent values are recomputed.

The computational graph can either be defined as a class, or is constructed dynamically by registering nodes.

Installation

To use progeval, it can be installed with pip:

(.venv) $ pip install progeval

Examples

Consider the following toy computation:

alpha = x * y
beta = x + y
total = alpha + beta

We can define this computational graph in multiple ways, all via the computational graph class ProgEval. The first is by dynamically adding nodes corresponding to the computable objects:

from progeval import ProgEval

graph = ProgEval()
graph.alpha = lambda x, y: x * y
graph.beta = lambda x, y: x + y
graph.total = lambda alpha, beta: alpha + beta

# we can then set inputs ...
graph.x, graph.y = 3, 6
# ... and evaluate any (intermediate) value
graph.total == 27

The above example relies on the fact that the inputs (x, alpha, etc.) can be read from the function signatures. If we want to reuse some external function this is not possible. The following syntax allows one to (optionally) specify the input arguments:

from progeval import ProgEval


def add(x, y):
    return x + y


graph = ProgEval()
graph.register('alpha', lambda x, y: x * y)
graph.register('beta', add)
# here the inputs we want are different from the names 
# in the function definition of `add`
graph.register('total', add, ['alpha', 'beta'])

Another way to define the computational graph is by collecting all parts of the computation as methods of a class:

from progeval import ProgEval


class Computation(ProgEval):

    def __init__(self, x, y):
        super().__init__(x=x, y=y)

    @staticmethod
    def alpha(x, y):
        return x * y

    @staticmethod
    def beta(x, y):
        return x + y

    @staticmethod
    def total(alpha, beta):
        return alpha + beta

# The class then acts almost like a normal function
Computation(3, 8).total == 35

Related

A similar functionality of delayed evaluation of a computational graph is provided by Dask delayed. However, the construction is slightly different. There, the graph is built dynamically by replacing intermediate quantities with Delayed objects and wrapping functions. The functionality of Dask, which is intended for computational parallelism, can even be combined with the structures here, as shown in the how-to section of the documentation.

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

progeval-1.0.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

progeval-1.0.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file progeval-1.0.0.tar.gz.

File metadata

  • Download URL: progeval-1.0.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for progeval-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9e3a7b08af4194fe6a771156f7e35efeb3fd2ff462aed0432f175e966be8f886
MD5 2a19924857e2cc3bab7dfcf47f1d5ad8
BLAKE2b-256 2c9fb694fc121af9d12632540be8817748217006fc973cbcf699e586f0d63a54

See more details on using hashes here.

File details

Details for the file progeval-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: progeval-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for progeval-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0576a65ae7016109ad5bc8e10f221fc731b691c55f01a3464096980343b5cc3e
MD5 d0e93a86c2b8989e15283a8c367bc4fd
BLAKE2b-256 a849fe957cbb0100af93ccba3ed13a5c2531a9d4a91e2336de22f933d0067087

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