Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ArneDefauw opened this issue Oct 30, 2024 · 2 comments

Comments

@ArneDefauw
Copy link

ArneDefauw commented Oct 30, 2024

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 #273, it may be).

@melonora
Copy link
Collaborator

melonora commented Nov 8, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants