.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\03_quantify\plot_06_whole_habitat_radiomics.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_03_quantify_plot_06_whole_habitat_radiomics.py: Whole-habitat radiomics ======================= **Background.** Per-habitat radiomics describes the MRI inside each habitat. Whole-habitat radiomics instead describes the habitat map itself: the label image (habitat ids as grey values) is fed to PyRadiomics as the image, and the whole labelled region as the mask. **Purpose.** You get one row of features describing the partition (here first-order ``Mean`` / ``Entropy`` of the labels and ``shape`` features such as ``Sphericity`` and ``SurfaceArea``), a habitat overlay, and a map of local label entropy. **Key terms.** * **shape feature** -- geometry of the labelled region (e.g. how close it is to a sphere, its surface area), independent of intensity. * **label entropy** -- how mixed the ids are in a small window around each voxel; zero inside one uniform habitat, higher where habitats (or the background at the tumour edge) meet. * **habitat / one-step habitats** -- see :doc:`/auto_examples/03_quantify/plot_01_volume_fractions` and :doc:`/auto_examples/04_designs/plot_02_inside_each_subject`. Quantify the **shape and spatial distribution of the partition map itself** using :class:`~habit.habitat_features.WholeHabitatRadiomicsFeatures`. The habitat label image plays both intensity and mask roles. .. GENERATED FROM PYTHON SOURCE LINES 32-34 One-step habitats, then whole-map PyRadiomics on the label field. sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 34-72 .. code-block:: Python from pathlib import Path from typing import Any, Dict import matplotlib.pyplot as plt import numpy as np from habit.contracts import MaskVolume, cohort_from_directory from habit.datasets import fetch_demo from habit.habitat_features import WholeHabitatRadiomicsFeatures from habit.kernels import local_entropy_map from habit.recipes import one_step_habitat from habit.viz import plot_habitat_overlay, plot_voxel_texture_slice DATA = fetch_demo() MODALITIES = ("LAP",) ROI = "LAP" cohort = cohort_from_directory(DATA, modalities=MODALITIES, roi=ROI)[:1] result = one_step_habitat( modalities=MODALITIES, n_habitats=3, random_seed=0, roi=ROI ).fit_predict(cohort) subject = cohort[0] habitat_map = result.habitat_maps[0] params: Dict[str, Any] = { "imageType": {"Original": {}}, "featureClass": { "firstorder": ["Mean", "Entropy"], "shape": ["Sphericity", "SurfaceArea"], }, # binWidth=1 keeps each integer habitat id in its own grey-level bin. "setting": {"binWidth": 1, "voxelArrayShift": 0}, } table = WholeHabitatRadiomicsFeatures(params=params)(subject, habitat_map) row = table.frame.iloc[0] print("Whole-habitat radiomics:") print(row.to_string()) row .. rst-class:: sphx-glr-script-out .. code-block:: none HABIT demo data (cached) DATA (preprocessed root): C:\Users\dongm\.habit_data\demo-data-v1\preprocessed On-disk inventory of this folder: subjects (5): subj001, subj002, subj003, subj004, subj005 image series: LAP, PVP, delay_3min, pre_contrast mask keys: LAP, PVP, delay_3min, pre_contrast example image: images/subj001/delay_3min/WATER__BH_Ax_LAVA_Flex_3min_Series0012.nrrd example mask: masks/subj001/delay_3min/WATER__BH_Ax_LAVA_Flex_10min_Series0017_mask.nrrd Your own data must use the same folder tree (change IDs / series names): DATA/ images/// masks/// Then load it with the same call the demos use: cohort = cohort_from_directory(DATA, modalities=("LAP",), roi="LAP") Swap DATA / modalities / roi to match your tree. Mask key is often the same as one image series (here LAP). Cohort.map[_DefineAndLabelWithinSubject]: 0%| | 0/1 [00:00 0).astype(np.uint8), habitat_map.geometry, roi_name=ROI, ) fig = plot_voxel_texture_slice( entropy, anatomy=image_vol, roi_mask=habitat_roi, axis=0, crop_to="roi", title="local entropy of habitat labels", feature_label="label entropy", ) fig.savefig("out/whole_habitat_radiomics_texture.png", dpi=150, bbox_inches="tight") plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/03_quantify/images/sphx_glr_plot_06_whole_habitat_radiomics_001.png :alt: habitats :srcset: /auto_examples/03_quantify/images/sphx_glr_plot_06_whole_habitat_radiomics_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/03_quantify/images/sphx_glr_plot_06_whole_habitat_radiomics_002.png :alt: local entropy of habitat labels :srcset: /auto_examples/03_quantify/images/sphx_glr_plot_06_whole_habitat_radiomics_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none F:\work\habit_project\habit\viz\habitat_overlay.py:806: UserWarning: Display geometry conflict: image/anatomy direction does not match mask/label direction. Using the mask/label direction so coronal/sagittal superior-up follows the labelled anatomy. Pass direction= to override. resolved_direction, resolved_spacing = resolve_display_geometry( F:\work\habit_project\habit\viz\voxel_texture.py:1019: UserWarning: Display geometry conflict: image/anatomy direction does not match mask/label direction. Using the mask/label direction so coronal/sagittal superior-up follows the labelled anatomy. Pass direction= to override. direction, spacing = resolve_display_geometry( .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 22.960 seconds) .. _sphx_glr_download_auto_examples_03_quantify_plot_06_whole_habitat_radiomics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_06_whole_habitat_radiomics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_06_whole_habitat_radiomics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_06_whole_habitat_radiomics.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_