Skip to content

InvalidIndexError: (slice(None, None, None), (0, 1)) when using sc.pl.spatial #29

Open

Description

@Flyingcattle

Hi there,
Thanks for the powerful tool.
When reproducing the tutorial, I got an error when using

with mplscience.style_context():
sc.pl.spatial(
adata,
color=genes,
cmap=cmap,
frameon=False,
vmin='p0',
vmax='p95',
spot_size=30,
)

The error looks like this:

TypeError Traceback (most recent call last)
File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
3620 try:
-> 3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/pandas/_libs/index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/pandas/_libs/index.pyx:142, in pandas._libs.index.IndexEngine.get_loc()

TypeError: '(slice(None, None, None), (0, 1))' is an invalid key

During handling of the above exception, another exception occurred:

InvalidIndexError Traceback (most recent call last)
Input In [22], in <cell line: 1>()
1 with mplscience.style_context():
----> 2 sc.pl.spatial(
3 adata,
4 color=genes,
5 cmap=cmap,
6 frameon=False,
7 vmin='p0',
8 vmax='p95',
9 spot_size=30,
10 )

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/scanpy/plotting/_tools/scatterplots.py:1003, in spatial(adata, basis, img, img_key, library_id, crop_coord, alpha_img, bw, size, scale_factor, spot_size, na_color, show, return_fig, save, **kwargs)
1000 cmap_img = None
1001 circle_radius = size * scale_factor * spot_size * 0.5
-> 1003 axs = embedding(
1004 adata,
1005 basis=basis,
1006 scale_factor=scale_factor,
1007 size=circle_radius,
1008 na_color=na_color,
1009 show=False,
1010 save=False,
1011 **kwargs,
1012 )
1013 if not isinstance(axs, list):
1014 axs = [axs]

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/scanpy/plotting/_tools/scatterplots.py:286, in embedding(adata, basis, color, gene_symbols, use_raw, sort_order, edges, edges_width, edges_color, neighbors_key, arrows, arrows_kwds, groups, components, dimensions, layer, projection, scale_factor, color_map, cmap, palette, na_color, na_in_legend, size, frameon, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, colorbar_loc, vmax, vmin, vcenter, norm, add_outline, outline_width, outline_color, ncols, hspace, wspace, title, show, save, ax, return_fig, **kwargs)
284 color_source_vector = color_source_vector[order]
285 color_vector = color_vector[order]
--> 286 coords = basis_values[:, dims][order, :]
288 # if plotting multiple panels, get the ax from the grid spec
289 # else use the ax value (either user given or created previously)
290 if grid:

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/pandas/core/frame.py:3505, in DataFrame.getitem(self, key)
3503 if self.columns.nlevels > 1:
3504 return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
3506 if is_integer(indexer):
3507 indexer = [indexer]

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/pandas/core/indexes/base.py:3628, in Index.get_loc(self, key, method, tolerance)
3623 raise KeyError(key) from err
3624 except TypeError:
3625 # If we have a listlike key, _check_indexing_error will raise
3626 # InvalidIndexError. Otherwise we fall through and re-raise
3627 # the TypeError.
-> 3628 self._check_indexing_error(key)
3629 raise
3631 # GH#42269

File ~/anaconda3/envs/hotspotsc2/lib/python3.8/site-packages/pandas/core/indexes/base.py:5637, in Index._check_indexing_error(self, key)
5633 def _check_indexing_error(self, key):
5634 if not is_scalar(key):
5635 # if key is not a scalar, directly raise an error (the code below
5636 # would convert to numpy arrays and raise later any way) - GH29926
-> 5637 raise InvalidIndexError(key)

InvalidIndexError: (slice(None, None, None), (0, 1))

Be grateful if you could give some help
Thank you for your time!

Activity

  1. chuangzhao0601 commented on Sep 20, 2024

    @chuangzhao0601

    encountered the same error, did you figure out how to resolve it?

  2. yuanwxu commented on Jan 9, 2025

    @yuanwxu

    This worked for me
    adata.obsm['spatial'] = adata.obsm['spatial'].to_numpy()

  3. AdotaLover commented on Jan 22, 2025

    @AdotaLover

    This worked for me adata.obsm['spatial'] = adata.obsm['spatial'].to_numpy()

    Thank you!!!It worked for me as well,but why changing it to numpy will work??

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

Metadata

Metadata

Assignees

No one assigned

    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