Skip to main content

Stochastic simulation of biochemical reaction networks with noise

Project description

bcrnnoise

pre-commit static analysis workflow

A Python library for simulating Bio-Chemical Reaction Networks (BCRNs) with noise. Supports deterministic ODE integration, exact Markov chain simulation via Gillespie's algorithm, and stochastic SDE simulation via Euler-Maruyama — including a vectorised batch mode for efficient multi-trajectory sampling.

Available on PyPI:

pip install bcrnnoise

Overview

Models are defined by subclassing BCRN and implementing two things:

  • stoichiometry — numpy array of shape (n_reactions, n_species) giving net stoichiometric changes per reaction
  • reaction_rates(state) — returns per-reaction propensities as list[Quantity]

All quantities carry physical units via pint, so dimensional errors are caught at runtime rather than buried in wrong results.

Simulation modes

Method Type Description
simulate_ode() deterministic Integrates the mean-field ODE via scipy.solve_ivp
simulate_markov_chain(seed) stochastic Exact sample path via Gillespie's algorithm
simulate_sde(noise_fun, seed) stochastic Euler-Maruyama with custom additive noise
simulate_sde_batch(noise_fun, n, seed) stochastic Vectorised Euler-Maruyama for n trajectories simultaneously

All methods return a Timeseries (or BatchTimeseries for the batch variant) with a shared time axis and unit-carrying states.

Quick start

from collections.abc import Sequence
import numpy as np
from pint import Quantity, UnitRegistry
from bcrnnoise import BCRN, plot_timeseries

u = UnitRegistry()

class TranscriptionSystem(BCRN):
    """0 -> mRNA [alpha],  mRNA -> 0 [delta]"""

    def __init__(self, alpha, delta, **kwargs):
        super().__init__(**kwargs)
        self._alpha, self._delta = alpha, delta

    @property
    def stoichiometry(self) -> np.ndarray:
        return np.array([[1], [-1]])

    def reaction_rates(self, state: Sequence[Quantity]) -> list[Quantity]:
        return [self._alpha, self._delta * state[0]]

sys = TranscriptionSystem(
    alpha=1.0 / u.minute / u.femtoliter,
    delta=0.1 / u.minute,
    init_state=[0.0 / u.femtoliter],
    volume=1.0 * u.femtoliter,
    time_horizon=200.0 * u.minute,
    dt=0.1 * u.minute,
)

# Deterministic
ode_ts = sys.simulate_ode()

# Exact stochastic
mc_ts = sys.simulate_markov_chain(seed=0)

# SDE with Gaussian noise
sigma = 5.0 / u.femtoliter / u.minute**0.5
def gaussian_noise(rng, t, state):
    return [sigma * np.sqrt(sys.dt) * rng.normal()]

sde_ts = sys.simulate_sde(noise_fun=gaussian_noise, seed=0)

plot_timeseries([ode_ts, mc_ts, sde_ts], labels=["ODE", "Markov chain", "SDE"])

Batch SDE simulation

For multi-trajectory studies, simulate_sde_batch runs n trajectories in a single vectorised loop — roughly n× faster than calling simulate_sde repeatedly:

def gaussian_batch_noise(rng, t, states):
    n = states[0].magnitude.shape[0]
    return [sigma * np.sqrt(sys.dt) * rng.normal(size=n)]

batch = sys.simulate_sde_batch(noise_fun=gaussian_batch_noise, n=500, seed=0)

# Access individual trajectory
ts_42 = batch.trajectory(42)

# Unpack all at once (single pass, more efficient than looping trajectory())
all_ts = batch.to_timeseries_list()

The noise function receives and returns list[Quantity] where each Quantity wraps a (n,) numpy array. Any reaction_rates implementation that uses only numpy-compatible arithmetic works in batch mode without modification.

Examples

Citation

@misc{bcrnnoise:github,
  author       = {Arman Ferdowsi and Mattias F{\"u}gger and Thomas Nowak},
  title        = {bcrnnoise: simulate Bio-Chemical Reaction Networks with noise},
  year         = {2025},
  howpublished = {\url{https://github.com/BioDisCo/bcrnnoise}},
}

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

bcrnnoise-3.0.6.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

bcrnnoise-3.0.6-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file bcrnnoise-3.0.6.tar.gz.

File metadata

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

File hashes

Hashes for bcrnnoise-3.0.6.tar.gz
Algorithm Hash digest
SHA256 475eb49a0cf9d563436bd076f98aa22904180ce456023d2f72bea63fbabd59ea
MD5 8f10a252b03e5ec8e00132ccce7356e6
BLAKE2b-256 01b6fd7a63b7cb97157876105ffe4f20afa8839562e3a49fe4187105b99da5bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for bcrnnoise-3.0.6.tar.gz:

Publisher: publish.yaml on BioDisCo/bcrnnoise

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

File details

Details for the file bcrnnoise-3.0.6-py3-none-any.whl.

File metadata

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

File hashes

Hashes for bcrnnoise-3.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 64d3e34c4522fc3b28b9999790702f6fc77502849d6b1d120c6f034d54aa8156
MD5 14fcbf5f0a01fe4be9f6c42cea15982e
BLAKE2b-256 620a4b56d98bc979cdc254bcfa4a62fef9f418c1b0ed59fe3dd871c4c4336893

See more details on using hashes here.

Provenance

The following attestation bundles were made for bcrnnoise-3.0.6-py3-none-any.whl:

Publisher: publish.yaml on BioDisCo/bcrnnoise

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