You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Colors categorical column in table annotated by a labels layer not correct in napari-spatialdata #328
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 #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:
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.
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
self.itemDoubleClicked.connect(lambda item: self._onAction((item.text(),)))
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari_spatialdata/_widgets.py", line 155, in _onAction
cmap = DirectLabelColormap(color_dict=ddict)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari/utils/colormaps/colormap.py", line 401, in init
super().init(*args, **kwargs)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari/utils/colormaps/colormap.py", line 80, in init
super().init(colors=colors, **data)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari/utils/events/evented_model.py", line 255, in init
super().init(**kwargs)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/pydantic/v1/main.py", line 341, in init
raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for DirectLabelColormap
color_dict
cannot convert type '<class 'float'>' to a color array. (type=value_error)
Activity
melonora commentedon 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 commentedon Nov 8, 2024
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.LucaMarconato commentedon Jan 5, 2025
Thanks @ArneDefauw for reporting. This is now fixed in #337. Please notice that there is no need to pass
cmaptopl.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.
ArneDefauw commentedon Jan 10, 2025
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:
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
self.itemDoubleClicked.connect(lambda item: self._onAction((item.text(),)))
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari_spatialdata/_widgets.py", line 155, in _onAction
cmap = DirectLabelColormap(color_dict=ddict)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari/utils/colormaps/colormap.py", line 401, in init
super().init(*args, **kwargs)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari/utils/colormaps/colormap.py", line 80, in init
super().init(colors=colors, **data)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/napari/utils/events/evented_model.py", line 255, in init
super().init(**kwargs)
File "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/pydantic/v1/main.py", line 341, in init
raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for DirectLabelColormap
color_dict
cannot convert type '<class 'float'>' to a color array. (type=value_error)