Skip to content

Support negative label values #1329

Open

Description

@jan-glx

Note

AI-generated (Claude Opus), found while reviewing #1167. The checks were run by the agent on main (9a1fbca) with numpy 2, scikit-image and napari; reviewed by me before posting.

Labels may contain negative values: OME-NGFF 0.5 allows the signed types int8 … int64 for labels, and the SpatialData storage format treats negative values as instances. spatialdata reads and writes them, but several operations drop them or fail. It would be good to support them fully, or to reject them with a clear error where that is not possible.

Tested with a label image that holds instance -4, instance 2 and background 0:

  • ✅ Labels2DModel.parse, write and read (also multiscale)
  • ✅ join_spatialelement_table with a table whose instance_key holds -4
  • ✅ get_centroids
  • ✅ napari Labels layer (displays -4 with its own colour)
  • ❌ relabel_sequential silently wrong: [[0, -4, 7], [7, 0, -4]] → [[1, 0, 2], [2, 1, 0]], so background becomes 1 and -4 becomes background. The negative values index from the end of the lookup array (map.py L258).
  • ❌ to_polygons silently drops -4: skimage.measure.regionprops ignores labels ≤ 0 (vectorize.py L271). With only negative instances it raises IndexError: single positional indexer is out-of-bounds.
  • ❌ to_circles: AssertionError: Index mismatch between zonal stats and labels. (aggregate.py L333, which assumes 0 is the smallest value)
  • ❌ aggregate(values=image, by=labels): the same AssertionError

For reference, outside spatialdata: skimage.segmentation.relabel_sequential and np.bincount raise on negative values, and scipy.ndimage.find_objects ignores them.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions