8.4.3. Computing a connectome with sparse inverse covariance

This example constructs a functional connectome using the sparse inverse covariance.

We use the MSDL atlas of functional regions in rest, and the nilearn.input_data.NiftiMapsMasker to extract time series.

Note that the inverse covariance (or precision) contains values that can be linked to negated partial correlations, so we negated it for display.

As the MSDL atlas comes with (x, y, z) MNI coordinates for the different regions, we can visualize the matrix as a graph of interaction in a brain. To avoid having too dense a graph, we represent only the 20% edges with the highest values. Retrieve the atlas and the data

from nilearn import datasets
atlas = datasets.fetch_atlas_msdl()
# Loading atlas image stored in 'maps'
atlas_filename = atlas['maps']
# Loading atlas data stored in 'labels'
labels = atlas['labels']

# Loading the functional datasets
data = datasets.fetch_adhd(n_subjects=1)

# print basic information on the dataset
print('First subject functional nifti images (4D) are at: %s' %
      data.func[0])  # 4D data


First subject functional nifti images (4D) are at: /home/varoquau/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz Extract time series

from nilearn.input_data import NiftiMapsMasker
masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=True,
                         memory='nilearn_cache', verbose=5)

time_series = masker.fit_transform(data.func[0],


[NiftiMapsMasker.fit_transform] loading regions from /home/varoquau/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii
Resampling maps
[Memory]0.0s, 0.0min    : Loading resample_img...
________________________________________resample_img cache loaded - 0.0s, 0.0min
[Memory] Calling nilearn.input_data.base_masker.filter_and_extract...
<nilearn.input_data.nifti_maps_masker._ExtractionFunctor object at 0x7f6fd9f91b10>,
{ 'allow_overlap': True,
  'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'maps_img': '/home/varoquau/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii',
  'mask_img': None,
  'smoothing_fwhm': None,
  'standardize': True,
  't_r': None,
  'target_affine': None,
  'target_shape': None}, dtype=None, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=5, confounds=['/home/varoquau/nilearn_data/adhd/data/0010042/0010042_regressors.csv'])
[NiftiMapsMasker.transform_single_imgs] Loading data from /home/varoquau/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz
[NiftiMapsMasker.transform_single_imgs] Extracting region signals
[NiftiMapsMasker.transform_single_imgs] Cleaning extracted signals
_______________________________________________filter_and_extract - 9.9s, 0.2min Compute the sparse inverse covariance

from sklearn.covariance import GraphLassoCV
estimator = GraphLassoCV()

estimator.fit(time_series) Display the connectome matrix

from nilearn import plotting
# Display the covariance

# The covariance can be found at estimator.covariance_
plotting.plot_matrix(estimator.covariance_, labels=labels,
                     figure=(9, 7), vmax=1, vmin=-1,
../../_images/sphx_glr_plot_inverse_covariance_connectome_001.png And now display the corresponding graph

coords = atlas.region_coords

plotting.plot_connectome(estimator.covariance_, coords,
../../_images/sphx_glr_plot_inverse_covariance_connectome_002.png Display the sparse inverse covariance

we negate it to get partial correlations

plotting.plot_matrix(-estimator.precision_, labels=labels,
                     figure=(9, 7), vmax=1, vmin=-1,
                     title='Sparse inverse covariance')
../../_images/sphx_glr_plot_inverse_covariance_connectome_003.png And now display the corresponding graph

plotting.plot_connectome(-estimator.precision_, coords,
                         title='Sparse inverse covariance')

../../_images/sphx_glr_plot_inverse_covariance_connectome_004.png 3D visualization in a web browser

An alternative to nilearn.plotting.plot_connectome is to use nilearn.plotting.view_connectome that gives more interactive visualizations in a web browser. See 3D Plots of connectomes for more details.

view = plotting.view_connectome(-estimator.precision_, coords)

# uncomment this to open the plot in a web browser:
# view.open_in_browser()

In a Jupyter notebook, if view is the output of a cell, it will be displayed below the cell


Total running time of the script: ( 0 minutes 16.822 seconds)

Generated by Sphinx-Gallery