Skip to content

Commit

Permalink
rechunking raster data after spatial query (#479)
Browse files Browse the repository at this point in the history
* rechunking raster data after spatial query

* using xarray chunk() instead of dask rechunk()
  • Loading branch information
LucaMarconato authored Mar 14, 2024
1 parent b6897a8 commit 09e339e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/spatialdata/_core/query/spatial_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ def _(
if 0 in query_result.shape:
return None
assert isinstance(query_result, SpatialImage)
# rechunk the data to avoid irregular chunks
image = image.chunk("auto")
else:
assert isinstance(image, MultiscaleSpatialImage)
assert isinstance(query_result, DataTree)
Expand All @@ -579,6 +581,9 @@ def _(
else:
d[k] = xdata
query_result = MultiscaleSpatialImage.from_dict(d)
# rechunk the data to avoid irregular chunks
for scale in query_result:
query_result[scale]["image"] = query_result[scale]["image"].chunk("auto")
query_result = compute_coordinates(query_result)

# the bounding box, mapped back to the intrinsic coordinate system is a set of points. The bounding box of these
Expand Down
21 changes: 20 additions & 1 deletion tests/io/test_readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from spatial_image import SpatialImage
from spatialdata import SpatialData, read_zarr
from spatialdata._io._utils import _are_directories_identical
from spatialdata.models import TableModel
from spatialdata.models import Image2DModel, TableModel
from spatialdata.transformations.operations import (
get_transformation,
set_transformation,
Expand Down Expand Up @@ -319,3 +319,22 @@ def test_io_table(shapes):
shapes2.table = adata
assert shapes2.table is not None
assert shapes2.table.shape == (5, 10)


def test_bug_rechunking_after_queried_raster():
# https://github.com/scverse/spatialdata-io/issues/117
##
single_scale = Image2DModel.parse(RNG.random((100, 10, 10)), chunks=(5, 5, 5))
multi_scale = Image2DModel.parse(RNG.random((100, 10, 10)), scale_factors=[2, 2], chunks=(5, 5, 5))
images = {"single_scale": single_scale, "multi_scale": multi_scale}
sdata = SpatialData(images=images)
queried = sdata.query.bounding_box(
axes=("x", "y"), min_coordinate=[2, 5], max_coordinate=[12, 12], target_coordinate_system="global"
)
with tempfile.TemporaryDirectory() as tmpdir:
f = os.path.join(tmpdir, "data.zarr")
queried.write(f)

##

pass

0 comments on commit 09e339e

Please sign in to comment.