# Examples from pyvista.DataSetFilters.color_labels
# =================================================

# Load labeled data and crop it with `extract_subset()`
# to simplify the data.
import pyvista as pv
from pyvista import examples
import numpy as np
image_labels = examples.load_channels()
image_labels = image_labels.extract_subset(voi=(75, 109, 75, 109, 85, 100))

# Plot the dataset with default coloring using a categorical color map. The
# plotter by default uniformly samples from all 256 colors in the color map based
# on the data's range.
image_labels.plot(cmap='glasbey_category10')

# Show label ids of the dataset.
label_ids = np.unique(image_labels.active_scalars)
label_ids

# Color the labels with the filter. Note that the
# `'glasbey_category10'` color map is used by default.
colored_labels, color_dict = image_labels.color_labels(return_dict=True)

# Plot the labels. We define a helper function to include a legend with the plot.
def labels_plotter(mesh, color_dict):
    pl = pv.Plotter()
    pl.add_mesh(mesh)
    pl.add_legend(color_dict)
    return pl

labels_plotter(colored_labels, color_dict).show()

# Since the labels are unsigned integers, the `'index'` coloring mode is used
# by default. Unlike the uniform sampling used by the plotter in the previous
# plot, the colormap is instead indexed using the label values. This ensures
# that labels have a consistent coloring regardless of the input. For example,
# we can crop the dataset further.
subset_labels = image_labels.extract_subset(voi=(15, 34, 28, 34, 12, 15))

# And show that only three labels remain.
label_ids = np.unique(subset_labels.active_scalars)
label_ids

# Despite the changes to the dataset, the regions have the same coloring
# as before.
colored_labels, color_dict = subset_labels.color_labels(return_dict=True)
labels_plotter(colored_labels, color_dict).show()

# Use the `'cycle'` coloring mode instead to map label values to colors
# sequentially.
colored_labels, color_dict = subset_labels.color_labels(
    coloring_mode='cycle', return_dict=True
)
labels_plotter(colored_labels, color_dict).show()

# Map the colors explicitly using a dictionary.
color_dict = {0: 'black', 1: 'red', 2: 'lime', 3: 'blue', 4: 'gold'}
colored_labels = image_labels.color_labels(color_dict)
labels_plotter(colored_labels, color_dict).show()

# Omit the background value from the mapping and specify float colors. When
# floats are specified, values without a mapping are assigned `nan` values
# and are not plotted by default.
color_dict.pop(0)
colored_labels = image_labels.color_labels(
    color_dict, color_type='float_rgba'
)
labels_plotter(colored_labels, color_dict).show()

# Modify the scalars and make two of the labels negative.
scalars = image_labels.active_scalars
scalars[scalars > 2] *= -1
np.unique(scalars)

# Color the mesh and enable `negative_indexing`. With this option enabled,
# the `'index'` coloring mode is used by default, and therefore the positive
# values `0`, `1`, and `2` are colored with the first, second, and third
# color in the colormap, respectively. Negative values `-3` and `-4` are
# colored with the third-last and fourth-last color in the colormap, respectively.
colored_labels, color_dict = image_labels.color_labels(
    negative_indexing=True, return_dict=True
)
labels_plotter(colored_labels, color_dict).show()

# If `negative_indexing` is disabled, the coloring defaults to the
# `'cycle'` coloring mode instead.
colored_labels, color_dict = image_labels.color_labels(
    negative_indexing=False, return_dict=True
)
labels_plotter(colored_labels, color_dict).show()

# Load the `download_foot_bones()` dataset.
dataset = examples.download_foot_bones()

# Label the bones using `connectivity()` and show
# the label values.
labeled_data = dataset.connectivity()
np.unique(labeled_data.active_scalars)

# Color the dataset with default arguments. Despite having 26 separately colored
# regions, the colors from the default glasbey-style colormap are all relatively
# distinct.
colored_labels = labeled_data.color_labels()
colored_labels.plot()

# Color the mesh with fewer colors than there are label values. In this case
# the `'cycle'` mode is used by default and the colors are reused.
colored_labels = labeled_data.color_labels(['red', 'lime', 'blue'])
colored_labels.plot()

# Color all labels with a single color.
colored_labels = labeled_data.color_labels('red')
colored_labels.plot()

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code
