LabelLookup#

class voxel.LabelLookup(mapping=None)#

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

update(mapping) → None.  Update D from mapping/iterable E and F.#

If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

setdefault(key, default=None)#

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

pop(key, *args) → v, remove specified key and return the corresponding value.#

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()#

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

clear() → None#

Remove all items from the dict.

property device: device#

The device of the label colors and cached lookup tensor.

property indices_tensor: Tensor#

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

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:

LabelLookup – The moved lookup table.

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, optional) – 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, optional) – If True, require an exact (case-insensitive) name match rather than a substring match.

Returns:

list of (int, Label) – The matching (index, label) pairs.

property indices: list[int]#

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

colors(default=(0, 0, 0)) → Tensor#

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

Parameters:

default (Tensor or list or tuple, optional) – 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, optional) – The format of the file. If None, the format is determined by the file extension.

classmethod load(filename, fmt=None) → LabelLookup#

Load a lookup table from a file.

Parameters:
  • filename (PathLike) – The path to the file to load.

  • fmt (str, optional) – The format of the file. If None, the format is determined by the file extension.

Returns:

LabelLookup – The loaded lookup table.