Backends ​
livn supports three simulation backends, each providing a full implementation of the Env protocol. Select a backend via the LIVN_BACKEND environment variable before importing livn:
export LIVN_BACKEND=brian2 # requires livn[brian2] dependencies
export LIVN_BACKEND=diffrax # requires livn[diffrax] dependencies
export LIVN_BACKEND=neuron # requires livn[neuron] dependencies and MPIWhen no LIVN_BACKEND is set, livn uses a neutral default backend that provides the full Env interface without running any simulation. This is useful for working with systems, I/O, and datasets without installing a simulation engine. To run actual simulations, set one of the backends above.
All backends share the same user-facing API - you write your simulation code once and switch backends by changing the environment variable.
brian2 ​
brian2 is a lightweight backend suitable for rapid prototyping and small-to-medium systems. It models neurons as point processes using brian2's equation-based description language.
Strengths:
- Fast setup, no external system libraries needed
- Good for systems up to ~1,000 neurons on a single machine
- Includes Izhikevich and Leaky Integrate-and-Fire (LIF) models
import os
os.environ["LIVN_BACKEND"] = "brian2"
from livn import make
env = make("EI2")
it, t, iv, v, *_ = env.run(100)GSL integration ​
By default, brian2 uses the forward Euler method with a small timestep (0.005 ms) for numerical integration. For improved accuracy and performance, you can enable the GNU Scientific Library (GSL) adaptive solver by setting the LIVN_USE_LIBGSL environment variable:
export LIVN_USE_LIBGSL=1When enabled, the integrator switches to gsl_rkf45 (adaptive Runge-Kutta-Fehlberg 4(5)) with a base timestep of 0.025 ms. The adaptive method automatically adjusts step sizes to maintain accuracy, allowing a larger base timestep while preserving numerical stability for stiff biophysical equations.
This requires GSL to be installed on your system:
# Ubuntu/Debian
sudo apt install libgsl-dev
# macOS
brew install gslJax ​
A JAX-based backend that enables differentiable simulations through Diffrax and Equinox. This allows you to compute exact gradients through the simulation and use gradient-based optimization to learn stimulus parameters, decode neural activity, or train surrogate models end-to-end.
Strengths:
- End-to-end differentiable: backpropagate through the full simulation
- GPU-accelerated via JAX
- JIT-compiled for fast repeated evaluation
Install with:
pip install livn[diffrax]import os
os.environ["LIVN_BACKEND"] = "diffrax"
from livn import make
import jax
env = make("EI2")
# Gradients through the simulation are now availableSee the Differentiable Simulation example for a full walkthrough.
NEURON ​
The NEURON backend provides high-fidelity, multi-compartment biophysical simulations with MPI-based parallelism. It integrates with the MiV-Simulator for large-scale network simulations on HPC infrastructure.
Strengths:
- Detailed biophysical neuron models (multi-compartment, ion channels, calcium dynamics)
- MPI-parallel: scales to millions of neurons on supercomputers
- Best choice for generating realistic synthetic data
Requires system-level MPI and HDF5 libraries. See Installation for setup instructions.
pip install livn[neuron]import os
os.environ["LIVN_BACKEND"] = "neuron"
from livn import make
env = make("EI2")Comparison ​
| Feature | brian2 | Diffrax | NEURON |
|---|---|---|---|
| Differentiable | No | Yes | No |
| GPU support | No | Yes | No |
| Multi-compartment models | No | Yes | Yes |
| Built-in opsins | No | Yes (RhO3c) | Yes (RhO3c, RhO6c) |
| MPI parallelism | No | No | Yes |
| Setup complexity | Low | Medium | High |
| Ideal scale | ≤1,000 neurons | ≤10,000 neurons | ≤millions |