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

Categorical color obs column crashes the plotting

Open sinanugur opened this issue 1 year ago • 2 comments

Hi,

When the "color" is categorical, it crashes and returns ValueError in version 0.2.6. The categorial "test" column contains NaN values. There are 11 possible categories.

This code was working perfectly in version 0.2.4.

Cheers,

sdata_small.pl.render_images("queried_cytassist",alpha=1).pl.render_shapes(
            f"{sample_id}_square_00{bin_size}um", color="test",method="matplotlib"
        ).pl.show(coordinate_systems="global", title="Test clusters", legend_fontsize=8,figsize=(10,10),dpi=200,save=None)
[359](https://file+.vscode-resource.vscode-cdn.net/Users/sium/projects/visiumhd/testing/notebooks/~/miniconda/envs/testing/lib/python3.11/site-packages/spatialdata_plot/pl/utils.py:359)             "Could not convert values in the `color` column to float, if `color` column represents"
    [360](https://file+.vscode-resource.vscode-cdn.net/Users/sium/projects/visiumhd/testing/notebooks/~/miniconda/envs/testing/lib/python3.11/site-packages/spatialdata_plot/pl/utils.py:360)             " categories, set the column to categorical dtype."
    [361](https://file+.vscode-resource.vscode-cdn.net/Users/sium/projects/visiumhd/testing/notebooks/~/miniconda/envs/testing/lib/python3.11/site-packages/spatialdata_plot/pl/utils.py:361)         ) from e

TypeError: '<' not supported between instances of 'str' and 'float'

sinanugur avatar Sep 12 '24 14:09 sinanugur

I tried to reproduce with Visium HD data and I report the following:

  • plotting categoricals when no values are np.nan works
  • plotting categoricals when there are np.nan values gives an error. I have tried both when the column is in a table.obs and when the column is directly in the GeoDataFrame.
    • When the nan value is in table.obs I get your error
    • When the nan value is in a column of the GeoDataFrame, the plot fails with a different error.
  • I have tried also putting a nan value in a continuous column. I report the following:
    • When the nan value is in table.obs, no exception is raised but the plot looks all gray (while it should not). So it still fails.
    • When the nan value is in a column of the GeoDataFrame, the plot fails (with the same error for the categorical case for dataframe columns).

Actionable for @sinanugur. I suggest to remove the rows with nan values for the moment.

Actionable for us. I suggest to support the handing of nan values and add the following tests:

  • [ ] tests for labels, points, shapes:
    • [ ] add nan values in table.obs columns (both categorical and continous).
    • [ ] add nan values in table.X
  • [ ] tests for points, shapes:
    • [ ] add nan values in the dataframe column (both categorical and continous)

LucaMarconato avatar Oct 03 '24 17:10 LucaMarconato

Thank you so much for the triage @LucaMarconato! ❤️

timtreis avatar Oct 03 '24 18:10 timtreis