Skip to main content

Convert Python ODE systems to SBML (Level 3 Version 2) and PEtab bundles

Project description

Convert Python ODE systems to SBML (Level 3 Version 2) and PEtab bundles.

CI Python 3.10+ License: MIT PyPI Coverage

ode2sbml is a Python package that converts ordinary differential equation (ODE) systems written in Python into valid SBML Level 3 Version 2 files and optionally PEtab bundles for parameter estimation.


Installation

pip install ode2sbml

Or install from source:

git clone https://github.com/bibymaths/ode2sbml.git
cd ode2sbml
pip install -e .

Dependencies (installed automatically):

  • python-libsbml >= 5.20
  • sympy >= 1.12
  • sbmlmath >= 0.3
  • petab >= 0.4
  • click >= 8.0
  • rich >= 13.0
  • numpy >= 1.24
  • pydantic >= 2.0

Quick Start

Convention A — scipy-style function

from ode2sbml import from_function, to_sbml

def lotka_volterra(t, y, p):
    x, y_val = y
    alpha, beta, delta, gamma = p
    dx = alpha * x - beta * x * y_val
    dy = -gamma * y_val + delta * x * y_val
    return [dx, dy]

model = from_function(
    func=lotka_volterra,
    state_vars=["x", "y"],
    param_names=["alpha", "beta", "delta", "gamma"],
    initial_conditions={"x": 10.0, "y": 5.0},
    parameter_values={"alpha": 1.1, "beta": 0.4, "delta": 0.1, "gamma": 0.4},
    model_id="lotka_volterra",
    model_name="Lotka-Volterra",
)

to_sbml(model, "lotka_volterra.xml")

Convention B — dict-of-formulas

from ode2sbml import from_dict, to_sbml

model_spec = {
    "species": {"S": 990.0, "I": 10.0, "R": 0.0},
    "parameters": {"beta": 0.3, "gamma": 0.1},
    "odes": {
        "S": "-beta*S*I",
        "I": "beta*S*I - gamma*I",
        "R": "gamma*I",
    },
    "compartments": {"host": 1.0},
}

model = from_dict(model_spec, model_id="sir", model_name="SIR Epidemic Model")
to_sbml(model, "sir_model.xml")

Convention C — SymPy symbolic system

import sympy as sp
from ode2sbml import from_sympy, to_sbml

x, y, z = sp.symbols("x y z")
sigma, rho, b = sp.symbols("sigma rho b")

odes = {
    x: sigma * (y - x),
    y: x * (rho - z) - y,
    z: x * y - b * z,
}
params = {sigma: 10.0, rho: 28.0, b: 8.0 / 3.0}
inits = {x: 0.0, y: 1.0, z: 0.0}

model = from_sympy(odes=odes, params=params, inits=inits,
                   model_id="lorenz", model_name="Lorenz System")
to_sbml(model, "lorenz.xml")

PEtab Export

from ode2sbml import from_dict, to_petab

model = from_dict(model_spec, model_id="sir", model_name="SIR")
yaml_path = to_petab(model, outdir="petab_bundle/", observables={
    "obs_I": {"formula": "I", "noise_formula": "0.1 * I"},
})
print(f"PEtab bundle written. Manifest: {yaml_path}")

CLI

# Convert a Python model file to SBML
ode2sbml convert model.py --output model.xml --format sbml

# Convert to PEtab bundle
ode2sbml convert model.py --output ./bundle/ --format petab

# Validate an SBML file
ode2sbml validate model.xml

Supported SBML Features

Feature Supported
SBML Level 3 Version 2
Compartments
Species (state variables)
Parameters
Rate rules (ODEs)
Assignment rules
Events (with triggers)
Unit definitions
Model notes
MathML via sbmlmath
libsbml consistency check
PEtab bundle export
JSON serialization (IR)
Reactions / stoichiometry ❌ (use RateRules)
Spatial SBML

API Reference

Parsers

  • from_function(func, state_vars, param_names, initial_conditions, parameter_values, ...) — Convention A
  • from_dict(model_dict, ...) — Convention B
  • from_sympy(odes, params, inits, ...) — Convention C

Exporters

  • to_sbml(model, filepath) — Write SBML Level 3 Version 2 XML
  • to_petab(model, outdir, observables, ...) — Write PEtab bundle

Validators

  • validate_sbml_file(filepath) — Validate from file
  • validate_sbml_string(xml_str) — Validate from string
  • validate_sbml_doc(doc) — Validate libsbml document

IR Classes

  • ODEModel, Compartment, Species, Parameter, RateRule, AssignmentRule, EventTrigger

References


License

MIT — see LICENSE.

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

ode2sbml-0.1.2.tar.gz (700.8 kB view details)

Uploaded Source

Built Distribution

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

ode2sbml-0.1.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file ode2sbml-0.1.2.tar.gz.

File metadata

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

File hashes

Hashes for ode2sbml-0.1.2.tar.gz
Algorithm Hash digest
SHA256 edbdeb0fcb303f4567f22e1e811c1602de086d51720b5352bb2502ac0ebd395b
MD5 61c146b00e87168ae11c320eb648b5cc
BLAKE2b-256 2e9e62ce45a751097f6cef76ac1f3d3075896151c056f9f5f7c95511301e587c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ode2sbml-0.1.2.tar.gz:

Publisher: publish.yml on bibymaths/ode2sbml

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

File details

Details for the file ode2sbml-0.1.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for ode2sbml-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b5362e99afb6a895130ca12b949e80b0dc35157246d279419bbe482e539bec8a
MD5 73144f5104b28ca6fbc837b875a4593f
BLAKE2b-256 c916f6a1e88358aff7c7f6bf1ad7f8a65d54e316c673bc54863b5819e6355df5

See more details on using hashes here.

Provenance

The following attestation bundles were made for ode2sbml-0.1.2-py3-none-any.whl:

Publisher: publish.yml on bibymaths/ode2sbml

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