.. 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: Multivariate decompositions: Independent component analysis of fMRI =================================================================== This example is meant to demonstrate nilearn as a low-level tools used to combine feature extraction with a multivariate decomposition algorithm for movie-watching. This example is a toy. To apply ICA to :term:`fMRI` timeseries data, it is advised to look at the example :ref:`sphx_glr_auto_examples_03_connectivity_plot_compare_decomposition.py`. The example here applies the scikit-learn :term:`ICA` to movie watching timeseries data. Note that following the code in the example, any unsupervised decomposition model, or other latent-factor models, can be applied to the data, as the scikit-learn API enables to exchange them as almost black box (though the relevant parameter for brain maps might no longer be given by a call to fit_transform). .. include:: ../../../examples/masker_note.rst .. GENERATED FROM PYTHON SOURCE LINES 25-26 Load movie watching dataset .. GENERATED FROM PYTHON SOURCE LINES 26-35 .. code-block:: Python from nilearn import datasets # Here we use only single subject to get faster-running code. dataset = datasets.fetch_development_fmri(n_subjects=1) func_filename = dataset.func[0] # print basic information on the dataset print(f"First subject functional nifti image (4D) is at: {dataset.func[0]}") .. rst-class:: sphx-glr-script-out .. code-block:: none First subject functional nifti image (4D) is at: /home/himanshu/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz .. GENERATED FROM PYTHON SOURCE LINES 36-37 Preprocess .. GENERATED FROM PYTHON SOURCE LINES 37-52 .. code-block:: Python from nilearn.maskers import NiftiMasker # 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 masker = NiftiMasker( smoothing_fwhm=8, memory="nilearn_cache", memory_level=1, mask_strategy="epi", standardize="zscore_sample", ) data_masked = masker.fit_transform(func_filename) .. GENERATED FROM PYTHON SOURCE LINES 53-54 Apply ICA .. GENERATED FROM PYTHON SOURCE LINES 54-73 .. code-block:: Python from sklearn.decomposition import FastICA n_components = 10 ica = FastICA(n_components=n_components, random_state=42) components_masked = ica.fit_transform(data_masked.T).T # Normalize estimated components, for thresholding to make sense components_masked -= components_masked.mean(axis=0) components_masked /= components_masked.std(axis=0) # Threshold import numpy as np components_masked[np.abs(components_masked) < 0.8] = 0 # Now invert the masking operation, going back to a full 3D # representation component_img = masker.inverse_transform(components_masked) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/.local/miniconda3/envs/nilearnpy/lib/python3.12/site-packages/sklearn/decomposition/_fastica.py:128: ConvergenceWarning: FastICA did not converge. Consider increasing tolerance or the maximum number of iterations. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 74-75 Visualize the results .. GENERATED FROM PYTHON SOURCE LINES 75-88 .. code-block:: Python from nilearn import image from nilearn.plotting import plot_stat_map, show # Show some interesting components # Use the mean as a background mean_img = image.mean_img(func_filename) plot_stat_map(image.index_img(component_img, 0), mean_img) plot_stat_map(image.index_img(component_img, 1), 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.777 seconds) **Estimated memory usage:** 533 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.10.4?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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_