Skip to content

Commit 681097d

Browse files
Merge pull request #211 from ArneDefauw/visium_hd
Visium hd
2 parents 147126d + 261dbbb commit 681097d

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/spatialdata_io/readers/visium_hd.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
from geopandas import GeoDataFrame
1717
from imageio import imread as imread2
1818
from multiscale_spatial_image import MultiscaleSpatialImage
19+
from numpy.random import default_rng
1920
from spatial_image import SpatialImage
20-
from spatialdata import SpatialData
21+
from spatialdata import SpatialData, rasterize_bins, rasterize_bins_link_table_to_labels
2122
from spatialdata.models import Image2DModel, ShapesModel, TableModel
2223
from spatialdata.transformations import (
2324
Affine,
@@ -31,6 +32,8 @@
3132
from spatialdata_io._constants._constants import VisiumHDKeys
3233
from spatialdata_io._docs import inject_docs
3334

35+
RNG = default_rng(0)
36+
3437

3538
@inject_docs(vx=VisiumHDKeys)
3639
def visium_hd(
@@ -39,6 +42,7 @@ def visium_hd(
3942
filtered_counts_file: bool = True,
4043
bin_size: int | list[int] | None = None,
4144
bins_as_squares: bool = True,
45+
annotate_table_by_labels: bool = False,
4246
fullres_image_file: str | Path | None = None,
4347
load_all_images: bool = False,
4448
imread_kwargs: Mapping[str, Any] = MappingProxyType({}),
@@ -67,6 +71,9 @@ def visium_hd(
6771
bins_as_squares
6872
If `True`, the bins are represented as squares. If `False`, the bins are represented as circles. For a correct
6973
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.
7077
fullres_image_file
7178
Path to the full-resolution image. By default the image is searched in the ``{vx.MICROSCOPE_IMAGE!r}``
7279
directory.
@@ -89,6 +96,7 @@ def visium_hd(
8996
tables = {}
9097
shapes = {}
9198
images: dict[str, Any] = {}
99+
labels: dict[str, Any] = {}
92100

93101
if dataset_id is None:
94102
dataset_id = _infer_dataset_id(path)
@@ -347,7 +355,32 @@ def _get_bins(path_bins: Path) -> list[str]:
347355
affine1 = transform_matrices["spot_colrow_to_microscope_colrow"]
348356
set_transformation(image, Sequence([affine0, affine1]), "global")
349357

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
351384

352385

353386
def _infer_dataset_id(path: Path) -> str:

0 commit comments

Comments
 (0)