Note
Click here to download the full example code or to run this example in your browser via Binder
9.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 movie watching, 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.
9.4.3.1. 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_development_fmri(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:
/home/nicolas/anaconda3/envs/nilearn/lib/python3.8/site-packages/numpy/lib/npyio.py:2405: VisibleDeprecationWarning: Reading unicode strings without specifying the encoding argument is deprecated. Set the encoding, use None for the system default.
output = genfromtxt(fname, **kwargs)
First subject functional nifti images (4D) are at: /home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz
9.4.3.2. 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/nicolas/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii
Resampling maps
/home/nicolas/GitRepos/nilearn-fork/nilearn/_utils/cache_mixin.py:303: UserWarning: memory_level is currently set to 0 but a Memory object has been provided. Setting memory_level to 1.
warnings.warn("memory_level is currently set to 0 but "
[Memory]0.1s, 0.0min : Loading resample_img...
________________________________________resample_img cache loaded - 0.0s, 0.0min
[Memory]0.2s, 0.0min : Loading filter_and_extract...
__________________________________filter_and_extract cache loaded - 0.0s, 0.0min
9.4.3.3. Compute the sparse inverse covariance¶
try:
from sklearn.covariance import GraphicalLassoCV
except ImportError:
# for Scitkit-Learn < v0.20.0
from sklearn.covariance import GraphLassoCV as GraphicalLassoCV
estimator = GraphicalLassoCV()
estimator.fit(time_series)
Out:
GraphicalLassoCV()
9.4.3.4. 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,
title='Covariance')
Out:
/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/matrix_plotting.py:22: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
ylabel_width = ax.yaxis.get_tightbbox(renderer).inverse_transformed(
/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/matrix_plotting.py:30: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
xlabel_height = ax.xaxis.get_tightbbox(renderer).inverse_transformed(
<matplotlib.image.AxesImage object at 0x7f881d184e20>
9.4.3.5. And now display the corresponding graph¶
coords = atlas.region_coords
plotting.plot_connectome(estimator.covariance_, coords,
title='Covariance')
Out:
<nilearn.plotting.displays.OrthoProjector object at 0x7f881e666640>
9.4.3.6. 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')
Out:
/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/matrix_plotting.py:22: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
ylabel_width = ax.yaxis.get_tightbbox(renderer).inverse_transformed(
/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/matrix_plotting.py:30: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
xlabel_height = ax.xaxis.get_tightbbox(renderer).inverse_transformed(
<matplotlib.image.AxesImage object at 0x7f881e39bbe0>
9.4.3.7. And now display the corresponding graph¶
plotting.plot_connectome(-estimator.precision_, coords,
title='Sparse inverse covariance')
plotting.show()
9.4.3.8. 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)
# In a Jupyter notebook, if ``view`` is the output of a cell, it will
# be displayed below the cell
view
# uncomment this to open the plot in a web browser:
# view.open_in_browser()
Total running time of the script: ( 0 minutes 22.933 seconds)