.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_advanced/plot_ica_resting_state.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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_07_advanced_plot_ica_resting_state.py: Independent Component Analysis (ICA) of fMRI timeseries ======================================================= This example applies the scikit-learn :class:`~sklearn.decomposition.FastICA` algorithm to :term:`fMRI` data from a movie-watching task, accessed via the :func:`~nilearn.datasets.fetch_development_fmri` fetcher. Note that any :sklearn:`unsupervised decomposition model ` --- or other latent-factor models --- can be accessed from `scikit-learn `_ and applied to the data by following the same procedure described in this example. For decomposition methods that are specifically tailored to :term:`fMRI` data, please refer to :ref:`sphx_glr_auto_examples_03_connectivity_plot_compare_decomposition.py`. .. GENERATED FROM PYTHON SOURCE LINES 22-28 Load the movie-watching dataset ------------------------------- Here we use only single subject :term:`fMRI` timeseries for computational efficiency. We quickly check the size of this timeseries. .. GENERATED FROM PYTHON SOURCE LINES 28-37 .. code-block:: Python from nilearn import datasets, image dataset = datasets.fetch_development_fmri(n_subjects=1) func_filename = dataset.func[0] # Print basic information on the dataset. img = image.load_img(dataset.func[0]) print(f"Functional nifti image (4D) is of shape: {img.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none [fetch_development_fmri] Dataset directory found: /home/runner/work/nilearn/nilearn/nilearn_data/development_fmri Functional nifti image (4D) is of shape: (50, 59, 50, 168) .. GENERATED FROM PYTHON SOURCE LINES 38-52 Minimally process the data -------------------------- This is fMRI timeseries data: the background has not been removed yet, thus we need to use `mask_strategy='epi'` to compute the mask from the EPI images. We further want to pass a smoothing kernel of 8mm (``smoothing_fwhm=8``) in order to spatially blur the data and improve our ability to capture smooth :term:`ICA` components. We therefore use a :class:`~nilearn.maskers.NiftiMasker` to apply these preprocessing steps and extract the processed signal. .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python from nilearn.maskers import NiftiMasker masker = NiftiMasker( smoothing_fwhm=8, standardize="zscore_sample", memory="nilearn_cache", memory_level=1, mask_strategy="epi", verbose=1, ) data_masked = masker.fit_transform(func_filename) .. rst-class:: sphx-glr-script-out .. code-block:: none [NiftiMasker.wrapped] Loading data from sub-pixar123_task-... [NiftiMasker.wrapped] Computing mask [NiftiMasker.wrapped] Resampling mask [NiftiMasker.wrapped] Finished fit .. GENERATED FROM PYTHON SOURCE LINES 65-78 Apply Independent Component Analysis (:term:`ICA`) -------------------------------------------------- We use :class:`~sklearn.decomposition.FastICA` to apply :term:`ICA` on this single-subject :term:`fMRI` time series. As the timeseries has only 168 volumes, we request a relatively small number of components by specifying ``n_components``. In real data analysis, we may want to instead set ``n_components=None`` to find as many components as the rank of the data. For more detail on :term:`ICA`, please refer to the :sklearn:`scikit-learn user guide `. .. GENERATED FROM PYTHON SOURCE LINES 78-90 .. code-block:: Python from sklearn.decomposition import FastICA n_components = 10 ica = FastICA( n_components=n_components, random_state=42, max_iter=2000, tol=0.01 ) components_masked = ica.fit_transform(data_masked.T).T # Normalize estimated components, for sensible thresholding. components_masked -= components_masked.mean(axis=0) components_masked /= components_masked.std(axis=0) .. GENERATED FROM PYTHON SOURCE LINES 91-99 Threshold and project the resulting components ---------------------------------------------- To obtain more visually interpretable components maps, we threshold all values below 0.8 and then use the ``inverse_transform`` method of our pre-defined :class:`~nilearn.maskers.NiftiMasker` object ``masker`` to project the thresholded maps back to the brain. .. GENERATED FROM PYTHON SOURCE LINES 99-108 .. code-block:: Python import numpy as np # First, threshold the components. components_masked[np.abs(components_masked) < 0.8] = 0 # Now invert the masking operation, # going from 2D to a 3D representation. component_img = masker.inverse_transform(components_masked) .. rst-class:: sphx-glr-script-out .. code-block:: none [NiftiMasker.inverse_transform] Computing image from signals ________________________________________________________________________________ [Memory] Calling nilearn.masking.unmask... unmask(array([[ 1.282833, ..., -2.579048], ..., [ 0. , ..., 0. ]]), ) ___________________________________________________________unmask - 0.0s, 0.0min .. GENERATED FROM PYTHON SOURCE LINES 109-111 Visualize the results --------------------- .. GENERATED FROM PYTHON SOURCE LINES 111-117 .. code-block:: Python from nilearn import image from nilearn.plotting import plot_stat_map, show # Use the mean image as a background. mean_img = image.mean_img(func_filename) .. GENERATED FROM PYTHON SOURCE LINES 118-123 We cherry-pick and plot two component images, the first showing obvious pulsatility-related noise in the CerebroSpinal Fluid (CSF; the first map) and the second with recognizable signal from the Default Mode Network (the second map). .. GENERATED FROM PYTHON SOURCE LINES 123-127 .. code-block:: Python plot_stat_map(image.index_img(component_img, 2), mean_img) plot_stat_map(image.index_img(component_img, 6), mean_img) show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/07_advanced/images/sphx_glr_plot_ica_resting_state_001.png :alt: plot ica resting state :srcset: /auto_examples/07_advanced/images/sphx_glr_plot_ica_resting_state_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/07_advanced/images/sphx_glr_plot_ica_resting_state_002.png :alt: plot ica resting state :srcset: /auto_examples/07_advanced/images/sphx_glr_plot_ica_resting_state_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 128-134 We can see that the generated components represent both signal and noise, underscoring the complex spatiotemporal patterns in real :term:`fMRI` time series. For decomposition methods that are specifically tailored to :term:`fMRI` data, please refer to :ref:`sphx_glr_auto_examples_03_connectivity_plot_compare_decomposition.py`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.272 seconds) **Estimated memory usage:** 258 MB .. _sphx_glr_download_auto_examples_07_advanced_plot_ica_resting_state.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/0.14.1?urlpath=lab/tree/notebooks/auto_examples/07_advanced/plot_ica_resting_state.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_ica_resting_state.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_ica_resting_state.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_ica_resting_state.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_