Skip to content

Using cmaps fails when rendering multiscale images #354

Closed
@gdurif

Description

@gdurif

Hi,
I want to render a specific channel in a Xenium data image using .pl.render_images().

If I don't specified a colormap (i.e. input argument cmap=None by default), it works, however if I specify a colormap using cmap = "xxx", I get the error below.

Note: I would really like to change the colormap because the default one with the black color corresponding to 0 is not suited to visualize that kind of image (it works on small crops but not on the full image).

MWE using spatialdata documentation tutorial on Xenium data:

Note: Data can be downloaded with this script and transformed to zarr format with this one. It is also possible to directly works with Xenium data (c.f. below).

xenium_path = "./xenium_2.0.0.zarr"

import spatialdata as sd

sdata = sd.read_zarr(xenium_path)  # or use directly Xenium data (c.f. below)
sdata

%matplotlib inline
import matplotlib.pyplot as plt
import spatialdata_plot

### works
sdata.pl.render_images(
    "morphology_focus", channel=0, scale = "scale4"
).pl.show(title="DAPI (nuclear)", coordinate_systems="global")

image

### error
sdata.pl.render_images(
    "morphology_focus", channel=0, scale = "scale4", cmap="Greys"
).pl.show(title="DAPI (nuclear)", coordinate_systems="global")

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[167], line 1
----> 1 sdata.pl.render_images(
      2     "morphology_focus", channel=0, scale = "scale4", cmap="Greys"
      3 ).pl.show(title="DAPI (nuclear)", coordinate_systems="global")

File ~/.conda/envs/spatialdata-env/lib/python3.11/site-packages/spatialdata/_utils.py:261, in _deprecation_alias.<locals>.deprecation_decorator.<locals>.wrapper(*args, **kwargs)
    259     raise ValueError("version for deprecation must be specified")
    260 rename_kwargs(f.__name__, kwargs, alias_copy, class_name, library, version)
--> 261 return f(*args, **kwargs)

File ~/.conda/envs/spatialdata-env/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:476, in PlotAccessor.render_images(self, element, channel, cmap, norm, na_color, palette, alpha, scale, **kwargs)
    415 @_deprecation_alias(elements="element", quantiles_for_norm="percentiles_for_norm", version="version 0.3.0")
    416 def render_images(
    417     self,
   (...)
    426     **kwargs: Any,
    427 ) -> sd.SpatialData:
    428     """
    429     Render image elements in SpatialData.
    430 
   (...)
    474         The SpatialData object with the rendered images.
    475     """
--> 476     params_dict = _validate_image_render_params(
    477         self._sdata,
    478         element=element,
    479         channel=channel,
    480         alpha=alpha,
    481         palette=palette,
    482         na_color=na_color,
    483         cmap=cmap,
    484         norm=norm,
    485         scale=scale,
    486     )
    488     sdata = self._copy()
    489     sdata = _verify_plotting_tree(sdata)

File ~/.conda/envs/spatialdata-env/lib/python3.11/site-packages/spatialdata_plot/pl/utils.py:1911, in _validate_image_render_params(sdata, element, channel, alpha, palette, na_color, cmap, norm, scale)
   1909         cmap_length = len(channel) if channel is not None else len(spatial_element.c)
   1910         cmap = cmap * cmap_length
-> 1911     if (channel is not None and len(cmap) != len(channel)) or len(cmap) != len(spatial_element.c):
   1912         cmap = None
   1913 element_params[el]["cmap"] = cmap

File ~/.conda/envs/spatialdata-env/lib/python3.11/site-packages/datatree/common.py:54, in TreeAttrAccessMixin.__getattr__(self, name)
     52         with suppress(KeyError):
     53             return source[name]
---> 54 raise AttributeError(
     55     f"{type(self).__name__!r} object has no attribute {name!r}"
     56 )

AttributeError: 'DataTree' object has no attribute 'c'

Here is the code to directly read Xenium data:

import spatialdata_io

xenium_path = "Xenium_V1_humanLung_Cancer_FFPE"
sdata = spatialdata_io.xenium(
    xenium_path,
    cells_boundaries=False, 
    nucleus_boundaries=True, 
    cells_as_circles=False, 
    cells_labels=True, 
    nucleus_labels=False, 
    transcripts=False, 
    morphology_focus=True, 
    aligned_images=True, 
    cells_table=True,
    n_jobs=12
)

Note: I use the following to get possible colormaps (and see here for the corresponding visualization of the colormaps):

import matlplotlib as mpl
mpl.colormaps.__dict__

FYI, using a totally different dataset, I do not get the error:

from spatialdata.datasets import blobs
sdata = blobs()
sdata
sdata.pl.render_images("blobs_image", channel=0, cmap="Greys").pl.show(title="Blobs")

image

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions