.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_connectivity/plot_probabilistic_atlas_extraction.py" .. LINE NUMBERS ARE GIVEN BELOW. .. 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_probabilistic_atlas_extraction.py: Extracting signals of a probabilistic atlas of 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 movie-watching. The key to extract signals is to use the :class:`nilearn.maskers.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. .. GENERATED FROM PYTHON SOURCE LINES 23-25 Retrieve the atlas and the data -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-38 .. code-block:: default 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_development_fmri(n_subjects=1) print('First subject resting-state nifti image (4D) is located at: %s' % data.func[0]) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none First subject resting-state nifti image (4D) is located at: /home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz .. GENERATED FROM PYTHON SOURCE LINES 39-41 Extract the time series ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: default from nilearn.maskers import NiftiMapsMasker masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=True, memory='nilearn_cache', verbose=5) masker.fit(data.func[0]) time_series = masker.transform(data.func[0], confounds=data.confounds) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [NiftiMapsMasker.fit] 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:304: UserWarning: memory_level is currently set to 0 but a Memory object has been provided. Setting memory_level to 1. ________________________________________________________________________________ [Memory] Calling nilearn.image.resampling.resample_img... resample_img(, interpolation='continuous', target_shape=(50, 59, 50), target_affine=array([[ 4., 0., 0., -96.], [ 0., 4., 0., -132.], [ 0., 0., 4., -78.], [ 0., 0., 0., 1.]])) _____________________________________________________resample_img - 1.1s, 0.0min ________________________________________________________________________________ [Memory] Calling nilearn.maskers.base_masker._filter_and_extract... _filter_and_extract('/home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz', , { 'allow_overlap': True, 'detrend': False, 'dtype': None, 'high_pass': None, 'high_variance_confounds': False, 'low_pass': None, 'maps_img': '/home/nicolas/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii', 'mask_img': None, 'reports': True, 'smoothing_fwhm': None, 'standardize': True, 'standardize_confounds': True, 't_r': None, 'target_affine': None, 'target_shape': None}, confounds=[ '/home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_desc-reducedConfounds_regressors.tsv'], sample_mask=None, dtype=None, memory=Memory(location=nilearn_cache/joblib), memory_level=1, verbose=5) [NiftiMapsMasker.transform_single_imgs] Loading data from /home/nicolas/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz [NiftiMapsMasker.transform_single_imgs] Extracting region signals [NiftiMapsMasker.transform_single_imgs] Cleaning extracted signals _______________________________________________filter_and_extract - 1.8s, 0.0min .. GENERATED FROM PYTHON SOURCE LINES 49-54 We can generate an HTML report and visualize the components of the :class:`~nilearn.maskers.NiftiMapsMasker`. You can pass the indices of the spatial maps you want to include in the report in the order you want them to appear. Here, we only include maps 2, 6, 7, 16, and 21 in the report: .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: default report = masker.generate_report(displayed_maps=[2, 6, 7, 16, 21]) report .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/nicolas/anaconda3/envs/nilearn/lib/python3.8/site-packages/numpy/ma/core.py:2825: UserWarning: Warning: converting a masked element to nan. .. raw:: html

NiftiMapsMasker Class for masking of Niimg-like objects. NiftiMapsMasker is useful when data from overlapping volumes should be extracted (contrarily to :class:`nilearn.maskers.NiftiLabelsMasker`). Use case: Summarize brain signals from large-scale networks obtained by prior PCA or :term:`ICA`. Note that, Inf or NaN present in the given input images are automatically put to zero rather than considered as missing data.

Component

image

This reports shows the spatial maps provided to the mask.

The masker has 39 spatial maps in total.

Only 5 spatial maps are shown in this report.

Parameters
Parameter Value
allow_overlap True
detrend False
dtype None
high_pass None
high_variance_confounds False
low_pass None
maps_img /home/nicolas/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii
mask_img None
memory Memory(location=nilearn_cache/joblib)
memory_level 1
reports True
resampling_target data
smoothing_fwhm None
standardize True
standardize_confounds True
t_r None
verbose 5


.. GENERATED FROM PYTHON SOURCE LINES 58-60 `time_series` is now a 2D matrix, of shape (number of time points x number of regions) .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: default print(time_series.shape) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (168, 39) .. GENERATED FROM PYTHON SOURCE LINES 63-65 Build and display a correlation matrix --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 65-76 .. code-block:: default 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) .. image-sg:: /auto_examples/03_connectivity/images/sphx_glr_plot_probabilistic_atlas_extraction_001.png :alt: plot probabilistic atlas extraction :srcset: /auto_examples/03_connectivity/images/sphx_glr_plot_probabilistic_atlas_extraction_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 77-79 And now display the corresponding graph ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-89 .. code-block:: default 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) plotting.show() .. image-sg:: /auto_examples/03_connectivity/images/sphx_glr_plot_probabilistic_atlas_extraction_002.png :alt: plot probabilistic atlas extraction :srcset: /auto_examples/03_connectivity/images/sphx_glr_plot_probabilistic_atlas_extraction_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-96 3D visualization in a web browser --------------------------------- An alternative to :func:`nilearn.plotting.plot_connectome` is to use :func:`nilearn.plotting.view_connectome` that gives more interactive visualizations in a web browser. See :ref:`interactive-connectome-plotting` for more details. .. GENERATED FROM PYTHON SOURCE LINES 96-104 .. code-block:: default view = plotting.view_connectome(correlation_matrix, coords, edge_threshold='80%') # In a Jupyter notebook, if ``view`` is the output of a cell, it will # be displayed below the cell view .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: default # uncomment this to open the plot in a web browser: # view.open_in_browser() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 14.093 seconds) **Estimated memory usage:** 913 MB .. _sphx_glr_download_auto_examples_03_connectivity_plot_probabilistic_atlas_extraction.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_probabilistic_atlas_extraction.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_probabilistic_atlas_extraction.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_probabilistic_atlas_extraction.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_