Repository navigation
Faster math steps, fix complex-dtype crash, optional bench suite - #133
Conversation
…bench suite - kabsch_weighted: replace triple Python loop with vectorized covariance - get_cm/get_inertia_tensor: indexed weight lookup, drop dead loop, avoid NxN mass matrix - quaternion_rotate: vectorized (N,4,4) stacks + einsum, keep makeW/makeQ API unchanged - inertia paths: eigh instead of eig (real output, fixes complex128 cdist crash on NumPy>=2) - check_reflections: skip redundant copies and diag matmul - benchmarks: pytest-benchmark suite behind rmsd[bench] extra
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Unresolved correctness, input-isolation, validation, and optional-test integration issues remain.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 2
Open (3)
What changed in this PR
Improves RMSD performance, fixes complex-eigenvalue handling, and adds optional benchmarks.
Changes:
- Vectorizes several numerical routines.
- Uses
eighfor symmetric matrices. - Adds optional
pytest-benchmarksupport.
| File | Changes |
|---|---|
rmsd/calculate_rmsd.py |
Numerical optimizations and eigenvalue handling |
pyproject.toml |
Optional benchmark dependency |
benchmarks/test_bench.py |
Performance benchmark suite |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| # Symmetric tensor: eigh is correct (real) and faster; indexing kept | ||
| eigval, eigvec = np.linalg.eigh(inertia) | ||
|
|
||
| principal_axis: ndarray = eigvec[np.argmax(eigval)] |
There was a problem hiding this comment.
Leaving this one open on purpose: the row indexing predates this PR (only eig->eigh changed here, which was required to fix the complex128 crash), no test covers this helper, and changing it would alter return values for existing callers. Proposing it as a separate follow-up instead.
- benchmarks/test_bench.py: skip module when pytest-benchmark is not installed so plain pytest stays green - check_reflections: cheap q_atoms.copy() keeps isolation from custom reorder callables while still skipping the coordinate deepcopy
|
Thanks for the review — addressed two findings in fd2b6f3, replying to all three below. 1. Benchmark tests breaking pytest discovery (fixed). You were right: plain 2. Principal eigenvector row vs column (kept as-is, on purpose). The 3. q_atoms isolation in check_reflections (fixed). Fair point about custom reorder callables. It is now On the failing CI: I checked, and it is not caused by this PR. Every CI job fails in the |
|
Overall looks good (however very wordy). I need some extra time to validate these two sections
|
|
Build / Format has been fixed, and simplified requirements. Please merge with master and your tests should run through |
Simplify weight assignment logic for RMSD calculation.
Updated dependencies to remove version pinning for numpy and scipy.
|
I added the fixes to make the PR build. Since I want to address the inertia index you found, I will just merge. Thanks for the clean-up PR. |
rmsd 1.7.0 merged charnley/rmsd#133, so the local kabsch_weighted_fast / kabsch_weighted_rmsd_fast copies are removed and clusttraj calls rmsd.kabsch_weighted / rmsd.kabsch_weighted_rmsd directly. Same math, same speed (MOx full matrix still 0.45 s preloaded). Bump the floor to rmsd>=1.7.0 in requirements.txt. The remaining _fastmath helpers (scatter rebuild, weight vector, Hungarian reference cache) are clusttraj-side savings upstream does not provide. Verified: 30/30 tests pass; 9 option combos vs original master code agree to 1.8e-15 on both preload and streaming paths.


What this does
This makes parts of the program faster, fixes a crash, and adds speed tests. Nothing about how you use the program changes: same commands, same options, same function names. All existing tests pass without any changes to them.
1. Fixes a crash
With new versions of NumPy, any run with the default reordering method crashed with this error:
That happened because the code used
np.linalg.eigon a symmetric 3x3 table, and new NumPy answers that with complex numbers, which the next step (cdist) refuses. Switching tonp.linalg.eigh(the right tool for symmetric tables) fixes the crash and is also a little faster (1.4x on that step). Before this fix, 9 of the existing tests failed; now all 52 pass.2. Makes the slow math steps faster
Three steps did their work item-by-item in slow Python loops. They now do the same math with fast array operations (NumPy only, no new required packages):
kabsch_weighted: the triple loop is gone.quaternion_rotate: building one small table per atom in a loop is gone.get_inertia_tensor: a per-atom loop that was never even used (dead code) is gone, and the mass table is no longer built as a big NxN block.get_cm: weight look-up is now a single array step.check_reflections: no more extra copies of the data on each of the 48 tries.3. Adds speed tests (optional)
pip install -e ".[bench]"thenpytest benchmarksruns timing tests for small (8 atoms), medium (60 atoms) and large (200–1064 atoms) cases. Normal users never need to install this.Timings
Measured with pytest-benchmark (best of several runs). One note first: unmodified master crashes on every reorder run in this setup (see crash section above — that is also why 9 tests failed before the fix), so pure master has no timing at all. To keep the speed comparison fair, every "Before" number below was measured with master plus only the crash fix.
Single steps, medium molecule (60 atoms, ChEMBL 3039407):
Single steps, large molecule (1000 atoms):
Full default run (
--reorder), which is what most users run:Answers are unchanged everywhere: same atom matching, same RMSD values (equal to 13–16 digits).
Honest conclusions
numpy+scipy).