scverse / spatialdata-io

BSD 3-Clause "New" or "Revised" License
33 stars 24 forks source link

Problem with Anndata2SpatialData conversion #171

Closed gboscagli closed 1 month ago

gboscagli commented 1 month ago

Greetings all, I'm trying to create a SpatialData object from an Anndata object using the from_legacy_anndata function. The Anndata file was created with Squidpy's read_nanostring (Squidpy version 1.5.0) from this Nanostring CosMx public dataset. I'm using a Windows machine. Unfortunately, I'm obtaining this error.

{
    "name": "UnboundLocalError",
    "message": "cannot access local variable 'lowres' where it is not associated with a value",
    "stack": "---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[47], line 1
----> 1 sdata = spatialdata_io.experimental.from_legacy_anndata(adata)

File c:\\Users\\Gabriele\\anaconda3\\envs\\cell2loc_env\\Lib\\site-packages\\spatialdata_io\\converters\\legacy_anndata.py:300, in from_legacy_anndata(adata)
    298     scale_hires = Scale([tissue_hires_scalef, tissue_hires_scalef], axes=(\"x\", \"y\"))
    299     shapes_transformations[f\"{dataset_id}_downscaled_hires\"] = scale_hires
--> 300 if lowres is not None:
    301     # prepare the lowres image
    302     assert (
    303         tissue_lowres_scalef is not None
    304     ), \"tissue_lowres_scalef is required when an the lowres image is present\"
    305     lowres_image = Image2DModel.parse(
    306         lowres, dims=(\"y\", \"x\", \"c\"), transformations={f\"{dataset_id}_downscaled_lowres\": Identity()}
    307     )

UnboundLocalError: cannot access local variable 'lowres' where it is not associated with a value"
}

Even if the reason of the error is clear (the lowres is actually absent in the uns slot) does it mean that I must have a lowres image to convert an Anndata object to a SpatialData object?

LucaMarconato commented 1 month ago

Hi, thanks for reporting. It looks like a bug as the image should not always be expected. Can you please try this PR? https://github.com/scverse/spatialdata-io/pull/172

LucaMarconato commented 1 month ago

Maybe tiny adjustments are needed to that PR, in such case a contribution would be welcome 😊