GeezerVison is showing github.com — Fix undefined vector angles and preserve planar dihedrals by Oblivionis028 · Pull Request #5352 · biopython/biopython
Conversation
-
I hereby agree to dual licence this and any previous contributions under both
the Biopython License Agreement AND the BSD 3-Clause License.
-
I have read the CONTRIBUTING.rst file, have run pre-commit
locally, and understand that continuous integration checks will be used to
confirm the Biopython unit tests and style checks pass with these changes.
-
I have added my name to the alphabetical contributors listings in the files
NEWS.rst and CONTRIB.rst as part of this pull request, am listed
already, or do not wish to be listed. (This acknowledgement is optional.)
Closes #5342
Zero-length vectors currently produce NaN when calculating the cosine in Vector.angle,
but the roundoff clamp converts that NaN into -1. As a result, four collinear
points return a finite dihedral of -pi, although the angle is undefined.
This change raises ValueError for zero-length vector angles and uses a
NaN-preserving clamp for roundoff. It also uses the scalar triple product to
determine the dihedral sign, so valid planar dihedrals do not require an angle
with a zero intermediate vector. The 180-degree endpoint is returned as +pi,
consistent with the documented interval. vector_to_axis uses a dot-product
projection so projecting the origin remains valid.
Seven new test methods cover zero vectors in either argument, NaN propagation,
roundoff, coincident points, undefined planes, signed and planar dihedrals, and
projection of the origin. The API docstrings and release notes document the
new exception behavior.
Behavior to review
This proposes ValueError for undefined zero-length angles rather than returning
NaN with a warning. calc_angle and calc_dihedral inherit that behavior.
Callers that previously accepted a finite result for degenerate geometry may now
need to handle the exception. Feedback on this API choice is welcome.
Validation
- Python 3.12.14, NumPy 2.3.5, Windows 11.
- 31 focused vector, polypeptide and exposure tests passed. The added regressions
fail against unmodified Biopython 1.88.
- The offline Bio.PDB run passed 26 module groups and skipped three:
test_PDB_alphafold_db, test_PDB_PDBList (offline), and
test_PDB_PSEA (external executable unavailable).
- 1,000 seeded nondegenerate dihedrals matched an independent
atan2 calculation
to a maximum absolute difference of approximately 5.1e-14 radians.
- All applicable configured pre-commit checks passed for the three changed files.
- The offline full runner completed and reported 502 test entries, with two
failing module groups: test_Align_aligncore and
test_align_substitution_matrices. The failing checks also reproduce against
the unmodified 1.88 wheel.
The local environment lacks a source-build C toolchain. Validation used the
current Python source with the official 1.88 wheel's extension binaries in a
separate staging directory. The two failing groups exercise newer C-extension
behavior that those binaries do not provide. This is not a clean source-build
pass; source-built CI validation is still needed. Online and unavailable
external-tool tests were skipped.
This contribution was prepared with AI assistance; the implementation, regression
results, and validation limitations are documented for review.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Fix undefined vector angles and preserve planar dihedrals
|
Thank you for declaring the AI usage, #5334 would add that to the PR template. I'm going to leave this on hold pending agreement on our AI policy, see the mailing list or eg #5241.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thank you for your feedback and for considering my contribution. I understand the decision to put this PR on hold while the AI policy is being discussed. I’ll continue to follow the discussion and look forward to contributing to Biopython in the future.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
dbolser
pushed a commit
to BioAGI-org/biop-ai
that referenced
this pull request
Nov 10, 2026
With calc_angle and calc_dihedral now raising ValueError for an undefined
angle (biopython/biopython#5352), one degenerate window, such as two
C-alpha atoms on the same spot, made get_tau_list and get_theta_list
raise and lose the whole list. Put None in that window instead, as
get_phi_psi_list already does and as is done for a residue without CA,
so the lists keep their lengths and positions.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
dbolser
pushed a commit
to BioAGI-org/biop-ai
that referenced
this pull request
Nov 10, 2026
Vector.angle now raises ValueError for a zero-length vector, where it
used to return pi (biopython/biopython#5352). In the half-sphere
exposure classes:
- a residue whose CA-CB (or pseudo-CB) direction has zero length is left
out, as for missing atoms; before, every neighbour came out as "down";
- a neighbouring CA exactly on the residue's own CA is counted in neither
half sphere; before, it was counted as "down";
- HSExposureCA gives None for the CB to pseudo-CB angle when the CB sits
on the CA, as it does when there is no CB.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewers
JoaoRodrigues
Awaiting requested review from JoaoRodrigues
JoaoRodrigues is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|