8.4.2. Extracting signals of a probabilistic atlas of rest functional regions

This example extracts the signal on regions defined via a probabilistic atlas, to construct a functional connectome.

We use the MSDL atlas of functional regions in rest.

The key to extract signals is to use the nilearn.input_data.NiftiMapsMasker that can transform nifti objects to time series using a probabilistic atlas.

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']

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

print('First subject resting-state nifti image (4D) is located at: %s' %


First subject resting-state nifti image (4D) is located at: /home/kshitij/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz Extract the 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/kshitij/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]0.2s, 0.0min    : Loading filter_and_extract...
__________________________________filter_and_extract cache loaded - 0.0s, 0.0min

time_series is now a 2D matrix, of shape (number of time points x number of regions)



(176, 39) Build and display a correlation matrix

from nilearn.connectome import ConnectivityMeasure
correlation_measure = ConnectivityMeasure(kind='correlation')
correlation_matrix = correlation_measure.fit_transform([time_series])[0]

# Display the correlation matrix
import numpy as np
from nilearn import plotting
# Mask out the major diagonal
np.fill_diagonal(correlation_matrix, 0)
plotting.plot_matrix(correlation_matrix, labels=labels, colorbar=True,
                     vmax=0.8, vmin=-0.8)
../../_images/sphx_glr_plot_probabilistic_atlas_extraction_001.png And now display the corresponding graph

from nilearn import plotting
coords = atlas.region_coords

# We threshold to keep only the 20% of edges with the highest value
# because the graph is very dense
plotting.plot_connectome(correlation_matrix, coords,
                         edge_threshold="80%", colorbar=True)

../../_images/sphx_glr_plot_probabilistic_atlas_extraction_002.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(correlation_matrix, coords, threshold='80%')

# 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 1.825 seconds)

Generated by Sphinx-Gallery