Metadata-Version: 2.4
Name: crn-craft
Version: 0.10.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Classifier: Topic :: Scientific/Engineering :: Chemistry
Requires-Dist: numpy>=1.24,<3
Requires-Dist: pandas>=2.0,<4
Requires-Dist: xarray>=2024.6
Requires-Dist: scipy>=1.12,<2 ; extra == 'likelihood'
Requires-Dist: scipy==1.17.0 ; extra == 'fit'
Requires-Dist: fcmaes-rust==0.4.0 ; extra == 'fit'
Requires-Dist: sqp-craft==0.17.0 ; extra == 'fit'
Requires-Dist: fcmaes==2.2.0 ; extra == 'publications'
Requires-Dist: scipy>=1.12,<2 ; extra == 'publications'
Requires-Dist: matplotlib>=3.8,<4 ; extra == 'publications'
Requires-Dist: pyyaml>=6,<7 ; extra == 'publications'
Requires-Dist: python-libsbml>=5.20,<6 ; extra == 'publications'
Requires-Dist: xarray>=2024.6 ; extra == 'xarray'
Provides-Extra: likelihood
Provides-Extra: fit
Provides-Extra: publications
Provides-Extra: xarray
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
Summary: Reproducible well-mixed reaction network simulation and analysis
Keywords: chemical reaction networks,stochastic simulation,Gillespie,SSA,systems biology,Rust
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://dietmarwo.github.io/CRN-craft/
Project-URL: Issues, https://github.com/dietmarwo/CRN-craft/issues
Project-URL: Changelog, https://github.com/dietmarwo/CRN-craft/blob/main/CHANGELOG.md
Project-URL: Optimization ecosystem, https://dietmarwo.github.io/optimization-jobs/
Project-URL: AI agent guide, https://github.com/dietmarwo/CRN-craft/blob/main/docs/developer/ai-context.md
Project-URL: Repository, https://github.com/dietmarwo/CRN-craft

# CRN-craft

<!-- check: standalone -->

![CRN-craft: chemical reaction networks in native Rust with stochastic simulation](https://raw.githubusercontent.com/dietmarwo/CRN-craft/main/docs/images/crn-craft-banner.jpg)

CRN-craft simulates chemical reaction networks stochastically, from Python,
with a native engine. Models, requests and results are versioned JSON
documents with strict input checks, explicit budgets and replayable seeds.

```sh
pip install crn-craft
```

Wheels for CPython 3.10 and newer (stable ABI) on Linux x86_64, Windows x64
and macOS (x86_64 and arm64); other platforms build from the source
distribution with a Rust toolchain.

## Example

Immigration–death (birth rate 4, death rate 0.2, starting from zero) has a
Poisson law with mean `20 (1 - exp(-0.2 t))`: 12.64 at t = 5 and 17.29 at
t = 10. A model is built from Python calls; a number is a mass-action
constant, a string the reaction's whole propensity.

```python
import crn_craft as crn

model = crn.Model("immigration_death")
model.add_parameter("k_in", 4.0).add_species("X")
model.add_reaction("k_in", [], ["X"], name="immigration")  # string: whole propensity
model.add_reaction(0.2, ["X"], [], name="death")  # number: mass action, 0.2 X
result = crn.ensemble(
    model,
    end=10.0,
    report_times=[0.0, 5.0, 10.0],
    replicates=1000,
    seed=1,
    threads=2,
    method="static_direct",
    keep_paths="all",
).simulate()
print(result.metadata["sampler"])
print(result.statistics["mean"])
print(result.statistics["median"])
print(result.rule_events.sum().to_dict())
print(result.paths.sel(variable="X").to_pandas().head(3))  # cells x times
```

```text
chacha8-u53-ziggurat/rules-static-direct/1
           X
time        
0.0    0.000
5.0   12.623
10.0  17.305
         X
time      
0.0    0.0
5.0   12.0
10.0  17.0
{'immigration': 39800, 'death': 22495}
time       0.0   5.0   10.0
replicate                  
0           0.0  14.0  19.0
1           0.0  19.0  20.0
2           0.0  17.0  17.0
```

The means lie within one standard error of the exact 12.64 and 17.29. The same
seed gives the same result on every run and for every thread count.
`result.statistics` also holds the median and the 2.5% and 97.5% quantiles,
`result.save("run.md")` writes the model and the results to a markdown report
(`.json` writes a JSON report), and results are pandas DataFrames: per-replicate
records and rule counts, statistics by report time and trackers. Kept paths and
event logs are xarray DataArrays with the replicate first.

The same model has an exact distribution, from finite state projection, and a
structural analysis:

```python
import crn_craft as crn

model = crn.Model("immigration_death")
model.add_parameter("k_in", 4.0).add_species("X")
model.add_reaction("k_in", [], ["X"], name="immigration")
model.add_reaction(0.2, ["X"], [], name="death")
distribution = model.fsp(end=10.0, report_times=[10.0], bounds={"X": 60})
print(distribution)
print(distribution.mean)  # 17.29, the exact mean at t = 10
report = model.analyze()
print(report["schema"], report["analysis"]["conservation"]["status"])
```

```text
FspResult(61 states of X, 1 report times)
species          X
time              
10.0     17.293294
crn-craft.analysis/1.0 exact
```

## What is included

- A model is a `crn-craft.model/1.0` document, a run request is
  `crn-craft.run/1.0`, and the result is `crn-craft.result/1.0`; the Rust
  crate and the `crn-craft` command use the same contracts.
- Exact stochastic simulation: `static_direct` with the event selectors
  `linear`, `composition_rejection`, `sorting`, `rssa_cr` and `auto`,
  `next_reaction`, and the exact `tree` method for models whose species carry
  attributes (clones, sizes, lineages).
- Explicitly approximate methods: Poisson leaping (`tau`), fixed splitting
  (`binomial`) and `hybrid` for models with continuous variables.
- Prepared runs and parameter binding for optimization loops
  (`run.bind_parameters(...)`), trackers, event logs and callbacks, and
  an opt-in compiled event loop (`crn.compile_loop`, needs a Rust toolchain).
- Exact distributions (`Model.fsp`), network analysis and conservation laws
  (`Model.analyze`), GillesPy2 export (`Model.to_gillespy2`), and strict
  SBML and BioNetGen import
  (`crn.read_sbml`, `crn.read_bngl`, `crn-craft-import`).
- Quantile statistics and the median, and `Result.save` to markdown or JSON.
- The `crn-craft` command for `check`, `run`, `verify` (sealed archives),
  `analyze`, `convert` and `export`. The native command-line tool
  (`cargo install crn-craft-cli --locked`) has the same commands.
- `pip install "crn-craft[fit]"` adds the pinned SciPy, fcmaes-rust and
  SQP-craft versions used by the fitting examples.
- `pip install "crn-craft[publications]"` adds fcmaes, SciPy, matplotlib,
  PyYAML and libSBML for the optimization lessons C5 to C10 of the
  repository.
- `pip install "crn-craft[likelihood]"` adds SciPy for the
  binomial-detection and fluorescence readouts of `crn_craft.likelihood`.

ODE right-hand sides and Jacobians are in the Rust crate
(`crn_craft::kinetics`). The optional diffsol BDF integrator with forward
sensitivities and the job worker for
[optimization-jobs](https://github.com/dietmarwo/optimization-jobs) build from
the repository and are not part of the wheel.

## Documentation

- [Documentation site](https://dietmarwo.github.io/CRN-craft/): the guide, reference and evidence, with search
- [How CRN-craft compares](https://dietmarwo.github.io/CRN-craft/comparison.html) with GillesPy2 and rebop
- [User guide](https://dietmarwo.github.io/CRN-craft/guide/01-install.html): read chapters 1 to 4 first
- [How do I ...?](https://dietmarwo.github.io/CRN-craft/howto.html): fifty tasks, each with a link to the answer
- [Case study: mtDNA mutants](https://dietmarwo.github.io/CRN-craft/tutorials/simulation/S1-mtdna.html)
- [Reference](https://dietmarwo.github.io/CRN-craft/reference/model-language.html): model language, methods, Python API, contracts, diagnostics
- [Changelog](https://github.com/dietmarwo/CRN-craft/blob/main/CHANGELOG.md)

Release 0.7.0 merged two earlier engines into one; `crn_craft.rules` keeps
the API of release 0.6.0 with its defaults, and the changelog says how to
migrate. A successful numerical run is not biological validation. MIT
licensed; see [THIRD_PARTY_NOTICES.md](https://github.com/dietmarwo/CRN-craft/blob/main/THIRD_PARTY_NOTICES.md)
for the Ziggurat tables adapted from Julia.

