Base Class#
The BaseSolver class is inherited by all solvers. It implements regularization with various methods (generalized cross-validation, L-Curve and product method). Furthermore, it specifies the number of regularization parameters (n_reg_params) and the range of parameters to test.
- class invert.solvers.base.BaseSolver(regularisation_method='GCV', n_reg_params=24, prep_leadfield=True, use_last_alpha=False, rank='auto', reduce_rank=False, plot_reg=False, verbose=0)#
- Parameters
regularisation_method (str) –
- Can be either
”GCV” -> generalized cross validation “L” -> L-Curve method using triangle method “Product” -> Minimal product method
n_reg_params (int) – The number of regularisation parameters to try. The higher, the more accurate the regularisation and the slower the computations.
prep_leadfield (bool) – If True -> Apply common average referencing and normalisation of the leadfield columns.
reduce_rank (bool) – Whether to reduce the rank of the M/EEG data
rank (str/int) –
Can be either int -> select only the <rank> largest eigenvectors of the data “auto” -> automatically select the optimal rank using the L-curve method and
an eigenvalue drop-off criterion
plot_reg (bool) – Plot the regularization parameters.
- apply_inverse_operator(mne_obj) mne.source_estimate.SourceEstimate #
Apply the inverse operator
- Parameters
mne_obj ([mne.Evoked, mne.Epochs, mne.io.Raw]) – The MNE data object.
- Returns
stc – The mne SourceEstimate object.
- Return type
mne.SourceEstimate
- static calc_area_tri(AB, AC, CB)#
Calculates area of a triangle given the length of each side.
- static delete_from_list(a, idc)#
Delete elements of list at idc.
- static euclidean_distance(A, B)#
Euclidean Distance between two points (A -> B).
- static filter_norms(r_vals, l2_norms)#
Filter l2_norms where they are not monotonically decreasing.
- Parameters
r_vals ([list, numpy.ndarray]) – List or array of r-values
l2_norms ([list, numpy.ndarray]) – List or array of l2_norms
- Returns
bad_idc – List where indices are increasing
- Return type
list
- find_corner(r_vals, l2_norms)#
Find the corner of the l-curve given by plotting regularization levels (r_vals) against norms of the inverse solutions (l2_norms).
- Parameters
r_vals (list) – Levels of regularization
l2_norms (list) – L2 norms of the inverse solutions per level of regularization.
- Returns
idx – Index at which the L-Curve has its corner.
- Return type
int
- get_alphas(reference=None)#
Create list of regularization parameters (alphas) based on the largest eigenvalue of the leadfield or some reference matrix.
- Parameters
reference ([None, numpy.ndarray]) – If None: use leadfield to calculate regularization parameters, else use reference matrix (e.g., M/EEG covariance matrix).
- Returns
alphas – List of regularization parameters (alphas)
- Return type
list
- make_inverse_operator(forward: mne.forward.forward.Forward, *args, alpha='auto', **kwargs)#
- Base function to create the inverse operator based on the forward
model.
- Parameters
forward (mne.Forward) – The mne Forward model object.
alpha (["auto", float]) – If “auto”: Try out multiple regularization parameters and choose the optimal one, otherwise use the float.
- Return type
None
- prepare_forward()#
Prepare leadfield for calculating inverse solutions by applying common average referencing and unit norm scaling.
- regularise_gcv(M, plot=False)#
Find optimally regularized inverse solution using the generalized cross-validation method [1].
- Parameters
M (numpy.ndarray) – The M/EEG data matrix (n_channels, n_timepoints)
- Returns
source_mat (numpy.ndarray) – The inverse solution (dipoles x time points)
optimum_idx (int) – The index of the selected (optimal) regularization parameter
References
[1] Grech, R., Cassar, T., Muscat, J., Camilleri, K. P., Fabri, S. G., Zervakis, M., … & Vanrumste, B. (2008). Review on solving the inverse problem in EEG source analysis. Journal of neuroengineering and rehabilitation, 5(1), 1-33.
- regularise_lcurve(M, plot=False)#
Find optimally regularized inverse solution using the L-Curve method [1].
- Parameters
M (numpy.ndarray) – The M/EEG data matrix (n_channels, n_timepoints)
- Returns
source_mat (numpy.ndarray) – The inverse solution (dipoles x time points)
optimum_idx (int) – The index of the selected (optimal) regularization parameter
References
[1] Grech, R., Cassar, T., Muscat, J., Camilleri, K. P., Fabri, S. G., Zervakis, M., … & Vanrumste, B. (2008). Review on solving the inverse problem in EEG source analysis. Journal of neuroengineering and rehabilitation, 5(1), 1-33.
- regularise_product(M, plot=False)#
Find optimally regularized inverse solution using the product method [1].
- Parameters
M (numpy.ndarray) – The M/EEG data matrix (n_channels, n_timepoints)
- Returns
source_mat (numpy.ndarray) – The inverse solution (dipoles x time points)
optimum_idx (int) – The index of the selected (optimal) regularization parameter
References
[1] Grech, R., Cassar, T., Muscat, J., Camilleri, K. P., Fabri, S. G., Zervakis, M., … & Vanrumste, B. (2008). Review on solving the inverse problem in EEG source analysis. Journal of neuroengineering and rehabilitation, 5(1), 1-33.
- save(path)#
Saves the solver object.
- pathstr
The path to save the solver.
- Returns
self – Function returns itself.
- Return type
- source_to_object(source_mat)#
Converts the source_mat matrix to the mne.SourceEstimate object.
- Parameters
source_mat (numpy.ndarray) – Source matrix (dipoles, time points)-
- Returns
stc
- Return type
mne.SourceEstimate
- unpack_data_obj(mne_obj, pick_types=None)#
Unpacks the mne data object and returns the data.
- Parameters
mne_obj ([mne.Evoked, mne.EvokedArray, mne.Epochs, mne.EpochsArray, mne.Raw]) –
- Returns
data – The M/EEG data matrix.
- Return type
numpy.ndarray
- class invert.solvers.base.InverseOperator(inverse_operator, solver_name)#
This class holds the inverse operator, which may be a simple numpy.ndarray matrix or some object like an esinet.net()
- Parameters
operator (inverse) –
- apply(M)#
Apply the precomputed inverse operator to the data matrix M. :param M: The M/EEG data matrix (n_channels, n_timepoints) :type M: numpy.ndarray
- Returns
J – The source estimate matrix (n_sources, n_timepoints)
- Return type
numpy.ndarray
- has_multiple_operators()#
Check if there are multiple inverse_operators.