mvpure_py.localizer#

mvpure_py.localizer.mvpure_localizer#

Localizing sources.

class mvpure_py.localizer.mvpure_localizer.Localized(subject: str, subjects_dir: str, **kwargs)[source]#

Bases: dict

Class to handle localized sources.

add_power_of_reconstructed(stc: SourceEstimate = None)[source]#

Compute power of reconstructed sources. By default, adds key power_of_reconstructed with power of localized sources. If stc (type: mne.SourceEstimate) has data for specific hemisphere (lh_data/rh_data), computes also power just for given hemisphere.

Parameters:

stc (mne.SourceEstimate) – source estimate for given subject Can be None if key stc already exists in Localized object, then uses source estimate object saved there.

add_stc(stc: SourceEstimate)[source]#

Add mne.SourceEstimate to class keys in order to perform power analysis.

Parameters:

stc (mne.SourceEstimate) – source estimate for given subject

add_vertices_info(lh_vertices: list = None, lh_indices: list = None, rh_vertices: list = None, rh_indices: list = None)[source]#

Add key vertices that saves the information about:

  • hemisphere of vertex [hemi]

  • original leadfield index [lf_idx]

  • ordinal position of the activity index for given vertex [activity_index_order]

  • sub-leadfield index for data of given source [lf_order]

It will be in a form:

self['vertices'] = dict(
    {vertex_number}: dict(
        'hemi': 'lh' | 'rh',
        'lf_idx': {lf_idx},
        'activity_index_order': {activity_index_order},
        'lf_order': {activity_index_order - 1}
    )
)

Note

After using assign_brain_regions() function there will be key ‘brain_region’ with the appropriate brain region assigned based on the atlas.

Parameters:
  • lh_vertices (list | None) – vertex numbers of localized sources in the left hemisphere

  • lh_indices (list | None) – original leadfield indies of localized sources in the left hemisphere

  • rh_vertices (list | None) – vertex numbers of localized sources in the right hemisphere

  • rh_indices (list | None) – original leadfield indies of localized sources in the right hemisphere

assign_brain_regions(parc: str)[source]#

Assign label from given parcellation parc to each source in source estimate. If requested parcellation is present in FreeSurfer folder for given subject, labels from there are being read. If not code checks presence of ‘fsaverage’ folder in subjects_dir directory and try to read labels from there. If successful, labels are then morphed to given subject.

Parameters:

parc (str) – Name of parcellation to be performed

plot_by_brain_region(hemi: str = 'both', cmap: str = 'PiYG', scale_mapping: bool = True, scale_factor: float = 1.0, save_html: bool = False, parc: str = None, **kwargs)[source]#

Visualize positions of localized sources taking into consideration brain region of each vertex.

Note

It is necessary to perform add_vertices_info() before trying to plot brain regions.

Parameters:
  • hemi (str (Default to 'both'.)) – Hemisphere which vertex is in

  • cmap (str (Default to 'PiYG'.)) – matplotlib colormap to use for plotting

  • scale_mapping (bool (Default to True)) – Whether to take into account the order of source locations using the foci scale for visualization (the largest foci corresponds to the first located source etc.).

  • scale_factor (float (Default to 1.0)) – Scaling factor in the range [0,1] for the first located source.

  • save_html (bool (Default to False)) – Whether to save 3D output plot to html file.

  • parc (str (Default to None)) – Name of parcellation to be performed. Needed only if ‘brain_region’ key is not present in Localized[‘vertices’] yet.

plot_localized_brain_regions(hemi: str, parc: str, subjects_dir: str = None, surf: str = 'inflated', cmap: str = 'Reds', none_color: str = 'gainsboro', return_df_info: bool = False, **kwargs)[source]#
plot_localized_sources(hemi: str = 'both', color_mapping: bool = True, color: str = 'crimson', cmap: str = 'Reds', scale_mapping: bool = True, scale_factor: float = 1.0, save_html: bool = False, **kwargs)[source]#
plot_sources_power(stc: SourceEstimate = None, norm: str = 'max', cmap: str = 'Reds', **kwargs)[source]#
save(output_path: str)[source]#

Save mvpure_py.Localized object to pickle file.

Parameters:

output_path (str) – Directory where mvpure_py.Localized data should be stored.

mvpure_py.localizer.mvpure_localizer.localize(subject: str, subjects_dir: str, localizer_to_use: str | list, n_sources_to_localize: int, R: ndarray, N: ndarray, forward: Forward = None, leadfield: ndarray = None, r: str | int | list = 'full', show_progress: bool = True)[source]#
mvpure_py.localizer.mvpure_localizer.read_localized(fname: str) Localized[source]#

Read a mvpure_py.Localized object from pickle file.

Parameters:

fname (str) – Path to file where pickle file with mvpure_py.Localized object is stored

Returns:

mvpure_py.Localized

Return type:

read mvpure_py.Localized object

mvpure_py.localizer.localizer_utils#

Functions used during sources localization.

class mvpure_py.localizer.localizer_utils.PSDPinvApply(U: ndarray, lam: ndarray, cutoff: float)[source]#

Bases: object

Robust pseudo-inverse operator for PSD (possibly low-rank) matrix A:

A^+ ≈ U diag(1/lam) U^T (truncated eigenpairs)

Use: op.apply(X) = A^+ @ X

U#
apply(X: ndarray) ndarray[source]#
cutoff#
lam#
mvpure_py.localizer.localizer_utils.get_activity_index(localizer_to_use: str, H: ndarray, R: ndarray, N: ndarray, n_sources_to_localize: int, r: int, batch_size: int = 16384, show_progress: bool = True) Tuple[List[int], ndarray, int, ndarray][source]#

Greedy source localization algorithm.

mvpure_py.localizer.localizer_utils.psd_pinv_operator(A: ndarray, rmax: int | None = None, eps_rel: float = 1e-10) PSDPinvApply[source]#

Build a robust pseudo-inverse operator for PSD A (low-rank supported).

Parameters:
  • A ((m,m) PSD matrix (may be singular))

  • rmax (Optional[int]) – Keep at most rmax eigenpairs (largest).

  • eps_rel (float) – Keep eigenvalues > eps_rel * max_eig.

Returns:

.apply(X) computes A^+ @ X

Return type:

PSDPinvApply