Low-level image I/O helpers
Convenience re-exports for image volumes and file I/O.
Canonical types live in habit.contracts.image. File I/O and resampling
live in habit.adapters.volume_io. Import this package from notebooks
or CLI helpers that need both in one place; L0-L3 code must import types from
habit.contracts and must not import this package (it pulls adapters).
User guide: Data model (habit.contracts). Prefer contracts volumes inside
pipelines; use these when you need SimpleITK-backed read / geometry
checks outside a Subject.
Classes
Define how an image/mask geometry mismatch is handled. |
|
Describe image/mask geometry compatibility and any correction applied. |
|
An image array with explicit physical-space metadata. |
|
An image-space segmentation mask with explicit label semantics. |
|
Pair one image and mask with the geometry result used by downstream code. |
Structured output from radiomics extraction for one image/mask pair. |
|
Structured batch output with successful feature rows and failures. |
Functions
Read an image file into a geometry-aware |
|
Read a mask file into a geometry-aware |
|
Return an explicit geometry comparison without changing either input. |
|
Validate or explicitly correct image/mask geometry according to |
Extract segment-based radiomics features from one image/mask pair. |
|
Extract radiomics features deterministically for an ordered image/mask batch. |
File I/O helpers (read_image / read_mask / geometry checks). Prefer
contracts volumes inside pipelines (Data model (habit.contracts)); use these when you
need SimpleITK-backed read / geometry checks outside a Subject.
from habit.image import (
GeometryPolicy,
ImageMaskPair,
align_image_mask,
read_image,
read_mask,
validate_geometry,
)
image = read_image("data/subj001/T2.nii.gz", modality="T2")
mask = read_mask("data/subj001/mask_T2.nii.gz")
report = validate_geometry(image, mask)
print(report.compatible, report.mismatches)
pair = align_image_mask(
ImageMaskPair(image, mask),
policy=GeometryPolicy.RESAMPLE_MASK,
)
aligned_image, aligned_mask = pair.image, pair.mask
print(pair.geometry_report)
GeometryPolicy modes
Policy |
Behaviour on mismatch |
|---|---|
|
Raise |
|
Emit |
|
Resample mask onto the image grid (nearest neighbour); report
|
|
Resample image onto the mask grid (linear); report
|
|
When shapes match, copy the image spacing, origin, and direction
onto the mask array without resampling
( |
Exports: GeometryPolicy, GeometryReport, ImageVolume,
MaskVolume, ImageMaskPair, read_image, read_mask,
validate_geometry, align_image_mask.
Warning
Top-level ImageVolume / MaskVolume here are the API types.
Pipeline code should use habit.contracts.ImageVolume /
habit.contracts.MaskVolume.
Low-level radiomics extraction
Component API (not the YAML workflow):
from habit.image import GeometryPolicy
from habit.radiomics.extract import extract_batch, extract_features
result = extract_features(image, mask, params="params.yaml")
batch = extract_batch(
cases,
params="params.yaml",
geometry_policy=GeometryPolicy.STRICT,
fail_fast=True, # default: raise on first pair failure
)
fail_fast=False keeps successful rows and records per-subject errors in
FeatureTableResult.failures (see Fault tolerance patterns).
Returns FeatureResult / FeatureTableResult.