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

Conversation

ilan-gold
Copy link
Contributor

@ilan-gold ilan-gold commented Nov 8, 2024

@ilan-gold ilan-gold changed the title (fix): raise error on non-integer floating types (fix): raise error on non-integer floating types in iterables Nov 8, 2024
@ilan-gold ilan-gold added this to the 0.11.1 milestone Nov 8, 2024
@ilan-gold ilan-gold marked this pull request as ready for review November 8, 2024 10:31
Copy link

codecov bot commented Nov 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.53%. Comparing base (6ae9de8) to head (150d8aa).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1746      +/-   ##
==========================================
- Coverage   86.99%   84.53%   -2.47%     
==========================================
  Files          40       40              
  Lines        6053     6057       +4     
==========================================
- Hits         5266     5120     -146     
- Misses        787      937     +150     
Files with missing lines Coverage Δ
src/anndata/_core/index.py 95.09% <100.00%> (+0.12%) ⬆️

... and 11 files with indirect coverage changes

@@ -82,6 +82,10 @@ def name_idx(i):
indexer = np.array(indexer)
if len(indexer) == 0:
indexer = indexer.astype(int)
if isinstance(indexer, np.ndarray) and indexer.dtype == float:
Copy link
Member

Choose a reason for hiding this comment

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

Please fix the comparison so it doesn’t only work with float64:

>>> np.dtype(np.float64) == float
True
>>> np.dtype(np.float32) == float
False

(and parametrize the tests please)

Comment on lines +817 to +830
@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]
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.

@ilan-gold ilan-gold modified the milestones: 0.11.1, 0.11.2 Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected Behavior: AnnData Allows Indexing with Float Arrays Without Error
2 participants