scverse / pertpy

Perturbation Analysis in the scverse ecosystem.
https://pertpy.readthedocs.io/en/latest/
MIT License
129 stars 20 forks source link

pt.pl.milo.da_beeswarm is not working #546

Closed VladimirShitov closed 6 months ago

VladimirShitov commented 7 months ago

Report

I have 2 errors.

  1. Basically, the same as https://github.com/theislab/pertpy/issues/525 (at the end of the traceback)
  2. Adding "Mixed" category for neighborhood annotation does not add a color for it in .uns["cell_type_colors"] of AnnData. This causes error ValueError: The palette dictionary is missing keys: {'Mixed'}

I don't have a short way to reproduce it, but I received it after running the following code from the best practices book chapter with my data:

milo.annotate_nhoods(mdata, anno_col="cell_label")
# Define as mixed if fraction of cells in nhood with same label is lower than 0.75

mdata["milo"].var.loc[
    mdata["milo"].var["nhood_annotation_frac"] < 0.75, "nhood_annotation"
] = "Mixed"

The full traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/pertpy/plot/_milopy.py:206](...pertpy/plot/_milopy.py:206), in MilopyPlot.da_beeswarm(mdata, feature_key, anno_col, alpha, subset_nhoods, palette)
    [203](...pertpy/plot/_milopy.py:203)         palette = dict(
    [204](...pertpy/plot/_milopy.py:204)             zip(mdata[feature_key].obs[obs_col].cat.categories, mdata[feature_key].uns[f"{obs_col}_colors"])
    [205](...pertpy/plot/_milopy.py:205)         )
--> [206](...pertpy/plot/_milopy.py:206)     sns.violinplot(
    [207](...pertpy/plot/_milopy.py:207)         data=anno_df,
    [208](...pertpy/plot/_milopy.py:208)         y=anno_col,
    [209](...pertpy/plot/_milopy.py:209)         x="logFC",
    [210](...pertpy/plot/_milopy.py:210)         order=sorted_annos,
    [211](...pertpy/plot/_milopy.py:211)         size=190,
    [212](...pertpy/plot/_milopy.py:212)         inner=None,
    [213](...pertpy/plot/_milopy.py:213)         orient="h",
    [214](...pertpy/plot/_milopy.py:214)         palette=palette,
    [215](...pertpy/plot/_milopy.py:215)         linewidth=0,
    [216](...pertpy/plot/_milopy.py:216)         scale="width",
    [217](...pertpy/plot/_milopy.py:217)     )
    [218](...pertpy/plot/_milopy.py:218) except BaseException:  # noqa: BLE001

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/seaborn/categorical.py:1754](...seaborn/categorical.py:1754), in violinplot(data, x, y, hue, order, hue_order, orient, color, palette, saturation, fill, inner, split, width, dodge, gap, linewidth, linecolor, cut, gridsize, bw_method, bw_adjust, density_norm, common_norm, hue_norm, formatter, log_scale, native_scale, legend, scale, scale_hue, bw, inner_kws, ax, **kwargs)
   [1753](...seaborn/categorical.py:1753) saturation = saturation if fill else 1
-> [1754](...seaborn/categorical.py:1754) p.map_hue(palette=palette, order=hue_order, norm=hue_norm, saturation=saturation)
   [1755](...seaborn/categorical.py:1755) color = _default_color(
   [1756](...seaborn/categorical.py:1756)     ax.fill_between, hue, color,
   [1757](...seaborn/categorical.py:1757)     {k: v for k, v in kwargs.items() if k in ["c", "color", "fc", "facecolor"]},
   [1758](...seaborn/categorical.py:1758)     saturation=saturation,
   [1759](...seaborn/categorical.py:1759) )

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/seaborn/_base.py:838](...seaborn/_base.py:838), in VectorPlotter.map_hue(self, palette, order, norm, saturation)
    [837](...seaborn/_base.py:837) def map_hue(self, palette=None, order=None, norm=None, saturation=1):
--> [838](...seaborn/_base.py:838)     mapping = HueMapping(self, palette, order, norm, saturation)
    [839](...seaborn/_base.py:839)     self._hue_map = mapping

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/seaborn/_base.py:150](...seaborn/_base.py:150), in HueMapping.__init__(self, plotter, palette, order, norm, saturation)
    [149](...seaborn/_base.py:149)     cmap = norm = None
--> [150](...seaborn/_base.py:150)     levels, lookup_table = self.categorical_mapping(
    [151](...seaborn/_base.py:151)         data, palette, order,
    [152](...seaborn/_base.py:152)     )
    [154](...seaborn/_base.py:154) # --- Option 3: datetime mapping
    [155](...seaborn/_base.py:155) 
    [156](...seaborn/_base.py:156) else:
    [157](...seaborn/_base.py:157)     # TODO this needs actual implementation

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/seaborn/_base.py:234](...seaborn/_base.py:234), in HueMapping.categorical_mapping(self, data, palette, order)
    [233](...seaborn/_base.py:233)     err = "The palette dictionary is missing keys: {}"
--> [234](...seaborn/_base.py:234)     raise ValueError(err.format(missing))
    [236](...seaborn/_base.py:236) lookup_table = palette

ValueError: The palette dictionary is missing keys: {'Mixed'}

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
[/home/icb/vladimir.shitov/projects/2023_12_COPD_Francesca_analysis/03_data_analysis.ipynb](https://vscode-remote+localhost-003a8081.vscode-resource.vscode-cdn.net/home/icb/vladimir.shitov/projects/2023_12_COPD_Francesca_analysis/03_data_analysis.ipynb) Cell 43 line 1
----> [1](vscode-notebook-cell://localhost:8081/home/icb/vladimir.shitov/projects/2023_12_COPD_Francesca_analysis/03_data_analysis.ipynb#Y161sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0) pt.pl.milo.da_beeswarm(mdata)
      [2](vscode-notebook-cell://localhost:8081/home/icb/vladimir.shitov/projects/2023_12_COPD_Francesca_analysis/03_data_analysis.ipynb#Y161sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1) plt.show()

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/pertpy/plot/_milopy.py:219](...pertpy/plot/_milopy.py:219), in MilopyPlot.da_beeswarm(mdata, feature_key, anno_col, alpha, subset_nhoods, palette)
    [206](...pertpy/plot/_milopy.py:206)     sns.violinplot(
    [207](...pertpy/plot/_milopy.py:207)         data=anno_df,
    [208](...pertpy/plot/_milopy.py:208)         y=anno_col,
   (...)
    [216](...pertpy/plot/_milopy.py:216)         scale="width",
    [217](...pertpy/plot/_milopy.py:217)     )
    [218](...pertpy/plot/_milopy.py:218) except BaseException:  # noqa: BLE001
--> [219](...pertpy/plot/_milopy.py:219)     sns.violinplot(
    [220](...pertpy/plot/_milopy.py:220)         data=anno_df,
    [221](...pertpy/plot/_milopy.py:221)         y=anno_col,
    [222](...pertpy/plot/_milopy.py:222)         x="logFC",
    [223](...pertpy/plot/_milopy.py:223)         order=sorted_annos,
    [224](...pertpy/plot/_milopy.py:224)         size=190,
    [225](...pertpy/plot/_milopy.py:225)         inner=None,
    [226](...pertpy/plot/_milopy.py:226)         orient="h",
    [227](...pertpy/plot/_milopy.py:227)         linewidth=0,
    [228](...pertpy/plot/_milopy.py:228)         scale="width",
    [229](...pertpy/plot/_milopy.py:229)     )
    [230](...pertpy/plot/_milopy.py:230) sns.stripplot(
    [231](...pertpy/plot/_milopy.py:231)     data=anno_df,
    [232](...pertpy/plot/_milopy.py:232)     y=anno_col,
   (...)
    [239](...pertpy/plot/_milopy.py:239)     alpha=0.5,
    [240](...pertpy/plot/_milopy.py:240) )
    [241](...pertpy/plot/_milopy.py:241) plt.legend(loc="upper left", title=f"< {int(alpha * 100)}% SpatialFDR", bbox_to_anchor=(1, 1), frameon=False)

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/seaborn/categorical.py:1770](...seaborn/categorical.py:1770), in violinplot(data, x, y, hue, order, hue_order, orient, color, palette, saturation, fill, inner, split, width, dodge, gap, linewidth, linecolor, cut, gridsize, bw_method, bw_adjust, density_norm, common_norm, hue_norm, formatter, log_scale, native_scale, legend, scale, scale_hue, bw, inner_kws, ax, **kwargs)
   [1767](...seaborn/categorical.py:1767) kde_kws = dict(cut=cut, gridsize=gridsize, bw_method=bw_method, bw_adjust=bw_adjust)
   [1768](...seaborn/categorical.py:1768) inner_kws = {} if inner_kws is None else inner_kws.copy()
-> [1770](...seaborn/categorical.py:1770) p.plot_violins(
   [1771](...seaborn/categorical.py:1771)     width=width,
   [1772](...seaborn/categorical.py:1772)     dodge=dodge,
   [1773](...seaborn/categorical.py:1773)     gap=gap,
   [1774](...seaborn/categorical.py:1774)     split=split,
   [1775](...seaborn/categorical.py:1775)     color=color,
   [1776](...seaborn/categorical.py:1776)     fill=fill,
   [1777](...seaborn/categorical.py:1777)     linecolor=linecolor,
   [1778](...seaborn/categorical.py:1778)     linewidth=linewidth,
   [1779](...seaborn/categorical.py:1779)     inner=inner,
   [1780](...seaborn/categorical.py:1780)     density_norm=density_norm,
   [1781](...seaborn/categorical.py:1781)     common_norm=common_norm,
   [1782](...seaborn/categorical.py:1782)     kde_kws=kde_kws,
   [1783](...seaborn/categorical.py:1783)     inner_kws=inner_kws,
   [1784](...seaborn/categorical.py:1784)     plot_kws=kwargs,
   [1785](...seaborn/categorical.py:1785) )
   [1787](...seaborn/categorical.py:1787) p._add_axis_labels(ax)
   [1788](...seaborn/categorical.py:1788) p._adjust_cat_axis(ax, axis=p.orient)

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/seaborn/categorical.py:1047](...seaborn/categorical.py:1047), in _CategoricalPlotter.plot_violins(self, width, dodge, gap, split, color, fill, linecolor, linewidth, inner, density_norm, common_norm, kde_kws, inner_kws, plot_kws)
   [1045](...seaborn/categorical.py:1045) # Plot the main violin body
   [1046](...seaborn/categorical.py:1046) plot_func = {"x": ax.fill_betweenx, "y": ax.fill_between}[self.orient]
-> [1047](...seaborn/categorical.py:1047) plot_func(
   [1048](...seaborn/categorical.py:1048)     inv_val(data[value_var]),
   [1049](...seaborn/categorical.py:1049)     inv_pos(data[self.orient] - offsets[0]),
   [1050](...seaborn/categorical.py:1050)     inv_pos(data[self.orient] + offsets[1]),
   [1051](...seaborn/categorical.py:1051)     **violin["kwargs"]
   [1052](...seaborn/categorical.py:1052) )
   [1054](...seaborn/categorical.py:1054) # Adjust the observation data
   [1055](...seaborn/categorical.py:1055) obs = violin["observations"]

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/__init__.py:1465](...matplotlib/__init__.py:1465), in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   [1462](...matplotlib/__init__.py:1462) @functools.wraps(func)
   [1463](...matplotlib/__init__.py:1463) def inner(ax, *args, data=None, **kwargs):
   [1464](...matplotlib/__init__.py:1464)     if data is None:
-> [1465](...matplotlib/__init__.py:1465)         return func(ax, *map(sanitize_sequence, args), **kwargs)
   [1467](...matplotlib/__init__.py:1467)     bound = new_sig.bind(ax, *args, **kwargs)
   [1468](...matplotlib/__init__.py:1468)     auto_label = (bound.arguments.get(label_namer)
   [1469](...matplotlib/__init__.py:1469)                   or bound.kwargs.get(label_namer))

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/axes/_axes.py:5512](...matplotlib/axes/_axes.py:5512), in Axes.fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
   [5510](...matplotlib/axes/_axes.py:5510) def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
   [5511](...matplotlib/axes/_axes.py:5511)                  step=None, **kwargs):
-> [5512](...matplotlib/axes/_axes.py:5512)     return self._fill_between_x_or_y(
   [5513](...matplotlib/axes/_axes.py:5513)         "x", x, y1, y2,
   [5514](...matplotlib/axes/_axes.py:5514)         where=where, interpolate=interpolate, step=step, **kwargs)

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/axes/_axes.py:5493](...matplotlib/axes/_axes.py:5493), in Axes._fill_between_x_or_y(self, ind_dir, ind, dep1, dep2, where, interpolate, step, **kwargs)
   [5489](...matplotlib/axes/_axes.py:5489)         pts = pts[:, ::-1]
   [5491](...matplotlib/axes/_axes.py:5491)     polys.append(pts)
-> [5493](...matplotlib/axes/_axes.py:5493) collection = mcoll.PolyCollection(polys, **kwargs)
   [5495](...matplotlib/axes/_axes.py:5495) # now update the datalim and autoscale
   [5496](...matplotlib/axes/_axes.py:5496) pts = np.vstack([np.hstack([ind[where, None], dep1[where, None]]),
   [5497](...matplotlib/axes/_axes.py:5497)                  np.hstack([ind[where, None], dep2[where, None]])])

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/collections.py:1198](...matplotlib/collections.py:1198), in PolyCollection.__init__(self, verts, sizes, closed, **kwargs)
   [1178](...matplotlib/collections.py:1178) def __init__(self, verts, sizes=None, *, closed=True, **kwargs):
   [1179](...matplotlib/collections.py:1179)     """
   [1180](...matplotlib/collections.py:1180)     Parameters
   [1181](...matplotlib/collections.py:1181)     ----------
   (...)
   [1196](...matplotlib/collections.py:1196)         Forwarded to `.Collection`.
   [1197](...matplotlib/collections.py:1197)     """
-> [1198](...matplotlib/collections.py:1198)     super().__init__(**kwargs)
   [1199](...matplotlib/collections.py:1199)     self.set_sizes(sizes)
   [1200](...matplotlib/collections.py:1200)     self.set_verts(verts, closed)

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/collections.py:203](...matplotlib/collections.py:203), in Collection.__init__(self, edgecolors, facecolors, linewidths, linestyles, capstyle, joinstyle, antialiaseds, offsets, offset_transform, norm, cmap, pickradius, hatch, urls, zorder, **kwargs)
    [200](...matplotlib/collections.py:200) self._offset_transform = offset_transform
    [202](...matplotlib/collections.py:202) self._path_effects = None
--> [203](...matplotlib/collections.py:203) self._internal_update(kwargs)
    [204](...matplotlib/collections.py:204) self._paths = None

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/artist.py:1219](...matplotlib/artist.py:1219), in Artist._internal_update(self, kwargs)
   [1212](...matplotlib/artist.py:1212) def _internal_update(self, kwargs):
   [1213](...matplotlib/artist.py:1213)     """
   [1214](...matplotlib/artist.py:1214)     Update artist properties without prenormalizing them, but generating
   [1215](...matplotlib/artist.py:1215)     errors as if calling `set`.
   [1216](...matplotlib/artist.py:1216) 
   [1217](...matplotlib/artist.py:1217)     The lack of prenormalization is to maintain backcompatibility.
   [1218](...matplotlib/artist.py:1218)     """
-> [1219](...matplotlib/artist.py:1219)     return self._update_props(
   [1220](...matplotlib/artist.py:1220)         kwargs, "{cls.__name__}.set() got an unexpected keyword argument "
   [1221](...matplotlib/artist.py:1221)         "{prop_name!r}")

File [~/software/miniconda3/envs/2024_01_COPD_analysis/lib/python3.10/site-packages/matplotlib/artist.py:1193](...matplotlib/artist.py:1193), in Artist._update_props(self, props, errfmt)
   [1191](...matplotlib/artist.py:1191)             func = getattr(self, f"set_{k}", None)
   [1192](...matplotlib/artist.py:1192)             if not callable(func):
-> [1193](...matplotlib/artist.py:1193)                 raise AttributeError(
   [1194](...matplotlib/artist.py:1194)                     errfmt.format(cls=type(self), prop_name=k))
   [1195](...matplotlib/artist.py:1195)             ret.append(func(v))
   [1196](...matplotlib/artist.py:1196) if ret:

AttributeError: PolyCollection.set() got an unexpected keyword argument 'size'

Version information


adjustText 1.0.4 anndata 0.10.5.post1 matplotlib 3.8.3 mudata 0.2.3 numpy 1.26.4 pandas 2.2.0 pertpy 0.6.0 scanpy 1.10.0.dev217+g1ac74a78 scipy 1.12.0 seaborn 0.13.2 session_info 1.0.0

PIL 10.2.0 absl NA arviz 0.17.0 asciitree NA asttokens NA attr 23.2.0 bokeh 3.3.4 brotli 1.1.0 certifi 2024.02.02 cffi 1.16.0 chardet 5.2.0 charset_normalizer 3.3.2 chex 0.1.85 cloudpickle 3.0.0 colorama 0.4.6 comm 0.2.1 contextlib2 NA cycler 0.12.1 cython_runtime NA cytoolz 0.12.3 dask 2024.2.0 dateutil 2.8.2 debugpy 1.8.1 decorator 5.1.1 decoupler 1.6.0 docrep 0.3.2 etils 1.7.0 exceptiongroup 1.2.0 executing 2.0.1 flax 0.8.1 fsspec 2024.2.0 gmpy2 2.1.2 h5py 3.10.0 idna 3.6 igraph 0.11.4 importlib_metadata NA importlib_resources NA ipykernel 6.29.2 ipywidgets 8.1.2 jax 0.4.19 jaxlib 0.4.19.dev20231025 jaxopt NA jedi 0.19.1 jinja2 3.1.3 joblib 1.3.2 kiwisolver 1.4.5 legacy_api_wrap NA leidenalg 0.10.2 lightning 2.2.0.post0 lightning_fabric 2.1.3 lightning_utilities 0.10.1 llvmlite 0.42.0 lz4 4.3.3 markupsafe 2.1.5 matplotlib_inline 0.1.6 mizani 0.11.0 ml_collections NA ml_dtypes 0.3.2 mpl_toolkits NA mpmath 1.3.0 msgpack 1.0.7 multipledispatch 0.6.0 natsort 8.4.0 numba 0.59.0 numcodecs 0.12.1 numpyro 0.13.2 opt_einsum v3.3.0 optax 0.1.9 ott 0.4.5 packaging 23.2 parso 0.8.3 patsy 0.5.6 pickleshare 0.7.5 pkg_resources NA platformdirs 3.11.0 plotnine 0.13.0 ply 3.11 prompt_toolkit 3.0.42 psutil 5.9.8 pure_eval 0.2.2 pyarrow 13.0.0 pycparser 2.21 pydev_ipython NA pydevconsole NA pydevd 2.9.5 pydevd_file_utils NA pydevd_plugins NA pydevd_tracing NA pygments 2.17.2 pynndescent 0.5.11 pyomo 6.7.0 pyparsing 3.1.1 pyro 1.9.0 pytorch_lightning 2.1.3 pytz 2024.1 requests 2.31.0 rich NA rpy2 3.5.15 scvi 1.1.1 six 1.16.0 sklearn 1.4.1.post1 skmisc 0.3.1 socks 1.7.1 sparse 0.15.1 sparsecca 0.3.1 stack_data 0.6.2 statsmodels 0.14.1 sympy 1.12 tblib 3.0.0 texttable 1.7.0 threadpoolctl 3.3.0 tlz 0.12.3 toolz 0.12.1 torch 2.2.0 torchaudio 2.2.0 torchgen NA torchmetrics 1.2.1 torchvision 0.17.0 torio NA tornado 6.3.3 tqdm 4.66.2 traitlets 5.14.1 typing_extensions NA tzdata 2024.1 tzlocal NA umap 0.5.5 urllib3 2.2.1 wcwidth 0.2.13 xarray 2024.2.0 xarray_einstats 0.7.0 xyzservices 2023.10.1 yaml 6.0.1 zarr 2.17.0 zipp NA zmq 25.1.2 zoneinfo NA

IPython 8.21.0 jupyter_client 8.6.0 jupyter_core 5.7.1

Python 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) [GCC 12.3.0] Linux-4.18.0-513.11.1.el8_9.x86_64-x86_64-with-glibc2.28

Session information updated at 2024-02-21 18:10