Skip to content

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

Open

Description

@ArneDefauw

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).

Activity

  1. melonora commented on Nov 8, 2024

    @melonora
    Collaborator

    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.

  2. ArneDefauw commented on Nov 8, 2024

    @ArneDefauw
    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.

  3. LucaMarconato commented on Jan 5, 2025

    @LucaMarconato
    Member

    Thanks @ArneDefauw for reporting. This is now fixed in #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.

  4. ArneDefauw commented on Jan 10, 2025

    @ArneDefauw
    Author

    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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions