Skip to content

render_labels color plot no longer working #403

@mezwick

Description

@mezwick

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions