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

Using cmaps fails when rendering multiscale images #354

Open
gdurif opened this issue Sep 10, 2024 · 4 comments
Open

Using cmaps fails when rendering multiscale images #354

gdurif opened this issue Sep 10, 2024 · 4 comments
Labels
bug Something isn't working images 🖼️ Anything related to Images priority: medium

Comments

@gdurif
Copy link

gdurif commented Sep 10, 2024

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

@gdurif gdurif changed the title cmap not working in render_image with Xenium data Bug: cmap argument not working in render_image with Xenium data Sep 10, 2024
@timtreis
Copy link
Member

Hey @gdurif, currently at a conference so I'll try to have a deeper look later. But my first assumption is that the channel is a string and not a number? Could you maybe try "0" instead of 0?

Also, in this notebook (https://github.com/PMBio/spatialdata-workshops/blob/main/notebooks/nb2_static_plotting.ipynb) we're showing how you can use a modified cmap that has true 0s as transparent - maybe that could also help you

@gdurif
Copy link
Author

gdurif commented Sep 11, 2024

Hey @timtreis no problem, it is not urgent.

I tried with channel="0" and got the same error.

And thanks for the modified cmap with 0s as transparent, that is exactly what I was aiming for. Saddly, I got the same error but I'll keep that for later.

@timtreis timtreis changed the title Bug: cmap argument not working in render_image with Xenium data Using cmaps fails when rendering multiscale images Sep 11, 2024
@timtreis
Copy link
Member

Hey @gdurif, I was able to reproduce your bug. I took the liberty of renaming your issue to better reflect the actual error. Will put it on the todo-list 👍 You'll get automatically notified when I merge a PR that addresses it :)

@timtreis timtreis self-assigned this Sep 11, 2024
@timtreis timtreis added bug Something isn't working priority: medium images 🖼️ Anything related to Images labels Sep 11, 2024
@gdurif
Copy link
Author

gdurif commented Sep 12, 2024

No problem. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working images 🖼️ Anything related to Images priority: medium
Projects
None yet
Development

No branches or pull requests

3 participants