-
Notifications
You must be signed in to change notification settings - Fork 181
feat: accessors #2290
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
flying-sheep
wants to merge
61
commits into
main
Choose a base branch
from
pa/acc
base: main
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.
+2,001
−240
Open
feat: accessors #2290
Changes from all commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
4c7767c
WIP accessors
flying-sheep 5103b0d
fix: AdPath oversight
flying-sheep 6878fc3
chore: more API
flying-sheep c24206a
tests
flying-sheep 6ffcb29
fix equality
flying-sheep 05d0900
move parsing out
flying-sheep 61fc920
parse all
flying-sheep 809ad8d
tests
flying-sheep 95b35a6
Merge branch 'main' into pa/acc
flying-sheep b95b3b9
pattern matching
flying-sheep 63d0d87
simpler
flying-sheep 85a9a51
WIP docs
flying-sheep f83fc85
fix tests
flying-sheep 3943f19
warnings
flying-sheep e4e6c37
move types up
flying-sheep 8054045
adref
flying-sheep 1261d84
mostly fix docs
flying-sheep fa7f136
really fix docs
flying-sheep 0aa542c
fix test
flying-sheep 7c98c49
rename all the things
flying-sheep bf1d630
bit more explicit
flying-sheep f279003
Merge branch 'main' into pa/acc
flying-sheep f7ea522
WIP
flying-sheep 3652c2e
Merge branch 'main' into pa/acc
flying-sheep fdc3ed8
ext metadata
flying-sheep 4a8a636
works!
flying-sheep b967e4c
Merge branch 'main' into pa/acc
flying-sheep f47fd37
JSON parser
flying-sheep ff123d3
simplify
flying-sheep af846f8
test serialization
flying-sheep 588a6b6
add examples for all vector accessors
flying-sheep 30407a3
Merge branch 'main' into pa/acc
flying-sheep 7f7b0be
also allow pandas index
flying-sheep c7abcd5
Merge branch 'main' into pa/acc
flying-sheep 9dc9975
add schema
flying-sheep c804f70
schema in docs
flying-sheep c4799b0
feat: working __contains__
flying-sheep 2a7fc64
ax→dim
flying-sheep 8dc8d07
typeerror on `in` check
flying-sheep ad2e4a2
v1
flying-sheep d524c3c
deprecate obs_vector and var_vector
flying-sheep f1c0fe4
rebase {obs,var}_vector
flying-sheep 006fdb5
get tests
flying-sheep 170606e
Merge branch 'main' into pa/acc
flying-sheep 7d76636
dask
flying-sheep 28b7897
nomenclature
flying-sheep affb0dd
cupy
flying-sheep 77f4a2d
docs
flying-sheep 821762f
yq
flying-sheep 951bc9b
yq
flying-sheep 6dc5d13
fix tests
flying-sheep afdc446
docs: ref
flying-sheep f41f611
Merge branch 'main' into pa/acc
flying-sheep a53771a
array-api
flying-sheep e433ce8
Idx2D docs
flying-sheep 366034b
style
flying-sheep fc1e057
fix docs
flying-sheep 899126e
Merge branch 'main' into pa/acc
flying-sheep d8bae96
change getter API
flying-sheep e5493a5
more examples
flying-sheep 7aaa1cd
extension docs
flying-sheep 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,198 @@ | ||
| .. _accessors: | ||
|
|
||
| Accessors and paths | ||
| =================== | ||
|
|
||
| .. module:: anndata.acc | ||
|
|
||
| :mod:`!anndata.acc` creates references to 1D and 2D arrays in an :class:`~anndata.AnnData` object. | ||
| You can use these to drive e.g. plotting or validation code. | ||
| For these purposes, they are | ||
|
|
||
| #. easy to create: | ||
|
|
||
| The central :attr:`A` object allows you to create | ||
| :class:`AdRef` objects that reference arrays | ||
| along one or two dimensions of an :class:`~anndata.AnnData` object: | ||
|
|
||
| >>> from anndata.acc import A | ||
| >>> A[:, "gene-3"] # reference to `adata[:, "gene-3"].X` as 1D vector | ||
| A[:, 'gene-3'] | ||
| >>> type(A[:, "gene-3"]) | ||
| <class 'anndata.acc.AdRef'> | ||
|
|
||
| … and to use: | ||
|
|
||
| >>> import scanpy as sc | ||
| >>> adata = sc.datasets.pbmc3k_processed() | ||
|
|
||
| E.g. to check if `adata.varm["PCs"]` has at least 30 columns: | ||
|
|
||
| >>> A.varm["PCs"][:, 30] in adata | ||
| True | ||
|
|
||
| or to extract the referenced vector: | ||
|
|
||
| >>> ref = A.obs["louvain"] | ||
| >>> adata[ref].categories[:2] | ||
| Index(['CD4 T cells', 'CD14+ Monocytes'], dtype='str') | ||
|
|
||
| #. introspectible: | ||
|
|
||
| :class:`AdRef`\ s have the :attr:`AdRef.dims`, :attr:`AdRef.idx`, and :attr:`AdRef.acc` attributes, | ||
| allowing you to inspect all relevant properties. | ||
|
|
||
| >>> pc0 = A.obsm["pca"][:, 0] | ||
| >>> pc0 | ||
| A.obsm['pca'][:, 0] | ||
| >>> pc0.idx | ||
| 0 | ||
| >>> pc0.acc | ||
| A.obsm['pca'] | ||
| >>> A.var["symbol"].dims | ||
| {'var'} | ||
| >>> pc0.acc.k | ||
| 'pca' | ||
|
|
||
| #. convenient: | ||
|
|
||
| Want to reference multiple vectors from the same object? | ||
| Pass a list of indices to the vector accessor: | ||
|
|
||
| >>> A.obsp["connectivities"][:, ["cell0", "cell1"]] | ||
| [A.obsp['connectivities'][:, 'cell0'], A.obsp['connectivities'][:, 'cell1']] | ||
|
|
||
| #. extensible: see `extending accessors`_. | ||
|
|
||
| API | ||
| --- | ||
|
|
||
| The central starting point is :data:`!A`: | ||
|
|
||
| .. autodata:: A | ||
|
|
||
| See :class:`AdAcc` for examples of how to use it to create :attr:`AdRef`\ s. | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :template: class-minimal | ||
|
|
||
| AdRef | ||
|
|
||
| .. _reference accessors: | ||
|
|
||
| Reference accessors | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The following :class:`!RefAcc` subclasses can be accessed using :attr:`AdRef.acc`, | ||
| and are therefore useful in :ref:`matches <match>` or :func:`isinstance` checks: | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :template: class-minimal | ||
|
|
||
| RefAcc | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
|
|
||
| - - Class | ||
| - Attributes | ||
| - Examples | ||
| - - :class:`AdAcc` | ||
| - is a :class:`LayerAcc` with `.k=None` | ||
| - `A["c1", :]`, `A[:, "g1"]`, `A[:, :]` | ||
| - - :class:`LayerAcc` | ||
| - :attr:`LayerAcc.k` | ||
| - `A.layers["c"][:, "g0"]` | ||
| - - :class:`MetaAcc` | ||
| - :attr:`MetaAcc.dim` | ||
| - `A.obs["a"]`, `A.var["b"]` | ||
| - - :class:`MultiAcc` | ||
| - :attr:`MultiAcc.dim`, :attr:`MultiAcc.k` | ||
| - `A.obsm["d"][:, 2]` | ||
| - - :class:`GraphAcc` | ||
| - :attr:`GraphAcc.dim`, :attr:`GraphAcc.k` | ||
| - `A.obsp["e"][:, "c1"]`, `A.vbsp["e"]["g0", :]` | ||
|
|
||
| .. hidden | ||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :template: class-minimal | ||
|
|
||
| AdAcc | ||
| LayerAcc | ||
| MetaAcc | ||
| MultiAcc | ||
| GraphAcc | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
||
| Idx2D | ||
|
|
||
| .. toctree:: | ||
| :hidden: | ||
|
|
||
| generated/anndata.acc.AdAcc | ||
| generated/anndata.acc.LayerAcc | ||
| generated/anndata.acc.MetaAcc | ||
| generated/anndata.acc.MultiAcc | ||
| generated/anndata.acc.GraphAcc | ||
| generated/anndata.acc.Idx2D | ||
|
|
||
| Mapping accessors | ||
| ~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Finally, these classes are mostly useful for extending, | ||
| but might be useful for APIs that take a reference to a :class:`collections.abc.Mapping` | ||
| of arrays: | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :template: class-minimal | ||
|
|
||
| MapAcc | ||
| LayerMapAcc | ||
| MultiMapAcc | ||
| GraphMapAcc | ||
|
|
||
| .. _extending accessors: | ||
|
|
||
| Extending accessors | ||
| ------------------- | ||
|
|
||
| There are three layers of extensibility: | ||
|
|
||
| #. subclassing :class:`RefAcc` and creating a new :class:`AdRef` instance for creating them: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import my_plotting_library as pl | ||
|
|
||
| class AdDim(RefAcc, pl.Dimension): ... | ||
| A = AdAcc(ref_class=AdDim) | ||
|
|
||
| pl.scatter(adata, A[:, "Actb"], color=A.obs["cell_type"]) | ||
|
|
||
|
|
||
| #. subclass one or more of the `reference accessors`_, and create a new :class:`AdAcc` instance: | ||
|
|
||
| >>> from anndata.acc import AdAcc, AdRef, MetaAcc | ||
| >>> | ||
| >>> class TwoDRef(AdRef): | ||
| ... """A reference able to refer to multiple metadata columns.""" | ||
| ... ... | ||
| >>> | ||
| >>> class MyMetaAcc(MetaAcc): | ||
| ... def __getitem__(self, k): | ||
| ... if isinstance(k, list): | ||
| ... # override default behavior of returning a list of refs | ||
| ... return self.ref_class(self, k) | ||
| ... return super().__getitem__(k) | ||
| >>> | ||
| >>> A = AdAcc(ref_class=TwoDRef, meta_cls=MyMetaAcc) | ||
| >>> A.obs[["a", "b"]] | ||
| A.obs[['a', 'b']] | ||
|
|
||
| #. .. attention:: TODO | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.