MultiLabel Segmentation
MultiLabelSegmentation – 4D label volume with group semantics.
This module provides:
MultiLabelSegmentation: Container of label groups backed by a 4D numpy array. Label values are globally unique across all groups; value 0 is reserved. Supports group-level image access, label properties editing, and value remapping.LabelGroup: A named collection of labels within aMultiLabelSegmentation.Label: A single label with value, name, color, opacity, visibility, and lock state.
- class mitk_workbench_remote.multilabel.Label(value=None, name='', *, color=(1.0, 1.0, 1.0), opacity=1.0, visible=True, locked=False, tracking_id=None, tracking_uid=None, description=None)
Bases:
objectA single label within a MultiLabelSegmentation.
Label value 0 is reserved as UNLABELED_VALUE and is rejected. When created with
value=None, the value is assigned byMultiLabelSegmentation.add_label()via_assign_value().- Parameters:
value (
int|None(default:None)) – Integer label value (>= 1), orNonefor deferred assignment.name (
str(default:'')) – Human-readable label name.color (
tuple[float,float,float] (default:(1.0, 1.0, 1.0))) – RGB color tuple with components in [0.0, 1.0].opacity (
float(default:1.0)) – Opacity in [0.0, 1.0].visible (
bool(default:True)) – Whether the label is visible.locked (
bool(default:False)) – Whether the label is locked for editing.tracking_id (
str|None(default:None)) – Optional external tracking identifier.tracking_uid (
str|None(default:None)) – Optional external tracking UID.description (
str|None(default:None)) – Optional free-text description.
- Raises:
ValueError – If
value == 0(reserved) orcolorcomponents out of range.
- class mitk_workbench_remote.multilabel.LabelGroup(name=None)
Bases:
objectA named collection of label values within a MultiLabelSegmentation.
Label objects themselves are stored in the parent
MultiLabelSegmentation._labelsregistry; this class only tracks the ordered list of label values belonging to the group.
- class mitk_workbench_remote.multilabel.MultiLabelSegmentation(*, groups, labels, group_images=None, spacing=None, origin=None, direction=None, properties=None, _shape=None)
Bases:
objectContainer of label groups backed by per-group spatial images.
Label values are globally unique across all groups. Value 0 is reserved as
UNLABELED_VALUE. Group pixel data is stored as individualImageobjects (Noneuntil set or lazily allocated byget_group_image()).Use
create()to build a new segmentation from scratch. The constructor is the primary path for I/O deserialization.- Parameters:
groups (
list[LabelGroup]) – Ordered list of label groups (each pre-populated with label IDs).labels (
dict[int,Label]) – Global label registry mapping value -> Label.group_images (
Sequence[Any|None] |None(default:None)) – Per-group pixel data. Filled withNoneif not given.spacing (
Sequence[float] |ndarray|None(default:None)) – Voxel spacing (3D).origin (
Sequence[float] |ndarray|None(default:None)) – World-space origin (3D).direction (
Sequence[Any] |ndarray|None(default:None)) – Direction cosine matrix (3x3).properties (
dict[str,Any] |None(default:None)) – Data-scope properties dict.
- Raises:
ValueError – On shape/geometry inconsistency or duplicate/missing label IDs.
- classmethod create(*, groups=None, reference=None, shape=None, spacing=None, origin=None, direction=None)
Create a new empty segmentation.
Either
referenceorshapemust be supplied.- Parameters:
groups (
list[LabelGroup] |None(default:None)) – Initial groups. Defaults to an empty list.reference (
Any|None(default:None)) – AnImagefrom which geometry is inherited (overrideable via spacing/origin/direction).shape (
tuple[int,...] |None(default:None)) – Spatial shape(x, y, z)used whenreferenceis not given.spacing (
Sequence[float] |None(default:None)) – Override spacing (defaults to reference geometry or (1, 1, 1)).origin (
Sequence[float] |None(default:None)) – Override origin (defaults to reference geometry or (0, 0, 0)).direction (
ndarray|None(default:None)) – Override direction (defaults to reference geometry or identity).
- Return type:
- Returns:
A new
MultiLabelSegmentationwith no pixel data.- Raises:
ValueError – If neither
referencenorshapeis given.
- property groups: list[LabelGroup]
Ordered list of label groups (copy).
- property labels: list[Label]
All Label objects across all groups, sorted by value.
- Raises:
ValueError – If any label still has
value is None. By construction,add_labelassigns a value before storing, so aNonehere indicates internal-state corruption — aliasing it onto the reservedUNLABELED_VALUE(0) would hide a real bug.
- get_property(key)
Get a property by key.
- set_property(key, value)
Set a property by key.
- remove_property(key)
Remove a property by key.
- to_mitk()
Convert to a
mitk.MultiLabelSegmentation(native MITK Python binding).Requires the
mitkpackage, which is typically only available inside a MITK-provided Python environment. Geometry, pixel data, and all label metadata (groups, labels, colors, lock state, etc.) are transferred via NRRD round-trip.Note: data-scope properties stored in
propertiesare not transferred, because the remote library’s NRRD writer does not embed arbitrary properties. For a full round-trip including data-scope properties, useDataNode.get_data()/DataNode.set_data()withas_type=DataRepresentation.MITK.- Return type:
- Returns:
A
mitk.MultiLabelSegmentationinstance.- Raises:
ImportError – If
mitkis not installed.
- classmethod from_mitk(mitk_seg)
Create from a
mitk.MultiLabelSegmentation(native MITK Python binding).Geometry, pixel data, and all label metadata are transferred via NRRD round-trip.
- Parameters:
mitk_seg (
Any) – A native MITK MultiLabelSegmentation object.- Return type:
- Returns:
A new
MultiLabelSegmentationinstance.- Raises:
ImportError – If
mitkis not installed.
- get_label(value)
Look up a label by value.
- get_group(index)
Return the group at the given index.
- Parameters:
index (
int) – Group index.- Return type:
- Returns:
The
LabelGroup.- Raises:
IndexError – If
indexis out of range.
- get_group_by_name(name)
Find a group by name using a linear search.
- Parameters:
name (
str) – Group name to search for.- Return type:
- Returns:
The first matching
LabelGroup, orNone.
- get_group_labels(index)
Return all Label objects belonging to a group.
- Parameters:
index (
int) – Group index.- Return type:
- Returns:
Ordered list of
Labelobjects.- Raises:
IndexError – If
indexis out of range.
- add_label(label, group)
Add a label to the specified group.
If
label.valueisNone, the next free integer >= 1 is assigned.- Parameters:
- Return type:
- Returns:
The label (with its value assigned).
- Raises:
ValueError – If the value is 0 or already exists in any group.
IndexError – If
groupis out of range.
- remove_label(value, *, clear_pixels=True)
Remove a label by value.
- Parameters:
- Raises:
ValueError – If
valueis not found in any group.- Return type:
- add_group(name=None)
Append a new empty group and return its index.
- get_group_image(index)
Return the pixel data for a group as an Image.
If no data has been set for this group, a zero-filled Image is created and cached. Edits to the returned Image’s array are reflected in subsequent calls.
- Parameters:
index (
int) – Group index.- Return type:
- Returns:
An
Imagewith this group’s pixels.- Raises:
IndexError – If
indexis out of range.ValueError – If shape is unknown and no pixel data has been set yet.
- set_group_image(index, data)
Set pixel data for a group.
- Parameters:
- Raises:
IndexError – If
indexis out of range.ValueError – If the array shape does not match the segmentation’s shape, ndim < 3, or dtype is not integer-compatible.
- Return type:
- import_group_image(index, data, *, value_map)
Import and remap pixel data into a group.
Source pixel values are remapped according to
value_map. Unmapped values pass through unchanged. Value 0 always maps to 0.- Parameters:
- Raises:
IndexError – If
indexis out of range.ValueError – If a target label name does not exist in the group, or a target integer value does not belong to the group.
- Return type: