Skip to main content

Core simulation library for Biosimulant

Project description

biosim

PyPI - Version PyPI - Python Version

Composable simulation runtime + UI layer for orchestrating runnable biomodules.


Executive Summary & System Goals

Vision

Provide a small, stable composition layer for simulations: wire reusable components ("biomodules") into a BioWorld, run them with a single orchestration contract, and visualize/debug runs via a lightweight web UI (SimUI). Biomodules are self-contained Python packages that can wrap external simulators internally (SBML/NeuroML/CellML/etc.) without a separate adapter layer.

Core Mission

  • Compose simulations from reusable, interoperable biomodules.
  • Make "run + visualize + share a config" the default workflow (local-first; hosted later).
  • Keep the runtime small and predictable while letting biomodules embed their own simulator/tooling.

Primary Users

  • Developers and researchers who need composable simulation workflows and fast iteration.
  • Near-term beachhead: neuroscience demos (single neuron + small E/I microcircuits) with strong visuals and reproducible configs.

Installation

Preferred (pinned GitHub ref):

pip install "biosim @ git+https://github.com/<org>/biosim.git@<ref>"

Alternative (package index):

pip install biosim

For the shared ONNX biomodule helpers:

pip install "biosim[ml]"

Publishing to PyPI

See the release guide: docs/releasing.md.

Packaging Models And Labs

biosim can package one model or one lab into a single archive for portability, upload, caching, and validation.

Common commands:

# Build a package from a directory that contains model.yaml or lab.yaml
python -m biosim pack build path/to/model-or-lab

# Validate an existing package file
python -m biosim pack validate dist/local__counter-1.0.0.bsimpkg

# Build a self-contained lab package (.bsilab)
python -m biosim pack build path/to/lab

Notes:

  • build prefers package: and version: from model.yaml or lab.yaml when present.
  • model dependencies in manifests must use exact == pins.
  • lab builds are always self-contained and preserve the full runnable source tree inside the .bsilab.
  • nested lab dependencies must use relative path refs and must already exist inside the packaged lab directory.
  • validate prints human-readable success or failure output by default; add --json for machine-readable output.

See docs/packaging.md for the full package layout, recommended authoring flow, and CLI examples.

Examples

  • See examples/ for quick-start scripts. Try:
pip install -e .
python examples/basic_usage.py

For advanced curated demos (neuro/ecology), wiring configs, and model-pack templates, see the companion repo:

Quick Start: BioWorld

Minimal usage:

import biosim
from biosim import ScalarSignal, SignalSpec


class Counter(biosim.BioModule):
    def __init__(self):
        self.value = 0
        self._t = 0.0

    def outputs(self):
        return {"count": SignalSpec.scalar(dtype="int64", emitted_unit="1")}

    def advance_window(self, start: float, end: float) -> None:
        _ = start
        self.value += 1
        self._t = end

    def get_outputs(self):
        return {
            "count": ScalarSignal(
                source="counter",
                name="count",
                value=self.value,
                emitted_at=self._t,
                spec=self.outputs()["count"],
            )
        }

    def snapshot(self) -> dict:
        return {"value": self.value, "t": self._t}

    def restore(self, snapshot: dict) -> None:
        self.value = int(snapshot.get("value", 0))
        self._t = float(snapshot.get("t", 0.0))


world = biosim.BioWorld(communication_step=0.1)
world.add_biomodule("counter", Counter())
world.run(duration=1.0)

Visuals from Modules

Modules may optionally expose visuals via visualize(), returning a dict or list of dicts with keys render and data. The world can collect them without any transport layer:

class MyModule(biosim.BioModule):
    def advance_window(self, start: float, end: float) -> None:
        _ = start, end

    def get_outputs(self):
        return {}

    def snapshot(self) -> dict:
        return {}

    def restore(self, snapshot: dict) -> None:
        _ = snapshot

    def visualize(self):
        return {
            "render": "timeseries",
            "data": {"series": [{"name": "s", "points": [[0.0, 1.0]]}]},
        }

world = biosim.BioWorld(communication_step=0.1)
world.add_biomodule("module", MyModule())
world.run(duration=0.1)
print(world.collect_visuals())  # [{"module": "module", "visuals": [...]}]

See examples/visuals_demo.py for a minimal end-to-end example.

ONNX Modules

biosim can host ONNX-backed modules without changing the core runtime. Install the ML extras and wrap the ONNX model behind the standard BioModule interface:

from biosim import OnnxClassifierModule, ScalarSignal, SignalSpec

classifier = OnnxClassifierModule(
    model_path="artifacts/model.onnx",
    class_labels=["quiescent", "subthreshold", "spiking"],
    input_port="state_vector",
    probabilities_port="state_probabilities",
    predicted_port="predicted_state",
    input_vector_length=4,
)

classifier.set_inputs(
    {
        "state_vector": ScalarSignal(
            source="adapter",
            name="state_vector",
            value=-64.0,
            emitted_at=0.0,
            spec=SignalSpec.scalar(dtype="float64"),
        )
    }
)
classifier.advance_window(0.0, 0.001)
print(classifier.get_outputs()["predicted_state"].value)

Model packs can subclass OnnxClassifierModule to set model-relative model_path, port names, and label sets while keeping the inference logic in the shared library.

SimUI (Python-Declared UI)

SimUI lets you build and launch a small web UI entirely from Python (similar to Gradio's ergonomics), backed by FastAPI and a prebuilt React SPA that renders visuals from JSON. The frontend uses Server-Sent Events (SSE) for real-time updates.

  • User usage (no Node/npm required):
    • Install UI extras: pip install -e '.[ui]'

    • Try the demo: python examples/ui_demo.py then open http://127.0.0.1:7860/ui/.

    • From your own code:

      from biosim.simui import Interface, Number, Button, EventLog, VisualsPanel
      world = biosim.BioWorld(communication_step=0.1)
      ui = Interface(
          world,
          controls=[Number("duration", 10), Button("Run")],
          outputs=[EventLog(), VisualsPanel()],
      )
      ui.launch()
      
    • The UI provides endpoints under /ui/api/...:

      • GET /api/spec – UI layout (controls, outputs, modules)
      • POST /api/run – Start a simulation run
      • GET /api/status – Runner status (running/paused/error + optional progress fields)
      • GET /api/state – Full state (status + last step + modules)
      • GET /api/events – Buffered world events (?since_id=&limit=)
      • GET /api/visuals – Collected module visuals
      • GET /api/snapshot – Full snapshot (status + visuals + events)
      • GET /api/stream – SSE endpoint for real-time event streaming
      • POST /api/pause – Pause running simulation
      • POST /api/resume – Resume paused simulation
      • POST /api/reset – Stop, reset, and clear buffers
      • Editor sub-API (/api/editor/...): visual config editor for loading, saving, validating, and applying YAML wiring configs as node graphs. Endpoints include modules, current, config, apply, validate, layout, to-yaml, from-yaml, and files.

Per-run resets for clean visuals

  • On each Run, the backend clears its event buffer and calls reset() on modules if they implement it.

  • The frontend clears visuals/events before posting /api/run.

  • To avoid overlapping charts across runs, add reset() to modules that accumulate history (e.g., time series points).

  • Maintainer flow (building the frontend SPA):

    • Edit the React/Vite app under src/biosim/simui/_frontend/.
    • Build via Python: python -m biosim.simui.build (requires Node/npm). This writes src/biosim/simui/static/app.js.
    • Alternatively: bash scripts/build_simui_frontend.sh.
    • Packaging includes src/biosim/simui/static/**, so end users never need npm.
  • CI packaging (recommended): run the frontend build before python -m build so wheels/sdists ship the bundled assets.

Troubleshooting:

  • If you see SimUI static bundle missing at .../static/app.js, build the frontend with python -m biosim.simui.build (requires Node/npm) before launching. End users installing a release wheel won't see this.

SimUI Design Notes

  • Transport: SSE (Server-Sent Events). The SPA connects to /api/stream for real-time updates. Polling endpoints (/api/status, /api/visuals, /api/events) remain available for fallback/debugging.
  • Objective progress fields are based on simulation-time progress ((sim_time - sim_start) / duration), not wall-clock time.
  • /api/status may include: sim_time, sim_start, sim_end, sim_remaining, progress, progress_pct (all optional/additive).
  • Events API: /api/events?since_id=<int>&limit=<int> returns { events, next_since_id } where events are appended world events and next_since_id is the cursor for subsequent calls.
  • VisualSpec types supported now:
    • timeseries: data = { "series": [{ "name": str, "points": [[x, y], ...] }, ...] }
    • bar: data = { "items": [{ "label": str, "value": number }, ...] }
    • scatter: data = { "points": [{ "x": number, "y": number, "label"?: str, "series"?: str }, ...] }
    • heatmap: data = { "values": [[number, ...], ...], "x_labels"?: [str, ...], "y_labels"?: [str, ...] }
    • table: data = { "columns": [..], "rows": [[..], ...] } or data = { "items": [{...}, ...] }
    • image: data = { "src": str, "alt"?: str, "width"?: number, "height"?: number }
    • graph: simple node-edge graph renderer
    • structure3d: data = { "title"?: str, "source": { "kind": "url", "url": str } | { "kind": "artifact", "artifact_id": str }, "format": "mmcif" | "pdb", "annotations"?: [{ "label": str, "value": str|number|bool }], "initial_view"?: {...} }
  • VisualSpec may also include an optional description (string) for hover text or captions.
  • SimUI serves artifact-backed structure3d files through /api/artifacts/{artifact_id} so browser clients do not receive raw local filesystem paths.

Terminology

Understanding the core concepts is essential for working with biosim effectively.

Term Description
BioWorld Runtime container that orchestrates multi-rate biomodules, routes signals, and publishes lifecycle events.
BioModule Pluggable unit of behavior with local state. Implements the runnable contract (setup/reset/advance_to/...).
BioSignal Typed, versioned data payload exchanged between modules via named ports.
WorldEvent Runtime events emitted by the BioWorld (STARTED, TICK, FINISHED, etc.).
Wiring Module connection graph. Defined programmatically, via WiringBuilder, or loaded from YAML/TOML configs.
VisualSpec JSON structure returned by module.visualize() with render type and data payload.

Event Lifecycle

Every simulation follows this sequence:

STARTED -> TICK (xN) -> FINISHED

PAUSED, RESUMED, STOPPED, and ERROR may also be emitted depending on runtime control flow.

License

MIT. See LICENSE.txt.

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

biosim-0.0.5.tar.gz (255.7 kB view details)

Uploaded Source

Built Distribution

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

biosim-0.0.5-py3-none-any.whl (217.7 kB view details)

Uploaded Python 3

File details

Details for the file biosim-0.0.5.tar.gz.

File metadata

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

File hashes

Hashes for biosim-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f6556dc5ec2c1998d6b8c7b96d2f4a7c03dba027918887473fb296020c5ac899
MD5 caa4e6a5bcacdb2e2cf592f85648027d
BLAKE2b-256 36466efc602d23630ae162d1f1f4ad8fedcb8b6449fbfd9e3b57045b55a62b8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for biosim-0.0.5.tar.gz:

Publisher: publish-pypi.yml on Biosimulant/biosim

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

File details

Details for the file biosim-0.0.5-py3-none-any.whl.

File metadata

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

File hashes

Hashes for biosim-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 68a86c4d0a017f7f2328a50bc67a12fd7cf9bb35dd82c69d9a5d5624c145b202
MD5 0166ecd5123bb5ec052f1fb577d30102
BLAKE2b-256 411dcd7deac7d7942b7f5e9143d1537c3124ac9d912f62aa1c109dfefa98e9b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for biosim-0.0.5-py3-none-any.whl:

Publisher: publish-pypi.yml on Biosimulant/biosim

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