#13806 Python: Add category mapping API for discrete properties

Introduce set_discrete_property_category_names on Case to bind integer
result values to text labels and optional colors. Internally it creates a
custom ColorLegend, appends one item per value (with an auto-assigned
palette color when the caller does not provide one) and registers the
legend as the default for the (case, resultName) pair.

Also add Project.color_legend_collection() as a dedicated accessor for
the project's ColorLegendCollection, pytest coverage for legend creation
and the default-legend-for-result binding, and two PythonExamples
demonstrating the end-to-end discrete property + category label flow.

#13806 Python: Silence mypy warnings for category_mapping module
This commit is contained in:
Magne Sjaastad committed 2026-04-20 18:55:36 +02:00
1 parent 29fc0aeaf5
commit 03409aed70
7 files changed
+265 -1

No files matched your search

@@ -0,0 +1,28 @@
######################################################################
# This script creates a discrete (integer) property for all active
# cells in the first case in the project.
#
# Passing data_type="INTEGER" flags the property as discrete regardless
# of the property name (previously this required the name to end with
# "NUM").
######################################################################
import rips
resinsight = rips.Instance.find()
case = resinsight.project.case(case_id=0)
# Derive a discrete region id from a continuous property.
poro = case.active_cell_property("STATIC_NATIVE", "PORO", 0)
region_ids = [int(value * 100) % 4 for value in poro]
print("Applying discrete values to active cells")
case.set_active_cell_property(
region_ids, "GENERATED", "MY_REGION", 0, data_type="INTEGER"
)
# Bind integer values to text labels so the legend shows names instead of numbers.
case.set_discrete_property_category_names(
property_name="MY_REGION",
value_names={0: "Sand", 1: "Shale", 2: "Coal", 3: "Limestone"},
)
@@ -0,0 +1,33 @@
######################################################################
# This script creates a discrete (integer) grid property for all grid
# cells in the first case in the project.
#
# A discrete property is visualized with a category legend, in contrast
# to the continuous legend used for floating point properties. Passing
# data_type="INTEGER" flags the property as discrete regardless of the
# property name (previously this required the name to end with "NUM").
######################################################################
import rips
resinsight = rips.Instance.find()
case = resinsight.project.case(case_id=0)
grid = case.grid()
grid_cell_count = grid.cell_count()
print("total cell count : " + str(grid_cell_count))
# Assign a simple region id based on IJK index.
values = []
for i in range(0, grid_cell_count):
values.append(i % 4)
print("Applying discrete values to main grid")
case.set_grid_property(values, "STATIC_NATIVE", "MY_REGION", 0, data_type="INTEGER")
# Bind integer values to text labels so the legend shows names instead of numbers.
# Colors are optional — values without a color get an auto-assigned palette color.
case.set_discrete_property_category_names(
property_name="MY_REGION",
value_names={0: "Sand", 1: "Shale", 2: "Coal", 3: "Limestone"},
value_colors={0: "#e6c878", 1: "#646464"},
)