Skip to content

sc.tl.leiden does not overwrite the old leiden_color #3716

@seungteakim

Description

@seungteakim

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the main branch of scanpy.

What happened?

Hi,
I'm writing to report a new issue that I'm encountering. I'm using the newest version of scanpy and python 3.13.
In pre-processing, after I remove the clusters, I re-run the scanpy.tl.leiden to reassign the leiden clusters.
When I try to plot umap it creates the error below, and I think it's due to the change in cluster numbers.
Shouldn't scanpy.tl.leiden function overwrite the old colors? I had no issues before the upgrade.

Deleting leiden_colors in adata.uns resolves the issue, but this is not a desirable as I iterate this step many times.

Thank you for looking into this matter!

Minimal code sample

sc.tl.leiden(adata, resolution = 0.1, random_state=0, neighbors_key='neighbors_10')
sc.tl.umap(adata, neighbors_key = 'neighbors_10', min_dist=0.1)
sc.pl.umap(adata, color = 'timepoint')

Error output

ValueError                                Traceback (most recent call last)
Cell In[126], line 1
----> 1 sc.pl.umap(adata, color = 'leiden')

File ~/miniconda3/envs/scrnaseq/lib/python3.13/site-packages/scanpy/plotting/_tools/scatterplots.py:686, in umap(adata, **kwargs)
    627 @_wraps_plot_scatter
    628 @_doc_params(
    629     adata_color_etc=doc_adata_color_etc,
   (...)    633 )
    634 def umap(adata: AnnData, **kwargs) -> Figure | Axes | list[Axes] | None:
    635     """Scatter plot in UMAP basis.
    636 
    637     Parameters
   (...)    684 
    685     """
--> 686     return embedding(adata, "umap", **kwargs)

File ~/miniconda3/envs/scrnaseq/lib/python3.13/site-packages/scanpy/plotting/_tools/scatterplots.py:288, in embedding(adata, basis, color, mask_obs, 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, marker, **kwargs)
    278 kwargs_scatter = kwargs.copy()  # is potentially mutated for each plot
    279 color_source_vector = _get_color_source_vector(
    280     adata,
    281     value_to_plot,
   (...)    286     groups=groups,
    287 )
--> 288 color_vector, color_type = _color_vector(
    289     adata,
    290     value_to_plot,
    291     values=color_source_vector,
    292     palette=palette,
    293     na_color=na_color,
    294 )
    296 # Order points
    297 order = slice(None)

File ~/miniconda3/envs/scrnaseq/lib/python3.13/site-packages/scanpy/plotting/_tools/scatterplots.py:1256, in _color_vector(adata, values_key, values, palette, na_color)
   1251 elif not isinstance(values, pd.Categorical):
   1252     return values, "cont"
   1254 color_map = {
   1255     k: to_hex(v)
-> 1256     for k, v in _get_palette(adata, values_key, palette=palette).items()
   1257 }
   1258 # If color_map does not have unique values, this can be slow as the
   1259 # result is not categorical
   1260 if Version(pd.__version__) < Version("2.1.0"):

File ~/miniconda3/envs/scrnaseq/lib/python3.13/site-packages/scanpy/plotting/_tools/scatterplots.py:1224, in _get_palette(adata, values_key, palette)
   1222 else:
   1223     _utils._validate_palette(adata, values_key)
-> 1224 return dict(zip(values.categories, adata.uns[color_key], strict=True))

ValueError: zip() argument 2 is longer than argument 1

Versions


Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions