1010import pandas as pd
1111from anndata import AnnData
1212from geopandas import GeoDataFrame
13+ from multiscale_spatial_image import MultiscaleSpatialImage
1314from pandas import CategoricalDtype
1415from scipy .sparse import issparse
16+ from spatial_image import SpatialImage
1517from torch .utils .data import Dataset
1618
1719from 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 )
0 commit comments