From ef986c24553606bd3e56a98ff5c5a0d5496dfb5d Mon Sep 17 00:00:00 2001 From: ckmah Date: Tue, 15 Oct 2024 14:51:28 -0700 Subject: [PATCH] force 3D points to 2D xy. fixes #161 --- bento/io/_io.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/bento/io/_io.py b/bento/io/_io.py index 431df92..a37645e 100644 --- a/bento/io/_io.py +++ b/bento/io/_io.py @@ -2,6 +2,7 @@ from typing import List, Union import emoji +import spatialdata as sd from anndata.utils import make_index_unique from spatialdata import SpatialData from spatialdata.models import TableModel @@ -57,6 +58,21 @@ def prep( shape_gdf[shape_key] = shape_gdf["geometry"] shape_gdf.index = make_index_unique(shape_gdf.index.astype(str)) + if "global" in sdata.points[points_key].attrs["transform"]: + # Force points to 2D for Xenium data + xyz_scale = sd.transformations.get_transformation(sdata.points[points_key]) + if isinstance(xyz_scale, sd.transformations.Scale): + xy_scale = sd.transformations.Scale( + scale=xyz_scale.to_scale_vector(["x", "y"]), axes=["x", "y"] + ) + sdata.points[points_key] = sd.models.PointsModel.parse( + sdata.points[points_key].compute().reset_index(drop=True), + coordinates={"x": "x", "y": "y"}, + feature_key=feature_key, + instance_key=instance_key, + transform=xy_scale, + ) + # sindex points and sjoin shapes if they have not been indexed or joined point_sjoin = [] shape_sjoin = []