.. 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_extract_rois_statistical_maps.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_extract_rois_statistical_maps.py: Region Extraction using a t-statistical map (3D) ================================================ This example shows how to extract regions or separate the regions from a statistical map. We use localizer t-statistic maps from :func:`~nilearn.datasets.fetch_neurovault_auditory_computation_task` as an input image. The idea is to threshold an image to get foreground objects using a function :func:`~nilearn.image.threshold_img` and extract objects using a function :func:`~nilearn.regions.connected_regions`. .. GENERATED FROM PYTHON SOURCE LINES 19-21 Fetching t-statistic image of localizer contrasts by loading from datasets utilities .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python from nilearn.datasets import fetch_neurovault_auditory_computation_task localizer = fetch_neurovault_auditory_computation_task(timeout=30.0) tmap_filename = localizer.images[0] .. rst-class:: sphx-glr-script-out .. code-block:: none [fetch_neurovault_auditory_computation_task] Dataset created in /home/runner/nilearn_data/neurovault .. GENERATED FROM PYTHON SOURCE LINES 27-28 Threshold the t-statistic image by importing threshold function .. GENERATED FROM PYTHON SOURCE LINES 28-43 .. code-block:: Python from nilearn.image import threshold_img # Two types of strategies can be used from this threshold function # Type 1: strategy used will be based on scoreatpercentile threshold_percentile_img = threshold_img( tmap_filename, threshold="97%", copy=False, copy_header=True ) # Type 2: threshold strategy used will be based on image intensity # Here, threshold value should be within the limits i.e. less than max value. threshold_value_img = threshold_img( tmap_filename, threshold=3.0, copy=False, copy_header=True ) .. GENERATED FROM PYTHON SOURCE LINES 44-46 Visualization Showing thresholding results by importing plotting modules and its utilities .. GENERATED FROM PYTHON SOURCE LINES 46-68 .. code-block:: Python from nilearn.plotting import plot_stat_map, show # Showing percentile threshold image plot_stat_map( threshold_percentile_img, display_mode="z", cut_coords=5, title="Threshold image with string percentile", colorbar=False, ) # Showing intensity threshold image plot_stat_map( threshold_value_img, display_mode="z", cut_coords=5, title="Threshold image with intensity value", colorbar=False, ) show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_001.png :alt: plot extract rois statistical maps :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_002.png :alt: plot extract rois statistical maps :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 69-70 Extracting the regions by importing connected regions function .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. code-block:: Python from nilearn.regions import connected_regions regions_percentile_img, index = connected_regions( threshold_percentile_img, min_region_size=1500 ) regions_value_img, index = connected_regions( threshold_value_img, min_region_size=1500 ) .. GENERATED FROM PYTHON SOURCE LINES 81-82 Visualizing region extraction results .. GENERATED FROM PYTHON SOURCE LINES 82-99 .. code-block:: Python from nilearn.plotting import plot_prob_atlas images = [regions_percentile_img, regions_value_img] for image, strategy in zip(images, ["percentile", "image intensity"]): title = ( f"ROIs using {strategy} thresholding. " "Each ROI in same color is an extracted region" ) plot_prob_atlas( image, bg_img=tmap_filename, view_type="contours", display_mode="z", cut_coords=5, title=title, ) show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_003.png :alt: plot extract rois statistical maps :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_004.png :alt: plot extract rois statistical maps :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_rois_statistical_maps_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_axes.py:94: UserWarning: No contour levels were found within the data range. im = getattr(ax, type)( /home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_slicers.py:730: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. self._colorbar_ax = figure.add_axes(lt_wid_top_ht) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.716 seconds) **Estimated memory usage:** 108 MB .. _sphx_glr_download_auto_examples_06_manipulating_images_plot_extract_rois_statistical_maps.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.12.0?urlpath=lab/tree/notebooks/auto_examples/06_manipulating_images/plot_extract_rois_statistical_maps.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_extract_rois_statistical_maps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_extract_rois_statistical_maps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_extract_rois_statistical_maps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_