Geometry
Note
This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the Habitat Guide and Python API guide (v2.0) for usage.
- class Geometry(shape: Tuple[int, ...], spacing: Tuple[float, ...], origin: Tuple[float, ...], direction: Tuple[float, ...], frame_of_reference: str | None = None)[source]
Bases:
objectSpatial definition shared by every volumetric object of one subject.
Two volumetric objects may only be combined when their geometries are compatible. Making geometry an explicit, comparable value is what lets HABIT accept images produced by other tools (nnU-Net, MONAI, 3D Slicer) without a directory convention acting as the implicit contract.
Axis-order convention follows the existing public
habit.imagecontract:shapeis the NumPy array shape in(z, y, x)order, whilespacing,originanddirectionkeep the SimpleITK physical-space axis order(x, y, z)so that round-tripping throughSimpleITK.Imagenever transposes metadata.- direction
Row-major direction cosine matrix, flattened (9 values for 3D volumes).
- Type:
Tuple[float, …]
- frame_of_reference
Optional identifier tying several series to the same physical space, used to detect silently mismatched registrations.
- Type:
str | None
- is_compatible_with(other: Geometry, *, tolerance: float = 1e-05, direction_tolerance: float = 0.0001) bool[source]
Report whether two geometries describe the same voxel grid.
Spacing and origin use
tolerance(default1e-5absolute). Direction cosines use the looserdirection_tolerance(default1e-4absolute): DICOM / ITK round-trips routinely differ by ~1e-5 in individual cosine entries without any meaningful grid misalignment, while a true axis swap or oblique mismatch remains far above1e-4.- Parameters:
other – Geometry to compare against.
tolerance – Absolute tolerance for spacing and origin.
direction_tolerance – Absolute tolerance for the flattened direction cosine matrix. Kept separate from
toleranceso spacing/origin stay strict while DICOM noise in direction is tolerated.
- Returns:
Truewhen the grids coincide within the stated tolerances.
- classmethod from_array(shape: Tuple[int, ...], *, spacing: Tuple[float, ...] | None = None, origin: Tuple[float, ...] | None = None, direction: Tuple[float, ...] | None = None, frame_of_reference: str | None = None) Geometry[source]
Build a geometry from an array shape with identity defaults.
- Parameters:
shape – Voxel grid size as the NumPy array shape
(z, y, x).spacing – Physical voxel size; defaults to 1 mm isotropic.
origin – Physical origin; defaults to the zero vector.
direction – Flattened direction cosine matrix; defaults to identity.
frame_of_reference – Optional shared-space identifier.
- Returns:
A geometry describing the grid.