spatialdata-plot icon indicating copy to clipboard operation
spatialdata-plot copied to clipboard

Images with alpha channel being plotted incorrectly

Open ivirshup opened this issue 1 year ago • 1 comments

Hey! I've come across an issue when trying to plot some images where one of the channels is an alpha level. This is a visium image that was submitted to cellxgene.

sdata.pl.render_images().pl.show("d551b400-b2e5-454d-b5a9-ece03e6b4739")

Image

As you can see, this looks quite funny. I don't think it's an issue with the image, since plotting it through other routes appears normal:

Plotting with matplotlib

import matplotlib.pyplot as plt
img = sdata.images["d551b400-b2e5-454d-b5a9-ece03e6b4739_library"].compute()
plt.imshow(img.transpose("y", "x", "c"))

Image

Plotting with squidpy

from spatialdata_io.experimental import from_legacy_anndata, to_legacy_anndata
import squidpy as sq

adata = to_legacy_anndata(sdata, include_images=True)
sq.pl.spatial_scatter(adata)

Image

It seems to me like the alpha level is being misinterpreted in spatialdata_plot.

ivirshup avatar Jan 16 '25 23:01 ivirshup

Thanks for reporting. This is how rgba is dealt with in napari-spatialdata, the issue may be related: https://github.com/scverse/napari-spatialdata/pull/153/files; following the napari-spatialdata approach, the desired implementation may be the following:

  • if r, g, b (a) are the channel names of the image, then interpret it as RGB(A)
  • (optional) if not, let the user pass an explicit parameter to pl.render_images(), such as rgb=True (default false)

In napari-spatialdata, this approach replaced the previous method, which was based on a heuristic (checking if the channels were 3 or 4 and interpreting the image as RGB(A)).

@timtreis @Sonja-Stockhaus wdyt?

LucaMarconato avatar Jan 20 '25 14:01 LucaMarconato