DataSetFilters.pack_labels

DataSetFilters.pack_labels#

DataSetFilters.pack_labels(
*,
sort: bool = False,
scalars: str | None = None,
preference: PointLiteral | CellLiteral = 'point',
output_scalars: str | None = None,
progress_bar: bool = False,
inplace: bool = False,
) → _DataSetType[source]#

Renumber labeled data such that labels are contiguous.

This filter renumbers scalar label data of any type with N labels such that the output labels are contiguous from [0, N). The output may optionally be sorted by label count.

The output array 'packed_labels' is added to the output by default, and is automatically set as the active scalars.

Parameters:
sortbool, default: False

Whether to sort the output by label count in descending order (that is, from largest to smallest).

scalarsstr, optional

Name of scalars to pack. Defaults to currently active scalars.

preferencestr, default: “point”

When scalars is specified, this is the preferred array type to search for in the dataset. Must be either 'point' or 'cell'.

output_scalarsstr, None

Name of the packed output scalars. By default, the output is saved to 'packed_labels'.

progress_barbool, default: False

If True, display a progress bar.

inplacebool, default: False

If True, the mesh is updated in-place.

Returns:
pyvista.DataSet

Dataset with packed labels.

Notes#

This filter uses vtkPackLabels as the underlying method.

Added in version 0.43.

Examples#

Download Python source code | Download Jupyter notebook

Pack segmented image labels.

Load non-contiguous image labels

>>> from pyvista import examples
>>> import numpy as np
>>> image_labels = examples.load_frog_tissues()

Show range of labels

>>> image_labels.get_data_range()
(np.uint8(0), np.uint8(29))

Find ‘gaps’ in the labels

>>> label_numbers = np.unique(image_labels.active_scalars)
>>> label_max = np.max(label_numbers)
>>> missing_labels = set(range(label_max)) - set(label_numbers)
>>> len(missing_labels)
4

Pack labels to remove gaps

Show range of packed labels

>>> packed_labels.get_data_range()
(np.uint8(0), np.uint8(25))

See Also#

sort_labels

Similar function with sort=True by default.