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

Accessors for AnnData #1869

Open
srivarra opened this issue Feb 21, 2025 · 1 comment · May be fixed by #1870
Open

Accessors for AnnData #1869

srivarra opened this issue Feb 21, 2025 · 1 comment · May be fixed by #1870
Assignees
Milestone

Comments

@srivarra
Copy link

srivarra commented Feb 21, 2025

Please describe your wishes and possible alternatives to achieve the desired result.

Hi,

I would like to propose adding built-in support for custom accessors in AnnData. This feature would allow users to register and use dedicated namespaces on AnnData objects without the need for subclassing or mixins, similar to the mechanisms available in Polars and Xarray.

Motivation

Custom accessors provide a clean and modular way to extend the functionality of a core class. For example, in both Polars and Xarray, users can define custom functionality and then access it through a dedicated namespace (e.g., df.my_accessor.my_method()).

This enables:

  • Enhanced Modularity: Custom functionality can be isolated in separate modules.
  • Ease of Use: Users extend AnnData objects with additional methods without modifying or subclassing the core class.
  • Extensibility: Third-party developers can integrate domain-specific functionality that naturally augments the core AnnData behavior.

Example

import anndata as ad


@ad.extensions.register_anndata_namespace("transforms")
class Transforms:
    def __init__(self, adata: ad.AnnData):
        self._adata = adata

    def arcsinh(
        self, shift: float, scale: float, layer: str, inplace: bool = False
    ) -> ad.AnnData | None:
        """Apply an arcsinh transformation to the X matrix."""
        import numpy as np

        # Example transformation logic
        transformed = np.arcsinh(self._adata.X / scale) + shift
        return None if inplace else self._adata

and you'd use it like so:

adata = ad.AnnData(X=...)
adata.transforms.arcsinh(shift=1, scale=1, layer='arcsinh', inplace=True)

Existing examples out in the scverse

Similar accessor functionality has been implemented in other scverse projects:

@srivarra srivarra linked a pull request Feb 21, 2025 that will close this issue
3 tasks
@ilan-gold ilan-gold self-assigned this Feb 24, 2025
@ilan-gold ilan-gold added this to the 0.12.0 milestone Feb 24, 2025
@flying-sheep
Copy link
Member

flying-sheep commented Feb 24, 2025

And of course #342

I didn’t finish that, but you should definitely check it out @ilan-gold

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

Successfully merging a pull request may close this issue.

3 participants