view_habitat_napari
Note
This page is a reference documentation. It only explains the function signature, and not how to use it. Please refer to the Habitat Guide and Python API guide (v2.0) for usage.
- view_habitat_napari(images: ndarray | Sequence[ndarray], labels: ndarray, *, opacity: float = 1.0, title: str = 'HABIT habitat', show: bool = True, viewer: Any | None = None, image_names: Sequence[str] | None = None, spacing: Sequence[float] | None = None, direction: Sequence[float] | None = None, display_convention: Literal['radiological', 'neurological', 'native'] = 'radiological') Any[source]
Open (or populate) a napari viewer with image layer(s) + habitat labels.
Habitat IDs are added as a napari labels layer so background
0stays transparent and discrete colours stay categorical. Each greyscale anatomy volume is an image layer underneath (multi-sequence studies can pass several arrays).Volumes are oriented using
directionanddisplay_convention(same policy asplot_habitat_overlay()for in-plane A-P / L-R). Image and habitat layers share the same flip +scaleso they stay aligned. Whendirectionis omitted for 3D data, LPS identity is assumed (not RAS). Axialzis not flipped by default so slider indices match file order.- Parameters:
images – Greyscale source volume (2D or 3D; NumPy
(z, y, x)order matchesImageVolume.data/ SimpleITK array convention), or a sequence of such volumes sharing the same spatial shape aslabels.labels – Integer habitat map with the same spatial shape as each image. Background should be
0; habitats are>= 1.opacity – Labels-layer opacity in
(0, 1]. Default1.0paints habitat colours opaquely on the anatomy (same policy asplot_habitat_overlay()). Pass a value in(0, 1)only for an explicit blend.title – Window title (ASCII-sanitised for journal-safe defaults).
show – When
True(default), show the Qt window and block until the user closes it (vianapari.run()). PassFalsefor headless / unit tests (napari.Viewer(show=False)) — then the caller owns lifetime and should callviewer.close().viewer – Existing napari
Viewerto reuse, orNoneto create one.image_names – Optional display names for image layers (e.g. file stems). Defaults to
"image"/"image_1","image_2", …spacing – Optional SimpleITK-order spacing
(x, y[, z])fromImageVolume.spacing. Converted to napariscale=(z, y, x)so anisotropic voxels display correctly.direction – Optional SimpleITK direction cosines (9 floats for 3D). Same layout as
ImageVolume.direction. Controls anterior/posterior and left/right in-plane flips.display_convention –
"radiological"(default),"neurological", or"native". Seehabit.viz.orientation.
- Returns:
The napari
Viewerinstance. Withshow=Truethis returns only after the event loop exits (window closed); withshow=Falseit returns immediately and the caller shouldviewer.close().- Raises:
HABITAPIError – On shape / opacity validation errors.
OptionalDependencyError – When napari (
viewextra) is not installed or is a broken/partial install withoutViewer.