Repository navigation
nanobind: fix GetNeighbors()/GetBonds() indexing - #9658
Merged
Merged
Conversation
AtomSeqHolder::operator[] and BondSeqHolder::operator[] returned mol->getAtomWithIdx(idx) / mol->getBondWithIdx(idx), i.e. the molecule's i-th atom/bond instead of the i-th element of the held iterator. For the whole-molecule sequences (Mol.GetAtoms()/GetBonds()) the two are coincidentally identical, but for Atom.GetNeighbors()/GetBonds() the indexing silently returned wrong atoms/bonds (iteration was correct). Index the sequence's own iterator instead. std::advance resolves to O(1) for the random-access atom iterators and a linear walk for the bidirectional bond iterators. Also support negative indices, matching the boost.python tuple semantics and the convention used by the MolSupplier __getitem__ implementations.
Contributor
|
Member
|
Thanks for the contribution @barlowa124 |
greglandrum
pushed a commit
that referenced
this pull request
Nov 5, 2026
AtomSeqHolder::operator[] and BondSeqHolder::operator[] returned mol->getAtomWithIdx(idx) / mol->getBondWithIdx(idx), i.e. the molecule's i-th atom/bond instead of the i-th element of the held iterator. For the whole-molecule sequences (Mol.GetAtoms()/GetBonds()) the two are coincidentally identical, but for Atom.GetNeighbors()/GetBonds() the indexing silently returned wrong atoms/bonds (iteration was correct). Index the sequence's own iterator instead. std::advance resolves to O(1) for the random-access atom iterators and a linear walk for the bidirectional bond iterators. Also support negative indices, matching the boost.python tuple semantics and the convention used by the MolSupplier __getitem__ implementations. Co-authored-by: barlowa124 <[email protected]>
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.
Reference Issue
Fixes #9645
What does this implement/fix? Explain your changes.
Under the nanobind wrappers,
AtomSeqHolder::operator[]andBondSeqHolder::operator[]inCode/GraphMol/nbWrap/seqholders.hppresolved an index withmol->getAtomWithIdx(idx)/mol->getBondWithIdx(idx). For a sub-range sequence this returns the molecule's i-th atom or bond when the caller asked for the i-th element of the sequence. Iteration was correct, soseq[i]and the i-th iterated element disagreed.Only
Atom.GetNeighbors()andAtom.GetBonds()produced visibly wrong results. OnMol.GetAtoms()andMol.GetBonds()the molecule-global index coincidentally matches the sequence index.operator[]now bounds-checksidxagainstiter.size()and dereferences the sequence's own iterator viastd::advance. This resolves to a single+=for the random-access atom iterators and a linear walk for the bidirectional bond iterators. Negative indices are wrapped before the bounds check, matching the tuple semantics of the boost.python wrappers and the convention used by theMolSupplier__getitem__implementations.rough_test.py::test15Neighborsnow asserts indexed access matches iteration for both neighbors and bonds. It also covers negative-index wrapping and out-of-rangeIndexError.Any other comments?
Checked on a local nanobind build (
2026.09.1b1 nanobind, py3.12, macOS arm64,RDK_BUILD_NANOBIND_WRAPPERS=ONwith boost serialization off). WithCCNO,GetNeighbors()on atom 2 yields the same1, 3whether iterated or indexed, andGetBonds()yields1, 2both ways.Mol.GetAtoms()/GetBonds()indexing is unchanged.QueryAtomIterSeqandConformerIterSeqwere checked and already index their own ranges correctly.