pyvista.DataSetFilters.pack_labels#

DataSetFilters.pack_labels(
sort=False,
scalars=None,
preference='point',
output_scalars=None,
progress_bar=False,
inplace=False,
)[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 (i.e. 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. Has no effect if VTK version is lower than 9.3.

inplacebool, default: False

If True, the mesh is updated in-place.

Returns:
pyvista.Dataset

Dataset with packed labels.

See also

sort_labels

Similar function with sort=True by default.

Notes

This filter uses vtkPackLabels as the underlying method which requires VTK version 9.3 or higher. If vtkPackLabels is not available, packing is done with NumPy instead which may be slower. For best performance, consider upgrading VTK.

Added in version 0.43.

Examples

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

>>> packed_labels = image_labels.pack_labels()

Show range of packed labels

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