Skip to main content

Crate exg_source

Crate exg_source 

Source
Expand description

§exg-source — EEG Source Localization in Pure Rust

Crate Docs

Complete EEG source localization pipeline ported from MNE-Python, implemented in pure Rust with no C/BLAS dependencies. Uses faer for SVD and eigendecomposition.

Part of the exg workspace. Can be used standalone or via exg’s default source feature. See also exg-luna for the LUNA seizure-detection preprocessing pipeline.

§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

ModuleDescription
source_spaceIcosahedron and grid source space generation
forwardSpherical head model (Berg & Scherg) forward computation
covarianceNoise covariance estimation (empirical, Ledoit–Wolf, diagonal)
inverseInverse operator construction and application
eloretaeLORETA iterative weight solver
resolutionResolution matrix, PSF, CTF, and spatial metrics
snrSNR estimation from whitened data
linalgSVD, eigendecomposition, whitener (faer backend)

§Inverse methods

MethodNoise-normalised?Iterative?Best for
MNERaw current density estimates
dSPMStatistical maps, group studies
sLORETALocalisation with low bias
eLORETAExact zero-bias localisation

§Mathematical background

The whitened gain matrix is decomposed via SVD:

G_w = W @ G @ R^{1/2} = U @ S @ V^T

where 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 @ W

Noise 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 (see eloreta)

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§

EloretaOptions
Options for the eLORETA iterative solver.
ForwardOperator
Forward operator (gain matrix + metadata).
InverseOperator
Prepared inverse operator, ready for application to data.
NoiseCov
Noise covariance matrix.
SourceEstimate
Source-space estimate produced by apply_inverse.

Enums§

InverseMethod
Choice of inverse method.
PickOri
How to handle source orientations in free-orientation inverse solutions.
SourceOrientation
Source orientation constraint.