Vectorise edge_effect using tskit 1.0 mutation arrays - #193
Merged
Conversation
Replace the Python loop over causal sites and their mutations with an array-based computation using ts.mutations_edge, ts.mutations_inherited_state and ts.mutations_derived_state. Each causal site owns a contiguous run of mutation IDs, so the per-site boundaries come from a single searchsorted, the causal allele state changes are array comparisons, and the effects are accumulated with np.bincount. This is around 20 times faster: 1.44s -> 0.07s for 168k trait rows over a tree sequence with 338k edges. Extract the trait dataframe validation shared by genetic_value and edge_effect into _check_trait_df, and add a site_id bounds check there. Both functions now raise a ValueError for a site_id outside [0, ts.num_sites), where a negative value previously wrapped around to the end of the site table. Expand the edge_effect tests with a state-based reference implementation that is independent of the mutation walking approach, run over the tests/data.py tree sequences, all_trees_ts(2..5) with recurrent and back mutations, and simulations with and without recombination. Cross check against genetic_value(level="node") using the single tree identity edge_effect[e] == value[child(e)] - value[parent(e)], and cover multiple traits, shared causal sites, sites without mutations, stacked mutations, multi-character alleles, isolated nodes and multi-root forests.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #193 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 9 9
Lines 417 417
Branches 56 50 -6
=========================================
Hits 417 417
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
tskit provides the state that existed at the site before a mutation occurred, so there is no need to look up the parent mutation's derived state and fall back to the site's ancestral state by hand.
Member
Author
|
Vectorised numpy version is about 20X faster than Python loops, so good enough for now. Also use some tskit 1.0 infrastructure to simplify the code. |
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.
Replace the Python loop over causal sites and their mutations with an array-based computation using ts.mutations_edge,
ts.mutations_inherited_state and ts.mutations_derived_state. Each causal site owns a contiguous run of mutation IDs, so the per-site boundaries come from a single searchsorted, the causal allele state changes are array comparisons, and the effects are accumulated with np.bincount. This is around 20 times faster: 1.44s -> 0.07s for 168k trait rows over a tree sequence with 338k edges.
Extract the trait dataframe validation shared by genetic_value and edge_effect into _check_trait_df, and add a site_id bounds check there. Both functions now raise a ValueError for a site_id outside [0, ts.num_sites), where a negative value previously wrapped around to the end of the site table.
Expand the edge_effect tests with a state-based reference implementation that is independent of the mutation walking approach, run over the tests/data.py tree sequences, all_trees_ts(2..5) with recurrent and back mutations, and simulations with and without recombination. Cross check against genetic_value(level="node") using the single tree identity edge_effect[e] == value[child(e)] - value[parent(e)], and cover multiple traits, shared causal sites, sites without mutations, stacked mutations, multi-character alleles, isolated nodes and multi-root forests.
Closes #190