Skip to content

Commit e7aef86

Browse files
committed
always returning a single-scale image
1 parent a119850 commit e7aef86

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

‎src/spatialdata/dataloader/datasets.py‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import pandas as pd
1111
from anndata import AnnData
1212
from geopandas import GeoDataFrame
13+
from multiscale_spatial_image import MultiscaleSpatialImage
1314
from pandas import CategoricalDtype
1415
from scipy.sparse import issparse
16+
from spatial_image import SpatialImage
1517
from torch.utils.data import Dataset
1618

1719
from spatialdata._core.centroids import get_centroids
@@ -267,6 +269,14 @@ def _preprocess(
267269
assert np.all([i in self.tiles_coords for i in dims_])
268270
self.dims = list(dims_)
269271

272+
@staticmethod
273+
def _ensure_single_scale(data: SpatialImage | MultiscaleSpatialImage) -> SpatialImage:
274+
if isinstance(data, SpatialImage):
275+
return data
276+
if isinstance(data, MultiscaleSpatialImage):
277+
return SpatialImage(next(iter(data["scale0"].ds.values())))
278+
raise ValueError(f"Expected a SpatialImage or MultiscaleSpatialImage, got {type(data)}.")
279+
270280
def _get_return(
271281
self,
272282
return_annot: str | list[str] | None,
@@ -279,11 +289,14 @@ def _get_return(
279289
return_annot = [return_annot] if isinstance(return_annot, str) else return_annot
280290
# return tuple of (tile, table)
281291
if np.all([i in self.dataset_table.obs for i in return_annot]):
282-
return lambda x, tile: (tile, self.dataset_table.obs[return_annot].iloc[x].values.reshape(1, -1))
292+
return lambda x, tile: (
293+
self._ensure_single_scale(tile),
294+
self.dataset_table.obs[return_annot].iloc[x].values.reshape(1, -1),
295+
)
283296
if np.all([i in self.dataset_table.var_names for i in return_annot]):
284297
if issparse(self.dataset_table.X):
285-
return lambda x, tile: (tile, self.dataset_table[x, return_annot].X.A)
286-
return lambda x, tile: (tile, self.dataset_table[x, return_annot].X)
298+
return lambda x, tile: (self._ensure_single_scale(tile), self.dataset_table[x, return_annot].X.A)
299+
return lambda x, tile: (self._ensure_single_scale(tile), self.dataset_table[x, return_annot].X)
287300
raise ValueError(
288301
f"If `return_annot` is a `str`, it must be a column name in the table or a variable name in the table. "
289302
f"If it is a `list` of `str`, each element should be as above, and they should all be entirely in obs "
@@ -292,10 +305,12 @@ def _get_return(
292305
# return spatialdata consisting of the image tile and, if available, the associated table
293306
if table_name:
294307
return lambda x, tile: SpatialData(
295-
images={self.dataset_index.iloc[x][self.IMAGE_KEY]: tile},
308+
images={self.dataset_index.iloc[x][self.IMAGE_KEY]: self._ensure_single_scale(tile)},
296309
table=self.dataset_table[x],
297310
)
298-
return lambda x, tile: SpatialData(images={self.dataset_index.iloc[x][self.IMAGE_KEY]: tile})
311+
return lambda x, tile: SpatialData(
312+
images={self.dataset_index.iloc[x][self.IMAGE_KEY]: self._ensure_single_scale(tile)}
313+
)
299314

300315
def __len__(self) -> int:
301316
return len(self.dataset_index)

‎tests/dataloader/test_datasets.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,7 @@ def test_multiscale_images(self, sdata_blobs, rasterize: bool, return_annot):
132132
)
133133
if return_annot is None:
134134
sdata_tile = ds[0]
135-
if rasterize:
136-
# rasterize transforms teh multiscale image into a single scale image
137-
tile = sdata_tile["blobs_multiscale_image"]
138-
else:
139-
tile = next(iter(sdata_tile["blobs_multiscale_image"]["scale0"].ds.values()))
135+
tile = sdata_tile["blobs_multiscale_image"]
140136
else:
141137
tile, annot = ds[0]
142-
if not rasterize:
143-
tile = next(iter(tile["scale0"].ds.values()))
144138
assert tile.shape == (3, 10, 10)

0 commit comments

Comments
 (0)