8.4.8. Extract signals on spheres from an atlas and plot a connectome

This example shows how to extract signals from spherical regions centered on coordinates from Power-264 atlas [1] and Dosenbach-160 [2]. We estimate connectome using sparse inverse covariance, to recover the functional brain networks structure.


[1] Power, Jonathan D., et al. “Functional network organization of the human brain.” Neuron 72.4 (2011): 665-678.

[2] Dosenbach N.U., Nardos B., et al. “Prediction of individual brain maturity using fMRI.”, 2010, Science 329, 1358-1361. Load fMRI data and Power atlas

We are going to use a single subject from the ADHD dataset.

from nilearn import datasets

adhd = datasets.fetch_adhd(n_subjects=1)

We store the paths to its functional image and the confounds file.

fmri_filename = adhd.func[0]
confounds_filename = adhd.confounds[0]
print('Functional image is {0},\nconfounds are {1}.'.format(fmri_filename,


Functional image is /home/kamalakar/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz,
confounds are /home/kamalakar/nilearn_data/adhd/data/0010042/0010042_regressors.csv.

We fetch the coordinates of Power atlas.

power = datasets.fetch_coords_power_2011()
print('Power atlas comes with {0}.'.format(power.keys()))


Power atlas comes with ['rois', 'description']. Compute within spheres averaged time-series

We can compute the mean signal within spheres of a fixed radius around a sequence of (x, y, z) coordinates with the object nilearn.input_data.NiftiSpheresMasker. So we collect the regions coordinates in a numpy array

import numpy as np

coords = np.vstack((power.rois['x'], power.rois['y'], power.rois['z'])).T

print('Stacked power coordinates in array of shape {0}.'.format(coords.shape))


Stacked power coordinates in array of shape (264, 3).

and define spheres masker, with small enough radius to avoid regions overlap.

from nilearn import  input_data

spheres_masker = input_data.NiftiSpheresMasker(
    seeds=coords, smoothing_fwhm=4, radius=5.,
    detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2.5)

Voxel-wise time-series within each sphere are averaged. The resulting signal is then prepared by the masker object: Detrended, cleaned from counfounds, band-pass filtered and standardized to 1 variance.

timeseries = spheres_masker.fit_transform(fmri_filename,
                                          confounds=confounds_filename) Estimate correlations

All starts with the estimation of the signals covariance matrix. Here the number of ROIs exceeds the number of samples,

print('time series has {0} samples'.format(timeseries.shape[0]))


time series has 176 samples

in which situation the graphical lasso sparse inverse covariance estimator captures well the covariance structure.

from sklearn.covariance import GraphLassoCV

covariance_estimator = GraphLassoCV(verbose=1)

We just fit our regions signals into the GraphLassoCV object



[GraphLassoCV] Done refinement  1 out of 4:   5s
[GraphLassoCV] Done refinement  2 out of 4:  18s
[GraphLassoCV] Done refinement  3 out of 4:  37s
[GraphLassoCV] Done refinement  4 out of 4:  52s

and get the ROI-to-ROI covariance matrix.

matrix = covariance_estimator.covariance_
print('Covariance matrix has shape {0}.'.format(matrix.shape))


Covariance matrix has shape (264, 264). Plot matrix and graph

We use matplotlib plotting functions to visualize our correlation matrix and display the graph of connections with nilearn.plotting.plot_connectome.

import matplotlib.pyplot as plt
from nilearn import plotting

plt.imshow(matrix, vmin=-1., vmax=1., cmap='RdBu_r', interpolation='nearest')
plt.title('Power correlation matrix')

# Tweak edge_threshold to keep only the strongest connections.
plotting.plot_connectome(matrix, coords, title='Power correlation graph',
                         edge_threshold='99.8%', node_size=20, colorbar=True)
  • ../../_images/sphx_glr_plot_sphere_based_connectome_001.png
  • ../../_images/sphx_glr_plot_sphere_based_connectome_002.png

Note the 1. on the matrix diagonal: These are the signals variances, set to 1. by the spheres_masker. Hence the covariance of the signal is a correlation matrix Connectome extracted from Dosenbach’s atlas

We repeat the same steps for Dosenbach’s atlas.

dosenbach = datasets.fetch_coords_dosenbach_2010()

coords = np.vstack((

spheres_masker = input_data.NiftiSpheresMasker(
    seeds=coords, smoothing_fwhm=4, radius=4.5,
    detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2.5)

timeseries = spheres_masker.fit_transform(fmri_filename,

covariance_estimator = GraphLassoCV()
matrix = covariance_estimator.covariance_

plt.imshow(matrix, vmin=-1., vmax=1., cmap='RdBu_r', interpolation='nearest')
plt.title('Dosenbach correlation matrix')

plotting.plot_connectome(matrix, coords, title='Dosenbach correlation graph',
                         edge_threshold="99.7%", node_size=20, colorbar=True)
  • ../../_images/sphx_glr_plot_sphere_based_connectome_003.png
  • ../../_images/sphx_glr_plot_sphere_based_connectome_004.png

We can easily identify the Dosenbach’s networks from the matrix blocks.

print('Dosenbach networks names are {0}'.format(np.unique(dosenbach.networks)))



Dosenbach networks names are ['cerebellum' 'cingulo-opercular' 'default' 'fronto-parietal' 'occipital'

Total running time of the script: ( 1 minutes 41.779 seconds)

Generated by Sphinx-Gallery