Skip to content

render_points for categorical columns assumes there is always a table #486

@LucaMarconato

Description

@LucaMarconato
from spatialdata.datasets import blobs
import spatialdata_plot

sdata = blobs()
del sdata['table']
sdata.pl.render_points('blobs_points', color='genes').pl.show()

The table is not annotating the point, yet if there is no table we get an exception because of a sdata['table'] in the code.

Full traceback
[/opt/miniconda3/envs/ome/lib/python3.12/site-packages/legacy_api_wrap/__init__.py:82](http://localhost:8888/opt/miniconda3/envs/ome/lib/python3.12/site-packages/legacy_api_wrap/__init__.py#line=81): FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  return fn(*args_all, **kw)
[/opt/miniconda3/envs/ome/lib/python3.12/functools.py:909](http://localhost:8888/opt/miniconda3/envs/ome/lib/python3.12/functools.py#line=908): ImplicitModificationWarning: Transforming to str index.
  return dispatch(args[0].__class__)(*args, **kw)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[20], line 6
      4 sdata = blobs()
      5 del sdata['table']
----> 6 sdata.pl.render_points('blobs_points', color='genes').pl.show()

File [~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata-plot/src/spatialdata_plot/pl/basic.py:953](http://localhost:8888/lab/tree/dependencies/spatialdata-notebooks/notebooks/examples/~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata-plot/src/spatialdata_plot/pl/basic.py#line=952), in PlotAccessor.show(self, coordinate_systems, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, na_in_legend, colorbar, wspace, hspace, ncols, frameon, figsize, dpi, fig, title, share_extent, pad_extent, ax, return_ax, save)
    948     wanted_elements, wanted_points_on_this_cs, wants_points = _get_wanted_render_elements(
    949         sdata, wanted_elements, params_copy, cs, "points"
    950     )
    952     if wanted_points_on_this_cs:
--> 953         _render_points(
    954             sdata=sdata,
    955             render_params=params_copy,
    956             coordinate_system=cs,
    957             ax=ax,
    958             fig_params=fig_params,
    959             scalebar_params=scalebar_params,
    960             legend_params=legend_params,
    961         )
    963 elif cmd == "render_labels" and has_labels:
    964     wanted_elements, wanted_labels_on_this_cs, wants_labels = _get_wanted_render_elements(
    965         sdata, wanted_elements, params_copy, cs, "labels"
    966     )

File [~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata-plot/src/spatialdata_plot/pl/render.py:538](http://localhost:8888/lab/tree/dependencies/spatialdata-notebooks/notebooks/examples/~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata-plot/src/spatialdata_plot/pl/render.py#line=537), in _render_points(sdata, render_params, coordinate_system, ax, fig_params, scalebar_params, legend_params)
    535 # when user specified a single color, we emulate the form of `na_color` and use it
    536 default_color = color if col_for_color is None and color is not None else render_params.cmap_params.na_color
--> 538 color_source_vector, color_vector, _ = _set_color_source_vec(
    539     sdata=sdata_filt,
    540     element=points,
    541     element_name=element,
    542     value_to_plot=col_for_color,
    543     groups=groups,
    544     palette=palette,
    545     na_color=default_color,
    546     cmap_params=render_params.cmap_params,
    547     alpha=render_params.alpha,
    548     table_name=table_name,
    549     render_type="points",
    550 )
    552 # color_source_vector is None when the values aren't categorical
    553 if color_source_vector is None and render_params.transfunc is not None:

File [~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata-plot/src/spatialdata_plot/pl/utils.py:785](http://localhost:8888/lab/tree/dependencies/spatialdata-notebooks/notebooks/examples/~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata-plot/src/spatialdata_plot/pl/utils.py#line=784), in _set_color_source_vec(sdata, element, value_to_plot, na_color, element_name, groups, palette, cmap_params, alpha, table_name, table_layer, render_type)
    781 color_source_vector = pd.Categorical(color_source_vector)  # convert, e.g., `pd.Series`
    783 # TODO check why table_name is not passed here.
    784 color_mapping = _get_categorical_color_mapping(
--> 785     adata=sdata["table"],
    786     cluster_key=value_to_plot,
    787     color_source_vector=color_source_vector,
    788     cmap_params=cmap_params,
    789     alpha=alpha,
    790     groups=groups,
    791     palette=palette,
    792     na_color=na_color,
    793     render_type=render_type,
    794 )
    796 color_source_vector = color_source_vector.set_categories(color_mapping.keys())
    797 if color_mapping is None:

File [~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata/src/spatialdata/_core/spatialdata.py:2290](http://localhost:8888/lab/tree/dependencies/spatialdata-notebooks/notebooks/examples/~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata/src/spatialdata/_core/spatialdata.py#line=2289), in SpatialData.__getitem__(self, item)
   2277 def __getitem__(self, item: str) -> SpatialElement | AnnData:
   2278     """
   2279     Return the element with the given name.
   2280 
   (...)   2288     The element.
   2289     """
-> 2290     _, _, element = self._find_element(item)
   2291     return element

File [~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata/src/spatialdata/_core/spatialdata.py:2191](http://localhost:8888/lab/tree/dependencies/spatialdata-notebooks/notebooks/examples/~/embl/projects/basel/spatialdata-data-converter/dependencies/spatialdata/src/spatialdata/_core/spatialdata.py#line=2190), in SpatialData._find_element(self, element_name)
   2188         found.append((element_type, element_name_, element))
   2190 if len(found) == 0:
-> 2191     raise KeyError(f"Could not find element with name {element_name!r}")
   2193 if len(found) > 1:
   2194     raise ValueError(f"Found multiple elements with name {element_name!r}")

KeyError: "Could not find element with name 'table'"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpoints 🧮Anything related to Points

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions