Skip to main content

Chemical reaction networks in JAX: GPU-parallel stochastic simulations.

Project description

crn-jax

CI PyPI Python License: MIT Ruff

Chemical reaction networks in JAX: a lightweight, GPU-optimized Gillespie / SSA simulation library.

Birth-death benchmark: crn-jax on GPU vs GillesPy2 (C++) on CPU.   Linear-cascade benchmark: crn-jax on GPU vs GillesPy2 (C++) on CPU.

☝️ Wall-time to simulate 1,000,000 independent stochastic trajectories — each a full Gillespie run of the reaction network from t=0 to t=20, sampled at 200 time points (CPU vs RTX 5090 GPU).

24 stochastic trajectories of the repressilator (3-species oscillator), one panel per species.

☝️ 24 stochastic trajectories of the repressilator gene circuit, sampled in parallel from a one-line call.

Key features

  • 🎯 Exact Gillespie simulations: an exact implementation of the Gillespie (SSA) algorithm.
  • 🚀 GPU-optimized: run 1M+ independent trajectories on a single GPU with no Python overhead, powered by JAX.
  • 🎛️ Closed-loop simulations: propensity functions take an optional input argument that can vary per-interval and per-replicate, so each of N parallel trajectories can follow its own time-dependent input schedule (useful for closed-loop experiment simulations, RL-style rollouts, …).
  • 🎨 Pre-built suite of common reaction networks: a library of canonical examples already implemented with realistic default (tunable) parameters (useful for benchmarking, prototyping, and getting started).
  • 🧩 Customizable: implementing a new chemical reaction network is easy.

Install

crn-jax depends on jax / jaxlib only.

pip install crn-jax

# with NVIDIA GPU support:
pip install crn-jax "jax[cuda12]"

# with plotting helpers:
pip install "crn-jax[examples]"

# for local development (uses Poetry):
git clone https://github.com/robinhenry/crn-jax && cd crn-jax
poetry install                    # main deps + dev tools
poetry install --with gpu         # add jax[cuda12] on an NVIDIA host

Quickstart

To simulate 32 stochastic trajectories of the repressilator in parallel and plot them:

import jax, jax.numpy as jnp
from crn_jax import models
from crn_jax.plotting import plot_species_trajectories

# Initial counts: (n_replicates, n_species). You always supply x0 yourself.
# We deliberately don't sample initial conditions for users because the sensible
# initial distribution is often problem-specific.
key, k_x0 = jax.random.split(jax.random.PRNGKey(0))
x0 = jax.random.uniform(
    k_x0, (32, len(models.repressilator.SPECIES)), minval=0.0, maxval=50.0,
)

ds = models.sample_trajectories(models.repressilator, key, x0, n_steps=3000, dt=0.1)

plot_species_trajectories(ds, title="Repressilator")  # see the plot at the top of this file

The returned Dataset has the following fields:

  • ds.species: a tuple of species names, e.g. ("A", "B", "C").
  • ds.xs: a 3D array with shape (n_replicates, n_steps, n_species) that contains the full trajectories.
  • ds.X_t, ds.dX: two 2D arrays with shape (n_replicates * n_steps, n_species) that contain the timestamps and deltas of each reactions (useful to use as a training dataset for downstream ML).

Swapping models can be done by replacing models.repressilator with models.toggle_switch, models.incoherent_ffl, etc.

For a complete walkthrough of how to implement your own models, see Examples below.

Pre-built models

crn_jax.models provides a library of canonical reaction networks inspired by the literature, each exposing the same interface.

Currently, these are mostly gene reaction networks (GRNs), but this may evolve over time.

model species reactions shape
birth_death X 2 minimal one-species baseline
single_gene R, P 4 constitutive transcription-translation
negative_autoregulation X 2 Hill-repressed self-feedback
positive_autoregulation X 2 Hill self-activation (Params.bistable() for the bistable regime)
linear_cascade A, B 4 A → B activation cascade
toggle_switch A, B 4 mutual repression
incoherent_ffl A, B, C 6 adaptive / pulse-generating FFL
repressilator A, B, C 6 synthetic oscillator
cca_optogenetic R, P 4 light-driven gene expression (CcaS/CcaR) — input-driven

Override the defaults by passing your own Params values:

ds = models.sample_trajectories(
    models.positive_autoregulation, key, x0,
    params=models.positive_autoregulation.Params(n=3),  # default is n=2
    n_steps=3000, dt=0.1,
)

Examples

The examples/ folder walks through the main features end-to-end:

  • 1_library_examples.ipynb: sample and plot from every pre-built model. Start here.
  • 2_per-replicate_control.ipynb: closed-loop simulations where each replicate gets its own time-varying controller and setpoint.
  • 3_implement_your_own.ipynb: define a custom reaction network from scratch, and optionally use the low-level simulate_interval / simulate_until primitives for finer-grained control (RL-style rollouts, non-uniform time grids).

See Also

  • GillesPy2: C++ optimized Gillespie simulations on CPU.
  • jax-smfsb: JAX implementations of algorithms from the Stochastic Modelling for Systems Biology book.
  • myriad-jax: RL-style decision making fully in JAX, powered by crn-jax at its core.

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

crn_jax-0.3.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

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

crn_jax-0.3-py3-none-any.whl (35.4 kB view details)

Uploaded Python 3

File details

Details for the file crn_jax-0.3.tar.gz.

File metadata

  • Download URL: crn_jax-0.3.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for crn_jax-0.3.tar.gz
Algorithm Hash digest
SHA256 54bc8f3d8be176a3fa83d38eef632821170ab9b48e7bb83e8eab3ab7e27abe65
MD5 92cf2e2334f5d9ec06cd0667c9aa4094
BLAKE2b-256 15025d8c2d1d3a8e2a2b76b21697d59f16f992cbc5b26ddb6f690be8053190fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for crn_jax-0.3.tar.gz:

Publisher: release.yml on robinhenry/crn-jax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file crn_jax-0.3-py3-none-any.whl.

File metadata

  • Download URL: crn_jax-0.3-py3-none-any.whl
  • Upload date:
  • Size: 35.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for crn_jax-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 47a02bc579faed00ae1e03b34525624814f601c51bec0adcaa93c6ea57605d87
MD5 71e208ce410053846b621901e95fe8ae
BLAKE2b-256 d8e6d81b055559859365822e938b2fa8ee974961c55a247b5a819eda959aaa0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for crn_jax-0.3-py3-none-any.whl:

Publisher: release.yml on robinhenry/crn-jax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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