Skip to content

Tree.num_children(node), or Tree.is_unary(node) function? #431

Closed

Description

@hyanwong

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)?

Activity

  1. jeromekelleher commented on Dec 3, 2019

    @jeromekelleher
    Member

    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.

  2. hyanwong commented on Dec 3, 2019

    @hyanwong
    MemberAuthor

    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?

  3. jeromekelleher commented on Dec 4, 2019

    @jeromekelleher
    Member

    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_unary seems excessive then, since tree.num_children == 1 is (arguably) more readable.

  4. jeromekelleher commented on Dec 4, 2019

    @jeromekelleher
    Member

    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?

  5. jeromekelleher commented on Dec 4, 2019

    @jeromekelleher
    Member

    Also good to know this perf timing for a large msprime simulation and a tsinfer'd, like 1000G chr20.

  6. hyanwong commented on Dec 4, 2019

    @hyanwong
    MemberAuthor

    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?

  7. jeromekelleher commented on Dec 4, 2019

    @jeromekelleher
    Member

    Possibly worth it. If someone wants to write the C and the tests then it'd be a useful feature.

  8. hyanwong commented on Dec 4, 2019

    @hyanwong
    MemberAuthor

    I'll have a go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions