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

Publishing to PyPI

This repository publishes to PyPI using GitHub Trusted Publishing.

Release workflow:

  1. Update src/biosim/__about__.py with a new version (example: 0.0.2).
  2. Commit and push the version bump to main.
  3. Create and push a matching tag (v0.0.2).
  4. Verify GitHub Actions Publish to PyPI finishes successfully.

Manual commands:

git add src/biosim/__about__.py
git commit -m "Bump version to 0.0.2"
git push origin main
git tag v0.0.2
git push origin v0.0.2

Automation script:

bash scripts/release_pypi.sh
  • This reads the version from src/biosim/__about__.py, requires a clean git tree, pushes main, then creates and pushes v<version>.
  • You can also pass an explicit version: bash scripts/release_pypi.sh 0.0.2.
  • Use --no-push to only create the local tag: bash scripts/release_pypi.sh --no-push.

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 BioSignal, SignalMetadata

class Counter(biosim.BioModule):
    min_dt = 0.1

    def __init__(self):
        self.value = 0

    def reset(self) -> None:
        self.value = 0

    def advance_to(self, t: float) -> None:
        self.value += 1

    def get_outputs(self):
        source = getattr(self, "_world_name", "counter")
        return {
            "count": BioSignal(
                source=source,
                name="count",
                value=self.value,
                time=0.0,
                metadata=SignalMetadata(units="1", description="tick counter"),
            )
        }

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

Visuals from Modules

Modules may optionally expose web-native 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):
    min_dt = 0.1

    def advance_to(self, t: float) -> None:
        return

    def get_outputs(self):
        return {}

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

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

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

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()
      ui = Interface(
          world,
          controls=[Number("duration", 10), Number("tick_dt", 0.1), 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)
      • 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.
  • 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 }, ...] }
    • table: data = { "columns": [..], "rows": [[..], ...] } or data = { "items": [{...}, ...] }
    • image: data = { "src": str, "alt"?: str, "width"?: number, "height"?: number }
    • scatter: scatter plot data
    • heatmap: matrix/heatmap data
    • graph: placeholder renderer shows counts + JSON; richer graph lib can be added later
    • custom:<type>: custom renderer namespace for user-defined types
    • unknown types: rendered as JSON fallback
  • VisualSpec may also include an optional description (string) for hover text or captions.

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.1.tar.gz (260.4 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.1-py3-none-any.whl (248.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for biosim-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3ef1be3f39d68f4ae2af54c761072775fa8ea057250cdd631612c4ffe18a4d17
MD5 0d0900b5abda272be93997fae3671dc3
BLAKE2b-256 b484aff99ba0bd3c39fda242ec73f0b7600faffb7a4febded0988b3481bb4f4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for biosim-0.0.1.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.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for biosim-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f2b7ead571920ac98bf2352e4a4e44e166398d5e21d3cad203cf65aa3204690
MD5 d5982ae6e428baa8b9e59b0ce55e677d
BLAKE2b-256 1b06b6a56d37784cb73533fac07ed41afa99af34eb1a8a20752611ea8d63e137

See more details on using hashes here.

Provenance

The following attestation bundles were made for biosim-0.0.1-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