Fix infer_cbeta corrupting the cached inferred_cbeta array#361
Open
winklemad wants to merge 1 commit into
Open
Fix infer_cbeta corrupting the cached inferred_cbeta array#361winklemad wants to merge 1 commit into
winklemad wants to merge 1 commit into
Conversation
ProteinChain.infer_cbeta() takes the cached `inferred_cbeta` array by reference and NaN-fills the glycine rows in place to build a new chain. Because `inferred_cbeta` is a cached_property, this permanently corrupts the original chain's cached array -- and everything derived from it, e.g. `pdist_CB` -- so the value of `inferred_cbeta` / `pdist_CB` depends on whether `infer_cbeta()` was ever called on the chain. The same method already copies `atom37_positions` / `atom37_mask` before mutating them, and the sibling `ProteinComplex.infer_cbeta` computes a fresh array. Copy the array before the in-place NaN-fill. Added a regression test.
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.
No linked issue — found by reading the code.
The bug
ProteinChain.infer_cbeta()builds a new chain whose glycine CB positions are NaN (unlessinfer_cbeta_for_glycine=True). To do so it takes the cachedinferred_cbetaarray by reference and NaN-fills the glycine rows in place:Since
inferred_cbetais a@cached_property, this permanently corrupts the original chain's cached array, and everything derived from it (e.g.pdist_CB, which issquareform(pdist(self.inferred_cbeta))). So the value ofinferred_cbeta/pdist_CBsilently depends on whetherinfer_cbeta()was ever called:Why it's a bug (not intended)
atom37_positions = self.atom37_positions.copy()/atom37_mask = self.atom37_mask.copy().ProteinComplex.infer_cbetacomputesinferred_cbeta_positionsinto a fresh local, so it isn't affected.Fix
Copy the array before the in-place NaN-fill (
self.inferred_cbeta.copy()). The returned chain is unchanged (glycine CB still masked by default, kept withinfer_cbeta_for_glycine=True); only the mutation of the shared cache is removed.Testing
Added
esm/utils/structure/protein_chain_test.py::test_infer_cbeta_does_not_mutate_cached_inferred_cbeta, asserting thatinferred_cbetaandpdist_CBare unchanged after callinginfer_cbeta(). It fails before this change and passes after.ruff check/ruff formatare clean.