Skip to main content

Composable states with unidirectional value propagation for enumerating combinatorial spaces

Project description

StateTracker

PyPI version Documentation Status License: MIT

StateTracker is a Python library for creating composable states with unidirectional value propagation. It provides a powerful way to enumerate combinatorial spaces through state algebra operations.

Why StateTracker?

StateTracker was developed to support the design of complex DNA sequence libraries (see PoolParty), but it solves a general problem: random access to combinatorial spaces.

If you've ever written nested loops to enumerate a Cartesian product and then wished you could shuffle the order, sample a subset, or split into train/test sets—all while tracking which component indices correspond to each item—StateTracker is for you. Build your combinatorial structure once using state algebra, and StateTracker handles the index math automatically.

Features

  • Composable States: Build complex iteration patterns from simple states
  • Unidirectional Value Propagation: Set child value and parent values update automatically
  • State Algebra: Product (×), sum (+), slice, repeat, shuffle, and synchronize operations
  • Conflict Detection: Automatic detection of conflicting value assignments
  • Tree Visualization: Built-in ASCII tree visualization for debugging

Installation

pip install statetracker

For development:

git clone https://github.com/jbkinney/poolparty-statetracker.git
cd poolparty-statetracker/statetracker
pip install -e ".[dev]"

Quick Start

from statetracker import State, Manager, product, stack, repeat, shuffle, sync

with Manager():
    # Create leaf states
    A = State(num_values=2, name='A')
    B = State(num_values=3, name='B')

    # Combine with product (Cartesian product)
    C = product([A, B])  # 6 values total

    # Iterate and see parent values update
    for value in C:
        print(f"C={value}, A={A.value}, B={B.value}")

Output:

C=0, A=0, B=0
C=1, A=1, B=0
C=2, A=0, B=1
C=3, A=1, B=1
C=4, A=0, B=2
C=5, A=1, B=2

State Operations

Product (Cartesian Product)

with Manager():
    A = State(num_values=2, name='A')
    B = State(num_values=3, name='B')
    C = product([A, B])  # 6 values (2 × 3)

Stack

with Manager():
    A = State(num_values=2, name='A')
    B = State(num_values=3, name='B')
    C = stack([A, B])  # 5 values (2 + 3)

    for _ in C:
        # Only one of A or B is active (non-None) at a time
        print(f"A={A.value}, B={B.value}")

Slicing

with Manager():
    A = State(num_values=10, name='A')
    B = A[2:5]   # Values 2, 3, 4
    C = A[::2]   # Even values: 0, 2, 4, 6, 8
    D = A[::-1]  # Reversed: 9, 8, 7, ..., 0

Repeat

with Manager():
    A = State(num_values=3, name='A')
    B = repeat(A, 4)  # Repeat A four times (12 values)

Shuffle

with Manager():
    A = State(num_values=5, name='A')
    B = shuffle(A, seed=42)  # Randomly permuted order

Synchronize

with Manager():
    A = State(num_values=4, name='A')
    B = State(num_values=4, name='B')
    sync(A, B)  # A and B always have same value

Value Propagation

StateTracker uses unidirectional value propagation. When you set a child state's value, all parent states automatically update:

with Manager():
    A = State(num_values=2, name='A')
    B = State(num_values=3, name='B')
    C = product([A, B])

    C.value = 5  # Set child value
    print(A.value)  # 1 (automatically updated)
    print(B.value)  # 2 (automatically updated)

Visualization

Visualize state dependencies with ASCII trees:

with Manager():
    A = State(num_values=2, name='A')
    B = State(num_values=3, name='B')
    C = product([A, B])
    C.name = 'C'

    C.print_dag(style='minimal')

Output:

C (n=6)
└── [Product]
    ├── A (n=2)
    └── B (n=3)

Documentation

Full documentation is available at statetracker.readthedocs.io.

License

MIT License - see LICENSE for details.

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

statetracker-0.1.0.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

statetracker-0.1.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file statetracker-0.1.0.tar.gz.

File metadata

  • Download URL: statetracker-0.1.0.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for statetracker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a8ce229af1061679e435cc261f834426f87edf5fa847871ff7ce93648b499466
MD5 2a4616f0d10539ff53f7291a6f5f7b7c
BLAKE2b-256 7ff4503c53ab552db40826528b01b5682b2631e975e748821129a520b258e042

See more details on using hashes here.

File details

Details for the file statetracker-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: statetracker-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for statetracker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b092e61b007faf6171348c215e47c73afd736675663523220deb4c2e1f637f84
MD5 348ff01a128305001b09e1d62bed92d3
BLAKE2b-256 2d3ad17e9f13958565baa03b6acbab3de197daef12c5754d1934fe29bbc8cea5

See more details on using hashes here.

Supported by

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