Skip to content

feat(centroids): add return_area to get_centroids - #1337

Open
jan-glx wants to merge 5 commits into
scverse:mainfrom
jan-glx:feature/get-centroids-area
Open

jan-glx wants to merge 5 commits into
scverse:mainfrom
jan-glx:feature/get-centroids-area

Conversation

@jan-glx

@jan-glx jan-glx commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Split out of #1150 by @timtreis, keeping only the area part. Persisting centroids into tables is left for a separate design discussion (see #1150 (comment)). Builds on #1335, which already moved #1150's shared _points_from_centroids helper and small fixes to main.

What

get_centroids(..., return_area=True) adds the size of each geometry as an area column of the returned points element:

element measured as
2D labels pixel count (on scale0 for multiscale labels)
3D labels voxel count, i.e. a volume (as area in skimage's regionprops)
circles pi * radius**2
polygons / multipolygons geometric area
points raises ValueError

The area is reported in the requested coordinate_system, like the centroids: the intrinsic size is multiplied by |det| of the linear part of the transformation, which is exact for affine maps. This addresses the concern in #1150 that the area stayed intrinsic while the centroids were transformed, without restricting return_area to identity transformations.

Commits

The commits are meant to be read one by one (the PR is intended to be squash-merged):

  1. e82d5a8 adds return_area with feat(centroids): add return_area + SpatialData persist to get_centroids #1150's rule (area only for a coordinate system whose transformation is the identity) and feat(centroids): add return_area + SpatialData persist to get_centroids #1150's area tests, plus tests for multiscale and 3D labels.
  2. 831870b switches to feat(centroids): add return_area + SpatialData persist to get_centroids #1150's function structure (one registration plus _intrinsic_centroid_frame), no behaviour change. At this commit, the remaining differences to feat(centroids): add return_area + SpatialData persist to get_centroids #1150 are essentially its table persistence: https://github.com/scverse/spatialdata/compare/44af6dd42af50c23d355d924fbf366b2f014a388..831870b656a2237ba969e1e10026e438164650d9
  3. 66e4f88 replaces the identity-only rule by scaling the area with |det|, with tests for rotation + scale, anisotropic and 3D scales, and that the centroids are unchanged by return_area.
  4. 9af9f61 goes back to main's per-type registrations, no behaviour change.

jan-glx and others added 4 commits November 27, 2026 11:15
Add `return_area` to `get_centroids`: labels report their pixel (voxel)
count, circles pi * r**2 and polygons their geometric area, as an `area`
column of the returned points element (for 3D labels a volume, as in
skimage's regionprops). Points raise. Multiscale labels are measured on
scale0. As in scverse#1150, the area is intrinsic, so it is only allowed for a
coordinate system whose transformation is the identity.

Co-authored-by: Tim Treis <tim.treis@helmholtz-munich.de>
…d, no behaviour change)

Merge the per-type registrations into one registration with
_intrinsic_centroid_frame, _get_centroids_for_shapes and
_points_from_centroids as in scverse#1150, so this branch can be compared with
scverse#1150 without structural noise. Reverted at the end of this branch.
Instead of allowing return_area only for identity transformations, scale
the intrinsic area by |det| of the linear part of the transformation,
which is exact for affine maps; _points_from_centroids applies it next to
transforming the centroids. Tests cover rotation + scale, anisotropic and
3D scales, and that the centroids are unchanged by return_area.
Undo the review-aid restructure: keep main's per-type get_centroids
registrations (typed signatures, one docstring each) with the shared
_points_from_centroids, now also scaling the area.
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.78%. Comparing base (d1458f3) to head (d43f4f3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1337      +/-   ##
==========================================
+ Coverage   91.77%   91.78%   +0.01%     
==========================================
  Files          53       53              
  Lines        8398     8414      +16     
==========================================
+ Hits         7707     7723      +16     
  Misses        691      691              
Files with missing lines Coverage Δ
src/spatialdata/_core/centroids.py 98.93% <100.00%> (+0.21%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jan-glx jan-glx changed the title feat(centroids): add return_area to get_centroidsFeature/get centroids area feat(centroids): add return_area to get_centroids Nov 27, 2026
@jan-glx

jan-glx commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@LucaMarconato @timtreis could you review this one? It is the first step of a short stack towards the centroids/area API:

  1. feat(centroids): add return_area to get_centroids #1337 (this PR): get_centroids(..., return_area=True), with the area scaled into the requested coordinate system by |det| of the affine.
  2. perf(centroids): compute label centroids chunk by chunk #1338: label centroids and areas computed chunk by chunk. At 40k × 40k px with 871k labels it takes ~2 s and 2.6 GB peak; the current get_centroids needs 64 s and 15 GB already at 20k × 20k.
  3. Next: get_spatial_props(element, props=("centroid", "area", ...), coordinate_system=None), returning one row per instance (centroid, area, covariance, ellipse axes/orientation, bbox) for labels, shapes and points. get_centroids becomes a thin wrapper around it.
  4. Then: add_spatial_props(sdata, table_name, coordinate_system=...), writing obsm["spatial"] / obs["area"] plus a record in uns of what was written (coordinate system, per-element fingerprint). This would replace the table part of feat(centroids): add return_area + SpatialData persist to get_centroids #1150.

Comments on the overall direction are welcome here too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant