Optimize interaction matrix scoring kernel#13
Open
gwcmowry wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes FINCHES’ core interaction-matrix scoring pipeline by moving key masking work into compiled code, replacing the sliding-window scan with a prefix-sum (integral image) approach, and adding compatibility shims so existing callers/tests continue to work with minimal changes.
Changes:
- Replaced
matrix_scan(...)’s per-window nested loops with a prefix-sum based scan in the Cython extension. - Added a compiled
charge_weighted_mask(...)and switched charge-mask generation to use it; vectorized aliphatic-mask generation. - Updated epsilon calculation to compute directly from the weighted-matrix transform and introduced compatibility aliases/wrappers to preserve existing APIs and tests.
Reviewed changes
Copilot reviewed 7 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
finches/utils/matrix_manipulation.pyx |
Adds compiled charge mask generation and rewrites matrix_scan using an integral-image/prefix-sum method. |
finches/parsing_aminoacid_sequences.py |
Delegates charge mask to the Cython function and vectorizes aliphatic mask generation via table lookup. |
finches/epsilon_calculation.py |
Computes epsilon directly from the transformed weighted matrix; adds compatibility wrapper functions and aliases. |
finches/forcefields/mpipi.py |
Adds mpipi_model(...) compatibility alias with version-string mapping. |
finches/tests/test_FH_diagrams.py |
Fixes imports and uses the new mpipi_model alias. |
finches/tests/test_epsilon_calculation.py |
Makes test-data paths robust, fixes iteration bug, updates fixture key usage, and adds a reference test validating matrix_scan window scores. |
finches/tests/test_data/update_test_data.py |
Updates fixture-writing script (pathing, deterministic RNG, and fixture key naming). |
finches/tests/test_data/mPiPi_GGv1_seq_epsilon_and_vectors.npz |
Refreshed fixture data to match refactor outputs (within expected FP differences). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.

Summary
This PR speeds up the core FINCHES interaction-matrix scoring path while preserving existing outputs up to expected
floating-point differences and keeping the current test structure largely intact.
Main changes:
Why
Profiling pointed to three main costs in the pair-scoring path:
The biggest remaining bottleneck after mask optimization was matrix_scan, so the main algorithmic change here is
replacing the per-window nested-loop scan with a prefix-sum / integral-image approach.
Implementation details
finches/utils/matrix_manipulation.pyx
For a fixed window size, the old implementation effectively did work proportional to the number of windows times the
number of cells per window, while the new version transforms the matrix once, builds a prefix sum once, and scores
each window in constant time.
finches/parsing_aminoacid_sequences.py
finches/epsilon_calculation.py
These wrappers are intended as compatibility shims so the optimization refactor does not break existing callers or
the current test suite.
finches/forcefields/mpipi.py
Speedups
On representative synthetic sequence-pair benchmarks spanning 32x32 to 512x512 inputs, the optimized scorer matched
prior outputs up to floating-point noise and produced the following end-to-end speedups:
Mask-level improvements were also large:
The largest win came from the scan change itself, since matrix_scan was the dominant remaining bottleneck.
Tests
I kept the existing test layout and comments as intact as possible and only made the minimal fixes needed for pytest
to run reliably from the repo root.
That included:
Refreshed .npz fixtures were added to finches/tests/test_data/ so the existing regression tests run from the repo as
checked out; they are intended to restore self-contained test coverage, not to introduce a new expected behavior
baseline beyond floating-point-equivalent outputs from the refactor.
Test command used:
PYTHONPATH=.:/tmp/pytest_vendor MPLCONFIGDIR=/tmp/matplotlib HOME=/tmp
conda run --no-capture-output -n finches_clean python -m pytest finches/tests -q
Result:
11 passed in 2.23s