Skip to content

Commit

Permalink
Fix labels multiscales method (#697)
Browse files Browse the repository at this point in the history
* Add test case for generating labels pyramids

* Override default scaling method for labels
  • Loading branch information
aeisenbarth authored Sep 8, 2024
1 parent 364d2d3 commit 8323e15
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/spatialdata/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
**kwargs,
)

@classmethod
def parse( # noqa: D102
self,
*args: Any,
**kwargs: Any,
) -> DataArray | DataTree:
if kwargs.get("scale_factors") is not None and kwargs.get("method") is None:
# Override default scaling method to preserve labels
kwargs["method"] = Methods.DASK_IMAGE_NEAREST
return super().parse(*args, **kwargs)


class Labels3DModel(RasterSchema):
dims = DimsSchema((Z, Y, X))
Expand All @@ -270,6 +281,13 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
**kwargs,
)

@classmethod
def parse(self, *args: Any, **kwargs: Any) -> DataArray | DataTree: # noqa: D102
if kwargs.get("scale_factors") is not None and kwargs.get("method") is None:
# Override default scaling method to preserve labels
kwargs["method"] = Methods.DASK_IMAGE_NEAREST
return super().parse(*args, **kwargs)


class Image2DModel(RasterSchema):
dims = DimsSchema((C, Y, X))
Expand Down
21 changes: 21 additions & 0 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ def test_raster_schema(
with pytest.raises(ValueError):
model.parse(image, **kwargs)

@pytest.mark.parametrize("model", [Labels2DModel, Labels3DModel])
def test_labels_model_with_multiscales(self, model):
# Passing "scale_factors" should generate multiscales with a "method" appropriate for labels
dims = np.array(model.dims.dims).tolist()
n_dims = len(dims)

# A labels image with one label value 4, that partially covers 2×2 blocks.
# Downsampling with interpolation would produce values 1, 2, 3, 4.
image: ArrayLike = np.array([[0, 0, 0, 0], [0, 4, 4, 4], [4, 4, 4, 4], [0, 4, 4, 4]], dtype=np.uint16)
if n_dims == 3:
image = np.stack([image] * image.shape[0])
actual = model.parse(image, scale_factors=(2,))
assert isinstance(actual, DataTree)
assert actual.children.keys() == {"scale0", "scale1"}
assert actual.scale0.image.dtype == image.dtype
assert actual.scale1.image.dtype == image.dtype
assert set(np.unique(image)) == set(np.unique(actual.scale0.image)), "Scale0 should be preserved"
assert set(np.unique(image)) >= set(
np.unique(actual.scale1.image)
), "Subsequent scales should not have interpolation artifacts"

@pytest.mark.parametrize("model", [ShapesModel])
@pytest.mark.parametrize("path", [POLYGON_PATH, MULTIPOLYGON_PATH, POINT_PATH])
def test_shapes_model(self, model: ShapesModel, path: Path) -> None:
Expand Down

0 comments on commit 8323e15

Please sign in to comment.