Closed joonlee3 closed 2 months ago
hi @joonlee3 , so the plotting functions in squidpy don't work with spatialdata. In this specific case, they don't work for anndata either, because the anndata stored in spatialdata does not have all info required for the plotting function to work. In this specific case, if you would have set shape=None
then it would have worked, as spatial_scatter
would have defaulted to scatterpplot and not visium circles. However, I would suggest to use spatialdata-plot for plotting spatialdata and not squidpy
Description
I would like to visualize two features (total_counts and n_genes_by_counts) in my spatialdata objects by using
sq.pl.spatial_scatter
. I was able to generate those figures when I loaded my visium data by usingsq.read.visium
but it is not clear how to do it with my Xenium data in SpatialData format. Any thoughts or suggestions? Thank you.Minimal reproducible example
Here is my code:
sq.pl.spatial_scatter(sdata_list[0]["table"], color=["total_counts", "n_genes_by_counts"], ncols=2, img=False)
Traceback
Cell In[40], line 1 ----> 1 sq.pl.spatial_scatter(sdata_list[0]["table"], 2 color=["total_counts", "n_genes_by_counts"], ncols=2, img=False)
File ~/.local/lib/python3.9/site-packages/squidpy/pl/_spatial.py:420, in spatial_scatter(adata, shape, kwargs) 379 @d.dedent 380 @_wrap_signature 381 def spatial_scatter( (...) 384 kwargs: Any, 385 ) -> Axes | Sequence[Axes] | None: 386 """ 387 Plot spatial omics data with data overlayed on top. 388 (...) 418 %(spatial_plot.returns)s 419 """ --> 420 return _spatial_plot(adata, shape=shape, seg=None, seg_key=None, **kwargs)
File ~/.local/lib/python3.9/site-packages/squidpy/pl/_spatial.py:182, in _spatial_plot(adata, shape, color, groups, library_id, library_key, spatial_key, img, img_res_key, img_alpha, img_cmap, img_channel, seg, seg_key, seg_cell_id, seg_contourpx, seg_outline, use_raw, layer, alt_var, size, size_key, scale_factor, crop_coord, cmap, palette, alpha, norm, na_color, connectivity_key, edges_width, edges_color, library_first, frameon, wspace, hspace, ncols, outline, outline_color, outline_width, legend_loc, legend_fontsize, legend_fontweight, legend_fontoutline, legend_na, colorbar, scalebar_dx, scalebar_units, title, axis_label, fig, ax, return_ax, figsize, dpi, save, scalebar_kwargs, edges_kwargs, kwargs) 168 edges_kwargs = dict(edges_kwargs) 170 color_params = _prepare_args_plot( 171 adata=adata, 172 shape=shape, (...) 179 palette=palette, 180 ) --> 182 spatial_params = _image_spatial_attrs( 183 adata=adata, 184 shape=shape, 185 spatial_key=spatial_key, 186 library_id=library_id, 187 library_key=library_key, 188 img=img, 189 img_res_key=img_res_key, 190 img_channel=img_channel, 191 seg=seg, 192 seg_key=seg_key, 193 cell_id_key=seg_cell_id, 194 scale_factor=scale_factor, 195 size=size, 196 size_key=size_key, 197 img_cmap=img_cmap, 198 ) 200 coords, crops = _set_coords_crops( 201 adata=adata, 202 spatial_params=spatial_params, 203 spatial_key=spatial_key, 204 crop_coord=crop_coord, 205 ) 207 fig_params, cmap_params, scalebar_params, kwargs = _prepare_params_plot( 208 color_params=color_params, 209 spatial_params=spatial_params, (...) 228 kwargs, 229 )
File ~/.local/lib/python3.9/site-packages/squidpy/pl/_spatial_utils.py:289, in _image_spatial_attrs(adata, shape, spatial_key, library_id, library_key, img, img_res_key, img_channel, seg, seg_key, cell_id_key, scale_factor, size, size_key, img_cmap) 286 return False 287 return img is True or len(img) # type: ignore --> 289 library_id = _get_library_id( 290 adata=adata, 291 shape=shape, 292 spatial_key=spatial_key, 293 library_id=library_id, 294 library_key=library_key, 295 ) 296 if len(library_id) > 1 and library_key is None: 297 raise ValueError( 298 f"Found library_id: {library_id} but no library_key was specified. Please specify library_key." 299 )
File ~/.local/lib/python3.9/site-packages/squidpy/pl/_spatial_utils.py:128, in _get_library_id(adata, shape, spatial_key, library_id, library_key) 120 def _get_library_id( 121 adata: AnnData, 122 shape: _AvailShapes | None, (...) 125 library_key: str | None = None, 126 ) -> Sequence[str]: 127 if shape is not None: --> 128 library_id = Key.uns.library_id(adata, spatial_key, library_id, return_all=True) 129 if library_id is None: 130 raise ValueError(f"Could not fetch library_id, check that spatial_key: {spatial_key} is correct.")
File ~/.local/lib/python3.9/site-packages/squidpy/_constants/_pkg_constants.py:151, in Key.uns.library_id(cls, adata, spatial_key, library_id, return_all) 143 https://github.com/classmethod 144 def library_id( 145 cls, (...) 149 return_all: bool = False, 150 ) -> Sequence[str] | str | None: --> 151 library_id = cls._sort_haystack(adata, spatial_key, library_id, sub_key=None) 152 if return_all or library_id is None: 153 return library_id
File ~/.local/lib/python3.9/site-packages/squidpy/_constants/_pkg_constants.py:182, in Key.uns._sort_haystack(cls, adata, spatial_key, library_id, sub_key) 173 https://github.com/classmethod 174 def _sort_haystack( 175 cls, (...) 179 sub_key: str | None = None, 180 ) -> Sequence[str] | None: 181 if spatial_key not in adata.uns: --> 182 raise KeyError(f"Spatial key {spatial_key!r} not found in adata.uns.") 183 haystack = list(adata.uns[spatial_key]) 184 if library_id is not None:
KeyError: "Spatial key 'spatial' not found in adata.uns."