:orphan: Habitat Guide ============= **Background.** A habitat is a sub-region inside the tumour (the ROI) whose voxels behave alike across the input images. Habitat analysis paints every ROI voxel with a habitat id, then turns the habitat map into numbers (volume, spatial mixing, heterogeneity, radiomics) that can enter statistics or a model. **Purpose.** Start with the complete analysis, then open one stage at a time. Copy the Python, swap ``DATA`` / ``MODALITIES`` / ``ROI``. Figures on a page come from that page's ``plot_*`` call. * :doc:`0. Complete analysis ` — one two-step study, from images to habitat maps, a feature table and a saved model. * :doc:`1. Data In ` — build the cohort from a folder, image files, SimpleITK images or NumPy arrays. * :doc:`2. Each stage ` — what extract, preprocess, partition, fit and assign each do, one page per option. * :doc:`3. Habitat Quantification ` — turn a habitat map into volume, MSI, ITH, graph and radiomics features. * :doc:`4. Three habitat designs ` — two-step, clustering inside each subject, or pooling voxels directly. * :doc:`5. Apply a saved model ` — label new patients with a saved ``.habitatmodel`` without fitting again. * :doc:`6. Matching Habitat Labels ` — make habitat ids comparable when they come from separate fits. * :doc:`7. Running a whole cohort ` — backends, failed subjects, checkpoint resume, timeouts, and when parallel is worth it. * :doc:`8. Precise Feature Screening ` — keep only features that stay repeatable under simulated retest. Every example loads the official pack with :func:`~habit.datasets.fetch_demo` (downloads once). CLI / YAML: :doc:`/tutorial/quickstart` and :doc:`/configuration/index`. .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. thumbnail-parent-div-close .. raw:: html
0. Complete analysis ==================== Read this page first. It is one two-step study from images to a feature table and a saved model. Later sections open the same :class:`~habit.spec.HabitatSpec` one stage at a time. :doc:`/auto_examples/00_full_pipeline/plot_01_full_pipeline` .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/00_full_pipeline/images/thumb/sphx_glr_plot_01_full_pipeline_thumb.png :alt: :doc:`/auto_examples/00_full_pipeline/plot_01_full_pipeline` .. raw:: html
A complete habitat analysis
.. thumbnail-parent-div-close .. raw:: html
1. Data In ========== **Background.** Every habitat analysis starts by telling HABIT which images and which tumour mask belong to which patient. **Purpose.** Each page turns files or arrays you already have into a :class:`~habit.contracts.Cohort`; terms are defined on :doc:`/auto_examples/01_data_in/plot_01_directory`. The :doc:`complete analysis ` starts from a :class:`~habit.contracts.Cohort`. Every route on this section ends in one. Each page shows two assemblies: ``Cohort([one_subject])`` and ``Cohort([several_subjects])``. Pass that cohort to ``fit`` / ``fit_predict``. A Python list is not a cohort. Pick the page that matches the files you already have. * **HABIT directory** — ``images///`` and ``masks///``. One series or several. The mask folder name can differ from the series name. :doc:`/auto_examples/01_data_in/plot_01_directory`. * **Loose NIfTI, NRRD, or MetaImage** — one pair or many. You choose the series name and the ROI name. Image and mask grids may differ. :doc:`/auto_examples/01_data_in/plot_04_nifti_files`. * **SimpleITK images already in memory** — :doc:`/auto_examples/01_data_in/plot_02_simpleitk`. * **NumPy arrays or deep-learning tensors** — axis order ``(z, y, x)``, integer mask, ``0`` = background. :doc:`/auto_examples/01_data_in/plot_03_numpy_arrays`. DICOM, and series that still need resampling or bias correction, are not a load route. Preprocess them first (:doc:`/how_to/preprocess`), then use the directory page. The official demo pack is already preprocessed. ``read_image`` / ``read_mask`` accept what SimpleITK reads (``.nii``, ``.nii.gz``, ``.nrrd``, ``.mha``, ``.mhd``). .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/01_data_in/images/thumb/sphx_glr_plot_01_directory_thumb.png :alt: :doc:`/auto_examples/01_data_in/plot_01_directory` .. raw:: html
Load from directory
.. raw:: html
.. only:: html .. image:: /auto_examples/01_data_in/images/thumb/sphx_glr_plot_02_simpleitk_thumb.png :alt: :doc:`/auto_examples/01_data_in/plot_02_simpleitk` .. raw:: html
Load from SimpleITK
.. raw:: html
.. only:: html .. image:: /auto_examples/01_data_in/images/thumb/sphx_glr_plot_03_numpy_arrays_thumb.png :alt: :doc:`/auto_examples/01_data_in/plot_03_numpy_arrays` .. raw:: html
Load from NumPy arrays
.. raw:: html
.. only:: html .. image:: /auto_examples/01_data_in/images/thumb/sphx_glr_plot_04_nifti_files_thumb.png :alt: :doc:`/auto_examples/01_data_in/plot_04_nifti_files` .. raw:: html
Load from NIfTI files
.. thumbnail-parent-div-close .. raw:: html
2. Each stage ============= **Background.** A two-step habitat analysis is a chain of stages: extract voxel features, preprocess them, partition each tumour into supervoxels, pool the cohort, fit one habitat model, assign labels. **Purpose.** Each page runs one stage on demo data so you can see its input, its output, and the parameters that matter. These pages open the stage list of the :doc:`complete analysis `. ``Stage``'s first argument is a label. ``Spec`` names the component. * **extract** — :doc:`/auto_examples/02_stages/plot_06_voxel_intensities`, :doc:`/auto_examples/02_stages/plot_01_feature_routes`, :doc:`/auto_examples/02_stages/plot_02_expression`, :doc:`/auto_examples/02_stages/plot_02_custom_features`, :doc:`/auto_examples/02_stages/plot_03_voxel_texture`, :doc:`/auto_examples/02_stages/plot_07_voxel_texture`, :doc:`/auto_examples/02_stages/plot_08_derived_map`, :doc:`/auto_examples/02_stages/plot_09_texture_habitats`. * **preprocess** — :doc:`/auto_examples/02_stages/plot_04_feature_preprocessing`, :doc:`/auto_examples/02_stages/plot_10_preprocess_features`, :doc:`/auto_examples/02_stages/plot_11_preprocess_compare`, :doc:`/auto_examples/02_stages/plot_06_texture_preprocessing`. * **partition** — :doc:`/auto_examples/02_stages/plot_12_supervoxels`, :doc:`/auto_examples/02_stages/plot_05_supervoxel_features`. * **fit** — :doc:`/auto_examples/02_stages/plot_13_fit_model`. * **assign** — :doc:`/auto_examples/02_stages/plot_14_assign_labels`. .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_01_feature_routes_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_01_feature_routes` .. raw:: html
Voxel features
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_02_custom_features_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_02_custom_features` .. raw:: html
Custom features
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_02_expression_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_02_expression` .. raw:: html
Expression voxel features
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_03_voxel_texture_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_03_voxel_texture` .. raw:: html
Voxel texture and GPU
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_04_feature_preprocessing_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_04_feature_preprocessing` .. raw:: html
Feature preprocessing
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_05_supervoxel_features_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_05_supervoxel_features` .. raw:: html
Supervoxel feature extraction and acceleration
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_06_texture_preprocessing_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_06_texture_preprocessing` .. raw:: html
Preprocessing voxel texture before clustering
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_06_voxel_intensities_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_06_voxel_intensities` .. raw:: html
Extracting voxel intensities
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_07_voxel_texture_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_07_voxel_texture` .. raw:: html
Extracting a voxel texture
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_08_derived_map_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_08_derived_map` .. raw:: html
Clustering habitats from a derived map
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_09_texture_habitats_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_09_texture_habitats` .. raw:: html
Clustering habitats from a texture field
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_10_preprocess_features_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_10_preprocess_features` .. raw:: html
Preprocessing features before clustering
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_11_preprocess_compare_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_11_preprocess_compare` .. raw:: html
Comparing habitat maps with and without preprocessing
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_12_supervoxels_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_12_supervoxels` .. raw:: html
Partitioning a ROI into supervoxels
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_13_fit_model_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_13_fit_model` .. raw:: html
Fitting a cohort habitat model
.. raw:: html
.. only:: html .. image:: /auto_examples/02_stages/images/thumb/sphx_glr_plot_14_assign_labels_thumb.png :alt: :doc:`/auto_examples/02_stages/plot_14_assign_labels` .. raw:: html
Assigning habitat labels
.. thumbnail-parent-div-close .. raw:: html
3. Habitat Quantification ========================= **Background.** A habitat map is a picture; statistics and prediction models need numbers. **Purpose.** Each page turns one subject's habitat map into a row of features (sizes, spatial mixing, fragmentation, network shape, radiomics, embeddings) that you can join to outcomes downstream. These metrics are the quantify stages of the :doc:`complete analysis ` (volume, MSI, ITH, graph). Each page refits a small cohort so it can be copied on its own, then computes one family from the label map. Quantify habitats with atomic functions: volume and fractions, multiregional spatial interaction (MSI, Wu et al. 2018), intratumoral heterogeneity (ITH), graph topology networks, per-habitat radiomics, whole-habitat radiomics, and deep-learning masked embeddings. .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_01_volume_fractions_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_01_volume_fractions` .. raw:: html
Volume and fractions
.. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_02_msi_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_02_msi` .. raw:: html
Multiregional spatial interaction (MSI)
.. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_03_ith_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_03_ith` .. raw:: html
Intratumoral heterogeneity (ITH)
.. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_04_graph_features_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_04_graph_features` .. raw:: html
Graph features
.. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_05_each_habitat_radiomics_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_05_each_habitat_radiomics` .. raw:: html
Per-habitat radiomics
.. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_06_whole_habitat_radiomics_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_06_whole_habitat_radiomics` .. raw:: html
Whole-habitat radiomics
.. raw:: html
.. only:: html .. image:: /auto_examples/03_quantify/images/thumb/sphx_glr_plot_07_deep_learning_embeddings_thumb.png :alt: :doc:`/auto_examples/03_quantify/plot_07_deep_learning_embeddings` .. raw:: html
Deep-learning habitat embeddings
.. thumbnail-parent-div-close .. raw:: html
4. Three habitat designs ======================== **Background.** A habitat design decides which rows are clustered and whether one model is shared by the whole cohort or fitted per subject. **Purpose.** Run the same data through each design and see what changes: shared vs. per-subject habitat ids, supervoxels vs. voxels. The complete analysis is two-step: ``partition``, then ``pool``, then ``fit``. These pages change that stage list and nothing else. ==================== ============= ======== ===================== Design ``partition`` ``pool`` ``fit`` runs on ==================== ============= ======== ===================== two-step yes yes supervoxels, cohort inside each subject no no voxels, one subject pooling voxels no yes voxels, cohort ==================== ============= ======== ===================== ``two_step_habitat``, ``one_step_habitat`` and ``direct_pooling_habitat`` build those lists. Habitat ids match across subjects only when ``fit`` ran once on the cohort. Otherwise match labels first (:doc:`/auto_examples/06_matching/index`). * **Two steps** — :doc:`/auto_examples/04_designs/plot_01_two_step`. * **Inside each subject** — :doc:`/auto_examples/04_designs/plot_02_inside_each_subject`. * **Pooling voxels** — :doc:`/auto_examples/04_designs/plot_03_pool_voxels`. Applying a saved model is the next section. Opening each stage is :doc:`/auto_examples/02_stages/index`. .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/04_designs/images/thumb/sphx_glr_plot_01_two_step_thumb.png :alt: :doc:`/auto_examples/04_designs/plot_01_two_step` .. raw:: html
Defining habitats in two steps
.. raw:: html
.. only:: html .. image:: /auto_examples/04_designs/images/thumb/sphx_glr_plot_02_inside_each_subject_thumb.png :alt: :doc:`/auto_examples/04_designs/plot_02_inside_each_subject` .. raw:: html
Defining habitats inside each subject
.. raw:: html
.. only:: html .. image:: /auto_examples/04_designs/images/thumb/sphx_glr_plot_03_pool_voxels_thumb.png :alt: :doc:`/auto_examples/04_designs/plot_03_pool_voxels` .. raw:: html
Pooling voxels across the cohort
.. thumbnail-parent-div-close .. raw:: html
5. Apply a saved model ====================== **Background.** A fitted habitat model can be saved and reused, so later or external subjects get the same habitat ids as the training cohort. **Purpose.** Save the model once, reload it, and label new subjects with the training centroids without refitting. Train the habitat definition, write a ``.habitatmodel`` file, and label later subjects without fitting again. :doc:`/auto_examples/05_apply/plot_04_apply_saved_model` .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/05_apply/images/thumb/sphx_glr_plot_04_apply_saved_model_thumb.png :alt: :doc:`/auto_examples/05_apply/plot_04_apply_saved_model` .. raw:: html
Applying a saved habitat model
.. thumbnail-parent-div-close .. raw:: html
6. Matching Habitat Labels ========================== **Background.** A clustering run numbers its habitats in arbitrary order, so the same tissue can be habitat 1 in one run or patient and habitat 3 in another (**label switching**). **Purpose.** These pages show how HABIT matches the ids so that Dice, volume fractions and cohort tables compare like with like. Calling it on a fitted study: :doc:`/auto_examples/06_matching/plot_07_match_labels`. The pages below show why ids switch and how each matcher works. Habitat ids from independent clusterings are arbitrary: habitat 1 of one fit can be habitat 3 of another. Anything that compares habitats by id (Dice, volume fractions, a cohort feature table) must match the ids first. HABIT has two matchers, chosen by what the two sides share: * **Voxel overlap** -- the maps label the *same voxels* (a restart, another ``k``, another feature set, a perturbed image, a second reader). Hungarian assignment on the voxel-overlap table. :func:`~habit.precision.align_habitat_map`, :func:`~habit.precision.habitat_stability`. * **Shared prototypes** -- the maps label *different subjects*. Each subject's habitat summaries are matched one-to-one onto ``K`` shared prototypes, iterated until stable; prototypes can be frozen to name a new cohort. :func:`~habit.precision.align_habitat_maps_to_prototypes`. A shared cohort model (two-step, direct pooling, an applied saved model) already uses one id space and needs neither. Formulas, proofs, and literature: :doc:`/reference/habitat_matching`. .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_01_label_switching_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_01_label_switching` .. raw:: html
Why habitat ids must be matched
.. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_02_overlap_cases_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_02_overlap_cases` .. raw:: html
Matching maps of the same voxels by overlap
.. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_03_prototype_steps_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_03_prototype_steps` .. raw:: html
Prototype matching step by step
.. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_04_prototype_metrics_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_04_prototype_metrics` .. raw:: html
Choosing the distance for prototype matching
.. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_05_frozen_prototypes_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_05_frozen_prototypes` .. raw:: html
Naming a new cohort with frozen prototypes
.. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_06_downstream_tables_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_06_downstream_tables` .. raw:: html
What matching changes in a cohort feature table
.. raw:: html
.. only:: html .. image:: /auto_examples/06_matching/images/thumb/sphx_glr_plot_07_match_labels_thumb.png :alt: :doc:`/auto_examples/06_matching/plot_07_match_labels` .. raw:: html
Matching habitat labels across subjects
.. thumbnail-parent-div-close .. raw:: html
7. Running a whole cohort ========================= **Background.** A real study has tens to hundreds of subjects. Running them is a scheduling question, not a scientific one: the backend and run policy change speed and failure handling, never the habitat labels. **Purpose.** Run the same study on the serial and process backends and check the labels match; see what happens when one subject fails (``on_subject_failure``), resume from checkpoints, set a per-subject timeout, cap workers to the GPUs, and learn when parallel is actually worth it (on a tiny cohort serial can be faster because Windows spawn start-up dominates). :doc:`/auto_examples/07_parallel/plot_01_backends` .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/07_parallel/images/thumb/sphx_glr_plot_01_backends_thumb.png :alt: :doc:`/auto_examples/07_parallel/plot_01_backends` .. raw:: html
Running the same study on each backend
.. thumbnail-parent-div-close .. raw:: html
8. Precise Feature Screening ============================ **Background.** Some voxel features change a lot when the same tumour is rescanned or computed with slightly different settings; habitats built on them are unstable. **Purpose.** Keep only features that agree under a simulated retest and across settings (measured by ICC), then check whether habitats built on them are more stable on this demo. Repeatability and reproducibility screening (Prior et al., 2024): simulated image retest perturbation (noise, translation, rotation) and ROI contour edge perturbation, evaluated across kernel radii and bin widths with multi-panel ICC forest plots. The main gallery page also clusters habitats on the same subject with and without the precise whitelist under the Appendix S2 retest: without precise, original and perturbed habitat maps disagree; with precise, mean Dice rises and labelled-voxel disagreement falls on this demo (modestly). .. raw:: html
.. raw:: html
.. thumbnail-parent-div-open .. raw:: html
.. only:: html .. image:: /auto_examples/08_precision/images/thumb/sphx_glr_plot_01_precise_features_thumb.png :alt: :doc:`/auto_examples/08_precision/plot_01_precise_features` .. raw:: html
Precise voxel features
.. thumbnail-parent-div-close .. raw:: html
.. toctree:: :hidden: :includehidden: /auto_examples/00_full_pipeline/index.rst /auto_examples/01_data_in/index.rst /auto_examples/02_stages/index.rst /auto_examples/03_quantify/index.rst /auto_examples/04_designs/index.rst /auto_examples/05_apply/index.rst /auto_examples/06_matching/index.rst /auto_examples/07_parallel/index.rst /auto_examples/08_precision/index.rst .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_