scverse / napari-spatialdata

Interactive visualization of spatial omics data
https://spatialdata.scverse.org/projects/napari/en/latest/
BSD 3-Clause "New" or "Revised" License
41 stars 17 forks source link

Colors categorical column in table annotated by a labels layer not correct in napari-spatialdata #328

Open ArneDefauw opened 2 weeks ago

ArneDefauw commented 2 weeks ago

I provide some code to reproduce the issue:

import numpy as np
import scanpy as sc

from spatialdata.datasets import blobs

import matplotlib
import matplotlib.pyplot as plt
import spatialdata_plot

np.random.seed(10)

sdata=blobs(length=1000, n_channels=3)

sc.pp.pca(sdata[ "table" ], copy=False,)

sc.pp.neighbors(sdata["table" ], copy=False,)
sc.tl.umap(sdata["table"], copy=False,)

sdata[ "table" ].obs['new_category'] = np.random.randint(0, 15, size=len( sdata[ "table" ].obs ))
sdata[ "table" ].obs['new_category']=sdata[ "table" ].obs['new_category'].astype( "category" )

sc.pl.umap(sdata.tables["table"], color=["new_category"], show=True)

plt.figure(figsize=(5, 5))
ax = plt.gca()

column = "new_category"

adata = sdata[ "table" ]

cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
                    "new_map",
                    adata.uns[column + "_colors"],
                    N=len(adata.uns[column + "_colors"]),
                )

sdata.pl.render_labels("blobs_labels", color=column,cmap =cmap, method="datashader", fill_alpha=1).pl.show(
    coordinate_systems="global", ax=ax
)

Gives me the umap:

Image

spatialdata-plot correctly plots the column "new_category":

Image

But when I do


from napari_spatialdata import Interactive

Interactive( sdata )

I get:

Image

The large cell in the bottom is visualized as having "new_category" '6' with napari-spatialdata, while spatialdata_plot, correctly plots it as having "new_category" '7'.

I am using the latest version of napari-spatialdata ( 0.5.3 ), and I am using macOS (I do not know if this is relevant, but given https://github.com/scverse/napari-spatialdata/issues/273, it may be).

melonora commented 1 week ago

so here the colors are not stored in the SpatialData object so there is also no way to have this then show in napari-spatialdata. We would have to double check with storing the colors that we then have the way of making this work. If not this could be a task for the hackathon in Basel next week.

ArneDefauw commented 1 week ago

so here the colors are not stored in the SpatialData object so there is also no way to have this then show in napari-spatialdata. We would have to double check with storing the colors that we then have the way of making this work. If not this could be a task for the hackathon in Basel next week.

Hi @melonora in this example, the colors of "new_category" are stored in sdata["table"].uns[ "new_category_colors" ], as they are added there through:

sc.pl.umap(sdata.tables["table"], color=["new_category"], show=True)

So in a way, napari-spatialdata could look for "new_category_colors" in .uns. And if not found, fall back to some default cmap.

Note that the 'correct' colors are visible in the scatter widget (because in the background they are probably generated by the same call to sc.pl.umap - I have not checked the code). So after running the scatter widget, you run into a discripancy between colors visualized there in the umap, and the colors of the annotated labels layer, which could confuse users.