forked from scverse/anndata
-
Notifications
You must be signed in to change notification settings - Fork 0
adata.extensions module #4
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
Open
katosh
wants to merge
4
commits into
html_rep
Choose a base branch
from
extensions_register
base: html_rep
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """ | ||
| Public API for extending AnnData functionality. | ||
|
|
||
| This module provides registration mechanisms for: | ||
|
|
||
| 1. **Accessors** - Add custom namespaces to AnnData objects (e.g., `adata.myns.method()`) | ||
| 2. **HTML Formatters** - Customize how types are displayed in Jupyter notebooks | ||
|
|
||
| Examples | ||
| -------- | ||
| Register a custom accessor namespace:: | ||
|
|
||
| import anndata as ad | ||
| from anndata.extensions import register_anndata_namespace | ||
|
|
||
| @register_anndata_namespace("transform") | ||
| class TransformAccessor: | ||
| def __init__(self, adata: ad.AnnData): | ||
| self._adata = adata | ||
|
|
||
| def log1p(self): | ||
| import numpy as np | ||
| self._adata.X = np.log1p(self._adata.X) | ||
| return self._adata | ||
|
|
||
| # Usage: adata.transform.log1p() | ||
|
|
||
| Register a custom HTML formatter for a type:: | ||
|
|
||
| from anndata.extensions import register_formatter, TypeFormatter, FormattedOutput | ||
|
|
||
| @register_formatter | ||
| class MyArrayFormatter(TypeFormatter): | ||
| priority = 100 # Higher = checked first | ||
|
|
||
| def can_format(self, obj): | ||
| return isinstance(obj, MyArrayType) | ||
|
|
||
| def format(self, obj, context): | ||
| return FormattedOutput( | ||
| type_name=f"MyArray {obj.shape}", | ||
| css_class="dtype-custom", | ||
| ) | ||
|
|
||
| Register a custom section formatter (for packages like TreeData, SpatialData):: | ||
|
|
||
| from anndata.extensions import register_formatter, SectionFormatter | ||
| from anndata.extensions import FormattedEntry, FormattedOutput | ||
|
|
||
| @register_formatter | ||
| class ObstSectionFormatter(SectionFormatter): | ||
| section_name = "obst" | ||
| after_section = "obsm" # Position in display order | ||
|
|
||
| def should_show(self, obj): | ||
| return hasattr(obj, "obst") and len(obj.obst) > 0 | ||
|
|
||
| def get_entries(self, obj, context): | ||
| return [ | ||
| FormattedEntry( | ||
| key=k, | ||
| output=FormattedOutput(type_name=f"Tree ({v.n_nodes} nodes)"), | ||
| ) | ||
| for k, v in obj.obst.items() | ||
| ] | ||
|
|
||
| See Also | ||
| -------- | ||
| anndata._repr : Full documentation of the HTML representation system | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| # Accessor registration (from PR #1870) | ||
| from anndata._core.extensions import register_anndata_namespace | ||
|
|
||
| # HTML representation formatters | ||
| from anndata._repr import ( | ||
| # Core formatter classes | ||
| FormattedEntry, | ||
| FormattedOutput, | ||
| FormatterContext, | ||
| FormatterRegistry, | ||
| SectionFormatter, | ||
| TypeFormatter, | ||
| # Registration function | ||
| register_formatter, | ||
| # Global registry instance | ||
| formatter_registry, | ||
| # Type hint utilities for tagged data | ||
| UNS_TYPE_HINT_KEY, | ||
| extract_uns_type_hint, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| # Accessor registration | ||
| "register_anndata_namespace", | ||
| # HTML formatter registration | ||
| "register_formatter", | ||
| "TypeFormatter", | ||
| "SectionFormatter", | ||
| "FormattedOutput", | ||
| "FormattedEntry", | ||
| "FormatterContext", | ||
| "FormatterRegistry", | ||
| "formatter_registry", | ||
| # Type hint utilities | ||
| "extract_uns_type_hint", | ||
| "UNS_TYPE_HINT_KEY", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix Ruff RUF022 on
__all__(either sort, or explicitly ignore).Given other modules intentionally group exports by category, adding the same
# noqa: RUF022pattern here seems simplest.📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.14.8)
95-110:
__all__is not sortedApply an isort-style sorting to
__all__(RUF022)
🤖 Prompt for AI Agents