@@ -134,45 +134,37 @@ def _process_data_tree_query_result(query_result: DataTree) -> DataTree | None:
134
134
d = {k : Dataset ({"image" : d [k ]}) for k in scales_to_keep }
135
135
result = DataTree .from_dict (d )
136
136
137
- # rechunk the data to avoid irregular chunks
138
- coords = list (result ["scale0" ].coords .keys ())
139
- result = result .chunk (dict .fromkeys (coords , "auto" ))
140
-
141
137
from dask .array .core import _check_regular_chunks
142
138
143
- # check that the rechunking into regular chunks worked
144
- chunks_still_irregular = False
139
+ # rechunk to avoid irregular chunks
145
140
for scale in result :
146
141
data = result [scale ]["image" ].data
147
- chunks_still_irregular = chunks_still_irregular or not _check_regular_chunks (data .chunks )
148
-
149
- if chunks_still_irregular :
150
- # reported here: https://github.com/scverse/spatialdata/issues/821#issuecomment-2632201695
151
- # seemingly due to this bug: https://github.com/dask/dask/issues/11713
152
- CHUNK_SIZE = 1024
153
- rechunk_strategy = dict .fromkeys (coords , CHUNK_SIZE )
154
- if "c" in coords :
155
- rechunk_strategy ["c" ] = result ["scale0" ]["image" ].chunks [0 ][0 ]
156
- result = result .chunk (rechunk_strategy )
142
+ chunks = data .chunks
143
+ if not _check_regular_chunks (chunks ):
144
+ data = data .rechunk (data .chunksize )
145
+ if not _check_regular_chunks (data .chunks ):
146
+ raise ValueError (
147
+ f"Chunks are not regular for { scale } of the queried data: { chunks } "
148
+ "and could also not be rechunked regularly. Please report this bug."
149
+ )
150
+ result [scale ]["image" ].data = data
157
151
158
- for scale in result :
159
- data = result [scale ]["image" ].data
160
- assert _check_regular_chunks (data .chunks ), (
161
- f"Chunks are not regular for the { scale } of the queried data: { data .chunks } . Please report this bug."
162
- )
163
152
return result
164
153
165
154
166
155
def _process_query_result (
167
156
result : DataArray | DataTree , translation_vector : ArrayLike , axes : tuple [str , ...]
168
157
) -> DataArray | DataTree | None :
158
+ from dask .array .core import _check_regular_chunks
159
+
169
160
from spatialdata .transformations import get_transformation , set_transformation
170
161
171
162
if isinstance (result , DataArray ):
172
163
if 0 in result .shape :
173
164
return None
174
- # rechunk the data to avoid irregular chunks
175
- result = result .chunk ("auto" )
165
+ # rechunk to avoid irregular chunks
166
+ if not _check_regular_chunks (result .data .chunks ):
167
+ result .data = result .data .rechunk (result .data .chunksize )
176
168
elif isinstance (result , DataTree ):
177
169
result = _process_data_tree_query_result (result )
178
170
if result is None :
0 commit comments