Repository navigation
pl.render_labels cannot render when color is mapped to a categorical variable. Returns "ValueError: buffer source array is read-only" #353
Copy link
Copy link
Closed
Description
In general, sd.pl.render_labels returns a ValueError if color is set to a categorical variable in the attached anndata.obs table
Using the given cosmx data in the tutorial:
cosmx_sdata.pl.render_labels(elements = "2_labels", outline = True, color = 'Area', cmap = 'magma').pl.show(coordinate_systems=["2"])gives the expected output: segmentation map colored by size of the area in the 'magma' color map
However, the following:
cosmx_sdata.pl.render_labels(elements = "2_labels", outline = True, color = 'fov_labels').pl.show(coordinate_systems=["2"])Does not return the expected plot (segmentation map with a single color to represent the fov label). Instead it returns the following:
ValueError Traceback (most recent call last)
Cell In[418], [line 1](vscode-notebook-cell:?execution_count=418&line=1)
----> [1](vscode-notebook-cell:?execution_count=418&line=1) cosmx_sdata.pl.render_labels(color = 'fov_labels').pl.show(coordinate_systems=["2"])
File ~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:937, in PlotAccessor.show(self, coordinate_systems, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, na_in_legend, colorbar, wspace, hspace, ncols, frameon, figsize, dpi, fig, title, share_extent, pad_extent, ax, return_ax, save)
[925](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:925) _maybe_set_colors(
[926](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:926) source=sdata[table],
[927](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:927) target=sdata[table],
[928](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:928) key=params_copy.color,
[929](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:929) palette=params_copy.palette,
[930](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:930) )
[932](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:932) rasterize = (params_copy.scale is None) or (
[933](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:933) isinstance(params_copy.scale, str)
[934](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:934) and params_copy.scale != "full"
[935](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:935) and (dpi is not None or figsize is not None)
[936](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:936) )
--> [937](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:937) _render_labels(
[938](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:938) sdata=sdata,
[939](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:939) render_params=params_copy,
[940](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:940) coordinate_system=cs,
[941](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:941) ax=ax,
[942](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:942) fig_params=fig_params,
[943](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:943) scalebar_params=scalebar_params,
[944](https://file+.vscode-resource.vscode-cdn.net/Users/a.tu/dev_env/Translational_Analysis_Multi-program_mIF/~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/basic.py:944) legend_params=legend_params,
...
File <stringsource>:663, in View.MemoryView.memoryview_cwrapper()
File <stringsource>:353, in View.MemoryView.memoryview.__cinit__()
ValueError: buffer source array is read-only
identical error is found using other spatialdata objects (public, or custom constructed)
Environment is on m2 macbook pro, on python 3.11
spatialdata is version 0.2.2
xarray is version 2023.12.0
skimage is version 0.24.9
anndata is version 0.10.8
pandas is version 2.2.2
Metadata
Metadata
Assignees
Labels
No labels
Activity
clwgg commentedon Sep 10, 2024
which version of
spatialdata-plotare you using? some of this was "fixed" recently by copying the input array during plotting, although a proper fix withinskimagewould be preferable; see also the discussion here: #344MiTPenguin commentedon Sep 11, 2024
spatialdata-plotis 0.2.4I tried the saving to a zarr and re-read, but that did not work.
MiTPenguin commentedon Sep 11, 2024
I installed the version of
skimagewith the Cython update, and now I have a different errorThis time it's a dimension issue with numpy.dstack. Again data replicated using cosmx example data.
timtreis commentedon Sep 11, 2024
Could you try upgrading to
0.2.6and verify that the issue persists?MiTPenguin commentedon Sep 12, 2024
@timtreis
Upgraded to
0.2.6and the hotfixedskimage(installed from git) seems to have solved the problem (I also updated cython in my conda environment, for good measure)Also fixed some odd behavior with color mapping of discrete integer labels.
timtreis commentedon Sep 12, 2024
Happy to hear that!