Skip to main content

Basin stability estimation for dynamical systems

Project description

pybasin

Basin stability estimation for dynamical systems

Python Version License: GPL v3

pybasin is a Python library for estimating basin stability in dynamical systems. Given an ODE and a sampling region, it integrates thousands of initial conditions, extracts time-series features, and clusters the resulting trajectories to determine what fraction of the phase space leads to each attractor. The library is a modern port of the MATLAB bSTAB toolbox, extended with GPU acceleration, automated feature extraction, and parameter studies.

Features

  • Basin stability estimation with configurable sample counts and sampling strategies
  • Multiple ODE solver backends -- TorchDiffEqSolver (default), JaxSolver (GPU-optimized, optional), TorchOdeSolver, ScipyParallelSolver
  • Automated feature extraction from trajectories (PyTorch-based, plus optional tsfresh and nolds)
  • Unsupervised and supervised classification -- HDBSCAN clustering out of the box, or bring any scikit-learn estimator
  • Parameter studies -- sweep ODE parameters and track how basin stability changes
  • Visualization -- Matplotlib plotting included; interactive Dash/Plotly plotter available via pybasin[interactive]
  • Disk caching -- solver results are cached with safetensors for fast reruns
  • Full type annotations with py.typed marker

Installation

pip install pybasin

This installs the core library with TorchDiffEqSolver as the default backend. Optional extras unlock additional features:

Extra Command Adds
jax pip install pybasin[jax] JaxSolver -- fastest on GPU, event functions
interactive pip install pybasin[interactive] Dash/Plotly interactive plotter
tsfresh pip install pybasin[tsfresh] tsfresh feature extractor
nolds pip install pybasin[nolds] Nonlinear dynamics features (Lyapunov, correlation dimension)
torchode pip install pybasin[torchode] torchode solver (per-trajectory step sizes)
all pip install pybasin[all] Everything above

When JAX is installed and the ODE inherits from JaxODESystem, BasinStabilityEstimator automatically picks JaxSolver. If the ODE inherits from ODESystem (PyTorch), TorchDiffEqSolver is always the default.

Quick Start

Define an ODE, create a sampler, and run the estimator. Three objects are all you need:

import torch
from typing import TypedDict
from pybasin import BasinStabilityEstimator
from pybasin.ode_system import ODESystem
from pybasin.sampler import UniformRandomSampler


class DuffingParams(TypedDict):
    delta: float
    k3: float
    A: float


class DuffingODE(ODESystem[DuffingParams]):
    def ode(self, t: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        x, x_dot = y[..., 0], y[..., 1]
        delta, k3, A = self.params["delta"], self.params["k3"], self.params["A"]
        return torch.stack([x_dot, -delta * x_dot - k3 * x**3 + A * torch.cos(t)], dim=-1)


ode = DuffingODE({"delta": 0.08, "k3": 1.0, "A": 0.2})
sampler = UniformRandomSampler(min_limits=[-1, -0.5], max_limits=[1, 1])

bse = BasinStabilityEstimator(ode_system=ode, sampler=sampler, n=5000)
basin_stability = bse.run()
print(basin_stability)
# {'attractor_0': 0.52, 'attractor_1': 0.48}

Solver, feature extractor, and clusterer all have defaults -- you only configure what you need to change. For the full tutorial, see the Quick Start guide.

Documentation

Full documentation: https://adrianwix.github.io/pybasin/

Covers installation extras, solver comparison, feature extraction, parameter studies, and the complete API reference.

Case Studies

Validated against the original MATLAB bSTAB implementation:

Case Study Description
Duffing Oscillator Forced oscillator with bistability
Lorenz System Classic chaotic attractor
Pendulum Forced pendulum with bifurcations
Friction System Mechanical system with friction
Rossler Network Coupled oscillators synchronization

Each case study lives under case_studies/ and can be run with:

uv run python -m case_studies.pendulum.main_pendulum_case1

Project Structure

root/
├── src/pybasin/          # Published library
├── src/zigode/           # Zig-compiled ODE solver (separate package)
├── thesis_utils/         # Thesis-specific plotting (not part of published package)
├── case_studies/         # Research case studies
├── tests/                # Unit and integration tests
├── docs/                 # Documentation source (MkDocs)
├── benchmarks/           # Performance benchmarks
└── scripts/              # Helper scripts

Development

git clone https://github.com/adrianwix/pybasin.git
cd pyBasinWorkspace
uv sync --all-groups

This installs all dependencies (core + dev + docs + case studies + experiments) into a virtual environment managed by uv. Run the test suite and linters with:

uv run pytest                # tests
sh scripts/ci.sh             # ruff + pyright

For the full contributor workflow, see the Contributing guide.

Academic Context

pybasin is the main contribution of the bachelor thesis "Pybasin: A Python Toolbox for Basin Stability of Multi-Stable Dynamical Systems." It ports and extends the MATLAB bSTAB library with GPU acceleration, automated feature extraction, and modern Python packaging.

Citation

@software{pybasin2025,
  author = {Wix, Adrian},
  title = {Pybasin: A Python Toolbox for Basin Stability of Multi-Stable Dynamical Systems},
  year = {2025},
  url = {https://github.com/adrianwix/pybasin}
}

License

GPL-3.0 -- see LICENSE for details.

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

pybasin-0.2.0.tar.gz (164.1 kB view details)

Uploaded Source

Built Distribution

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

pybasin-0.2.0-py3-none-any.whl (218.6 kB view details)

Uploaded Python 3

File details

Details for the file pybasin-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for pybasin-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8abcc5dd7e31dc73e6770bd9af3bcaa16196342dfb164ea119038faa838ab716
MD5 0a841ebd3deca83f8667512d7a265280
BLAKE2b-256 bea928542f90b61ab8411bce3aaa16914d96398126be221ff608821c96549624

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybasin-0.2.0.tar.gz:

Publisher: publish.yml on adrianwix/pybasin

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

File details

Details for the file pybasin-0.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pybasin-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d96cf551084f79c0bd57872ce96bd8d641ed480492a0dcc567b8e54dca4116b
MD5 0cf73a9cf70821fa2e38302127b7ebe1
BLAKE2b-256 6d1793963b9285ab2478a03e8eaa6571c0efce326175ed65e295ebe0ddb26547

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybasin-0.2.0-py3-none-any.whl:

Publisher: publish.yml on adrianwix/pybasin

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