Expand description
§exg-source — EEG Source Localization in Pure Rust
Complete EEG source localization pipeline ported from
MNE-Python, implemented in pure Rust with no C/BLAS
dependencies. Uses faer for SVD and
eigendecomposition.
This crate can be used standalone or as an optional dependency of
exg (enabled by the default source
feature).
§End-to-end example
use exg_source::*;
use exg_source::covariance::Regularization;
use ndarray::Array2;
// 1. Define source space — 162 dipoles on a cortical sphere
let (src_pos, src_nn) = ico_source_space(2, 0.06, [0.0, 0.0, 0.04]);
// 2. Define electrode positions (e.g., from a 10-20 montage)
let n_elec = 32;
let electrodes = Array2::<f64>::zeros((n_elec, 3)); // your positions here
// 3. Compute forward model (3-shell spherical head)
let sphere = SphereModel::default(); // brain/skull/scalp
let fwd = make_sphere_forward(&electrodes, &src_pos, &src_nn, &sphere);
// 4. Estimate noise covariance from a baseline recording
let baseline = Array2::<f64>::zeros((n_elec, 5000));
let noise_cov = compute_covariance(&baseline, Regularization::ShrunkIdentity(None));
// 5. Build the inverse operator
let inv = make_inverse_operator(&fwd, &noise_cov, Some(0.8)).unwrap();
// 6. Apply to EEG data
let data = Array2::<f64>::zeros((n_elec, 1000));
let lambda2 = 1.0 / 9.0; // SNR² = 9
let stc = apply_inverse(&data, &inv, lambda2, InverseMethod::SLORETA).unwrap();
// 7. Assess solution quality
let (snr, snr_est) = estimate_snr(&data, &inv);
let r = make_resolution_matrix(&inv, &fwd, lambda2, InverseMethod::SLORETA).unwrap();
let errors = peak_localisation_error(&r);§Modules
| Module | Description |
|---|---|
source_space | Icosahedron and grid source space generation |
forward | Spherical head model (Berg & Scherg) forward computation |
covariance | Noise covariance estimation (empirical, Ledoit–Wolf, diagonal) |
inverse | Inverse operator construction and application |
eloreta | eLORETA iterative weight solver |
resolution | Resolution matrix, PSF, CTF, and spatial metrics |
snr | SNR estimation from whitened data |
linalg | SVD, eigendecomposition, whitener (faer backend) |
§Inverse methods
| Method | Noise-normalised? | Iterative? | Best for |
|---|---|---|---|
| MNE | ✗ | ✗ | Raw current density estimates |
| dSPM | ✓ | ✗ | Statistical maps, group studies |
| sLORETA | ✓ | ✗ | Localisation with low bias |
| eLORETA | ✓ | ✓ | Exact zero-bias localisation |
§Mathematical background
The whitened gain matrix is decomposed via SVD:
G_w = W @ G @ R^{1/2} = U @ S @ V^Twhere W is the whitener (C_noise^{-1/2}), R is the source
covariance (depth / orientation priors), and U, S, V are the
SVD factors.
The inverse kernel is:
K = R^{1/2} @ V @ diag(s / (s² + λ²)) @ U^T @ WNoise normalisation divides each source by its noise sensitivity:
- dSPM:
norm_k = ‖V_k @ diag(reginv)‖ - sLORETA:
norm_k = ‖V_k @ diag(reginv × √(1 + s²/λ²))‖ - eLORETA: iterative reweighting to achieve
R = I(seeeloreta)
Re-exports§
pub use inverse::apply_inverse;pub use inverse::apply_inverse_epochs;pub use inverse::apply_inverse_epochs_full;pub use inverse::apply_inverse_full;pub use inverse::apply_inverse_with_options;pub use inverse::make_inverse_operator;pub use inverse::prepare_inverse;pub use covariance::compute_covariance;pub use covariance::compute_covariance_epochs;pub use covariance::Regularization;pub use resolution::get_cross_talk;pub use resolution::get_point_spread;pub use resolution::make_resolution_matrix;pub use resolution::peak_localisation_error;pub use resolution::relative_amplitude;pub use resolution::spatial_spread;pub use snr::estimate_snr;pub use forward::make_sphere_forward;pub use forward::make_sphere_forward_free;pub use forward::SphereModel;pub use source_space::grid_source_space;pub use source_space::ico_n_vertices;pub use source_space::ico_source_space;
Modules§
- covariance
- Noise covariance estimation from sensor data.
- eloreta
- eLORETA (exact Low Resolution Electromagnetic Tomography) solver.
- forward
- Spherical forward model (EEG gain matrix computation).
- inverse
- Inverse operator construction and application.
- linalg
- Pure-Rust linear algebra helpers (SVD, eigendecomposition, whitener).
- resolution
- Resolution matrix computation.
- snr
- SNR estimation from sensor data and an inverse operator.
- source_
space - Source space generation.
Structs§
- Eloreta
Options - Options for the eLORETA iterative solver.
- Forward
Operator - Forward operator (gain matrix + metadata).
- Inverse
Operator - Prepared inverse operator, ready for application to data.
- Noise
Cov - Noise covariance matrix.
- Source
Estimate - Source-space estimate produced by
apply_inverse.
Enums§
- Inverse
Method - Choice of inverse method.
- PickOri
- How to handle source orientations in free-orientation inverse solutions.
- Source
Orientation - Source orientation constraint.