.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_01_plotting_plot_carpet.py>`     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.

Fetching data from ADHD dataset
-------------------------------


.. code-block:: default

    from nilearn import datasets

    adhd_dataset = datasets.fetch_adhd(n_subjects=1)

    # Print basic information on the dataset
    print('First subject functional nifti image (4D) is at: %s' %
          adhd_dataset.func[0])  # 4D data





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    /home/nicolas/GitRepos/nilearn-fork/nilearn/datasets/func.py:240: VisibleDeprecationWarning: Reading unicode strings without specifying the encoding argument is deprecated. Set the encoding, use None for the system default.
      phenotypic = np.genfromtxt(phenotypic, names=True, delimiter=',',
    First subject functional nifti image (4D) is at: /home/nicolas/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz




Deriving a mask
---------------


.. code-block:: default

    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])








Visualizing global patterns over time
-------------------------------------


.. code-block:: default

    import matplotlib.pyplot as plt

    from nilearn.plotting import plot_carpet

    display = plot_carpet(adhd_dataset.func[0], mask_img)

    display.show()




.. image:: /auto_examples/01_plotting/images/sphx_glr_plot_carpet_001.png
    :alt: plot carpet
    :class: sphx-glr-single-img





Deriving a label-based mask
---------------------------
Create a gray matter/white matter/cerebrospinal fluid mask from
ICBM152 tissue probability maps.


.. code-block:: default

    import nibabel as nib
    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 = nib.Nifti1Image(
        discrete_version,
        atlas_img.affine,
        atlas_img.header,
    )








Visualizing global patterns, separated by tissue type
-----------------------------------------------------


.. code-block:: default

    import matplotlib.pyplot as plt

    from nilearn.plotting import plot_carpet

    fig, ax = plt.subplots(figsize=(10, 10))

    display = plot_carpet(
        adhd_dataset.func[0],
        discrete_atlas_img,
        mask_labels=map_labels,
        axes=ax,
    )

    fig.show()



.. image:: /auto_examples/01_plotting/images/sphx_glr_plot_carpet_002.png
    :alt: plot carpet
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    /home/nicolas/GitRepos/nilearn-fork/nilearn/image/resampling.py:531: UserWarning: Casting data from int32 to float32
      warnings.warn("Casting data from %s to %s" % (data.dtype.name, aux))
    Coercing atlas_values to <class 'int'>





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  8.196 seconds)


.. _sphx_glr_download_auto_examples_01_plotting_plot_carpet.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/01_plotting/plot_carpet.ipynb
      :alt: Launch binder
      :width: 150 px


  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_carpet.py <plot_carpet.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_carpet.ipynb <plot_carpet.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_