Napari visualization of AnnData Visium containing only a single library is broken
Following the documentation and tutorials, code like
image = sq.im.ImageContainer(adata.uns["spatial"]["spaceranger100_count_30458_ST8059048_mm10-3_0_0_premrna"]["images"]["hires"], scale=adata.uns["spatial"]["spaceranger100_count_30458_ST8059048_mm10-3_0_0_premrna"]["scalefactors"]["tissue_hires_scalef"])
viewer = image.interactive(adata)
should work and has worked in the past. However, with Squidpy 1.2.0, it aborts with
KeyError: "Unable to get the spot diameter from `adata.uns['spatial']['0']['scalefactors']['spot_diameter_fullres']]`"
Explicitly passing the library id, i.e.
viewer = image.interactive(adata, library_id="spaceranger100_count_30458_ST8059048_mm10-3_0_0_premrna")
results in the same error. This is due to the passed library ID being overwritten by self.container.library_ids if library_key is None, which would be fine if self.container.library_ids had a sensible value instead of [0]. According to the documentation, library_key is optional and corresponds to the column in .obs containing Z coordinates. A single visium slice does not have such a column.
Hi @ilia-kats , you can also pass library_id in the __init__, i.e. this should work:
lid = "spaceranger100_count_30458_ST8059048_mm10-3_0_0_premrna"
image = sq.im.ImageContainer(
adata.uns["spatial"][lid]["images"]["hires"],
scale=adata.uns["spatial"][lid]["scalefactors"]["tissue_hires_scalef"]
)
viewer = image.interactive(adata, library_id=lid)
Alternatively, there's a function called .from_adata that's slightly more convenient:
img = sq.im.ImageContainer.from_adata(adata,
img_key="hires",
library_id="spaceranger100_count_30458_ST8059048_mm10-3_0_0_premrna"
)
when passing the library_id.
This PR might be relevant #531 (adata subsetting bug when interactively visualizing it)
Ah, thanks, I didn't know about from_adata, this works. The docs could definitely use some work though, neither library_id nor **kwargs are documented in the ImageContainer constructor, and the docs for ImageContainer.interactive say
library_id (Union[str, Sequence[str], None]) – Subset of library ids to visualize. If None, visualize all library ids.
which is obviously not what's happening.
**kwargs are documented in the ImageContainer constructor, and the docs for ImageContainer.interactive
There was a typo (an extra whitespace caused the add_img not to be included) that, fixed in #531 .
should be working at scverse/napari-spatialdata