Skip to content

Commit 6893a37

Browse files
committed
refactor(render): thread color_spec through _render_labels mutation pipeline (#700)
The rasterize-mask and groups-filter lockstep (src[mask]; col[mask]; remove_unused) — the exact sync footgun the review flagged — now go through color_spec.filter(mask). Byte-identical (visual failure set unchanged).
1 parent ba5a5eb commit 6893a37

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

src/spatialdata_plot/pl/render.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,6 @@ def _render_labels(
23042304
coordinate_system=coordinate_system,
23052305
)
23062306
colortype = color_spec.colortype
2307-
color_source_vector, color_vector = color_spec.source_vector, color_spec.color_vector
23082307
categorical = color_spec.is_categorical
23092308

23102309
# Outline color lookup must run BEFORE any masking so the returned vector aligns to
@@ -2348,45 +2347,38 @@ def _render_labels(
23482347
mask = np.isin(instance_id, unique_labels)
23492348
instance_id = instance_id[mask]
23502349
if col_for_color is not None:
2351-
color_vector = color_vector[mask]
2352-
if isinstance(color_vector.dtype, pd.CategoricalDtype):
2353-
color_vector = color_vector.remove_unused_categories()
2354-
assert color_source_vector is not None # noqa: S101
2355-
color_source_vector = color_source_vector[mask]
2356-
else:
2357-
assert color_source_vector is None # noqa: S101
2350+
color_spec = color_spec.filter(mask)
23582351
if outline_color_vector is not None:
23592352
outline_color_vector, outline_color_source_vector = _apply_mask_to_outline_vectors(
23602353
outline_color_vector, outline_color_source_vector, mask
23612354
)
23622355

2363-
_warn_groups(groups, colortype, color_source_vector, col_for_color)
2356+
_warn_groups(groups, colortype, color_spec.source_vector, col_for_color)
23642357

23652358
# When groups are specified, zero out non-matching label IDs so they render as background.
23662359
# Only show non-matching labels if the user explicitly sets na_color.
23672360
_na = render_params.cmap_params.na_color
23682361
if (
23692362
groups is not None
23702363
and categorical
2371-
and color_source_vector is not None
2364+
and color_spec.source_vector is not None
23722365
and (_na.default_color_set or _na.is_fully_transparent())
23732366
):
2374-
keep_vec = color_source_vector.isin(groups)
2367+
keep_vec = color_spec.source_vector.isin(groups)
23752368
matching_ids = instance_id[keep_vec]
23762369
keep_mask = np.isin(label.values, matching_ids)
23772370
label = label.copy()
23782371
label.values[~keep_mask] = 0
23792372
instance_id = instance_id[keep_vec]
2380-
color_source_vector = color_source_vector[keep_vec]
2381-
color_vector = color_vector[keep_vec]
2382-
if isinstance(color_vector.dtype, pd.CategoricalDtype):
2383-
color_vector = color_vector.remove_unused_categories()
2373+
color_spec = color_spec.filter(keep_vec)
23842374
if outline_color_vector is not None:
23852375
outline_color_vector, outline_color_source_vector = _apply_mask_to_outline_vectors(
23862376
outline_color_vector, outline_color_source_vector, keep_vec
23872377
)
23882378

2389-
color_vector = _maybe_apply_transfunc(colortype, color_vector, render_params.transfunc)
2379+
# carrier pipeline done; unpack the final vectors for the read/draw phase below
2380+
color_spec = color_spec.apply_transfunc(render_params.transfunc)
2381+
color_source_vector, color_vector = color_spec.source_vector, color_spec.color_vector
23902382

23912383
if render_params.as_points:
23922384
# Fast mode: one dot per label at its centroid. Compute on the *rendered* raster (already

0 commit comments

Comments
 (0)