Comments (9)
@flying-sheep I believe we have this behavior since we were early adopters of categorical values, and pandas threw a ton of errors if you passed categorical arrays where there was no instance of a particular category. However, pandas also did not remove categories when subsetting, nor did it make doing that easy.
I think the situation is much improved, and we should figure out how to deprecate this behavior.
from anndata.
I also had a related problem in the past. When initializing colors with scanpy for a categorical column, the colors were different after subsetting the AnnData object since some categories resulted being dropped.
The colors saved in the anndata should be updated so they don't change when unused categoricals are removed.
However, we should probably just save the mapping of categories to colors as a dict, and not need to bother keeping these things synced.
from anndata.
This has previously been requested and tracked in #890, so I'm going to close this as duplicated
from anndata.
Asking also @giovp since when plotting categorical values in Squidpy could have had to deal with this behavior.
from anndata.
Please notice that when subsetting a DataFrame
all the categories are kept, not just the ones appearing in the subset data.
from anndata.
Created a simple function to fix this:
def _inplace_fix_subset_categorical_obs(subset_adata: AnnData, original_adata: AnnData) -> None:
"""
Fix categorical obs columns of subset_adata to match the categories of original_adata.
Parameters
----------
subset_adata
The subset AnnData object
original_adata
The original AnnData object
Notes
-----
See discussion here: https://github.com/scverse/anndata/issues/997
"""
obs = subset_adata.obs
for column in obs.columns:
is_categorical = pd.api.types.is_categorical_dtype(obs[column])
if is_categorical:
c = obs[column].cat.set_categories(original_adata.obs[column].cat.categories)
obs[column] = c
from anndata.
hat's the purpose of this? cause if it is for the colormap saved in adata.uns[categ_colors]
then that is also updated.
from anndata.
I use it in the spatialdata
code for aggregate()
when aggregating a categorical column: when performing aggregation on subsets of rows I want to get as a result a matrix always with the same number of columns (one per category). But due to the behavior described in this issue, if I don't call the workaround function that I wrote above, the categories are subset and the output of aggregate()
are AnnData
objects with different columns.
I also had a related problem in the past. When initializing colors with scanpy
for a categorical column, the colors where different after subsetting the AnnData
object since some categories resulted being dropped.
from anndata.
This behavior is intentional, as anndata explicitly calls a method named _remove_unused_categories
. The question is why the behavior is there, as it happened in a commit that doesn’t have an associated PR with a rationale (8cabf9c)
anndata/anndata/_core/anndata.py
Lines 343 to 346 in e067515
from anndata.
Related Issues (20)
- Make checking var_names and obs_names for uniqueness optional HOT 4
- dask.array support with sparse chunks
- Drop python 3.8 support HOT 2
- Modifying `uns` on a view changes the parent object
- Lazy imports for optional dependencies HOT 2
- Assigning to X after initializing as NoneType HOT 2
- AnnData.__sizeof__ error on subsetted AnnData object
- AnnData.X should be allowed to be a SparseDataset
- Demo showing how to create a AnnData containing a dask array with sparse chunks HOT 1
- annData with CuPy HOT 2
- anncollection deletes var matrix. HOT 1
- Postpone dtype deprecation HOT 1
- AnnData raw is a reference when a copy is expected HOT 8
- Errors caused by X when subsetting AnnData object HOT 4
- Can't implicitly convert non-string objects to strings. Bug about tl.filter_rank_genes_groups and tl.rank_genes_groups HOT 2
- Init AnnData spend too much memery,What can I do to reduce memory usage? HOT 4
- Error while writing annData HOT 4
- Fix `ConvertType` type annotation
- can't write missing values in obsm and varm to h5ad HOT 2
- Subsetting w/ empty list when X is dense throws error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anndata.