You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tree.num_children(node), or Tree.is_unary(node) function? #431
With tree sequences now allowing unary nodes, I find that I'm relatively regularly checking if a node is unary. I might be wrong, but it seems to me that it might be a little inefficient to keep doing len(tree.children(u)) for this. If so, would it be sensible to simply report the number of children somehow, e.g. via a new method Tree.num_children(u)? Or (presumably even more efficiently, but much less generally), Tree.is_unary(u)?
Both of these are possible, and consistent with existing APIs. I'm guessing that we're not going to compute Tree.num_children any faster than len(tree.children(u)) unless we do it in C (which, unless it's a demonstrable bottleneck, doesn't seem worth it). is_unary is easy though, using the quintuply linked tree information.
Ah, I like the idea of is_unary via the quintuply linked information. Should I roll my own version in the code I write, or is this a useful node attribute to provide to other users of tskit?
Actually, adding in a C version of Tree.num_children doesn't seem like overkill, we should do this. Basically, this will iterate over the children of a node in the Python C module. We then call this from the Python API.
Would be good to know if there is actually a perf cost to just returning len(tree.children(u)) though. What's the difference between the times for [u for u in tree.nodes()] and [len(tree.children(u) for u in tree.nodes()] for a large tree?
You were right to ask to profile it. In case the creation of a large array took up the time, I stuck the evaluation in an if clause. On holly, using len(tree.children(u)) is only ~30% slower on a simulated ts of a million samples, and just under twice as slow on a truncated version of the 1000G chr 20 ts.
>>> t = timeit.Timer('[1 for u in tree.nodes() if len(tree.children(u))==-1]', 'import msprime; tree = msprime.simulate(int(1e6), recombination_rate=1000).first()')
>>> t.timeit(5)
13.252980103716254
>>> t = timeit.Timer('[1 for u in tree.nodes() if u==-1]', 'import msprime; tree = msprime.simulate(int(1e6), recombination_rate=1000).first()')
>>> t.timeit(5)
10.12563981115818
>>> t = timeit.Timer('[1 for tree in ts.trees() for u in tree.nodes() if len(tree.children(u))==-1]', 'import tskit; ts = tskit.load("1kg_chr20_trunc.trees")')
>>> t.timeit(1)
47.17694188468158
>>> t = timeit.Timer('[1 for tree in ts.trees() for u in tree.nodes() if u==-1]', 'import tskit; ts = tskit.load("1kg_chr20_trunc.trees")')
>>> t.timeit(1)
27.711492540314794
Not sure if this justifies C code or not? Seems on the verge of worth/not worth doing?
Activity
jeromekelleher commentedon Dec 3, 2019
Both of these are possible, and consistent with existing APIs. I'm guessing that we're not going to compute
Tree.num_childrenany faster thanlen(tree.children(u))unless we do it in C (which, unless it's a demonstrable bottleneck, doesn't seem worth it).is_unaryis easy though, using the quintuply linked tree information.hyanwong commentedon Dec 3, 2019
Ah, I like the idea of
is_unaryvia the quintuply linked information. Should I roll my own version in the code I write, or is this a useful node attribute to provide to other users of tskit?jeromekelleher commentedon Dec 4, 2019
Actually, adding in a C version of Tree.num_children doesn't seem like overkill, we should do this. Basically, this will iterate over the children of a node in the Python C module. We then call this from the Python API.
Would you like to take this on @hyanwong?
Adding
is_unaryseems excessive then, sincetree.num_children == 1is (arguably) more readable.jeromekelleher commentedon Dec 4, 2019
Would be good to know if there is actually a perf cost to just returning
len(tree.children(u))though. What's the difference between the times for[u for u in tree.nodes()]and[len(tree.children(u) for u in tree.nodes()]for a large tree?jeromekelleher commentedon Dec 4, 2019
Also good to know this perf timing for a large msprime simulation and a tsinfer'd, like 1000G chr20.
hyanwong commentedon Dec 4, 2019
You were right to ask to profile it. In case the creation of a large array took up the time, I stuck the evaluation in an if clause. On holly, using
len(tree.children(u))is only ~30% slower on a simulated ts of a million samples, and just under twice as slow on a truncated version of the 1000G chr 20 ts.Not sure if this justifies C code or not? Seems on the verge of worth/not worth doing?
jeromekelleher commentedon Dec 4, 2019
Possibly worth it. If someone wants to write the C and the tests then it'd be a useful feature.
hyanwong commentedon Dec 4, 2019
I'll have a go