Plotting categorical obs warns ImplicitModificationWarning
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.6k
- Forks
- 779
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 27
Description
- 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 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 creates a copy and warns:
ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.
self.data[key] = value
Issue
EDIT: colors are stored, but as I use a view, it creates a copy. My adata contains dozens of millions of cells, and thus making a copy creates severe memory and time issues. Is there an option not to store the colors on this view?
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal sc.pl.umap example with the categorical adata.obs["obs1"] and an AnnData view, then trace the warning from view initialization to modification of ._uns. Check how plotting colors are stored for categorical observations; done means plotting the view without creating a full copy while preserving the expected UMAP result and color behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100