Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeping original categories when subsetting an AnnData object #997

Closed
LucaMarconato opened this issue Jun 5, 2023 · 9 comments
Closed

Comments

@LucaMarconato
Copy link
Member

If one subsets an AnnData object that has a categorical obs column, the new categories of the column will be only the one that appear in the subset column, an not all the original one.

Is this a bug or the expected behavior?
If it's not a bug, is there an helper function/standard way to keep all the original categories?

@LucaMarconato LucaMarconato changed the title Keeping original categories when subsetting an AnnData objects Keeping original categories when subsetting an AnnData object Jun 5, 2023
@LucaMarconato
Copy link
Member Author

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

@LucaMarconato
Copy link
Member Author

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

@LucaMarconato
Copy link
Member Author

LucaMarconato commented Jun 5, 2023

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

@giovp
Copy link
Member

giovp commented Jun 6, 2023

hat's the purpose of this? cause if it is for the colormap saved in adata.uns[categ_colors] then that is also updated.

@LucaMarconato
Copy link
Member Author

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.

@flying-sheep
Copy link
Member

flying-sheep commented Jun 6, 2023

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)

@ivirshup
Copy link
Member

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

@ivirshup
Copy link
Member

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

@ivirshup
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants