Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/spatialdata/_core/operations/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def _aggregate_image_by_labels(
zones: ArrayLike = out["zone"].to_numpy()
outs.append(out.drop(columns=["zone"])) # remove the 0 (background)
df = pd.concat(outs, axis=1)
if len(df) == 0:
raise ValueError(
"No labelled instances (non-zero label ids) found in `by`, so there is nothing to aggregate by."
)

X = sparse.csr_matrix(df.values)

Expand Down
7 changes: 7 additions & 0 deletions tests/core/operations/test_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ def test_aggregate_image_by_labels(labels_blobs, image_schema, labels_schema) ->
assert len(out) == 3


def test_aggregate_image_by_background_only_labels() -> None:
image = Image2DModel.parse(RNG.normal(size=(2, 16, 16)))
labels = Labels2DModel.parse(np.zeros((16, 16), dtype=np.int32))
with pytest.raises(ValueError, match="No labelled instances"):
aggregate(values=image, by=labels, agg_func="mean")


@pytest.mark.parametrize("values", ["blobs_image", "blobs_points", "blobs_circles", "blobs_polygons"])
@pytest.mark.parametrize("by", ["blobs_labels", "blobs_circles", "blobs_polygons"])
def test_aggregate_requiring_alignment(sdata_blobs: SpatialData, values, by) -> None:
Expand Down
Loading