scanpy
scanpy copied to clipboard
Plotting categorical obs warns ImplicitModificationWarning
- [x] I have checked that this issue has not already been reported.
- [x] I have confirmed this bug exists on the latest version of scanpy.
- [ ] (optional) I have confirmed this bug exists on the master branch of scanpy.
Minimal code sample
I'm trying to plot a categorical observation on a UMAP for a view of my adata. I created this minimal code to reproduce the behavior:
from anndata import AnnData
import numpy as np
import pandas as pd
import scanpy as sc
n_obs, n_vars = 1000, 2
adata = AnnData(X=np.random.randn(n_obs, n_vars), dtype=np.float32)
adata.obsm["X_umap"] = np.random.randn(n_obs, 2)
adata.obs["obs1"] = pd.Categorical([f"Group {np.random.randint(2)}" for _ in range(n_obs)])
sc.pl.umap(adata[adata.X[:, 0] > 0], color="obs1")
It plots the desired result, but also warns:
ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.
self.data[key] = value
There is no reason for scanpy to modify anything for the plot, so I think this warning shouldn't exist. Especially, why is it trying to modify ._uns?
Note that plotting some float values, e.g. sc.pl.umap(adata[adata.X[:, 0] > 0], color="0"), returns no warning.
Versions
anndata 0.8.0 scanpy 1.9.1
PIL 9.2.0 beta_ufunc NA binom_ufunc NA cycler 0.10.0 cython_runtime NA dateutil 2.8.2 h5py 3.7.0 hypergeom_ufunc NA joblib 1.1.0 kiwisolver 1.4.4 llvmlite 0.39.0 matplotlib 3.5.3 mpl_toolkits NA natsort 8.1.0 nbinom_ufunc NA ncf_ufunc NA numba 0.56.0 numpy 1.22.4 packaging 21.3 pandas 1.4.4 pkg_resources NA pyparsing 3.0.9 pytz 2022.2.1 scipy 1.9.1 session_info 1.0.0 setuptools 65.3.0 six 1.16.0 sklearn 1.1.2 threadpoolctl 3.1.0
Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 17:01:00) [Clang 13.0.1 ] macOS-12.5.1-arm64-arm-64bit
Hi Quentin,
When you plot a categorical variable for the first time, scanpy stores the colors for each category in adata.uns, that's why it is modifying your adata. For continuous variables (like your adata.X), it does not do that, hence there is no warning there.
Thanks, @LisaSikkema; I figured it out shortly after I sent my message.
For now, I only have these solutions, but I don't like any of them:
- Filtering this warning. This is not very clean: I would prefer to really solve the issue. Also, it makes a copy, which is memory expensive, as I sometimes have about
10^7observations. - Creating a copy so that I don't use a view. Same issue about the memory.
- Plotting this UMAP on the complete anndata object so that it stores the colors, and then plotting with the view I'm creating. The problem is that, for my use case, I don't want to plot it with the entire dataset, so it makes a useless plot.
I think I will go for the first solution, even though I'm not really satisfied.