Skip to main content

PyDecisionGraph

PyDecisionGraph is an easy-to-use library to create custom decision trees, primarily designed for trading and financial decision-making. This package helps you build rule-based decision processes and visualize them effectively.

Layers

  • decision_graph.decision_tree - the public tree API used in the Quick Start. It uses the C API when the extensions are available (USING_CAPI) and falls back to the pure-Python model otherwise.
  • decision_graph.decision_tree.bake - the C-first bake engine underneath. Its CCP-bound wrappers hold a reference on their C block: when a block is released, any other wrapper over that block becomes a husk instead of reading freed memory. Its C diagnostics name the operation, the cause, and the consequence.

Installation

To install the package, run:

pip install PyDecisionGraph

Cython headers

The package ships its Cython sources (.pxd, .pyx) and the C headers they bind. When Cython is installed in the same environment, the build links decision_graph into Cython/Includes through cbase.cython_injection, so downstream projects can cimport decision_graph.

decision_graph.get_include() returns every Cython source directory: the package root, decision_tree, decision_tree/bake, decision_tree/capi, and the packaged includes/ mirror when it is present.

What's new in 0.3.3

  • CCP-bound bake wrappers. Each CCP-bound bake wrapper holds a reference on its C block. When the block is released - by the wrapper that owns it or by the C layer during consolidation - every other wrapper over that block becomes a husk: guarded accessors refuse instead of reading freed memory, and repr still answers for the debugger. __dealloc__ owns the block free; __ccp_dealloc__ resets the wrapper's extra fields, and derived hooks chain.
  • Clearer diagnostics. C-side failure reports name the operation, the DCG_ERR_* cause, and the state left behind. The placeholder-consolidation report no longer says "no room".
  • Slot preservation. Unlink, replace, and placeholder consolidation keep a node in the same arm instead of moving it to the end of the parent.
  • Packaging. get_include() returns all Cython source directories, and the Cython sources ship with the package.
  • Tests. New coverage for consolidation, logic-group lifetime, and node lifecycle, alongside the 12 C suites.

Requirements

  • Python 3.12 or higher
  • Building from source: a C compiler and Cython 3.x

Documentation

For detailed documentation, visit https://pydecisiongraph.readthedocs.io/.

Quick Start

Here is a quick demo on how to use PyDecisionGraph for building a decision tree based on various conditions:

from decision_graph.decision_tree import LOGGER, LongAction, ShortAction, NoAction, RootLogicNode, LogicMapping

# Mapping of attribute names to their values
state = {
    "exposure": 0,  # Current exposure
    "working_order": 0,  # Current working order
    "up_prob": 0.8,  # Probability of price going up
    "down_prob": 0.2,  # Probability of price going down
    "volatility": 0.26,  # Current market volatility
    "ttl": 15.3  # Time to live (TTL) of the decision tree
}

# The store the conditions read from, entered for the scope the tree is built in
with LogicMapping(name='Root', data=state) as lg:
    # Root of the logic tree
    with RootLogicNode() as root:
        # Condition for zero exposure
        with lg.exposure == 0:
            with lg.volatility > 0.25:  # Check if volatility is high
                with lg.down_prob > 0.1:  # Action for down probability
                    LongAction()

                with lg.up_prob < -0.1:  # Action for up probability
                    ShortAction()

            # No action if there's a working order
            with lg.working_order != 0:
                NoAction()

# Visualize the decision tree
root.to_html('tree.html')

# Log the evaluation result
LOGGER.info(root())

Explanation of the Script:

  • LogicNode & LogicMapping:

    • LogicNode: Represents a node in the decision tree where conditions are evaluated. A node evaluates its condition and takes one of its two arms.
    • LogicMapping: The named store the conditions read from, holding the state used in decision-making. Feed it a new value and the next walk moves.
  • State:

    • A dictionary containing the variables used for decision-making, such as exposure, working_order, up_prob, etc.
  • RootLogicNode:

    • The entry point for the decision tree; its single child is the top of the tree.
  • Decision Conditions:

    • Inside each with block, logical conditions are evaluated (e.g., lg.volatility > 0.25, lg.up_prob < -0.1) to determine which action to take.
    • Each with block nests one level deeper; the first block under a node is its true arm, the second its false arm.
    • Actions like LongAction() or ShortAction() are taken based on the conditions.
  • Action Handling:

    • LongAction(), ShortAction(), and NoAction() represent different actions you can trigger in the decision tree based on the conditions.
    • An arm left empty is filled with a NoAction when the tree is evaluated.
  • Logging:

    • The result of the tree evaluation is logged using the LOGGER object, which outputs to the console.
  • Visualization:

    • root.to_html('tree.html') generates a standalone HTML page visualizing the decision tree. Drop the file name and the page is named after the root.

Features

  • Easily define custom decision rules.
  • Actionable outcomes like LongAction, ShortAction, and NoAction.
  • Log outputs for debugging and tracking.
  • Visualize decision paths through HTML export.
  • Bake a finished tree once (root.bake()) and let it decide in a hot loop.
  • CCP-safe invalidation for bake wrappers when their C block is released.
  • C diagnostics that name the operation, the cause, and the consequence.
  • Slot-stable unlink, replace, and placeholder consolidation.

Contributing

Feel free to fork, submit issues, and create pull requests. Contributions are always welcome!

License

This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.

Metadata

Release files for PyDecisionGraph 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for PyDecisionGraph 0.3.3
File Actions
pydecisiongraph-0.3.3-cp315-cp315t-win_amd64.whl CPython 3.15 CPython 3.15 free-threading Windows x86-64 Details for pydecisiongraph-0.3.3-cp315-cp315t-win_amd64.whl
pydecisiongraph-0.3.3-cp315-cp315t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.15 CPython 3.15 free-threading Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pydecisiongraph-0.3.3-cp315-cp315t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pydecisiongraph-0.3.3-cp315-cp315-win_amd64.whl CPython 3.15 CPython 3.15 Windows x86-64 Details for pydecisiongraph-0.3.3-cp315-cp315-win_amd64.whl
pydecisiongraph-0.3.3-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.15 CPython 3.15 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pydecisiongraph-0.3.3-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pydecisiongraph-0.3.3-cp314-cp314t-win_amd64.whl CPython 3.14 CPython 3.14 free-threading Windows x86-64 Details for pydecisiongraph-0.3.3-cp314-cp314t-win_amd64.whl
pydecisiongraph-0.3.3-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pydecisiongraph-0.3.3-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pydecisiongraph-0.3.3-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for pydecisiongraph-0.3.3-cp314-cp314-win_amd64.whl
pydecisiongraph-0.3.3-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pydecisiongraph-0.3.3-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pydecisiongraph-0.3.3-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for pydecisiongraph-0.3.3-cp313-cp313-win_amd64.whl
pydecisiongraph-0.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pydecisiongraph-0.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pydecisiongraph-0.3.3-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for pydecisiongraph-0.3.3-cp312-cp312-win_amd64.whl
pydecisiongraph-0.3.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pydecisiongraph-0.3.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Total release size: 81.3 MB

Release files / pydecisiongraph-0.3.3-cp315-cp315t-win_amd64.whl

Download URL pydecisiongraph-0.3.3-cp315-cp315t-win_amd64.whl
Size 3.9 MB
Tags CPython 3.15 CPython 3.15 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
01ce7bdb57d64b54cd29e492920032373f04e68167f04fb2bfd69bac42a5c849
BLAKE2b-256 checksum
How to use checksums
9a170b5cd45f372790234810b7c7a474ec337cad6a65ee225d8d9704f6c1f401
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / pydecisiongraph-0.3.3-cp315-cp315t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pydecisiongraph-0.3.3-cp315-cp315t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 9.9 MB
Tags CPython 3.15 CPython 3.15 free-threading Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
1138aed0e0d8338e448c56e6b5b0be870c74cc5e9042482d23ea915aea93aef0
BLAKE2b-256 checksum
How to use checksums
9a5c541aaa66f16502bd55edb61448d79826cade4713ab38353ea7d59312221a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Release files / pydecisiongraph-0.3.3-cp315-cp315-win_amd64.whl

Download URL pydecisiongraph-0.3.3-cp315-cp315-win_amd64.whl
Size 3.7 MB
Tags CPython 3.15 Windows x86-64
SHA-256 checksum
How to use checksums
2c21205e456919d4577796f4911ab5f3b8ed1607cb3296d924ecece488632296
BLAKE2b-256 checksum
How to use checksums
ad583aa40aba31f028707fe2f9aa5f54b8a3419df00bd9cffa6a6084012aae70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / pydecisiongraph-0.3.3-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pydecisiongraph-0.3.3-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 9.7 MB
Tags CPython 3.15 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
9d790a61b2737b49a73e9461f50142e2eda8e62fadf175d9aea4920e68ec2692
BLAKE2b-256 checksum
How to use checksums
f623fa9cb04dd553062007654d5ceab8f3932eee3eeb3d9a3619ef6e2d46006f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Release files / pydecisiongraph-0.3.3-cp314-cp314t-win_amd64.whl

Download URL pydecisiongraph-0.3.3-cp314-cp314t-win_amd64.whl
Size 3.9 MB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
aa6f7bcb540a050ec49a1c6667931707b41cdd8a6be31f1f80d4d20d4bf99e55
BLAKE2b-256 checksum
How to use checksums
b11962140d3e520b305eda3341a1bf0a498bb4d2b4ace8bb2e403ac884ebed4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / pydecisiongraph-0.3.3-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pydecisiongraph-0.3.3-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 9.9 MB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
e02e97184b2d9d4d0d463739a3d12452eeda93b0c8889edb02b628a12cdc70e2
BLAKE2b-256 checksum
How to use checksums
dad1524c622963d31c8597b7d2d3e9944afff5a5a9c5a6b1e5e8b7beae19e89d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Release files / pydecisiongraph-0.3.3-cp314-cp314-win_amd64.whl

Download URL pydecisiongraph-0.3.3-cp314-cp314-win_amd64.whl
Size 3.7 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
c4085d03670ccb9430dd8766850b5222370aa97f72e455a2257784247129d41d
BLAKE2b-256 checksum
How to use checksums
6034539dbdc8d63360a95c347bb9810c4adf08c712c7c8e2ceec932408f51783
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / pydecisiongraph-0.3.3-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pydecisiongraph-0.3.3-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 9.7 MB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
5d326a812cf577d77a8dc44da9dbdac4cd4d781827065491e3dc9aa61bbff356
BLAKE2b-256 checksum
How to use checksums
be5c7c3edb9e271e459b91c2442747f52cdbe69e98473c03912c3cddd25963df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Release files / pydecisiongraph-0.3.3-cp313-cp313-win_amd64.whl

Download URL pydecisiongraph-0.3.3-cp313-cp313-win_amd64.whl
Size 3.7 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
c3ce534f8313ee301d03e58ab70abf37a0be124b3112eb4e504f8c426a18453e
BLAKE2b-256 checksum
How to use checksums
8d83e51cb351978569879ecab2d250409a1ae22474ada1928b247e8d1724c0d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / pydecisiongraph-0.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pydecisiongraph-0.3.3-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 9.7 MB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
d7cd9ae6fb59608b55dd7f7d8d36df00f0825a65eb4f10c7ec72ca7f73dd8d10
BLAKE2b-256 checksum
How to use checksums
4b39bfd8d2ad02850c4ac5e514db85fffeec6a365164a46c00812b513fbb066e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Release files / pydecisiongraph-0.3.3-cp312-cp312-win_amd64.whl

Download URL pydecisiongraph-0.3.3-cp312-cp312-win_amd64.whl
Size 3.7 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
9866648a241bff309de8a081776f4d556cf3c302da04227215779ea7d002232b
BLAKE2b-256 checksum
How to use checksums
700a2de33736b412560b3f0dfcd09baa957c9498555c9e6d93cab0a2f1fffde6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / pydecisiongraph-0.3.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pydecisiongraph-0.3.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 9.8 MB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
f08f5708363105051bfd2b1d78b16cb6a41a566f534133d5eb0bdb2a689cee66
BLAKE2b-256 checksum
How to use checksums
e95c354fe49b5669651616fb2bd907f24e517de0496a8f7d6eb020751be8d755
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Security for PyDecisionGraph 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page