.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_manipulating_images/plot_nifti_simple.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_06_manipulating_images_plot_nifti_simple.py: Simple example of NiftiMasker use ================================= Here is a simple example of automatic mask computation using the nifti masker. The mask is computed and visualized. .. include:: ../../../examples/masker_note.rst .. GENERATED FROM PYTHON SOURCE LINES 13-14 Retrieve the brain development functional dataset .. GENERATED FROM PYTHON SOURCE LINES 14-23 .. code-block:: Python from nilearn import datasets dataset = datasets.fetch_development_fmri(n_subjects=1) func_filename = dataset.func[0] # print basic information on the dataset print(f"First functional nifti image (4D) is at: {func_filename}") .. rst-class:: sphx-glr-script-out .. code-block:: none First 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 24-25 Compute the mask .. GENERATED FROM PYTHON SOURCE LINES 25-40 .. code-block:: Python from nilearn.maskers import NiftiMasker # As this is raw movie watching based EPI, the background is noisy and we # cannot rely on the 'background' masking strategy. We need to use the 'epi' # one nifti_masker = NiftiMasker( standardize="zscore_sample", mask_strategy="epi", memory="nilearn_cache", memory_level=2, smoothing_fwhm=8, ) nifti_masker.fit(func_filename) mask_img = nifti_masker.mask_img_ .. GENERATED FROM PYTHON SOURCE LINES 41-42 Visualize the mask using the plot_roi method .. GENERATED FROM PYTHON SOURCE LINES 42-50 .. code-block:: Python from nilearn.image.image import mean_img from nilearn.plotting import plot_roi # calculate mean image for the background mean_func_img = mean_img(func_filename) plot_roi(mask_img, mean_func_img, display_mode="y", cut_coords=4, title="Mask") .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_nifti_simple_001.png :alt: plot nifti simple :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_nifti_simple_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-55 Visualize the mask using the 'generate_report' method This report can be displayed in a Jupyter Notebook, opened in-browser using the .open_in_browser() method, or saved to a file using the .save_as_html(output_filepath) method. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python report = nifti_masker.generate_report() report .. raw:: html

NiftiMasker Applying a mask to extract time-series from Niimg-like objects. NiftiMasker is useful when preprocessing (detrending, standardization, resampling, etc.) of in-mask :term:`voxels<voxel>` is necessary. Use case: working with time series of :term:`resting-state` or task maps.

image

This report shows the input Nifti image overlaid with the outlines of the mask (in green). We recommend to inspect the report for the overlap between the mask and its input image.

Parameters
Parameter Value
detrend False
dtype None
high_pass None
high_variance_confounds False
low_pass None
mask_args None
mask_img None
mask_strategy epi
memory Memory(location=nilearn_cache/joblib)
memory_level 2
reports True
runs None
smoothing_fwhm 8
standardize zscore_sample
standardize_confounds True
t_r None
target_affine None
target_shape None
verbose 0


.. GENERATED FROM PYTHON SOURCE LINES 59-60 Preprocess data with the NiftiMasker .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: Python nifti_masker.fit(func_filename) fmri_masked = nifti_masker.transform(func_filename) # fmri_masked is now a 2D matrix, (n_voxels x n_time_points) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/Desktop/nilearn_work/nilearn/nilearn/maskers/base_masker.py:123: UserWarning: Persisting input arguments took 0.59s 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. region_signals, aux = cache( .. GENERATED FROM PYTHON SOURCE LINES 65-66 Run an algorithm .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. 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(fmri_masked.T).T .. 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 73-74 Reverse masking, and display the corresponding map .. GENERATED FROM PYTHON SOURCE LINES 74-94 .. code-block:: Python components = nifti_masker.inverse_transform(components_masked) from nilearn.image import index_img from nilearn.image.image import mean_img # Visualize results from nilearn.plotting import plot_stat_map, show # calculate mean image for the background mean_func_img = mean_img(func_filename) plot_stat_map( index_img(components, 0), mean_func_img, display_mode="y", cut_coords=4, title="Component 0", ) show() .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_nifti_simple_002.png :alt: plot nifti simple :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_nifti_simple_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 17.455 seconds) **Estimated memory usage:** 898 MB .. _sphx_glr_download_auto_examples_06_manipulating_images_plot_nifti_simple.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/06_manipulating_images/plot_nifti_simple.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_nifti_simple.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_nifti_simple.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_