napari-spatialdata
napari-spatialdata copied to clipboard
Colors categorical column in table annotated by a labels layer not correct in napari-spatialdata
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:
spatialdata-plot correctly plots the column "new_category":
But when I do
from napari_spatialdata import Interactive
Interactive( sdata )
I get:
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).
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.
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.
Thanks @ArneDefauw for reporting. This is now fixed in https://github.com/scverse/napari-spatialdata/pull/337. Please notice that there is no need to pass cmap to pl.render_labels() now.
Still, unfortunately (unrelated PR), now the color shown by the scatterplot widget is wrong. I will track this in a new issue.
Thank @LucaMarconato for the fix. Everything sems to work fine now on the dummy blobs dataset. However, if I try the solution on another dataset, with the same dummy categorical column, it fails. It tried to reproduce the issue it on the dummy blobs dataset, but I did not succeed.
I used this code:
import os
from pathlib import Path
import numpy as np
import pooch
from napari_spatialdata import Interactive
from pooch import Pooch
from spatialdata import read_zarr
__version__ = "0.0.1"
BASE_URL = "https://objectstor.vib.be/spatial-hackathon-public/sparrow/public_datasets"
def get_registry(path: str | Path | None = None) -> Pooch:
"""
Get the Pooch registry
Parameters
----------
path
If None, example data will be downloaded in the default cache folder of your os. Set this to a custom path, to change this behaviour.
Returns
-------
Pooch registry.
"""
registry = pooch.create(
path=pooch.os_cache("test_data") if path is None else path,
base_url=BASE_URL,
version=__version__,
registry={
"transcriptomics/resolve/mouse/sdata_transcriptomics.zarr.zip": "30a5649b8a463a623d4e573716f8c365df8c5eed3e77b3e81abf0acaf5ffd1f3",
},
)
return registry
def resolve_example():
"""Example transcriptomics dataset"""
# Fetch and unzip the file
registry = get_registry()
unzip_path = registry.fetch("transcriptomics/resolve/mouse/sdata_transcriptomics.zarr.zip", processor=pooch.Unzip())
sdata = read_zarr(os.path.commonpath(unzip_path))
sdata.path = None
return sdata
# this downloads a small dataet:
sdata=resolve_example()
table_layer = "table_transcriptomics_cluster"
np.random.seed( 42 )
sdata[ table_layer ].obs['new_category'] = np.random.randint(0, 15, size=len( sdata[ table_layer ].obs ))
sdata[ table_layer ].obs['new_category']=sdata[ table_layer].obs['new_category'].astype( int ).astype( "category" )
sc.pl.umap(sdata.tables[table_layer], color=["new_category"], show=True)
Interactive( sdata )
When I try to visualize "new_category" in napari-spatialdata I get the error:
Traceback (most recent call last):
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari_spatialdata/_widgets.py", line 60, in