Skip to content

Iterating over edges in a ts (or EdgeTable) by age of child, not parent #384

Closed

Description

@hyanwong

For tsdate, @awohns needs to be able to iterate over all the edges that point to a particular child node in order of the time of that child node. Currently, edges are ordered by time of parent node (which is also useful). Iterating over edges by child node time as well as parent node time seems a generally useful thing to want to do if we are doing pre- or post- order traversal through the ARG.

Presumably we could implement a different in-place edge sort algorithm to do this, although it would make an invalidly-sorted edge table, which would need re-sorting normally before being turned into a tree sequence. Would this be seen as a bit hacky?

Activity

  1. jeromekelleher commented on Oct 15, 2019

    @jeromekelleher
    Member

    I think implementing this as an argument to the TableCollection.sort() is the right approach. If no arguments are passed, then we sort in the canonical order. We can add an option called edge_order, which defaults to "parent_time" but we can also specify as "child_time". Being able to sort into a non-canonical ordering is fine --- this is an advanced operation.

  2. hyanwong commented on Oct 15, 2019

    @hyanwong
    MemberAuthor

    Great. Perfect. @awohns - would you like to work this into a PR, as you'll need it for the "outside" part of the tsdate algorithm?

  3. awohns commented on Oct 15, 2019

    @awohns
    Member
  4. hyanwong commented on Oct 15, 2019

    @hyanwong
    MemberAuthor

    Will do, thanks for raising the issue Yan!

    Great. Thanks. By the way, you should be able to go in reverse order after the sort by simply doing reversed(tables.edges) so there's no need to implement an ascending vs descending option to the sort params.

  5. hyanwong commented on Oct 16, 2019

    @hyanwong
    MemberAuthor

    @awohns : I remember that for some of the code we wanted to keep the edge ID, so we could map number of mutations above an edge to the ID. Be wary that re-sorting the edges will re-assign edge IDs, so that we may not have access to this any more. I'm not sure of that's a problem in the backwards step or not. It might be simply enough to keep a handle on the node IDs.

  6. hyanwong commented on Nov 6, 2019

    @hyanwong
    MemberAuthor

    I'm wondering if it would be better to provide a parameter to the ts.edges() method, see my comment here

  7. jeromekelleher commented on Sep 29, 2020

    @jeromekelleher
    Member

    Can we close this @hyanwong, or is this something you think is still worth doing?

  8. added
    enhancement
    Python API
    on Sep 29, 2020
  9. hyanwong commented on Apr 11, 2021

    @hyanwong
    MemberAuthor

    After a bit of experimenting, it turns out that it's just as quick to access the edges in an arbitrary order using ts.edge(i) than is it to use the edges() iterator:

    import msprime
    ts = msprime.simulate(100000, length=10000, Ne=10000, recombination_rate=0.00001, random_seed=123)
    

    In a jupyter cell:

    %%timeit
    for e in ts.edges():
        p = e.parent
    

    642 ms ± 3.94 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

    import numpy as np
    a = np.arange(ts.num_edges)
    np.random.shuffle(a)
    

    In a jupyter cell:

    %%timeit
    for i in a:
        p = ts.edge(i).parent
    

    590 ms ± 1.72 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

    So given that we want the edge id, it's much easier to sort the edge indices how we want first, then simply access them in that order. This is basically how we do it in tsdate:

    def edges_by_child_desc(ts):
        """
        Return a generator over edges grouped by child in descending order
        of the time of the child (and child node id in case of ties)
        """
        wtype = np.dtype([("Childage", "f4"), ("Childnode", "f4")])
        w = np.empty(ts.num_edges, dtype=wtype)
        w["Childage"] = ts.tables.nodes.time[ts.tables.edges.child[:]]
        w["Childnode"] = ts.tables.edges.child[:]
        return (
            ts.edge(i)
            for i in reversed(np.argsort(w, order=("Childage", "Childnode")))
        )
    

    So I think we can close this issue and add the code above to the "how to do this" section of tskit GitHub discussions.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python APIenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions