CaliBrain: A Python toolbox for uncertainty estimation and calibration in EEG/MEG 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.
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()
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.
Contributing
Contribution guidelines are available in CONTRIBUTING.md.
The full development guide is also available in the documentation.
Citation
If you use CaliBrain in academic work, please cite the software archive:
Orabe, Mohammad, Huseynov, Ismail T., Nagarajan, Srikantan, & Haufe, Stefan. (2026). CaliBrain: A Python toolbox for uncertainty estimation and calibration in EEG/MEG inverse source imaging (v1.0.2). Zenodo. https://doi.org/10.5281/zenodo.20721580
Workflow
The package follows this workflow:
- generate source-level ground truth under controlled sparsity and amplitude assumptions;
- project sources to sensors through a leadfield and add noise at defined SNR;
- reconstruct posterior means and uncertainty summaries with inverse solvers;
- convert uncertainty summaries into intervals, ellipses, or ellipsoids;
- compare empirical against nominal coverage;
- 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_sflexfor Gamma-MAP reconstruction with sparse basis field expansions;gamma_lambda_map_sflexfor the S-FLEX Gamma-MAP variant with joint sparsity and lambda regularization;BMNas a Bayesian minimum norm baseline;BMN_jointas a Bayesian minimum norm variant with joint gamma/lambda learning.
Relationship to related software
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 estimation 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
From a local checkout:
git clone https://github.com/braindatalab/CaliBrain.git
cd CaliBrain
python -m pip install -e .
License
CaliBrain is distributed under the BSD 3-Clause License. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file calibrain-1.0.3.tar.gz.
File metadata
- Download URL: calibrain-1.0.3.tar.gz
- Upload date:
- Size: 127.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98112665694b91366cf0eff022504d5e9a1466b9ae2a73ce61552c1ed86624e6
|
|
| MD5 |
9f124e1d13deeb73910ae37bf6f5ac7d
|
|
| BLAKE2b-256 |
cb90e8392477c981cd5822abdfea670906d0e530096bb0d5cdd236f1d9efd6fe
|
Provenance
The following attestation bundles were made for calibrain-1.0.3.tar.gz:
Publisher:
publish-pypi.yml on braindatalab/CaliBrain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
calibrain-1.0.3.tar.gz -
Subject digest:
98112665694b91366cf0eff022504d5e9a1466b9ae2a73ce61552c1ed86624e6 - Sigstore transparency entry: 2115771572
- Sigstore integration time:
-
Permalink:
braindatalab/CaliBrain@27f855a3aff6e9c5e4e00e4a3b6d4f9f37051ec4 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/braindatalab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@27f855a3aff6e9c5e4e00e4a3b6d4f9f37051ec4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file calibrain-1.0.3-py3-none-any.whl.
File metadata
- Download URL: calibrain-1.0.3-py3-none-any.whl
- Upload date:
- Size: 130.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc715b80119c3185b1a4c6ecc735879f44d38abe7c1635a062a6b9ae790a9bc8
|
|
| MD5 |
9a2f71ad56516816790f93bfb228fc8e
|
|
| BLAKE2b-256 |
a23709a064f0ced76fc2bb3760122c5897c4f1ef576227cde4a12584a9e4f972
|
Provenance
The following attestation bundles were made for calibrain-1.0.3-py3-none-any.whl:
Publisher:
publish-pypi.yml on braindatalab/CaliBrain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
calibrain-1.0.3-py3-none-any.whl -
Subject digest:
cc715b80119c3185b1a4c6ecc735879f44d38abe7c1635a062a6b9ae790a9bc8 - Sigstore transparency entry: 2115771797
- Sigstore integration time:
-
Permalink:
braindatalab/CaliBrain@27f855a3aff6e9c5e4e00e4a3b6d4f9f37051ec4 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/braindatalab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@27f855a3aff6e9c5e4e00e4a3b6d4f9f37051ec4 -
Trigger Event:
release
-
Statement type: