MNEInverseOperator
Namespace: MNELIB · Library: MNE Library
mne.InverseOperator in MNE-Python.
#include <mne/mne_inverse_operator.h>
class MNELIB::MNEInverseOperator
MNE-style inverse operator.
Encapsulates the SVD decomposition of the whitened and weighted lead-field matrix together with noise and source covariance, priors, and the source space. Supports reading/writing FIFF files, preparation for application (regularisation, whitening, noise normalisation), and gain-matrix clustering.
FsLabel v1;
FsLabel::read(data + "/subjects/sample/label/lh.V1.label", v1);
QFile fwdFile(data + "/Result/ref-sample_audvis-meg-eeg-oct-6-fwd.fif");
const MNEForwardSolution fwd = MNEForwardSolution(fwdFile).pick_regions({v1}); // 70 sources x 3 orientations
QFile covFile(data + "/MEG/sample/sample_audvis-cov.fif");
const FiffCov noiseCov(covFile);
QFile aveFile(data + "/MEG/sample/sample_audvis-ave.fif");
const FiffEvoked evoked(aveFile, 0); // "Left Auditory"
const MNEInverseOperator inverse = MNEInverseOperator::make_inverse_operator(evoked.info, fwd, noiseCov, 0.2f, 0.8f);
const float lambda2 = 1.0f / 9.0f; // SNR 3
InvMinimumNorm dspm(inverse, lambda2, "dSPM");
const InvSourceEstimate stc = dspm.calculateInverse(evoked); // one row per source, combined over orientations
// The same clustering on the inverse kernel: one x/y/z column triple per cluster of the transposed kernel
InvMinimumNorm clusteredMne(inverse, lambda2, "MNE");
clusteredMne.doInverseSetup(evoked.nave);
MatrixXd kernelMean; // 3 x 70 source components -> 3 x nClusters, averaging per orientation
const MatrixXd clusteredKernelT = clusteredMne.getPreparedInverseOperator().cluster_kernel(aparc, 20, kernelMean, "sqeuclidean");
Public Methods
MNEInverseOperator()
Constructs an empty inverse operator with invalid sentinel values.
MNEInverseOperator(p_IODevice)
Constructs an inverse operator by reading from a FIFF IO device.
Parameters:
- p_IODevice : QIODevice & IO device (e.g. QFile) containing the inverse operator.
MNEInverseOperator(info, forward, noiseCov, loose, depth, fixed, limit_depth_chs)
Constructs an inverse operator from measurement info, forward solution, and noise covariance.
Parameters:
-
info : const FiffInfo & Measurement info specifying channels; bad channels in info.bads are excluded.
-
forward : const MNEForwardSolution & Forward operator.
-
noiseCov : const FiffCov & Noise covariance matrix.
-
loose : float Tangential-component weight in [0, 1] (ignored when
fixedis true). -
depth : float Depth-weighting exponent in [0, 1]; 0 disables depth weighting.
-
fixed : bool If true, use fixed source orientations normal to the cortical mantle.
-
limit_depth_chs : bool If true, restrict depth weighting to gradiometers (or magnetometers/EEG as fallback).
MNEInverseOperator(other)
Copy constructor.
Parameters:
- other : const MNEInverseOperator & Inverse operator to copy.
operator=(other)
Copy assignment.
Every member has value semantics, so the compiler-generated member-wise assignment is correct. It is declared explicitly because declaring a copy constructor and/or a destructor deprecates the implicitly generated one.
Parameters:
- other : const MNEInverseOperator & Object to assign from.
Returns:
- MNEInverseOperator & — reference to this object.
~MNEInverseOperator()
Destructor.
assemble_kernel(label, method, pick_normal, K, noise_norm, vertno)
Assemble the inverse kernel matrix.
Applies eigenfield/eigenlead decomposition, regularisation, whitening, projection, and optional noise normalisation to produce the kernel matrix that maps sensor data to source estimates.
Parameters:
-
label : const FsLabel & Cortical label restricting the source space (empty for all sources).
-
method : const QString & Inverse method: "MNE", "dSPM", or "sLORETA".
-
pick_normal : bool If true, keep only the surface-normal component (requires free orientation).
-
K : Eigen::MatrixXd & Assembled kernel matrix (nSources x nChannels).
-
noise_norm : Eigen::SparseMatrix< double > & Diagonal noise-normalisation matrix (empty for
MNE). -
vertno : QList< Eigen::VectorXi > & Vertex numbers per hemisphere.
Returns:
- bool — true on success.
check_ch_names(info)
Verify that inverse-operator channels are present in the measurement info.
Parameters:
- info : const FiffInfo & Measurement info whose channel names are checked.
Returns:
- bool — true if all channels match, false otherwise.
cluster_kernel(annotationSet, clusterSize, D, method)
Cluster the inverse kernel by cortical parcellation.
Groups source-space points within each parcellation label using KMeans and returns a reduced kernel together with the cluster operator.
Parameters:
-
annotationSet : const FsAnnotationSet & Annotation set for left and right hemisphere.
-
clusterSize : qint32 Maximum number of sources per cluster.
-
D : Eigen::MatrixXd & Cluster operator matrix (nSources x nClusters).
-
method : const QString & Distance metric: "cityblock" or "sqeuclidean".
Returns:
- Eigen::MatrixXd — Clustered kernel matrix.
getKernel()
Access the most recently assembled kernel (mutable).
Returns:
- Eigen::MatrixXd & — Reference to the kernel matrix.
getKernel()
Access the most recently assembled kernel (const).
Returns:
- Eigen::MatrixXd — Copy of the kernel matrix.
isFixedOrient()
Check whether the inverse operator uses fixed source orientations.
Returns:
- bool — true if orientation is fixed-normal.
prepare_inverse_operator(nave, lambda2, dSPM, sLORETA)
Prepare the inverse operator for source estimation.
Scales covariances by the number of averages, builds the regularised pseudo-inverse, the whitener, the SSP projector, and (optionally) the noise-normalisation factors for dSPM or sLORETA.
Parameters:
-
nave : qint32 Number of averages (scales the noise covariance).
-
lambda2 : float Regularisation parameter (SNR^{-2}).
-
dSPM : bool Compute dSPM noise-normalisation factors.
-
sLORETA : bool Compute sLORETA noise-normalisation factors.
Returns:
- MNEInverseOperator — A prepared copy of the inverse operator ready for apply_inverse.
write(p_IODevice)
Write the inverse operator to a FIFF file.
Parameters:
- p_IODevice : QIODevice & IO device to write to.
Returns:
- bool — false if the device cannot be opened for writing.
writeToStream(p_pStream)
Write the inverse operator into an already-open FIFF stream.
Parameters:
- p_pStream : *FiffStream ** Open FIFF stream.
Static Methods
make_inverse_operator(info, forward, noiseCov, loose, depth, fixed, limit_depth_chs)
Assemble an inverse operator from a forward solution and noise covariance.
Performs depth weighting, source-covariance construction, whitening, and SVD decomposition of the weighted lead-field matrix.
Parameters:
-
info : const FiffInfo & Measurement info; bad channels in info.bads are excluded.
-
forward : MNEForwardSolution Forward operator (modified internally for fixed orientation if needed).
-
noiseCov : const FiffCov & Noise covariance matrix.
-
loose : float Tangential-component weight in [0, 1].
-
depth : float Depth-weighting exponent in [0, 1]; 0 disables.
-
fixed : bool Use fixed surface-normal orientations.
-
limit_depth_chs : bool Restrict depth weighting to gradiometers (or fallback).
Returns:
- MNEInverseOperator — Assembled inverse operator.
read_inverse_operator(p_IODevice, inv)
Read an inverse operator from a FIFF file.
Parameters:
-
p_IODevice : QIODevice & FIFF IO device (e.g. QFile or QTcpSocket).
-
inv : MNEInverseOperator & Receives the loaded inverse operator.
Returns:
- bool — true on success, false on failure.
Example
- Full source:
src/examples/ex_inv_api/main.cpp - Build target:
ex_inv_api(runs under CTest) - Data: mne-cpp-test-data
Used in examples
ex_inv_api
Inverse library: minimum-norm estimates, resolution matrices, label time courses, sparse solvers.
const MNEInverseOperator inverse = MNEInverseOperator::make_inverse_operator(evoked.info, fwd, noiseCov, 0.2f, 0.8f);
const float lambda2 = 1.0f / 9.0f; // SNR 3
InvMinimumNorm dspm(inverse, lambda2, "dSPM");
const InvSourceEstimate stc = dspm.calculateInverse(evoked); // one row per source, combined over orientations
// mne.minimum_norm.apply_inverse(evoked, inv, 1/9, "dSPM"): 70 x 421, sum |stc| = 372483.6, stc[0, 0] = 5.00927
ok &= expect(stc.data.rows() == 70 && stc.data.cols() == 421 && near(stc.data.cwiseAbs().sum(), 372483.6177246599, 1e-4) && near(stc.data(0, 0), 5.0092691144321755, 1e-4) && near(stc.tmin, -0.19979521, 1e-6),
"InvMinimumNorm/InvSourceEstimate: dSPM on 70 V1 sources matches mne.minimum_norm.apply_inverse");
ex_dsp_rt
DSP real-time workers: averaging, covariance, noise spectrum, connectivity, HPI fitting and inverse operators.
RtInvOp rtInvOp(rawInfo, fwd); // MEG-only operator, loose 0.2, depth 0.8
MNEInverseOperator inverse;
bool inverseDone = false;
QObject::connect(&rtInvOp, &RtInvOp::invOperatorCalculated, [&](const MNEInverseOperator& op) {
inverse = op;
inverseDone = true;
});
rtInvOp.append(noiseCov); // e.g. each new RtCov estimate
const bool inverseArrived = waitFor(inverseDone, 60000);
// 306 MEG channels in the forward solution, minus the bad MEG2443
ok &= expect(inverseArrived && inverse.nsource == fwd->nsource && inverse.nchan == 305 && rawInfo->bads.contains("MEG2443"),
QString("RtInvOp builds a %1-channel operator for %2 sources").arg(inverse.nchan).arg(inverse.nsource));
rtInvOp.stop();
ex_clustered_inverse_mne_raw
Example of the computation of a clustered inverse mne raw.
MNEInverseOperator inverse_operator(info, t_clusteredFwd, noise_cov, 0.2f, 0.8f);
Also used in ex_connectivity, ex_connectivity_comparison, ex_disp_3D, ex_histogram, ex_inverse_mne, ex_inverse_mne_raw.
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>
- Christof Pieloth <pieloth@labp.htwk-leipzig.de>
- Lorenz Esch <lorenz.esch@tu-ilmenau.de>
- Gabriel Motta <gabrielbenmotta@gmail.com>
- Juan GPC <jgarciaprieto@mgh.harvard.edu>
- Andreas Griesshammer <ag@fieldlineinc.com>