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

(feat): support ellipsis indexing #1729

Merged
merged 25 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d2b859d
(feat): support ellipsis indexing
ilan-gold Oct 22, 2024
df109f6
(chore): release note
ilan-gold Oct 22, 2024
db86a20
(chore): add tests for all axis combinations
ilan-gold Oct 22, 2024
5822ed5
(fix): index typing
ilan-gold Oct 22, 2024
2a6c837
(feat): add support for 3d ellipsis/error on >1 ellipsis
ilan-gold Oct 24, 2024
87a2dbf
Merge branch 'main' into ig/ellipsis_indexing
ilan-gold Oct 24, 2024
c69aa7e
(fix): `unpack_index` type
ilan-gold Oct 24, 2024
eb36543
Merge branch 'ig/ellipsis_indexing' of github.com:scverse/anndata int…
ilan-gold Oct 24, 2024
174596a
(fix): ignore ellipsis in docs
ilan-gold Oct 24, 2024
f018328
(fix): import
ilan-gold Oct 24, 2024
f580cad
Update docs/conf.py
ilan-gold Oct 25, 2024
e994076
(chore): `is` instead of `isinstance` checks
ilan-gold Oct 25, 2024
7cfbb57
Merge branch 'ig/ellipsis_indexing' of github.com:scverse/anndata int…
ilan-gold Oct 25, 2024
f0b7e7e
(fix): try making `unpack_index` more general-purpose
ilan-gold Oct 25, 2024
a06675a
Apply suggestions from code review
ilan-gold Oct 25, 2024
ff5eb93
fix test_backed_ellipsis_indexing
flying-sheep Oct 25, 2024
52ea6d3
(refactor): simplify 3-elem index handling
ilan-gold Oct 25, 2024
95d9d37
(refactor): simplify tests
ilan-gold Oct 25, 2024
57c479c
merge
ilan-gold Oct 25, 2024
defbf37
(fix): use correct base-comparison
ilan-gold Oct 25, 2024
8a08e2b
Update src/anndata/_core/index.py
ilan-gold Oct 25, 2024
916cd3d
(fix): use `id` per param
ilan-gold Oct 25, 2024
8edffaf
Merge branch 'ig/ellipsis_indexing' of github.com:scverse/anndata int…
ilan-gold Oct 25, 2024
6aff0f2
(chore): split ellipsis index
ilan-gold Oct 25, 2024
37fd1e0
fix types
flying-sheep Oct 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/1729.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for ellipsis indexing of the {class}`~anndata.AnnData` object {user}`ilan-gold`
9 changes: 6 additions & 3 deletions src/anndata/_core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Iterable, Sequence
from functools import singledispatch
from itertools import repeat
from types import EllipsisType
from typing import TYPE_CHECKING

import h5py
Expand Down Expand Up @@ -47,7 +48,8 @@
| str
| Sequence[bool | int | np.integer]
| np.ndarray
| pd.Index,
| pd.Index
| EllipsisType,
index: pd.Index,
) -> slice | int | np.ndarray: # ndarray of int or bool
if not isinstance(index, pd.RangeIndex):
Expand All @@ -72,6 +74,8 @@
return slice(start, stop, step)
elif isinstance(indexer, np.integer | int):
return indexer
elif isinstance(indexer, EllipsisType):
ilan-gold marked this conversation as resolved.
Show resolved Hide resolved
return slice(None)
elif isinstance(indexer, str):
return index.get_loc(indexer) # int
elif isinstance(
Expand Down Expand Up @@ -107,8 +111,7 @@
"are not valid obs/ var names or indices."
)
return positions # np.ndarray[int]
else:
raise IndexError(f"Unknown indexer {indexer!r} of type {type(indexer)}")
raise IndexError(f"Unknown indexer {indexer!r} of type {type(indexer)}")

Check warning on line 114 in src/anndata/_core/index.py

View check run for this annotation

Codecov / codecov/patch

src/anndata/_core/index.py#L114

Added line #L114 was not covered by tests


def _fix_slice_bounds(s: slice, length: int) -> slice:
Expand Down
3 changes: 2 additions & 1 deletion src/anndata/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from importlib.util import find_spec
from inspect import Parameter, signature
from pathlib import Path
from types import EllipsisType
from typing import TYPE_CHECKING, TypeVar
from warnings import warn

Expand Down Expand Up @@ -46,7 +47,7 @@ class Empty:
pass


Index1D = slice | int | str | np.int64 | np.ndarray
Index1D = slice | int | str | np.int64 | np.ndarray | EllipsisType
Index = Index1D | tuple[Index1D, Index1D] | scipy.sparse.spmatrix | SpArray
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
H5Group = h5py.Group
H5Array = h5py.Dataset
Expand Down
8 changes: 8 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,14 @@ def test_dataframe_view_index_setting():
assert a2.obs.index.values.tolist() == ["a", "b"]


def test_ellipsis_index(adata, subset_func, matrix_type):
ilan-gold marked this conversation as resolved.
Show resolved Hide resolved
adata = gen_adata((10, 10), X_type=matrix_type, **GEN_ADATA_DASK_ARGS)
subset_obs_names = subset_func(adata.obs_names)
subset_ellipsis = adata[subset_obs_names, ...]
subset = adata[subset_obs_names, :]
assert_equal(subset_ellipsis, subset)


# @pytest.mark.parametrize("dim", ["obs", "var"])
# @pytest.mark.parametrize(
# ("idx", "pat"),
Expand Down
Loading