.. 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_regions_labels_image.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_regions_labels_image.py: Breaking an atlas of labels in separated regions ================================================ This example shows how to use :class:`~nilearn.regions.connected_label_regions` to assign each spatially-separated region of the atlas a unique label. Indeed, often in a given atlas of labels, the same label (number) may be used in different connected regions, for instance a region in each hemisphere. If we want to operate on regions and not networks (for instance in signal extraction), it is useful to assign a different label to each region. We end up with a new atlas that has more labels, but each one points to a single region. We use the Yeo atlas as an example for labeling regions, :func:`~nilearn.datasets.fetch_atlas_yeo_2011` .. GENERATED FROM PYTHON SOURCE LINES 22-24 The original Yeo atlas ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-44 .. code-block:: Python # First we fetch the Yeo atlas from nilearn.datasets import fetch_atlas_yeo_2011 from nilearn.plotting import plot_roi, show atlas_yeo_2011 = fetch_atlas_yeo_2011(thickness="thick", n_networks=7) atlas_yeo = atlas_yeo_2011.maps # Let's now plot it plot_roi( atlas_yeo, title="Original Yeo atlas", cut_coords=(8, -4, 9), cmap="Paired", ) show() .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_001.png :alt: plot extract regions labels image :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [fetch_atlas_yeo_2011] Dataset found in /home/runner/nilearn_data/yeo_2011 .. GENERATED FROM PYTHON SOURCE LINES 45-50 The original Yeo atlas has 7 labels, that is indicated in the colorbar. The colorbar also shows the correspondence between the color and the label Note that these 7 labels correspond actually to networks that comprise several regions. We are going to split them up. .. GENERATED FROM PYTHON SOURCE LINES 52-57 Relabeling the atlas into separated regions ------------------------------------------- Now we use the connected_label_regions to break apart the networks of the Yeo atlas into separated regions .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python from nilearn.regions import connected_label_regions region_labels = connected_label_regions(atlas_yeo) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Plotting the new regions .. GENERATED FROM PYTHON SOURCE LINES 63-72 .. code-block:: Python plot_roi( region_labels, title="Relabeled Yeo atlas", cut_coords=(8, -4, 9), cmap="Paired", ) show() .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_002.png :alt: plot extract regions labels image :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-86 Note that the same cluster in original and labeled atlas could have different color, so, you cannot directly compare colors. However, you can see that the regions in the left and right hemispheres now have different colors. For some regions it is difficult to tell apart visually, as the colors are too close on the colormap (eg in the blue: regions labeled around 3). Also, we can see that there are many more labels: the colorbar goes up to 49. The 7 networks of the Yeo atlas are now broken up into 49 ROIs. You can save the new atlas to a nifti file using to_filename method. .. GENERATED FROM PYTHON SOURCE LINES 86-95 .. code-block:: Python from pathlib import Path output_dir = Path.cwd() / "results" / "plot_extract_regions_labels_image" output_dir.mkdir(exist_ok=True, parents=True) print(f"Output will be saved to: {output_dir}") region_labels.to_filename(output_dir / "relabeled_yeo_atlas.nii.gz") .. rst-class:: sphx-glr-script-out .. code-block:: none Output will be saved to: /home/runner/work/nilearn/nilearn/examples/06_manipulating_images/results/plot_extract_regions_labels_image .. GENERATED FROM PYTHON SOURCE LINES 96-101 Different connectivity modes ---------------------------- Using the parameter connect_diag=False we separate in addition two regions that are connected only along the diagonal. .. GENERATED FROM PYTHON SOURCE LINES 101-114 .. code-block:: Python region_labels_not_diag = connected_label_regions(atlas_yeo, connect_diag=False) plot_roi( region_labels_not_diag, title="Relabeling and connect_diag=False", cut_coords=(8, -4, 9), cmap="Paired", ) show() .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_003.png :alt: plot extract regions labels image :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 115-119 A consequence of using connect_diag=False is that we can get a lot of small regions, around 110 judging from the colorbar. Hence we suggest use connect_diag=True .. GENERATED FROM PYTHON SOURCE LINES 121-127 Parameter min_size ------------------ In the above, we get around 110 regions, but many of these are very small. We can remove them with the min_size parameter, keeping only the regions larger than 100mm^3. .. GENERATED FROM PYTHON SOURCE LINES 127-139 .. code-block:: Python region_labels_min_size = connected_label_regions( atlas_yeo, min_size=100, connect_diag=False ) plot_roi( region_labels_min_size, title="Relabeling and min_size", cut_coords=(8, -4, 9), cmap="Paired", ) show() .. image-sg:: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_004.png :alt: plot extract regions labels image :srcset: /auto_examples/06_manipulating_images/images/sphx_glr_plot_extract_regions_labels_image_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 26.663 seconds) **Estimated memory usage:** 722 MB .. _sphx_glr_download_auto_examples_06_manipulating_images_plot_extract_regions_labels_image.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_regions_labels_image.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_extract_regions_labels_image.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_extract_regions_labels_image.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_extract_regions_labels_image.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_