From 6f7a0ed51e883a5d1cd5db6be747dcd625d1e40f Mon Sep 17 00:00:00 2001 From: anon Date: Sat, 6 Jun 2026 20:40:30 +0200 Subject: [PATCH] perf(labels): compute np.unique(label) once per render _render_labels uniquified the (already-rasterized) label raster up to twice: once for the instance ids / table overlap check and again for the rasterize mask. Compute the unique label values once and reuse them at all three sites. Output is unchanged (RGBA buffers byte-identical to main across 8 label scenarios: plain, contour, continuous, continuous fill+outline, categorical, groups, groups+na_color, outline-only). Removes one full-raster np.unique pass per render; the saving scales with the displayed raster size (~5 ms at default figsize/dpi, ~40 ms at 12x12/dpi=300 on a 2000-px label). --- src/spatialdata_plot/pl/render.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/spatialdata_plot/pl/render.py b/src/spatialdata_plot/pl/render.py index b66ebc7e..430d5635 100644 --- a/src/spatialdata_plot/pl/render.py +++ b/src/spatialdata_plot/pl/render.py @@ -2111,11 +2111,15 @@ def _render_labels( # the above adds a useless c dimension of 1 (y, x) -> (1, y, x) label = label.squeeze() + # Unique label values are needed for the instance ids, the overlap check, and the + # rasterize mask below; compute them once over the (possibly rasterized) raster. + unique_labels = np.unique(label.values) + if table_name is None: - instance_id = np.unique(label) + instance_id = unique_labels table = None else: - _check_instance_ids_overlap(sdata_filt, table_name, element, np.unique(label.values)) + _check_instance_ids_overlap(sdata_filt, table_name, element, unique_labels) _, region_key, instance_key = get_table_keys(sdata[table_name]) table = sdata[table_name][sdata[table_name].obs[region_key].isin([element])] @@ -2185,8 +2189,7 @@ def _render_labels( # rasterize could have removed labels from label # only problematic if color is specified if rasterize and (col_for_color is not None or col_for_outline_color is not None): - labels_in_rasterized_image = np.unique(label.values) - mask = np.isin(instance_id, labels_in_rasterized_image) + mask = np.isin(instance_id, unique_labels) instance_id = instance_id[mask] if col_for_color is not None: color_vector = color_vector[mask]