Direct connected inverse covariance

Posted by Viper_4 on Mon, 08 Nov 2021 18:15:07 +0100

inverse covariance for direct connections
Interpretation of the directly connected inverse covariance matrix:
Given a set of time series (as extracted in the previous section), a functional connectivity group is a set of connections representing brain interactions between regions. Here we show the use of sparse inverse covariance to extract functional connectors that focus only on direct interactions between regions.
This is very useful. For example, you calculated the ROI of interest before, and then you want to make the functional connection of ROI. If you directly extract the time series as the correlation matrix, although you remove the confusion signals, these connections are still not accurate because they also point to other labels.
Therefore, the most correct way is to use the directly connected inverse covariance matrix.

# Import sparse inverse covariance estimator
from sklearn.covariance import GraphicalLassoCV
estimator = GraphicalLassoCV()
# Extract time series  
# Covariance matrix and inverse covariance matrix (precision matrix) can be in the covariance of estimator respectively_ And precision_ Found in property
estimator.covariance_  #covariance matrix 
estimator.precision_  #Inverse covariance matrix, that's what we want
# Display the covariance
from nilearn import plotting
# This is the display covariance matrix
plotting.plot_matrix(estimator.covariance_, labels=labels,
                     figure=(9, 7), vmax=1, vmin=-1,
# Covariance matrix visualization
from nilearn import datasets
atlas = datasets.fetch_atlas_msdl()
coords = atlas.region_coords
plotting.plot_connectome(estimator.covariance_, coords,
# Sparse inverse covariance matrix visualization
plotting.plot_matrix(-estimator.precision_, labels=labels,
                     figure=(9, 7), vmax=1, vmin=-1,
                     title='Sparse inverse covariance')

The inverse covariance matrix here uses -. Namely: - estimator.precision_, Because if it's negative, you get a partial correlation. Partial correlation is to take all other ROI as covariates and remove their influence, which is similar to covariance analysis, which is very practical.

plotting.plot_connectome(-estimator.precision_, coords,
                         title='Sparse inverse covariance')
# Visualization 2: visualization on the web
view = plotting.view_connectome(-estimator.precision_, coords)
# In a Jupyter notebook, if ``view`` is the output of a cell, it will
# be displayed below the cell

Partial correlation analysis refers to the process of eliminating the influence of the third variable when two variables are related to the third variable at the same time, and only analyzing the correlation degree between the other two variables. The judgment index is the R value of the correlation coefficient.

Partial correlation analysis of ROI of multiple subjects

import numpy as np
from nilearn import plotting
n_subjects = 4  # subjects to consider for group-sparse covariance (max: 40)
def plot_matrices(cov, prec, title, labels):
    """Plot covariance and precision matrices, for a given processing. """
    prec = prec.copy()  # avoid side effects
    # Put zeros on the diagonal, for graph clarity.
    size = prec.shape[0]
    prec[list(range(size)), list(range(size))] = 0
    span = max(abs(prec.min()), abs(prec.max()))
    # Display covariance matrix
                         vmin=-1, vmax=1, title="%s / covariance" % title,
    # Display precision matrix
                         vmin=-span, vmax=span, title="%s / precision" % title,

Firstly, a visual method is defined for convenience

# get data
from nilearn import datasets
msdl_atlas_dataset = datasets.fetch_atlas_msdl()
rest_dataset = datasets.fetch_development_fmri(n_subjects=n_subjects)
# Extract ROI signal
from nilearn import input_data
# A "memory" to avoid recomputation
from joblib import Memory
mem = Memory('nilearn_cache')
masker = input_data.NiftiMapsMasker(
    msdl_atlas_dataset.maps, resampling_target="maps", detrend=True,
    high_variance_confounds=True, low_pass=None, high_pass=0.01,
    t_r=2, standardize=True, memory='nilearn_cache', memory_level=1,
subject_time_series = []
func_filenames = rest_dataset.func
confound_filenames = rest_dataset.confounds
for func_filename, confound_filename in zip(func_filenames, confound_filenames):
    print("Processing file %s" % func_filename)
    region_ts = masker.transform(func_filename, confounds=confound_filename)
# Calculate the sparse inverse covariance matrix of group
from nilearn.connectome import GroupSparseCovarianceCV
gsc = GroupSparseCovarianceCV(verbose=2)
    from sklearn.covariance import GraphicalLassoCV
except ImportError:
    # for Scitkit-Learn < v0.20.0
    from sklearn.covariance import GraphLassoCV as GraphicalLassoCV
gl = GraphicalLassoCV(verbose=2)
# Display results
atlas_img = msdl_atlas_dataset.maps
atlas_region_coords = plotting.find_probabilistic_atlas_cut_coords(atlas_img)
labels = msdl_atlas_dataset.labels
                         atlas_region_coords, edge_threshold='90%',
plotting.plot_connectome(-gl.precision_, atlas_region_coords,
                         title="Sparse inverse covariance (GraphicalLasso)",
                         edge_vmax=.5, edge_vmin=-.5)
plot_matrices(gl.covariance_, gl.precision_, "GraphicalLasso", labels)
title = "GroupSparseCovariance"
plotting.plot_connectome(-gsc.precisions_[..., 0],
                         atlas_region_coords, edge_threshold='90%',
                         edge_vmax=.5, edge_vmin=-.5)
plot_matrices(gsc.covariances_[..., 0],
              gsc.precisions_[..., 0], title, labels)

1. Firstly, a visual method is defined to avoid repeated work, but unfortunately, nilearn can only process up to 40 subjects.
2. Import data
3. Use to extract ROI time series (multiple subjects, so it is a list)
4. Use groupsparsecovarencecv() instead of GraphicalLassoCV() to calculate the matrix of the group
5. The sparse inverse covariance matrix is negatively valued to obtain partial correlation, and then visualized.

Topics: Python