pyvista.DataSetFilters.sort_labels#

DataSetFilters.sort_labels(
scalars=None,
preference='point',
output_scalars=None,
progress_bar=False,
inplace=False,
)[source]#

Sort labeled data by number of points or cells.

This filter renumbers scalar label data of any type with N labels such that the output labels are contiguous from [0, N) and sorted in descending order from largest to smallest (by label count). I.e., the largest label will have a value of 0 and the smallest label will have a value of N-1.

The filter is a convenience method for pyvista.DataSetFilters.pack_labels() with sort=True.

Parameters:
scalarsstr, optional

Name of scalars to sort. 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 sorted 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 sorted labels.

Examples

Sort segmented image labels.

Load image labels

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

Show label info for first four labels

>>> label_number, label_size = np.unique(
...     image_labels['MetaImage'], return_counts=True
... )
>>> label_number[:4]
pyvista_ndarray([0, 1, 2, 3], dtype=uint8)
>>> label_size[:4]
array([30805713,    35279,    19172,    38129])

Sort labels

>>> sorted_labels = image_labels.sort_labels()

Show sorted label info for the four largest labels. Note the difference in label size after sorting.

>>> sorted_label_number, sorted_label_size = np.unique(
...     sorted_labels["packed_labels"], return_counts=True
... )
>>> sorted_label_number[:4]
pyvista_ndarray([0, 1, 2, 3], dtype=uint8)
>>> sorted_label_size[:4]
array([30805713,   438052,   204672,   133880])