Skip to content

pl.render_labels cannot render when color is mapped to a categorical variable. Returns "ValueError: buffer source array is read-only" #353

Closed

Description

@MiTPenguin

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

Activity

  1. transferred this issue fromscverse/spatialdataon Sep 9, 2024
  2. clwgg commented on Sep 10, 2024

    @clwgg

    which version of spatialdata-plot are you using? some of this was "fixed" recently by copying the input array during plotting, although a proper fix within skimage would be preferable; see also the discussion here: #344

  3. MiTPenguin commented on Sep 11, 2024

    @MiTPenguin
    Author

    spatialdata-plot is 0.2.4

    I tried the saving to a zarr and re-read, but that did not work.

  4. MiTPenguin commented on Sep 11, 2024

    @MiTPenguin
    Author

    I installed the version of skimage with the Cython update, and now I have a different error

    {
    	"name": "ValueError",
    	"message": "all the input arrays must have same number of dimensions, but the array at index 0 has 4 dimension(s) and the array at index 1 has 3 dimension(s)",
    	"stack": "---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    Cell In[11], line 1
    ----> 1 cosmx_sdata.pl.render_labels(elements = \"2_labels\", outline = True, color = 'fov_labels', cmap = 'magma').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                 _maybe_set_colors(
        926                     source=sdata[table],
        927                     target=sdata[table],
        928                     key=params_copy.color,
        929                     palette=params_copy.palette,
        930                 )
        932         rasterize = (params_copy.scale is None) or (
        933             isinstance(params_copy.scale, str)
        934             and params_copy.scale != \"full\"
        935             and (dpi is not None or figsize is not None)
        936         )
    --> 937         _render_labels(
        938             sdata=sdata,
        939             render_params=params_copy,
        940             coordinate_system=cs,
        941             ax=ax,
        942             fig_params=fig_params,
        943             scalebar_params=scalebar_params,
        944             legend_params=legend_params,
        945             rasterize=rasterize,
        946         )
        948 if title is None:
        949     t = cs
    
    File ~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/render.py:779, in _render_labels(sdata, render_params, coordinate_system, ax, fig_params, scalebar_params, legend_params, rasterize)
        765 color_source_vector, color_vector, categorical = _set_color_source_vec(
        766     sdata=sdata_filt,
        767     element=label,
       (...)
        774     table_name=table_name,
        775 )
        777 if (render_params.fill_alpha != render_params.outline_alpha) and render_params.contour_px is not None:
        778     # First get the labels infill and plot them
    --> 779     labels_infill = _map_color_seg(
        780         seg=label.values,
        781         cell_id=instance_id,
        782         color_vector=color_vector,
        783         color_source_vector=color_source_vector,
        784         cmap_params=render_params.cmap_params,
        785         seg_erosionpx=None,
        786         seg_boundaries=render_params.outline,
        787         na_color=render_params.cmap_params.na_color,
        788     )
        789     _cax = ax.imshow(
        790         labels_infill,
        791         rasterized=True,
       (...)
        796         zorder=render_params.zorder,
        797     )
        798     _cax.set_transform(trans_data)
    
    File ~/miniforge3/envs/spatial/lib/python3.11/site-packages/spatialdata_plot/pl/utils.py:781, in _map_color_seg(seg, cell_id, color_vector, color_source_vector, cmap_params, seg_erosionpx, seg_boundaries, na_color)
        779         seg = np.squeeze(seg, axis=0)
        780     seg_bound: ArrayLike = np.clip(seg_im - find_boundaries(seg)[:, :, None], 0, 1)
    --> 781     return np.dstack((seg_bound, np.where(val_im > 0, 1, 0)))  # add transparency here
        783 if len(val_im.shape) != len(seg_im.shape):
        784     val_im = np.expand_dims((val_im > 0).astype(int), axis=-1)
    
    File ~/miniforge3/envs/spatial/lib/python3.11/site-packages/numpy/lib/shape_base.py:715, in dstack(tup)
        713 if not isinstance(arrs, list):
        714     arrs = [arrs]
    --> 715 return _nx.concatenate(arrs, 2)
    
    ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 4 dimension(s) and the array at index 1 has 3 dimension(s)"
    }
    

    This time it's a dimension issue with numpy.dstack. Again data replicated using cosmx example data.

  5. timtreis commented on Sep 11, 2024

    @timtreis
    Member

    Could you try upgrading to 0.2.6 and verify that the issue persists?

  6. self-assigned this
    on Sep 11, 2024
  7. MiTPenguin commented on Sep 12, 2024

    @MiTPenguin
    Author

    @timtreis

    Upgraded to 0.2.6 and the hotfixed skimage (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.

  8. timtreis commented on Sep 12, 2024

    @timtreis
    Member

    Happy to hear that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions