Skip to content

Commit 7e2fead

Browse files
authored
Add details to to_anndata() docstring (#55)
1 parent 6941b0e commit 7e2fead

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

3-
## Version 0.0.11-0.0.12
3+
## Version 0.0.11-0.0.13
44
- BUGFIX: `to_anndata()` only populates `obsm` with spatial coordinates if the original `SpatialExperiment` has spatial coordinates (PR #53, #54)
5+
- Enhance docstring for `to_anndata()` to describe the structure of returned AnnData object (PR #55)
56

67

78
## Version 0.0.10

src/spatialexperiment/spatialexperiment.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import Any, Dict, List, Optional, Sequence, Union
2+
from typing import Any, Dict, List, Optional, Sequence, Union, Tuple
33
from urllib.parse import urlparse
44
from warnings import warn
55

@@ -1019,15 +1019,37 @@ def to_spatial_experiment():
10191019
######>> AnnData interop <<#####
10201020
################################
10211021

1022-
def to_anndata(self, include_alternative_experiments: bool = False) -> "anndata.AnnData":
1022+
def to_anndata(
1023+
self, include_alternative_experiments: bool = False
1024+
) -> Tuple["anndata.AnnData", Dict[str, "anndata.AnnData"]]:
10231025
"""Transform :py:class:`~SpatialExperiment`-like into a :py:class:`~anndata.AnnData` representation.
10241026
1027+
This method converts the main experiment data, spatial coordinates,
1028+
and image data into an AnnData structure. Image data, including the
1029+
image arrays and their scale factors, are stored within the ``obj.uns["spatial"]``
1030+
dictionary, adhering to a common convention for spatial single-cell data.
1031+
1032+
The ``obj.uns["spatial"]`` dictionary is structured as follows:
1033+
- It is a dictionary where each key is a unique ``library_id``.
1034+
The ``library_id`` is generated by combining ``sample_id`` and ``image_id``
1035+
from the input image data (e.g., "sample1-image01").
1036+
- Each ``library_id`` entry is itself a dictionary containing:
1037+
- ``"images"``: A dictionary to store image arrays.
1038+
Currently, images are stored under the key ``"hires"`` by default
1039+
(e.g., ``obj.uns["spatial"][library_id]["images"]["hires"] = image_numpy_array``).
1040+
- ``"scalefactors"``: A dictionary to store scale factors associated
1041+
with the images. Currently, scale factors are stored under the key
1042+
``"tissue_hires_scalef"`` by default (e.g.,
1043+
``obj.uns["spatial"][library_id]["scalefactors"]["tissue_hires_scalef"] = scale_factor_value``).
1044+
1045+
Spatial coordinates are stored in ``obj.obsm["spatial"]``.
1046+
10251047
Args:
10261048
include_alternative_experiments:
10271049
Whether to transform alternative experiments.
10281050
10291051
Returns:
1030-
An ``AnnData`` representation of the experiment.
1052+
A tuple containing an ``AnnData`` object with spatial information and a list of alternative experiments.
10311053
"""
10321054
obj, alt_exps = super().to_anndata(include_alternative_experiments=include_alternative_experiments)
10331055

0 commit comments

Comments
 (0)