Giter VIP home page Giter VIP logo

Comments (9)

ivirshup avatar ivirshup commented on August 17, 2024 1

@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.

ivirshup avatar ivirshup commented on August 17, 2024 1

@LucaMarconato

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.

ivirshup avatar ivirshup commented on August 17, 2024 1

This has previously been requested and tracked in #890, so I'm going to close this as duplicated

from anndata.

LucaMarconato avatar LucaMarconato commented on August 17, 2024

Asking also @giovp since when plotting categorical values in Squidpy could have had to deal with this behavior.

from anndata.

LucaMarconato avatar LucaMarconato commented on August 17, 2024

Please notice that when subsetting a DataFrame all the categories are kept, not just the ones appearing in the subset data.

from anndata.

LucaMarconato avatar LucaMarconato commented on August 17, 2024

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.

giovp avatar giovp commented on August 17, 2024

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.

LucaMarconato avatar LucaMarconato commented on August 17, 2024

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.

flying-sheep avatar flying-sheep commented on August 17, 2024

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)

# fix categories
uns = copy(adata_ref._uns)
self._remove_unused_categories(adata_ref.obs, obs_sub, uns)
self._remove_unused_categories(adata_ref.var, var_sub, uns)

from anndata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.