8.4.5. 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

Out:

First subject functional nifti images (4D) are at: /home/kamalakar/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],
                                   confounds=data.confounds)

Out:

[NiftiMapsMasker.fit_transform] loading regions from /home/kamalakar/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii
Resampling maps
________________________________________________________________________________
[Memory] Calling nilearn.image.resampling.resample_img...
resample_img(<nibabel.nifti1.Nifti1Image object at 0x2b9aebaa4150>, target_shape=(61, 73, 61), target_affine=array([[  -3.,   -0.,   -0.,   90.],
       [  -0.,    3.,   -0., -126.],
       [   0.,    0.,    3.,  -72.],
       [   0.,    0.,    0.,    1.]]), interpolation='continuous')
_____________________________________________________resample_img - 2.6s, 0.0min
[Memory]    2.8s, 0.0min: Loading filter_and_extract...
__________________________________filter_and_extract cache loaded - 0.0s, 0.0min

Compute the sparse inverse covariance

from sklearn.covariance import GraphLassoCV
estimator = GraphLassoCV()

estimator.fit(time_series)

Display the connectome matrix

from matplotlib import pyplot as plt

# Display the covariance
plt.figure(figsize=(10, 10))

# The covariance can be found at estimator.covariance_
plt.imshow(estimator.covariance_, interpolation="nearest",
           vmax=1, vmin=-1, cmap=plt.cm.RdBu_r)
# And display the labels
x_ticks = plt.xticks(range(len(labels)), labels, rotation=90)
y_ticks = plt.yticks(range(len(labels)), labels)
plt.title('Covariance')
../../_images/sphx_glr_plot_inverse_covariance_connectome_001.png

And now display the corresponding graph

from nilearn import plotting
coords = atlas.region_coords

plotting.plot_connectome(estimator.covariance_, coords,
                         title='Covariance')
../../_images/sphx_glr_plot_inverse_covariance_connectome_002.png

Display the sparse inverse covariance (we negate it to get partial correlations)

plt.figure(figsize=(10, 10))
plt.imshow(-estimator.precision_, interpolation="nearest",
           vmax=1, vmin=-1, cmap=plt.cm.RdBu_r)
# And display the labels
x_ticks = plt.xticks(range(len(labels)), labels, rotation=90)
y_ticks = plt.yticks(range(len(labels)), labels)
plt.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')

plotting.show()
../../_images/sphx_glr_plot_inverse_covariance_connectome_004.png

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

Generated by Sphinx-Gallery