Skip to main content

CaliBrain logo

PyPI version Supported Python versions Tests License Latest GitHub release DOI

CaliBrain: A Python toolbox for uncertainty quantification, calibration, and benchmarking in Bayesian M/EEG inverse source imaging.

Overview

Inverse source imaging is an ill-posed problem: different source configurations can explain the same sensor data. CaliBrain addresses a central question in Bayesian source imaging: are posterior uncertainty estimates empirically reliable? The toolbox provides simulation-based workflows for generating source activity, propagating it through forward models, reconstructing posterior source estimates, quantifying empirical coverage, and evaluating recalibration maps under controlled experimental conditions.

CaliBrain pipeline overview

Documentation

The documentation is hosted on Read the Docs: https://calibrain.readthedocs.io/

For runnable end-to-end examples, see the tutorials and workflow documentation on Read the Docs.

Citation

If you use CaliBrain in academic work, please cite the software archive. The DOI below always points to the latest version; Zenodo also lists a DOI for every release.

Orabe, Mohammad, Nagarajan, Srikantan, Haufe, Stefan, & Huseynov, Ismail T. (2026).
CaliBrain: A Python toolbox for uncertainty quantification, calibration, and
benchmarking in Bayesian M/EEG inverse source imaging. Zenodo.
https://doi.org/10.5281/zenodo.20683761
@software{calibrain,
  title = {CaliBrain: A Python toolbox for uncertainty quantification, calibration, and benchmarking in Bayesian M/EEG inverse source imaging},
  author = {Orabe, Mohammad and Nagarajan, Srikantan and Haufe, Stefan and Huseynov, Ismail T.},
  year = {2026},
  publisher = {Zenodo},
  doi = {10.5281/zenodo.20683761},
  url = {https://doi.org/10.5281/zenodo.20683761}
}

Example

The example below simulates one inverse problem, reconstructs sources, and plots empirical coverage before and after isotonic recalibration.

Simulate source activity:

import matplotlib.pyplot as plt
import numpy as np

from calibrain import (
    BMN,
    LeadfieldBuilder,
    SensorSimulator,
    SourceEstimator,
    SourceSimulator,
    UncertaintyCalibrator,
    UncertaintyEstimator,
)

# Simulate a small fixed-orientation inverse problem.
x_true, _ = SourceSimulator().simulate(n_sources=64, nnz=4, seed=0)

# Build a random leadfield, simulate sensor data, and reconstruct sources.
L = LeadfieldBuilder(leadfield_dir="unused").get_leadfield(
    retrieve_mode="random",
    orientation_type="fixed",
    n_sensors=20,
    n_sources=x_true.shape[0],
)
_, y_noisy, noise, _ = SensorSimulator().simulate(x_true, L, seed=0)
result = SourceEstimator(solver=BMN, noise_var=float(np.var(noise))).fit(L, y_noisy).predict()

# Compute calibration curves before and after isotonic recalibration.
nominal_coverages = np.linspace(0.0, 1.0, 11)
uncertainty = UncertaintyEstimator(nominal_coverages=nominal_coverages)
calibrator = UncertaintyCalibrator(nominal_coverages=nominal_coverages)
posterior_var = uncertainty.posterior_variance_from_cov(result["posterior_cov"])

pre_curve = uncertainty.calibration_curve_intervals_aggregated(
    x_true=x_true,
    x_hat=result["posterior_mean"],
    posterior_var=posterior_var,
)
mapping = calibrator.fit_mapping(
    x_true=x_true,
    x_hat=result["posterior_mean"],
    posterior_var=posterior_var,
)
post_curve = calibrator.evaluate_with_mapping(
    x_true=x_true,
    x_hat=result["posterior_mean"],
    posterior_var=posterior_var,
    mapping=mapping,
)

# Plot nominal vs empirical coverage.
plt.plot([0, 1], [0, 1], "--", color="0.5", label="perfect calibration")
plt.plot(pre_curve["nominal_coverages"], pre_curve["empirical_coverages"], "o-", label="before calibration")
plt.plot(post_curve["nominal_coverages"], post_curve["empirical_coverages"], "o-", label="after calibration")
plt.xlabel("Nominal coverage")
plt.ylabel("Empirical coverage")
plt.legend()
plt.tight_layout()
plt.show()

CaliBrain calibration example

Workflow

The package follows this workflow:

  1. generate source-level ground truth under controlled sparsity and amplitude assumptions;
  2. project sources to sensors through a leadfield and add noise at defined SNR;
  3. reconstruct posterior means and uncertainty summaries with inverse solvers;
  4. convert uncertainty summaries into intervals, ellipses, or ellipsoids;
  5. compare empirical against nominal coverage;
  6. fit isotonic recalibration functions on training splits and evaluate them on held-out splits.

CaliBrain currently supports fixed and free-orientation source models for inverse source imaging methods:

  • gamma_map_sflex for Gamma-MAP reconstruction with sparse basis field expansions;
  • gamma_lambda_map_sflex for the S-FLEX Gamma-MAP variant with joint sparsity and lambda regularization;
  • BMN as a Bayesian minimum norm baseline;
  • BMN_joint as a Bayesian minimum norm variant with joint gamma/lambda learning.

CaliBrain complements broader neurophysiology analysis libraries, general uncertainty-calibration toolkits, and standard inverse-solver workflows rather than replacing them.

Its scope is narrower and more specific: CaliBrain focuses on simulation-based uncertainty quantification and calibration for EEG/MEG inverse source imaging, including source-level intervals, local covariance-based ellipsoids, empirical coverage analysis, and recalibration across controlled evaluation conditions.

Installation

From PyPI:

python -m pip install calibrain

3D brain plotting needs extra GUI packages, which are optional:

python -m pip install "calibrain[viz]"

The example data are downloaded on demand, in three steps (download the forward solution, reduce it to 1284 sources, then create the leadfield files from it):

from calibrain import extract_leadfields, fetch_example_data, reduce_forward_solutions

fetch_example_data()
reduce_forward_solutions()
extract_leadfields()

For installing from a source checkout and using conda, see the installation guide.

Contributing

Contribution guidelines are available in CONTRIBUTING.md. The full development guide is also available in the documentation.

License

CaliBrain is distributed under the BSD 3-Clause License. See LICENSE.

Metadata

Release files for calibrain 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for calibrain 1.1.0
File Size Uploaded Provenance Actions
calibrain-1.1.0.tar.gz 149.3 kB With provenance Details for calibrain-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for calibrain 1.1.0
File Interpreter ABI Platform Actions
calibrain-1.1.0-py3-none-any.whl Python 3 none any Details for calibrain-1.1.0-py3-none-any.whl

Total release size: 288.3 kB

Release files / calibrain-1.1.0.tar.gz

Download URL calibrain-1.1.0.tar.gz
Size 149.3 kB
Tags Source
SHA-256 checksum
How to use checksums
568a379b33ac62d4300f74e880e675ff049db7b229b36aad7572307ce2338650
BLAKE2b-256 checksum
How to use checksums
a1e15d5307fd27ab0e57d6eff046410802a0c684a231c8fa774e50b42f135aab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / calibrain-1.1.0-py3-none-any.whl

Download URL calibrain-1.1.0-py3-none-any.whl
Size 139.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5012014565598dcbfd30603fd56d2e245d21b27f626495c950cfe5839885ce0c
BLAKE2b-256 checksum
How to use checksums
57aaa021932389970c0e768214937e56ebe6d279ccfb54c8f2acb034c83cf6a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for calibrain 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for calibrain-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page