scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
420 stars 76 forks source link

Invalid RGBA argument when use sq.pl.co_occurrence #855

Open Donbbit opened 1 month ago

Donbbit commented 1 month ago

When I used squidpy to draw Co-occurrence figure:

sq.gr.spatial_neighbors(adata) sq.gr.nhood_enrichment(adata, cluster_key="Level1_Filt") sq.pl.nhood_enrichment(adata, cluster_key="Level1_Filt",palette = 'tab20') sq.gr.co_occurrence(adata, cluster_key="Level1_Filt") sq.pl.co_occurrence( adata, cluster_key="Level1_Filt", clusters="B", figsize=(8, 4), )

I got this error:

ValueError Traceback (most recent call last) Cell In[115], line 1 ----> 1 sq.pl.co_occurrence( 2 adata, 3 cluster_key="Level1_Filt", 4 clusters="B", 5 figsize=(8, 4), 6 )

File /squidpy/lib/python3.10/site-packages/squidpy/pl/_graph.py:365, in co_occurrence(adata, cluster_key, palette, clusters, figsize, dpi, save, legend_kwargs, **kwargs) 362 clusters = _assert_non_empty_sequence(clusters, name="clusters") 363 clusters = sorted(_get_valid_values(clusters, categories)) --> 365 palette = _get_palette(adata, cluster_key=cluster_key, categories=categories, palette=palette) 367 fig, axs = plt.subplots( 368 1, 369 len(clusters), (...) 372 constrained_layout=True, 373 ) 374 axs = np.ravel(axs) # make into iterable

File /squidpy/lib/python3.10/site-packages/squidpy/pl/_color_utils.py:50, in _get_palette(adata, cluster_key, categories, palette, alpha) 45 if len(palette) != len(categories): 46 raise ValueError( 47 f"Expected palette to be of length {len(categories)}, found {len(palette)}. " 48 + f"Removing the colors in adata.uns with adata.uns.pop('{cluster_key}_colors') may help." 49 ) ---> 50 return {cat: to_hex(to_rgba(col)[:3] + (alpha,), keep_alpha=True) for cat, col in zip(categories, palette)} 51 except KeyError as e: 52 logg.error(f"Unable to fetch palette, reason: {e}. Using None.")

File /squidpy/lib/python3.10/site-packages/squidpy/pl/_color_utils.py:50, in (.0) 45 if len(palette) != len(categories): 46 raise ValueError( 47 f"Expected palette to be of length {len(categories)}, found {len(palette)}. " 48 + f"Removing the colors in adata.uns with adata.uns.pop('{cluster_key}_colors') may help." 49 ) ---> 50 return {cat: to_hex(to_rgba(col)[:3] + (alpha,), keep_alpha=True) for cat, col in zip(categories, palette)} 51 except KeyError as e: 52 logg.error(f"Unable to fetch palette, reason: {e}. Using None.")

File/squidpy/lib/python3.10/site-packages/matplotlib/colors.py:299, in to_rgba(c, alpha) 297 rgba = None 298 if rgba is None: # Suppress exception chaining of cache lookup failure. --> 299 rgba = _to_rgba_no_colorcycle(c, alpha) 300 try: 301 _colors_full_map.cache[c, alpha] = rgba

File /squidpy/lib/python3.10/site-packages/matplotlib/colors.py:374, in _to_rgba_no_colorcycle(c, alpha) 370 raise ValueError( 371 f"Invalid string grayscale value {orig_c!r}. " 372 f"Value must be within 0-1 range") 373 return c, c, c, alpha if alpha is not None else 1. --> 374 raise ValueError(f"Invalid RGBA argument: {orig_c!r}") 375 # turn 2-D array into 1-D array 376 if isinstance(c, np.ndarray):

ValueError: Invalid RGBA argument: 'B'

How can I solve this problem?

giovp commented 1 month ago

hi @Donbbit , see the error

 f"Expected palette to be of length {len(categories)}, found {len(palette)}. "

the len of the categories and palette needs to be the same.