spatialdata-plot icon indicating copy to clipboard operation
spatialdata-plot copied to clipboard

Transform columns to `category` when possible in `render_shapes`

Open quentinblampey opened this issue 1 year ago • 0 comments

Hello,

When rendering shapes, I get an error when trying to color shapes by a column of dtype object (containing strings) instead of dtype category. It would be great to transform it automatically into a categorical column, as in Scanpy. I think that a quick fix is to run the adata.strings_to_categoricals() method for safety.

Minimum reproducible example

Using the visium.zarr data sample from spatialdata. Here, I have three cell types, but I can't plot them without manually converting the column to dtype "category".

import spatialdata
import spatialdata_plot
import numpy as np

sdata = spatialdata.read_zarr("visium.zarr")

# here, the "cell_type" column is not of dtype "category"
sdata.table.obs["cell_type"] = np.random.choice(["a", "b", "c"], size=sdata.table.n_obs)

sdata.pl.render_shapes(color="cell_type").pl.show("ST8059048")

Error:

ValueError: could not convert string to float: 'a'

Let me know if you want me to do a PR

quentinblampey avatar Jan 29 '24 14:01 quentinblampey