.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code or to run this example in your browser via Binder
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_examples_03_connectivity_plot_atlas_comparison.py:
Comparing connectomes on different reference atlases
====================================================
This examples shows how to turn a parcellation into connectome for
visualization. This requires choosing centers for each parcel
or network, via :func:`nilearn.plotting.find_parcellation_cut_coords` for
parcellation based on labels and
:func:`nilearn.plotting.find_probabilistic_atlas_cut_coords` for
parcellation based on probabilistic values.
In the intermediary steps, we make use of
:class:`nilearn.input_data.NiftiLabelsMasker` and
:class:`nilearn.input_data.NiftiMapsMasker` to extract time series from nifti
objects using different parcellation atlases.
The time series of all subjects of the brain development dataset are concatenated and
given directly to :class:`nilearn.connectome.ConnectivityMeasure` for
computing parcel-wise correlation matrices for each atlas across all subjects.
Mean correlation matrix is displayed on glass brain on extracted coordinates.
# author: Amadeus Kanaan
Load atlases
-------------
.. code-block:: default
from nilearn import datasets
yeo = datasets.fetch_atlas_yeo_2011()
print('Yeo atlas nifti image (3D) with 17 parcels and liberal mask is located '
'at: %s' % yeo['thick_17'])
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Yeo atlas nifti image (3D) with 17 parcels and liberal mask is located at: /home/nicolas/nilearn_data/yeo_2011/Yeo_JNeurophysiol11_MNI152/Yeo2011_17Networks_MNI152_FreeSurferConformed1mm_LiberalMask.nii.gz
Load functional data
--------------------
.. code-block:: default
data = datasets.fetch_development_fmri(n_subjects=10)
print('Functional nifti images (4D, e.g., one subject) are located at : %r'
% data['func'][0])
print('Counfound csv files (of same subject) are located at : %r'
% data['confounds'][0])
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Functional nifti images (4D, e.g., one subject) are located at : '/home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz'
Counfound csv files (of same subject) are located at : '/home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_desc-reducedConfounds_regressors.tsv'
Extract coordinates on Yeo atlas - parcellations
------------------------------------------------
.. code-block:: default
from nilearn.input_data import NiftiLabelsMasker
from nilearn.connectome import ConnectivityMeasure
# ConenctivityMeasure from Nilearn uses simple 'correlation' to compute
# connectivity matrices for all subjects in a list
connectome_measure = ConnectivityMeasure(kind='correlation')
# useful for plotting connectivity interactions on glass brain
from nilearn import plotting
# create masker to extract functional data within atlas parcels
masker = NiftiLabelsMasker(labels_img=yeo['thick_17'], standardize=True,
memory='nilearn_cache')
# extract time series from all subjects and concatenate them
time_series = []
for func, confounds in zip(data.func, data.confounds):
time_series.append(masker.fit_transform(func, confounds=confounds))
# calculate correlation matrices across subjects and display
correlation_matrices = connectome_measure.fit_transform(time_series)
# Mean correlation matrix across 10 subjects can be grabbed like this,
# using connectome measure object
mean_correlation_matrix = connectome_measure.mean_
# grab center coordinates for atlas labels
coordinates = plotting.find_parcellation_cut_coords(labels_img=yeo['thick_17'])
# plot connectome with 80% edge strength in the connectivity
plotting.plot_connectome(mean_correlation_matrix, coordinates,
edge_threshold="80%",
title='Yeo Atlas 17 thick (func)')
.. image:: /auto_examples/03_connectivity/images/sphx_glr_plot_atlas_comparison_001.png
:alt: plot atlas comparison
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Plot a directed connectome - asymmetric connectivity measure
-----------------------------------------------------------------
In this section, we use the lag-1 correlation as the connectivity
measure, which leads to an asymmetric connectivity matrix.
The plot_connectome function accepts both symmetric and asymmetric
matrices, but plot the latter as a directed graph.
.. code-block:: default
import numpy as np
# Define a custom function to compute lag correlation on the time series
def lag_correlation(time_series, lag):
n_subjects = len(time_series)
n_samples, n_features = time_series[0].shape
lag_cor = np.zeros((n_subjects, n_features, n_features))
for subject, serie in enumerate(time_series):
for i in range(n_features):
for j in range(n_features):
if lag == 0:
lag_cor[subject, i, j] = np.corrcoef(serie[:, i],
serie[:, j])[0, 1]
else:
lag_cor[subject, i, j] = np.corrcoef(serie[lag:, i],
serie[:-lag, j])[0, 1]
return np.mean(lag_cor, axis=0)
# Compute lag-0 and lag-1 correlations and plot associated connectomes
for lag in [0, 1]:
lag_correlation_matrix = lag_correlation(time_series, lag)
plotting.plot_connectome(lag_correlation_matrix, coordinates,
edge_threshold="90%",
title='Lag-{} correlation'.format(
lag))
.. rst-class:: sphx-glr-horizontal
*
.. image:: /auto_examples/03_connectivity/images/sphx_glr_plot_atlas_comparison_002.png
:alt: plot atlas comparison
:class: sphx-glr-multi-img
*
.. image:: /auto_examples/03_connectivity/images/sphx_glr_plot_atlas_comparison_003.png
:alt: plot atlas comparison
:class: sphx-glr-multi-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/displays.py:2205: UserWarning: 'adjacency_matrix' is not symmetric. A directed graph will be plotted.
warnings.warn(("'adjacency_matrix' is not symmetric. "
Load probabilistic atlases - extracting coordinates on brain maps
-----------------------------------------------------------------
.. code-block:: default
dim = 64
difumo = datasets.fetch_atlas_difumo(dimension=dim,
resolution_mm=2)
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
/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)
Iterate over fetched atlases to extract coordinates - probabilistic
-------------------------------------------------------------------
.. code-block:: default
from nilearn.input_data import NiftiMapsMasker
# create masker to extract functional data within atlas parcels
masker = NiftiMapsMasker(maps_img=difumo.maps, standardize=True,
memory='nilearn_cache')
# extract time series from all subjects and concatenate them
time_series = []
for func, confounds in zip(data.func, data.confounds):
time_series.append(masker.fit_transform(func, confounds=confounds))
# calculate correlation matrices across subjects and display
correlation_matrices = connectome_measure.fit_transform(time_series)
# Mean correlation matrix across 10 subjects can be grabbed like this,
# using connectome measure object
mean_correlation_matrix = connectome_measure.mean_
# grab center coordinates for probabilistic atlas
coordinates = plotting.find_probabilistic_atlas_cut_coords(maps_img=difumo.maps)
# plot connectome with 85% edge strength in the connectivity
plotting.plot_connectome(mean_correlation_matrix, coordinates,
edge_threshold="85%",
title='DiFuMo with {0} dimensions (probabilistic)'.format(dim))
plotting.show()
.. image:: /auto_examples/03_connectivity/images/sphx_glr_plot_atlas_comparison_004.png
:alt: plot atlas comparison
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/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 "
/home/nicolas/GitRepos/nilearn-fork/nilearn/input_data/nifti_maps_masker.py:306: UserWarning: Persisting input arguments took 1.08s to run.
If this happens often in your code, it can cause performance problems
(results will be correct in all cases).
The reason for this is probably some large input arguments for a wrapped
function (e.g. large strings).
THIS IS A JOBLIB ISSUE. If you can, kindly provide the joblib's team with an
example so that they can fix the problem.
self._resampled_maps_img_ = self._cache(image.resample_img)(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
/home/nicolas/GitRepos/nilearn-fork/nilearn/image/image.py:1106: FutureWarning: The parameter "sessions" will be removed in 0.9.0 release of Nilearn. Please use the parameter "runs" instead.
data = signal.clean(
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 2 minutes 30.831 seconds)
.. _sphx_glr_download_auto_examples_03_connectivity_plot_atlas_comparison.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: binder-badge
.. image:: images/binder_badge_logo.svg
:target: https://mybinder.org/v2/gh/nilearn/nilearn.github.io/main?filepath=examples/auto_examples/03_connectivity/plot_atlas_comparison.ipynb
:alt: Launch binder
:width: 150 px
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_atlas_comparison.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_atlas_comparison.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_