Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better strategy to compare two color arrays #1884

Closed
wants to merge 2 commits into from

Conversation

OnlyBelter
Copy link

When RGB color represented by 3D arrays, we cannot compare them as list vs list.

Better strategy to compare two 3-dimensional arrays, such as `RGB` color list `[array([0.29803922, 0.44705882, 0.69019608]), array([0.86666667, 0.51764706, 0.32156863]), array([0.33333333, 0.65882353, 0.40784314]), array([0.76862745, 0.30588235, 0.32156863])]`.
The logic should be `np.any()` instead of `np.all()`.
@codecov
Copy link

codecov bot commented Jun 18, 2021

Codecov Report

Merging #1884 (d275026) into master (4dd8de9) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1884   +/-   ##
=======================================
  Coverage   71.22%   71.22%           
=======================================
  Files          92       92           
  Lines       11181    11181           
=======================================
  Hits         7964     7964           
  Misses       3217     3217           
Impacted Files Coverage Δ
scanpy/plotting/_utils.py 54.61% <100.00%> (ø)

@ivirshup
Copy link
Member

Can you tell us a bit about your use case? In general, we mostly assume colors are a list of hex values, and would suspect other things may break if you arrays here.

@OnlyBelter
Copy link
Author

OnlyBelter commented Jun 18, 2021

I don't know why sometimes it's broken when I use some specific column to set the parameter of color in function sc.pl.umap. But sometimes it works well. Is it random for using RGB (3D array) or hex values (a string) or at some specific situation it'll change to RGB?

@ivirshup
Copy link
Member

There was an issue where values were being set as rgb values unintentionally which should be fixed by #1886.

Can you give an example of how this was causing problems? What would cause you to hit errors here?

@OnlyBelter
Copy link
Author

I tried debug by myself and this is what I found at the break point: when the color represented by RGB values, such as [array([0.29803922, 0.44705882, 0.69019608]), array([0.86666667, 0.51764706, 0.32156863]), array([0.33333333, 0.65882353, 0.40784314]), array([0.76862745, 0.30588235, 0.32156863])], it throws error.

It may also cause by the default setting of seaborn. Since you have fixed it in #1886 , this problem should be fixed too.

@OnlyBelter
Copy link
Author

Once again, I got this error, even if I didn't import seaborn.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-a5c62c51242c> in <module>
      1 # plt.figure(figsize=(10, 10))
      2 rcParams['figure.figsize'] = 8, 8
----> 3 sc.pl.umap(all_dataset['pdac_pengj_02'], color='cluster', legend_loc='on data', legend_fontsize='small', title='', frameon=False)

f:\tools\miniconda3\envs\deside2\lib\site-packages\scanpy\plotting\_tools\scatterplots.py in umap(adata, **kwargs)
    601     If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
    602     """
--> 603     return embedding(adata, 'umap', **kwargs)
    604 
    605 

f:\tools\miniconda3\envs\deside2\lib\site-packages\scanpy\plotting\_tools\scatterplots.py in embedding(adata, basis, color, gene_symbols, use_raw, sort_order, edges, edges_width, edges_color, neighbors_key, arrows, arrows_kwds, groups, components, layer, projection, scale_factor, color_map, cmap, palette, na_color, na_in_legend, size, frameon, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, vmax, vmin, add_outline, outline_width, outline_color, ncols, hspace, wspace, title, show, save, ax, return_fig, **kwargs)
    244             groups=groups,
    245         )
--> 246         color_vector, categorical = _color_vector(
    247             adata,
    248             value_to_plot,

f:\tools\miniconda3\envs\deside2\lib\site-packages\scanpy\plotting\_tools\scatterplots.py in _color_vector(adata, values_key, values, palette, na_color)
   1128         return values, False
   1129     else:  # is_categorical_dtype(values)
-> 1130         color_map = _get_palette(adata, values_key, palette=palette)
   1131         color_vector = values.map(color_map).map(to_hex)
   1132 

f:\tools\miniconda3\envs\deside2\lib\site-packages\scanpy\plotting\_tools\scatterplots.py in _get_palette(adata, values_key, palette)
   1103         _utils._set_default_colors_for_categorical_obs(adata, values_key)
   1104     else:
-> 1105         _utils._validate_palette(adata, values_key)
   1106     return dict(zip(values.categories, adata.uns[color_key]))
   1107 

f:\tools\miniconda3\envs\deside2\lib\site-packages\scanpy\plotting\_utils.py in _validate_palette(adata, key)
    355         _palette.append(color)
    356     # Don't modify if nothing changed
--> 357     if _palette is not None and list(_palette) != list(adata.uns[color_key]):
    358         adata.uns[color_key] = _palette
    359 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@OnlyBelter
Copy link
Author

It seems that this problem has been corrected in version 1.8.0. So I close it.

@OnlyBelter OnlyBelter closed this Jul 4, 2021
@ivirshup
Copy link
Member

ivirshup commented Jul 5, 2021

Thanks for the update! Glad to hear it works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants