spatialdata-plot
spatialdata-plot copied to clipboard
Transform columns to `category` when possible in `render_shapes`
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