.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_plotting/plot_carpet.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_01_plotting_plot_carpet.py: Visualizing global patterns with a carpet plot ============================================== A common quality control step for functional MRI data is to visualize the data over time in a carpet plot (also known as a Power plot or a grayplot). The :func:`nilearn.plotting.plot_carpet()` function generates a carpet plot from a 4D functional image. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Fetching data from ADHD dataset ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 15-30 .. code-block:: Python from nilearn import datasets from nilearn.plotting import plot_carpet adhd_dataset = datasets.fetch_adhd(n_subjects=1) # plot_carpet can infer TR from the image header, # but preprocessing can often overwrite that particular header field, # so we will be explicit. t_r = 2.0 # Print basic information on the dataset print( f"First subject functional nifti image (4D) is at: {adhd_dataset.func[0]}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none First subject functional nifti image (4D) is at: /home/himanshu/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz .. GENERATED FROM PYTHON SOURCE LINES 31-33 Deriving a mask --------------- .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: Python from nilearn import masking # Build an EPI-based mask because we have no anatomical data mask_img = masking.compute_epi_mask(adhd_dataset.func[0]) .. GENERATED FROM PYTHON SOURCE LINES 39-41 Visualizing global patterns over time ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-53 .. code-block:: Python import matplotlib.pyplot as plt display = plot_carpet( adhd_dataset.func[0], mask_img, t_r=t_r, standardize="zscore_sample", title="global patterns over time", ) display.show() .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_carpet_001.png :alt: global patterns over time :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_carpet_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-58 Deriving a label-based mask --------------------------- Create a gray matter/white matter/cerebrospinal fluid mask from ICBM152 tissue probability maps. .. GENERATED FROM PYTHON SOURCE LINES 58-72 .. code-block:: Python import numpy as np from nilearn import image atlas = datasets.fetch_icbm152_2009() atlas_img = image.concat_imgs((atlas["gm"], atlas["wm"], atlas["csf"])) map_labels = {"Gray Matter": 1, "White Matter": 2, "Cerebrospinal Fluid": 3} atlas_data = atlas_img.get_fdata() discrete_version = np.argmax(atlas_data, axis=3) + 1 discrete_version[np.max(atlas_data, axis=3) == 0] = 0 discrete_atlas_img = image.new_img_like(atlas_img, discrete_version) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/Desktop/nilearn_work/nilearn/examples/01_plotting/plot_carpet.py:69: UserWarning: Data array used to create a new image contains 64-bit ints. This is likely due to creating the array with numpy and passing `int` as the `dtype`. Many tools such as FSL and SPM cannot deal with int64 in Nifti images, so for compatibility the data has been converted to int32. discrete_atlas_img = image.new_img_like(atlas_img, discrete_version) .. GENERATED FROM PYTHON SOURCE LINES 73-75 Visualizing global patterns, separated by tissue type ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 75-92 .. code-block:: Python from nilearn.plotting import plot_carpet fig, ax = plt.subplots(figsize=(10, 10)) display = plot_carpet( adhd_dataset.func[0], discrete_atlas_img, t_r=t_r, mask_labels=map_labels, axes=ax, cmap="gray", standardize="zscore_sample", title="global patterns over time separated by tissue type", ) plt.show() .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_carpet_002.png :alt: global patterns over time separated by tissue type :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_carpet_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/.local/miniconda3/envs/nilearnpy/lib/python3.12/site-packages/joblib/memory.py:353: UserWarning: Casting data from int32 to float32 return self.func(*args, **kwargs) Coercing atlas_values to .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.537 seconds) **Estimated memory usage:** 913 MB .. _sphx_glr_download_auto_examples_01_plotting_plot_carpet.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/main?urlpath=lab/tree/notebooks/auto_examples/01_plotting/plot_carpet.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_carpet.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_carpet.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_