-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function to export spatialdata to legacy anndata format #102
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #102 +/- ##
==========================================
+ Coverage 35.27% 37.13% +1.85%
==========================================
Files 16 18 +2
Lines 1219 1360 +141
==========================================
+ Hits 430 505 +75
- Misses 789 855 +66
|
should I be adding tests anywhere? |
Thank you @grst for the PR. I am adding a One note on your implementation. I see that the code assumes that def to_legacy_anndata(sdata: SpatialData, coordinate_system: str | None = None) -> AnnData:
"""
Convert a SpatialData object to a (legacy) spatial AnnData object.
This is useful for using packages expecting spatial information in AnnData, for example Scanpy and older versions
of Squidpy. Using this format for any new package is not recommended.
Parameters
----------
sdata
SpatialData object
coordinate_system
The coordinate system to consider. The AnnData object will be populated with the data aligned to this coordinate
system.
Returns
-------
The legacy spatial AnnData object
Notes
-----
Limitations and edge cases:
- The Table can only annotate Shapes, not Labels. If labels are needed, please manually compute the centroids,
approximate radii from the Labels areas, add this as a Shapes element to the SpatialData object and make the
table annotate the Shapes element.
- The Table cannot annotate more than one Shapes element. If more than one Shapes element are present, please
merge them into a single Shapes element and make the table annotate the new Shapes element.
- Table rows not annotating geometries in the coordinate system will be dropped. Similarly, Shapes rows
not annotated by Table rows will be dropped.
How resolutions, coordinates, indices and Table rows will be affected:
- The Images will be scaled (see later) and will not maintain the original resolution.
- The Shapes centroids will change iff their transformation is not an Identity.
- The Table and Shapes rows will have the same indices but, as mentioned above, some rows may be dropped.
The AnnData object does support only low-resolution images and limited coordinate transformations; eventual
transformations are applied before conversion. Precisely, this is how the Images are prepared for the AnnData
object:
- For each Image aligned to the specified coordinate system, the coordinate transformation to the coordinate
system is applied; all the Images are rendered to a common target bounding box, which is the bounding box of
the union of the Images aligned to the target coordinate system.
- Practically the above implies that if the Images have very dissimilar locations, most the rendered Images will
have empty spaces.
- Each Image is downscaled during rendering to fit a 2000x2000 pixels image (downscaled_hires) and a 600x600
pixels image (downscaled_lowres).
"""
pass What do you think about this? If you agree on the above I will continue your PR and proceed with the implementation. |
Actually I realized that even if the legacy I won't have time tomorrow, I will get back on ~monday. |
Sounds good, I'm just wondering how generalizable this function needs to be. Has the "legacy AnnData" format been used for anything but visium? |
Yes it has been for many techs, for instance in the legacy squidpy datasets and with SODB. Anyway I'll try to not overgeneralize: I want to lay down some foundations for eventual converters for other libraries but I'll still keep it focused on the legacy anndata. |
I finally finalized this, ready for review 😊 An example of the converter functions in action is in this notebook (updated version of the "squidpy interoperability" notebook in the docs): https://github.com/scverse/spatialdata-notebooks/blob/improve/docs/notebooks/examples/squidpy_integration.ipynb. Some notes:
|
from spatialdata_io.converters.legacy_anndata import ( | ||
from_legacy_anndata, | ||
to_legacy_anndata, | ||
) | ||
|
||
__all__ = ["from_legacy_anndata", "to_legacy_anndata"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary for a package with version number 0.0.x
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. The reason why we put experimental
(which we use also here: #75) is because we are not super sure about the need/utility of the code and because we would like to get more feedback from the users before adjusting things to a "definitive version" or dropping the support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Therefore even if the 0.0.x
indicates that the library is still, in a way, work in progress, I would still mark those parts of the code as experimental to suggest extra care/expect that things may not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I would probably argue that you should be 0.x since some time ;) Actually, the semver spec even suggest to start at 0.1.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, another good point 😁 We actually want to release a 0.1.0 this week, so we'll fix that!
I think I fixed the problem with the docs. Now all the errors (docs and tests) are due to the lack of a release in |
Testing against pre releases
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
I made a pre-release in spatialdata and changed the CI configuration to run the tests and build the docs against the latest pre-release. Now all the checks pass. I will merge already because the code is marked as experimental; happy to eventually reiterate on this. |
Add function to export spatialdata to legacy anndata format
As discussed in #47 and nf-core/spatialvi#39 (comment)