16
16
from geopandas import GeoDataFrame
17
17
from imageio import imread as imread2
18
18
from multiscale_spatial_image import MultiscaleSpatialImage
19
+ from numpy .random import default_rng
19
20
from spatial_image import SpatialImage
20
- from spatialdata import SpatialData
21
+ from spatialdata import SpatialData , rasterize_bins , rasterize_bins_link_table_to_labels
21
22
from spatialdata .models import Image2DModel , ShapesModel , TableModel
22
23
from spatialdata .transformations import (
23
24
Affine ,
31
32
from spatialdata_io ._constants ._constants import VisiumHDKeys
32
33
from spatialdata_io ._docs import inject_docs
33
34
35
+ RNG = default_rng (0 )
36
+
34
37
35
38
@inject_docs (vx = VisiumHDKeys )
36
39
def visium_hd (
@@ -39,6 +42,7 @@ def visium_hd(
39
42
filtered_counts_file : bool = True ,
40
43
bin_size : int | list [int ] | None = None ,
41
44
bins_as_squares : bool = True ,
45
+ annotate_table_by_labels : bool = False ,
42
46
fullres_image_file : str | Path | None = None ,
43
47
load_all_images : bool = False ,
44
48
imread_kwargs : Mapping [str , Any ] = MappingProxyType ({}),
@@ -67,6 +71,9 @@ def visium_hd(
67
71
bins_as_squares
68
72
If `True`, the bins are represented as squares. If `False`, the bins are represented as circles. For a correct
69
73
visualization one should use squares.
74
+ annotate_table_by_labels
75
+ If `True`, the tables will annotate labels layers representing the bins, if `False`, the tables will annotate
76
+ shapes layer.
70
77
fullres_image_file
71
78
Path to the full-resolution image. By default the image is searched in the ``{vx.MICROSCOPE_IMAGE!r}``
72
79
directory.
@@ -89,6 +96,7 @@ def visium_hd(
89
96
tables = {}
90
97
shapes = {}
91
98
images : dict [str , Any ] = {}
99
+ labels : dict [str , Any ] = {}
92
100
93
101
if dataset_id is None :
94
102
dataset_id = _infer_dataset_id (path )
@@ -347,7 +355,32 @@ def _get_bins(path_bins: Path) -> list[str]:
347
355
affine1 = transform_matrices ["spot_colrow_to_microscope_colrow" ]
348
356
set_transformation (image , Sequence ([affine0 , affine1 ]), "global" )
349
357
350
- return SpatialData (tables = tables , images = images , shapes = shapes )
358
+ sdata = SpatialData (tables = tables , images = images , shapes = shapes , labels = labels )
359
+
360
+ if annotate_table_by_labels :
361
+ for bin_size_str in bin_sizes :
362
+
363
+ shapes_name = dataset_id + "_" + bin_size_str
364
+
365
+ # add labels layer (rasterized bins).
366
+ labels_name = f"{ dataset_id } _{ bin_size_str } _labels"
367
+
368
+ labels_element = rasterize_bins (
369
+ sdata ,
370
+ bins = shapes_name ,
371
+ table_name = bin_size_str ,
372
+ row_key = VisiumHDKeys .ARRAY_ROW ,
373
+ col_key = VisiumHDKeys .ARRAY_COL ,
374
+ value_key = None ,
375
+ return_region_as_labels = True ,
376
+ )
377
+
378
+ sdata [labels_name ] = labels_element
379
+ rasterize_bins_link_table_to_labels (
380
+ sdata = sdata , table_name = bin_size_str , rasterized_labels_name = labels_name
381
+ )
382
+
383
+ return sdata
351
384
352
385
353
386
def _infer_dataset_id (path : Path ) -> str :
0 commit comments