scverse / spatialdata-io

BSD 3-Clause "New" or "Revised" License
42 stars 27 forks source link

experimental.to_legacy_anndata fails when no global image is provided #134

Closed cavenel closed 7 months ago

cavenel commented 7 months ago

Hi,

I am trying to get an AnnData object out of a SpatialData object created with spatialdata_io.visium(...):

import spatialdata_io
from spatialdata_io.experimental import to_legacy_anndata

spatialdata = spatialdata_io.visium(
    args.SRCountDir, counts_file="raw_feature_bc_matrix.h5", dataset_id="visium"
)
adata = to_legacy_anndata(sdata, coordinate_system="downscaled_hires", include_images=True)

But it fails:

>>> spatialdata = spatialdata_io.visium(
        args.SRCountDir, counts_file="raw_feature_bc_matrix.h5", dataset_id="visium"
    )
>>> adata = to_legacy_anndata(sdata, include_images=True, coordinate_system="downscaled_hires")
/opt/conda/lib/python3.10/site-packages/spatialdata/_core/query/relational_query.py:325: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
  groups_df = table.obs.groupby(by=region_column_name)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.10/site-packages/spatialdata_io/converters/legacy_anndata.py", line 176, in to_legacy_anndata
    bb = get_extent(sdata_images)
  File "/opt/conda/lib/python3.10/functools.py", line 889, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/opt/conda/lib/python3.10/site-packages/spatialdata/_core/data_extent.py", line 239, in _
    raise ValueError(
ValueError: The SpatialData object does not contain any element in the coordinate system 'global', please pass a different coordinate system with the argument 'coordinate_system'.

My guess is that the spatialdata_io.visium reader does not include the raw global transform image in the sdata object. So to_legacy_anndata would need to rely on sdata.shapes[dataset_id] instead of global images.

LucaMarconato commented 7 months ago

Thanks for reporting, I have fixed the issue here: https://github.com/scverse/spatialdata-io/commit/4bd563670eabd6360c886fdb5018e9f299880c94.

In the screenshot is an example use case.

image