diff --git a/src/spatialdata/_core/operations/rasterize.py b/src/spatialdata/_core/operations/rasterize.py index 72bb87a63..071f1e108 100644 --- a/src/spatialdata/_core/operations/rasterize.py +++ b/src/spatialdata/_core/operations/rasterize.py @@ -31,7 +31,7 @@ get_axes_names, get_model, ) -from spatialdata.models._utils import get_spatial_axes +from spatialdata.models._utils import _get_uint_dtype, get_spatial_axes from spatialdata.transformations._utils import _get_scale, compute_coordinates from spatialdata.transformations.operations import get_transformation, remove_transformation, set_transformation from spatialdata.transformations.transformations import ( @@ -767,10 +767,14 @@ def rasterize_shapes_points( max_label = next(iter(reversed(label_index_to_category.keys()))) else: max_label = int(agg.max().values) - max_uint16 = np.iinfo(np.uint16).max - if max_label > max_uint16: - raise ValueError(f"Maximum label index is {max_label}. Values higher than {max_uint16} are not supported.") - agg = agg.astype(np.uint16) + # datashader aggregates in float64: from 2**53 on, a value may be a rounded larger integer + # (from large input labels or from summing overlapping labels) + if max_label >= 2**53: + raise ValueError( + f"Maximum label is at least {max_label}. Labels of 2**53 or higher cannot be rasterized exactly " + "because datashader aggregates in float64." + ) + agg = agg.astype(_get_uint_dtype(max_label)) return Labels2DModel.parse(agg, transformations=transformations) agg = agg.expand_dims(dim={"c": 1}).transpose("c", "y", "x") diff --git a/tests/core/operations/test_rasterize.py b/tests/core/operations/test_rasterize.py index 53d362d15..56c314966 100644 --- a/tests/core/operations/test_rasterize.py +++ b/tests/core/operations/test_rasterize.py @@ -446,6 +446,79 @@ def _rasterize(element: DaskDataFrame, **kwargs) -> SpatialImage: assert d[res[1, 2]] == 4 +def test_rasterize_points_selects_label_dtype(): + n_points = np.iinfo(np.uint16).max + 1 + points = PointsModel.parse( + dd.from_pandas( + pd.DataFrame({"x": np.arange(n_points), "y": np.zeros(n_points)}), + npartitions=1, + ) + ) + + result = rasterize( + points, + axes=("x", "y"), + min_coordinate=[0, 0], + max_coordinate=[n_points, 1], + target_coordinate_system="global", + target_width=n_points, + return_regions_as_labels=True, + ) + + assert result.dtype == np.uint32 + assert result.max().compute() == n_points + + +def _rasterize_point_labels(x, labels, agg_func): + df = pd.DataFrame({"x": x, "y": [0.5] * len(x), "label": np.array(labels, dtype=np.uint64)}) + points = PointsModel.parse(dd.from_pandas(df, npartitions=1)) + return rasterize( + points, + axes=("x", "y"), + min_coordinate=[0, 0], + max_coordinate=[2, 1], + target_coordinate_system="global", + target_width=2, + value_key="label", + agg_func=agg_func, + return_regions_as_labels=True, + ) + + +# (x, labels, agg_func, expected maximum label) +_LARGE_LABEL_CASES = [ + ([0.5, 1.5], [1, 2**53], "first", 2**53), + ([0.5, 1.5], [1, 2**53 + 1], "first", 2**53 + 1), + # overlapping labels are summed: each label is below 2**53, the sum is not + ([0.5, 0.5], [2**53 - 1, 2], "sum", 2**53 + 1), +] + + +def test_rasterize_labels_below_float64_precision(): + result = _rasterize_point_labels([0.5, 1.5], [1, 2**53 - 1], "first") + assert result.dtype == np.uint64 + assert int(result.max().compute()) == 2**53 - 1 + + +@pytest.mark.parametrize(("x", "labels", "agg_func", "expected"), _LARGE_LABEL_CASES) +def test_rasterize_labels_above_float64_precision_raises(x, labels, agg_func, expected): + # until rasterization is exact for large labels, they must raise instead of being silently rounded + with pytest.raises(ValueError, match=r"2\*\*53"): + _rasterize_point_labels(x, labels, agg_func) + + +@pytest.mark.xfail( + reason="datashader aggregates in float64, so labels of 2**53 or higher cannot be rasterized exactly", + strict=False, # an unexpected pass (root cause fixed) should not fail CI; the ValueError test will flag the guard +) +@pytest.mark.parametrize(("x", "labels", "agg_func", "expected"), _LARGE_LABEL_CASES) +def test_rasterize_labels_above_float64_precision(x, labels, agg_func, expected): + # the desired behaviour once the root cause is fixed; the ValueError guard and its test should then be removed + result = _rasterize_point_labels(x, labels, agg_func) + assert result.dtype == np.uint64 + assert int(result.max().compute()) == expected + + def test_rasterize_spatialdata(full_sdata): sdata = full_sdata.subset( ["image2d", "image2d_multiscale", "labels2d", "labels2d_multiscale", "points_0", "circles"]