Skip to content
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

HESTData: update to_spatial_data()`` #63

Merged
merged 9 commits into from
Oct 30, 2024
Merged

Conversation

konst-int-i
Copy link
Collaborator

@konst-int-i konst-int-i commented Oct 29, 2024

This PR

Makes several changes to the HESTData.to_spatial_data() behaviour:

Test instructions

from hest import load_hest
hest_data = load_hest('../hest_data', id_list=['TENX68'])
st = hest_data[0]
st.to_spatial_data(fullres=True)

Alternatively, run the first cell in tutorials/2-Interacting-with-HEST-1k.ipynb.

Expected behaviour

Old SpatialData conversion for sample TENX68

SpatialData object
├── Images
│     └── 'he': DataArray[cyx] (3, 38232, 20690)
├── Shapes
│     ├── 'cellvit': GeoDataFrame shape: (77165, 3) (2D shapes)
│     └── 'tissue_contours': GeoDataFrame shape: (2, 2) (2D shapes)
└── Tables
      └── 'anndata': AnnData (1657, 18085)
with coordinate systems:
    ▸ 'global', with elements:
        he (Images), cellvit (Shapes), tissue_contours (Shapes)

⬇️ ⬇️ ⬇️

New SpatialData conversion:

SpatialData object
├── Images
│     ├── 'ST_downscaled_hires_image': SpatialImage[cyx] (3, 4779, 2586)
│     ├── 'ST_downscaled_lowres_image': SpatialImage[cyx] (3, 1000, 541)
│     └── 'ST_fullres_image': DataTree[cyx] (3, 38232, 20690), (3, 19116, 10345)
├── Shapes
│     ├── 'cellvit': GeoDataFrame shape: (77165, 3) (2D shapes)
│     ├── 'locations': GeoDataFrame shape: (1657, 2) (2D shapes)
│     └── 'tissue_contours': GeoDataFrame shape: (2, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (1657, 18085)
with coordinate systems:
    ▸ 'ST_downscaled_hires', with elements:
        ST_downscaled_hires_image (Images), cellvit (Shapes), locations (Shapes), tissue_contours (Shapes)
    ▸ 'ST_downscaled_lowres', with elements:
        ST_downscaled_lowres_image (Images), cellvit (Shapes), locations (Shapes), tissue_contours (Shapes)
    ▸ 'ST_fullres', with elements:
        ST_fullres_image (Images), cellvit (Shapes), locations (Shapes), tissue_contours (Shapes)

@konst-int-i konst-int-i added bug Something isn't working enhancement New feature or request labels Oct 29, 2024
@konst-int-i konst-int-i self-assigned this Oct 29, 2024
@guillaumejaume
Copy link
Collaborator

Thanks @konst-int-i! A couple of things to add:

  • in HESTData to_spatial_data:
    • save tissue contours as shape
    • save cellvit as shape
  • XeniumHESTData, override to_spatial_data to include:
    • xenium_nuc_seg
    • xenium_cell_seg

@pauldoucet, let me know if I'm missing anything. Pushing for better interfacing with spatial data seems like a good direction.

@@ -12,6 +12,9 @@
from hestcore.wsi import (WSI, CucimWarningSingleton, NumpyWSI,
contours_to_img, wsi_factory)
from loguru import logger
from spatialdata import SpatialData
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to move this import line inside the to_spatial_data() method, the SpatialData is huge

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved in 98d0acf - I had it outside to enable the type hint in the function, but appreciate that it comes with a lot of overhead

tissue_hires_scalef = scalefactors[TISSUE_HIRES_SCALEF]
else:
pixel_size=self.meta['pixel_size_um_estimated']
ds_factor = 4/pixel_size # proxy for visium hires scale factor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part going to work for non-visium ST?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - the hires image here is an approximation for ~4 MPPs which is standard with Visium. I have tested it with non-visium slides and the same logic works and accurately downsamples the image, although the hires terminology is not normally given for these slides to my knowledge.

@pauldoucet
Copy link
Collaborator

pauldoucet commented Oct 29, 2024

Hi @konst-int-i great work on your PR!

I added some checks and modified the conditional import statement (one cool trick, if you use from __future__ import annotations you can keep the return type of the method even if it's not imported yet)

@konst-int-i
Copy link
Collaborator Author

Addressed the additional requests from @guillaumejaume in 82f0779

@guillaumejaume guillaumejaume merged commit f24138a into main Oct 30, 2024
1 check passed
@guillaumejaume guillaumejaume deleted the feature/to_spatial_data branch November 2, 2024 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants