-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add iterative python parsimony #28
Open
benjeffery
wants to merge
1
commit into
tskit-dev:main
Choose a base branch
from
benjeffery:iterative
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import tskit | ||
import util | ||
import functools | ||
import numba | ||
import numpy as np | ||
import msprime | ||
|
||
VECTORISED = False | ||
|
||
@numba.njit() | ||
def _iterative_hartigan_parsimony(genotypes, samples, left_child, parent, postorder, preorder, root): | ||
num_nodes = len(postorder) | ||
num_alleles = np.max(genotypes) + 1 | ||
optimal_set = np.zeros((num_nodes, num_alleles), dtype=np.int8) | ||
# Simple version assuming non missing data and one root | ||
for allele, u in zip(genotypes, samples): | ||
optimal_set[u, allele] = 1 | ||
|
||
allele_count = np.zeros((num_nodes, num_alleles), dtype=np.int32) | ||
for node_j in postorder: | ||
if left_child[node_j] != tskit.NULL: | ||
max_allele_count = 0 | ||
for allele_k in range(num_alleles): | ||
if allele_count[node_j, allele_k] > max_allele_count: | ||
max_allele_count = allele_count[node_j, allele_k] | ||
for allele_k in range(num_alleles): | ||
if allele_count[node_j, allele_k] == max_allele_count: | ||
optimal_set[node_j, allele_k] = 1 | ||
for allele_k in range(num_alleles): | ||
allele_count[parent[node_j], allele_k] += optimal_set[node_j, allele_k] | ||
|
||
anc_index, max_val = -1, -1 | ||
for i in range(len(optimal_set[root])): | ||
if optimal_set[root, i] > max_val: | ||
anc_index, max_val = i, optimal_set[root, i] | ||
|
||
state = np.zeros((num_nodes), dtype=np.int32) | ||
state[:] = anc_index | ||
mutations = 0 | ||
for node_j in preorder: | ||
state[node_j] = state[parent[node_j]] | ||
if optimal_set[node_j, state[node_j]] == 0: | ||
maxval, argmax = -1, -1 | ||
for k in range(num_alleles): | ||
if optimal_set[node_j, k] > maxval: | ||
maxval = optimal_set[node_j, k] | ||
argmax = k | ||
state[node_j] = argmax | ||
mutations += 1 | ||
return mutations | ||
|
||
def iterative_hartigan_parsimony(tree, genotypes, alleles): | ||
samples = tree.tree_sequence.samples() | ||
left_child = tree.left_child_array | ||
parent = tree.parent_array | ||
postorder = tree.postorder() | ||
preorder = tree.preorder() | ||
return _iterative_hartigan_parsimony(genotypes, samples, left_child, parent, postorder, preorder, tree.root) | ||
|
||
def run(ts_path, max_sites): | ||
ts = tskit.load(ts_path) | ||
assert ts.num_trees == 1 | ||
tree = ts.first() | ||
return util.benchmark_python( | ||
ts, | ||
functools.partial(iterative_hartigan_parsimony, tree), | ||
"py_numba_iterative", | ||
max_sites=max_sites, | ||
) | ||
|
||
def warmup(): | ||
ts = msprime.sim_ancestry(100, sequence_length=100000, random_seed=43) | ||
genotypes = np.zeros(ts.num_samples, dtype=np.int8) | ||
iterative_hartigan_parsimony(ts.first(), genotypes, ["0"]) |
This file contains 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
File renamed without changes.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably better to hard-code -1 here. I have a feeling accessing a module variable like this could hurt perf? Although it's compiling down to nopython, so probably not.
Easily tested I guess.