Repository navigation
render_labels color plot no longer working #403
Copy link
Copy link
Closed
Description
Morning,
Trying to make a side-by-side render_image and render_label plot for my sdata object.
My code worked with earlier versions of spatialdata-plot, but since updating to 0.2.8 have found some issues. (e.g. for render_image, needed to switch from specifying vmin and vmax, to doing this via norm = matplit.colors.Normalize()). My render_image code is now working.
For render_labels though, I seem to get a 'KeyError' when I provide a color parameter. The function works fine when not providing a color parameter. Not sure why, the color name does seem to be a column in sdata.tables['table'].obs
Any pointers?
Am sure this code worked with earlier versions for spatialdata-plot.
Thanks
Code
# TODO: TROUBLESHOOT CODE
from matplotlib import colors
# Generate plot for chosen coords
coord = 'slide'
lbl = 'bulk4_ldn_n50_r0pt15_anno'
# lbl = 'bulk4_ldn_n50_r0pt15'
sdata.tables['table'].uns[f'{lbl}_colors'] = glasbey.create_palette(len(sdata.tables['table'].uns[f'{lbl}_colors']))
fig, axs = plt.subplots(nrows = 1, ncols = 2, dpi=600, figsize = (10, 5))
axs_rvl = axs.ravel()
## render_images, axis 0
# Set vmin and vmax, with clip
norm = colors.Normalize(vmin=0, vmax=200, clip=True)
sdata.pl.render_images(element = f'{coord}_images', channel='DNA1-Ir191', cmap='gray', norm=norm).pl.show(coordinate_systems=coord, title="DNA", colorbar=False, ax=axs_rvl[0])
## render_labels, axis 1
groups = list(sdata.tables['table'].obs[lbl].unique())
palette = sdata.tables['table'].uns[f'{lbl}_colors']
# Works, no color by label call
# sdata.pl.render_labels(element = f'{coord}_labels', table_name='table').pl.show(coordinate_systems=coord, ax=axs_rvl[1], title='bulk clustering', legend_loc='lower left', legend_fontsize=10)
# Errors on the color call
sdata.pl.render_labels(element = f'{coord}_labels', table_name='table', color=lbl, groups=groups, palette=palette).pl.show(coordinate_systems=coord, ax=axs_rvl[1], title='bulk clustering', legend_loc='lower left', legend_fontsize=10)
# Previously working
# sdata.pl.render_labels(element = f'{coord}_labels', color=lbl, groups=groups, palette=palette).pl.show(coordinate_systems=coord, ax=axs_rvl[1], title='bulk clustering', legend_loc='lower left', legend_fontsize=10)
# plt.suptitle(coord)
plt.tight_layout()
Error
Output exceeds the [size limit](command:workbench.action.openSettings?[). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?5f6746ed-c6f8-44ba-95f7-6663ee9aeeae)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File c:\Users\U062951\Miniconda3\envs\imc_ome_v1_spatial\lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key)
3804 try:
-> 3805 return self._engine.get_loc(casted_key)
3806 except KeyError as err:
File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()
File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc()
File pandas\\_libs\\hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item()
File pandas\\_libs\\hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'bulk4_ldn_n50_r0pt15_anno'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Cell In[95], line 28
22 palette = sdata.tables['table'].uns[f'{lbl}_colors']
24 # Works, no color by label call
25 # sdata.pl.render_labels(element = f'{coord}_labels', table_name='table').pl.show(coordinate_systems=coord, ax=axs_rvl[1], title='bulk clustering', legend_loc='lower left', legend_fontsize=10)
26
...
3815 # InvalidIndexError. Otherwise we fall through and re-raise
3816 # the TypeError.
3817 self._check_indexing_error(key)
KeyError: 'bulk4_ldn_n50_r0pt15_anno'
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
mezwick commentedon Jan 30, 2025
Morning.
Adding a minimal code example here to demo based on scverse/napari-spatialdata#324 (comment)
I've tried adding a 'strings' .obs column, with the colors as both a list and a dictionary.
If I use a dictionary, the colors come through with
napari_spatialdata.Interactive()viewer (but only for napari_spatialdata 0.5.4post0, these fail with 0.5.5). However, i can't call the colors via.render_labels()fromspatialdata_plot.Incidentally, I thought colors were supposed to be a list of hexcodes? Isn't that what scanpy would expect if pulling the same colors rather than a dictionary?
The error output is
LucaMarconato commentedon Jan 30, 2025
Hi, thanks for the minimal example.
If you add
obs["strings"] = obs["strings"].astype('category')afterobs["strings"] = ["A", "A", "B", "B", "C"]spatialdata-plot doesn't raise errors and makes this plot:This is the full code
You can add a header
You can add text within a collapsed section.
You can add an image or a code block, too.
@timtreis @Sonja-Stockhaus I think the error message/or check for type should take into account into this. Specifically, if the column is not converted to a categorical column, then the code gets an exception here:
spatialdata-plot/src/spatialdata_plot/pl/utils.py
Line 824 in 478ab27
A check on the
dtypeof the column could be added there, and if it's a string, we could try to convert it to categorical and raise a warning saying that the user should convert to categorical before.LucaMarconato commentedon Jan 30, 2025
I could reproduce the
naparibug, I linked this issue to this one: scverse/napari-spatialdata#324.LucaMarconato commentedon Jan 30, 2025
@mezwick btw tests are being added in this PR #413
mezwick commentedon Jan 30, 2025
Thanks for your help.
.astype('category')does solve the minimal code example for me and render_labels now works.Though I would expect render_labels(colors='strings') to render in the red, green, blue, colors specified in

.uns['strings_colors']rather than default seaborn categorical green, orange, blue?They do show as red, green, blue in the Interactive() napari?
LucaMarconato commentedon Jan 30, 2025
Good point, yes the bug in
spatialdata-plotis still present.