Repository navigation
Conversation
Indexing an `AtomArray` with a two-dimensional index array either created an object whose coordinates and annotations were two-dimensional, while `array_length()` reported the size of the first dimension only, or raised an unhelpful `TypeError` from the Rust `BondList` binding if bonds were present. Both cases are now rejected with the same `IndexError` that a multidimensional tuple index already raised. `_subarray()` is already annotated as taking a one-dimensional index, so this enforces the documented contract rather than changing it. Nested lists are rejected alongside `ndarray`s, while ragged nested sequences are left to NumPy so that its own error is not masked. One-dimensional indexing is unaffected. The docstring examples of `base_pairs()` and `base_stacking()`, as well as six call sites in the test suite, relied on the previous behaviour. They now index the annotation array directly, which yields identical output. Fixes biotite-dev#1109 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1109.
Problem
Indexing an
AtomArraywith a two-dimensional integer index array behaved in two different broken ways, neither matching the clearIndexErrorraised for a multidimensional tuple index:AtomArraywithcoord.shape == (2, 2, 3)and two-dimensional annotations, whilearray_length()reported2TypeError: 'ndarray' object is not an instance of 'ndarray'(from the RustBondListbinding)atoms[0, 1]IndexError: 'AtomArray' does not accept multidimensional indicesThe first case seems the more harmful one, since it silently produces an object that violates the
AtomArrayinvariants instead of failing.Change
_subarray()is already annotated as taking a one-dimensional index (NDArray1[...] | slice), so this enforces the documented contract rather than changing it. A_check_index_dimensionality()helper raises the sameIndexErroras the tuple branch, called from:_AtomArrayBase._subarray()— the shared funnel for the atom dimension, coveringAtomArray.__getitem__()and the atom dimension ofAtomArrayStack.__getitem__()AtomArrayStack.__getitem__()Nested lists are rejected alongside
ndarrays. Ragged nested sequences are left to NumPy so its own error is not masked. One-dimensional indexing — integer arrays, boolean masks, slices, lists, and the(model, atom)tuple form — is unchanged.The two
__getitem__docstrings claiming "All index types NumPy accepts, are valid" are corrected.This changes documented output
As you noted in the issue, the docstring examples of
base_pairs()andbase_stacking()relied on the invalid behaviour. They now index the annotation array directly, which gives identical output:Six call sites in the test suite used the same idiom and are updated the same way, in
test_basepairs.pyandtest_pseudoknots.py.Worth a look during review: that idiom reads quite naturally, so if you would rather support two-dimensional index arrays properly — returning correctly shaped annotations — than reject them, then this PR takes the wrong approach and I am happy to redo it that way.
Testing
pytest tests/structure— 4303 passed, 248 skipped, 0 failed (4298 passed onmain; the five new tests account for the difference)pytest tests --ignore=tests/interface— 11127 passed, 1 failed. That failure istest_pubchem.py::test_search_super_and_substructure, a live PubChem API test which also fails on an unmodified checkout.tests/interfaceis skipped locally becauseopenmm,rdkitandpymolare not installed.ruff checkandruff format --checkclean on all five changed filesNew tests cover a 2-D
ndarraywith and without bonds, nested lists, bothAtomArrayStackdimensions, and a regression guard that ordinary one-dimensional indexing still works.🤖 Generated with Claude Code