From 922a33b9f2d0b57d0102df21f66044b6d07326b0 Mon Sep 17 00:00:00 2001 From: keviny2 Date: Thu, 15 May 2025 11:47:56 -0700 Subject: [PATCH 1/3] Only populate obsm if spatial coordinates exist --- src/spatialexperiment/spatialexperiment.py | 6 +++++- tests/test_to_anndata.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/spatialexperiment/spatialexperiment.py b/src/spatialexperiment/spatialexperiment.py index b94b942..0af4c03 100644 --- a/src/spatialexperiment/spatialexperiment.py +++ b/src/spatialexperiment/spatialexperiment.py @@ -1051,7 +1051,11 @@ def to_anndata(self, include_alternative_experiments: bool = False) -> "anndata. "tissue_hires_scalef": row["scale_factor"] } # default to `tissue_hires_scalef` for now - obj.obsm["spatial"] = np.column_stack([self.spatial_coordinates[axis] for axis in self.spatial_coords_names]) + if len(self.spatial_coordinates) > 0: + coords_by_axis = [ + self.spatial_coordinates[axis] for axis in self.spatial_coords_names + ] + obj.obsm["spatial"] = np.column_stack(coords_by_axis) return obj, alt_exps diff --git a/tests/test_to_anndata.py b/tests/test_to_anndata.py index 258f6a5..b12b6c6 100644 --- a/tests/test_to_anndata.py +++ b/tests/test_to_anndata.py @@ -1,6 +1,9 @@ +import pytest from copy import deepcopy from pathlib import Path -import pytest +import anndata as ad + +from spatialexperiment import SpatialExperiment __author__ = "keviny2" __copyright__ = "keviny2" @@ -28,6 +31,14 @@ def test_to_anndata(spe): assert obj.obsm['spatial'].shape == (500, 2) +def test_to_anndata_empty(): + tspe = SpatialExperiment() + + obj, alt_exps = tspe.to_anndata() + + assert isinstance(obj, ad.AnnData) + + def test_to_anndata_spatial_key_exists(spe): tspe = deepcopy(spe) From 9b7e1638cf63de8a78804c8caf366e171e2d4372 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 May 2025 18:48:52 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/spatialexperiment/spatialexperiment.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/spatialexperiment/spatialexperiment.py b/src/spatialexperiment/spatialexperiment.py index 0af4c03..bdf69e6 100644 --- a/src/spatialexperiment/spatialexperiment.py +++ b/src/spatialexperiment/spatialexperiment.py @@ -1052,9 +1052,7 @@ def to_anndata(self, include_alternative_experiments: bool = False) -> "anndata. } # default to `tissue_hires_scalef` for now if len(self.spatial_coordinates) > 0: - coords_by_axis = [ - self.spatial_coordinates[axis] for axis in self.spatial_coords_names - ] + coords_by_axis = [self.spatial_coordinates[axis] for axis in self.spatial_coords_names] obj.obsm["spatial"] = np.column_stack(coords_by_axis) return obj, alt_exps From 63120a462f4c31ff6b976f741646a3a61ed0bb3a Mon Sep 17 00:00:00 2001 From: keviny2 Date: Thu, 15 May 2025 11:49:14 -0700 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa5fa18..ed80915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Changelog -## Version 0.0.10 +## Version 0.0.11 +- BUGFIX: `to_anndata()` only populates `obsm` with spatial coordinates if the original `SpatialExperiment` has spatial coordinates (PR #53) + +## Version 0.0.10 - Add an affine function that computes a `rasterio.Affine` object given a `scale_factor`. This assumes a simple scaling where the origin is (0,0) in the spatial coordinate system corresponding to the top-left pixel (0,0). More complex alignments would require explicit affine transforms. - Ensure img_raster() consistently returns a PIL.Image.Image. - Add to_numpy() method. @@ -11,9 +14,11 @@ ## Version 0.0.9 - Added `to_anndata()` in main `SpatialExperiment` class (PR #50) + ## Version 0.0.8 - Set the expected column names for image data slot (PR #46) + ## Version 0.0.7 - Added `img_source` function in main SpatialExperiment class and child classes of VirtualSpatialExperiment (PR #36) - Added `remove_img` function (PR #34) @@ -21,20 +26,21 @@ - Disambiguated `get_img_data` between `_imgutils.py` and `SpatialExperiment.py` - Moved `SpatialFeatureExperiment` into its own package + ## Version 0.0.6 - Added `read_tenx_visium()` function to load 10x Visium data as SpatialExperiment - Added `combine_columns` function - Implemented `__eq__` override for `SpatialImage` subclasses -## Version 0.0.5 +## Version 0.0.5 - Implementing a placeholder `SpatialFeatureExperiment` class. This version only implements the data structure to hold various geometries but none of the methods except for slicing. -## Version 0.0.3 - 0.0.4 +## Version 0.0.3 - 0.0.4 - Streamlining the `SpatialImage` class implementations. -## Version 0.0.1 - 0.0.2 +## Version 0.0.1 - 0.0.2 - Initial version of the SpatialExperiment class with the additional slots. - Allow spatial coordinates to be a numpy array