Skip to content

Fix Numpy 2.4 compatibility in PDB/vectors.py#5138

Open
Augustin-Zidek wants to merge 4 commits intobiopython:masterfrom
Augustin-Zidek:patch-2
Open

Fix Numpy 2.4 compatibility in PDB/vectors.py#5138
Augustin-Zidek wants to merge 4 commits intobiopython:masterfrom
Augustin-Zidek:patch-2

Conversation

@Augustin-Zidek
Copy link

  • 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.)

This addresses the expired deprecation in Numpy 2.4: https://numpy.org/devdocs/release/2.4.0-notes.html#raise-typeerror-on-attempt-to-convert-array-with-ndim-0-to-scalar

Closes #5135

@codecov
Copy link

codecov bot commented Jan 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.41%. Comparing base (54a8eda) to head (f117e91).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5138      +/-   ##
==========================================
+ Coverage   85.52%   86.41%   +0.89%     
==========================================
  Files         282      282              
  Lines       59497    59499       +2     
==========================================
+ Hits        50882    51418     +536     
+ Misses       8615     8081     -534     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@peterjc
Copy link
Member

peterjc commented Jan 13, 2026

@mdehoon you're probably more familar with NumPy than me, does this look good to merge? Thanks!

cosang = np.cos(angle_rads)
sinang = np.sin(angle_rads)
cosang = np.cos(angle_rads).item()
sinang = np.sin(angle_rads).item()
Copy link
Member

Choose a reason for hiding this comment

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

Looking at this, why not use math.sin(angle_rads) to ensure we get a plain float rather than np.float64 or similar?

@peterjc
Copy link
Member

peterjc commented Jan 17, 2026

Is this one line change not enough to fix it?

❯ git diff --no-ext-diff
diff --git a/Bio/PDB/vectors.py b/Bio/PDB/vectors.py
index 14a23a821..d4cc56644 100644
--- a/Bio/PDB/vectors.py
+++ b/Bio/PDB/vectors.py
@@ -559,7 +559,7 @@ def coord_space(

     # tx acs[1] to origin
     # tm = homog_trans_mtx(-a1[0][0], -a1[1][0], -a1[2][0])
-    set_homog_trans_mtx(-a1[0], -a1[1], -a1[2], tm)
+    set_homog_trans_mtx(-a1[0, 0], -a1[1, 0], -a1[2, 0], tm)

     # directly translate a2 using a1
     p = a2 - a1

@Augustin-Zidek
Copy link
Author

Yeah, that looks reasonable as well and more minimal -- I like it. I will test that on Monday.

@Augustin-Zidek
Copy link
Author

I tested this and changing that single line is not enough, all of the changes are needed. However, instead of

set_homog_trans_mtx(a1[0].item(), a1[1].item(), a1[2].item(), tm)

one could do:

set_homog_trans_mtx(a1[0, 0], a1[1, 0], a1[2, 0], tm)

But the changes to set_Z_homog_rot_mtx and set_Y_homog_rot_mtx are still necessary.

@peterjc
Copy link
Member

peterjc commented Jan 19, 2026

Can you combine this then please? The .item() bit is ugly, and likely less efficient than the explicit indexing. Thanks!

@Augustin-Zidek
Copy link
Author

All done + I also changed it to use math for trigonometric functions instead of Numpy where you suggested. This uncovered a few more issues with passing non-scalar values which I fixed. See the latest commit.

sinang = math.sin(angle_rads)

mtx[0][0] = mtx[1][1] = cosang
mtx[0][0] = cosang
Copy link
Member

Choose a reason for hiding this comment

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

These lines feel odd, like it was written before double indexing was possible. Does mtx[0, 0] et work here?

set_Z_homog_rot_mtx(-sc[1], mrz)
# rotate translated a2 -polar_angle about Y
set_Y_homog_rot_mtx(-sc[2], mry)
set_Y_homog_rot_mtx(-sc[2][0], mry)
Copy link
Member

Choose a reason for hiding this comment

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

Looks link -sc[2, 0] should work here as well or better than -cs[2][0] does.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, the linter is unhappy:


Bio/PDB/vectors.py: note: In function "coord_space":
Bio/PDB/vectors.py:576:26: error: Value of type "float" is not indexable 
[index]
        set_Y_homog_rot_mtx(-sc[2][0], mry)
                             ^~~~~~~~
Bio/PDB/vectors.py:623:25: error: Value of type "float" is not indexable 
[index]
        set_Y_homog_rot_mtx(sc[2][0], mry)
                            ^~~~~~~~
Found 2 errors in 1 file (checked 298 source files)

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.

Incompatible with NumPy 2.4

2 participants