Skip to content

LabelLookup

voxel.LabelLookup(mapping=None)

Bases: dict

An ordered lookup table mapping integer label values to Label entries.

Parameters:

  • mapping (dict, default: None ) –

    Initial integer-to-label entries.

device: torch.device property

The device of the label colors and cached lookup tensor.

indices_tensor: torch.Tensor property

A cached 1D tensor of the ordered label values (keys), on the table's device. Rebuilt automatically when the keys change.

indices: list[int] property

The ordered list of integer label values (keys) in the table.

to(device) -> LabelLookup

Return a copy of the lookup table with colors and cache on a new device.

Parameters:

  • device (device) –

    The target device.

Returns:

cpu() -> LabelLookup

Return a copy of the lookup table on the CPU.

cuda() -> LabelLookup

Return a copy of the lookup table on the current CUDA device.

add(index, name, color=None) -> None

Add a label entry to the lookup table.

Parameters:

  • index (int) –

    The integer label value.

  • name (str) –

    The human-readable name of the label.

  • color (Tensor or list or tuple, default: None ) –

    An RGB color in [0, 1].

search(query, exact=False) -> list[tuple[int, Label]]

Search for label entries by name.

Parameters:

  • query (str) –

    The name (or substring) to search for, case-insensitive.

  • exact (bool, default: False ) –

    If True, require an exact (case-insensitive) name match rather than a substring match.

Returns:

  • list of (int, Label

    The matching (index, label) pairs.

colors(default=(0, 0, 0)) -> torch.Tensor

Stack the label colors into an \((N, 3)\) tensor in insertion order.

Parameters:

  • default (Tensor or list or tuple, default: (0, 0, 0) ) –

    The RGB color to substitute for any label that has no color.

Returns:

  • Tensor

    An \((N, 3)\) float tensor of RGB colors in [0, 1].

save(filename, fmt=None) -> None

Save the lookup table to a file.

Parameters:

  • filename (PathLike) –

    The path to the file to save.

  • fmt (str, default: None ) –

    The format of the file. If None, the format is determined by the file extension.

load(filename, fmt=None) -> LabelLookup classmethod

Load a lookup table from a file.

Parameters:

  • filename (PathLike) –

    The path to the file to load.

  • fmt (str, default: None ) –

    The format of the file. If None, the format is determined by the file extension.

Returns: