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.

Every example loads the official pack with fetch_demo() (downloads once). CLI / YAML: Quickstart: run the demo (YAML + CLI) and CLI and YAML.

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 HabitatSpec one stage at a time.

A complete habitat analysis

A complete habitat analysis

A complete habitat analysis

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 Cohort; terms are defined on Load from directory.

The complete analysis starts from a 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/<subject>/<series>/<one file> and masks/<subject>/<roi>/<one file>. One series or several. The mask folder name can differ from the series name. Load from 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. Load from NIfTI files.

  • SimpleITK images already in memory — Load from SimpleITK.

  • NumPy arrays or deep-learning tensors — axis order (z, y, x), integer mask, 0 = background. Load from NumPy arrays.

DICOM, and series that still need resampling or bias correction, are not a load route. Preprocess them first (Preprocessing), 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).

Load from directory

Load from directory

Load from SimpleITK

Load from SimpleITK

Load from NumPy arrays

Load from NumPy arrays

Load from NIfTI files

Load from NIfTI files

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 complete analysis. Stage’s first argument is a label. Spec names the component.

Voxel features

Voxel features

Custom features

Custom features

Expression voxel features

Expression voxel features

Voxel texture and GPU

Voxel texture and GPU

Feature preprocessing

Feature preprocessing

Supervoxel feature extraction and acceleration

Supervoxel feature extraction and acceleration

Preprocessing voxel texture before clustering

Preprocessing voxel texture before clustering

Extracting voxel intensities

Extracting voxel intensities

Extracting a voxel texture

Extracting a voxel texture

Clustering habitats from a derived map

Clustering habitats from a derived map

Clustering habitats from a texture field

Clustering habitats from a texture field

Preprocessing features before clustering

Preprocessing features before clustering

Comparing habitat maps with and without preprocessing

Comparing habitat maps with and without preprocessing

Partitioning a ROI into supervoxels

Partitioning a ROI into supervoxels

Fitting a cohort habitat model

Fitting a cohort habitat model

Assigning habitat labels

Assigning habitat labels

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 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.

Volume and fractions

Volume and fractions

Multiregional spatial interaction (MSI)

Multiregional spatial interaction (MSI)

Intratumoral heterogeneity (ITH)

Intratumoral heterogeneity (ITH)

Graph features

Graph features

Per-habitat radiomics

Per-habitat radiomics

Whole-habitat radiomics

Whole-habitat radiomics

Deep-learning habitat embeddings

Deep-learning habitat embeddings

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 (6. Matching Habitat Labels).

Applying a saved model is the next section. Opening each stage is 2. Each stage.

Defining habitats in two steps

Defining habitats in two steps

Defining habitats inside each subject

Defining habitats inside each subject

Pooling voxels across the cohort

Pooling voxels across the cohort

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.

Applying a saved habitat model

Applying a saved habitat model

Applying a saved habitat model

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: Matching habitat labels across subjects. 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. align_habitat_map(), 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. 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: Matching habitat labels across fits and subjects.

Why habitat ids must be matched

Why habitat ids must be matched

Matching maps of the same voxels by overlap

Matching maps of the same voxels by overlap

Prototype matching step by step

Prototype matching step by step

Choosing the distance for prototype matching

Choosing the distance for prototype matching

Naming a new cohort with frozen prototypes

Naming a new cohort with frozen prototypes

What matching changes in a cohort feature table

What matching changes in a cohort feature table

Matching habitat labels across subjects

Matching habitat labels across subjects

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

Running the same study on each backend

Running the same study on each backend

Running the same study on each backend

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

Precise voxel features

Precise voxel features

Gallery generated by Sphinx-Gallery