Skip to content

Reject multidimensional index arrays in AtomArray and AtomArrayStack - #1113

Open
Aarav1357 wants to merge 1 commit into
biotite-dev:mainfrom
Aarav1357:fix-multidim-index-atomarray
Open

Aarav1357 wants to merge 1 commit into
biotite-dev:mainfrom
Aarav1357:fix-multidim-index-atomarray

Conversation

@Aarav1357

Copy link
Copy Markdown

Fixes #1109.

Problem

Indexing an AtomArray with a two-dimensional integer index array behaved in two different broken ways, neither matching the clear IndexError raised for a multidimensional tuple index:

index before
2-D array, no bonds returned an AtomArray with coord.shape == (2, 2, 3) and two-dimensional annotations, while array_length() reported 2
2-D array, with bonds TypeError: 'ndarray' object is not an instance of 'ndarray' (from the Rust BondList binding)
tuple, e.g. atoms[0, 1] IndexError: 'AtomArray' does not accept multidimensional indices

The first case seems the more harmful one, since it silently produces an object that violates the AtomArray invariants 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 same IndexError as the tuple branch, called from:

  • _AtomArrayBase._subarray() — the shared funnel for the atom dimension, covering AtomArray.__getitem__() and the atom dimension of AtomArrayStack.__getitem__()
  • the model-dimension branch of 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() and base_stacking() relied on the invalid behaviour. They now index the annotation array directly, which gives identical output:

# before
print(dna_helix[basepairs].res_name)
# after
print(dna_helix.res_name[basepairs])

Six call sites in the test suite used the same idiom and are updated the same way, in test_basepairs.py and test_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 on main; the five new tests account for the difference)
  • pytest tests --ignore=tests/interface — 11127 passed, 1 failed. That failure is test_pubchem.py::test_search_super_and_substructure, a live PubChem API test which also fails on an unmodified checkout. tests/interface is skipped locally because openmm, rdkit and pymol are not installed.
  • ruff check and ruff format --check clean on all five changed files
  • doctests pass

New tests cover a 2-D ndarray with and without bonds, nested lists, both AtomArrayStack dimensions, and a regression guard that ordinary one-dimensional indexing still works.


🤖 Generated with Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AtomArray.__getitem__(): two-dimensional index array gives invalid AtomArray or TypeError

1 participant