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

(fix): raise error on non-integer floating types in iterables #1746

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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/1746.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error out on floating point indices that are not actually integers {user}`ilan-gold`
6 changes: 6 additions & 0 deletions src/anndata/_core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def name_idx(i):
indexer = np.array(indexer)
if len(indexer) == 0:
indexer = indexer.astype(int)
if isinstance(indexer, np.ndarray) and np.issubdtype(
indexer.dtype, np.floating
):
indexer_int = indexer.astype(int)
if np.all((indexer - indexer_int) != 0):
raise IndexError(f"Indexer {indexer!r} has floating point values.")
if issubclass(indexer.dtype.type, np.integer | np.floating):
return indexer # Might not work for range indexes
elif issubclass(indexer.dtype.type, np.bool_):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,22 @@ def test_index_3d_errors(index: tuple[int | EllipsisType, ...], expected_error:
gen_adata((10, 10))[index]


@pytest.mark.parametrize(
"index",
[
pytest.param(sparse.csr_matrix(np.random.random((1, 10))), id="sparse"),
pytest.param([1.2, 3.4], id="list"),
*(
pytest.param(np.array([1.2, 2.3], dtype=dtype), id=f"ndarray-{dtype}")
for dtype in [np.float32, np.float64]
),
],
)
def test_index_float_sequence_raises_error(index):
with pytest.raises(IndexError, match=r"has floating point values"):
gen_adata((10, 10))[index]
Comment on lines +817 to +830
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@pytest.mark.parametrize(
"index",
[
pytest.param(sparse.csr_matrix(np.random.random((1, 10))), id="sparse"),
pytest.param([1.2, 3.4], id="list"),
*(
pytest.param(np.array([1.2, 2.3], dtype=dtype), id=f"ndarray-{dtype}")
for dtype in [np.float32, np.float64]
),
],
)
def test_index_float_sequence_raises_error(index):
with pytest.raises(IndexError, match=r"has floating point values"):
gen_adata((10, 10))[index]
@pytest.mark.parametrize(
"index",
[
pytest.param(sparse.csr_matrix(np.random.random((1, 10))), id="sparse"),
pytest.param([1.2, 3.4], id="list"),
pytest.param(np.array([1.2, 2.3], dtype=dtype), id=f"ndarray-{dtype}")
],
)
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_index_float_sequence_raises_error(index, dtype):
index = index.astype(dtype)
with pytest.raises(IndexError, match=r"has floating point values"):
gen_adata((10, 10))[index]

Copy link
Contributor Author

@ilan-gold ilan-gold Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flying-sheep is the goal here to ensure dtype is checked on sparse matrices as well? I think it's enough to check the numpy arrays. Also we can't astype a list. Also dtype is not accessible outside of its parametrization/the test.



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