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
Iterating over edges in a ts (or EdgeTable) by age of child, not parent #384
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?
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.
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.
@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.
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:
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.
Activity
jeromekelleher commentedon Oct 15, 2019
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 callededge_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.hyanwong commentedon Oct 15, 2019
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?
awohns commentedon Oct 15, 2019
hyanwong commentedon Oct 15, 2019
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.hyanwong commentedon Oct 16, 2019
@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.
hyanwong commentedon Nov 6, 2019
I'm wondering if it would be better to provide a parameter to the
ts.edges()method, see my comment herejeromekelleher commentedon Sep 29, 2020
Can we close this @hyanwong, or is this something you think is still worth doing?
hyanwong commentedon Apr 11, 2021
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 theedges()iterator:In a jupyter cell:
In a jupyter cell:
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:
So I think we can close this issue and add the code above to the "how to do this" section of tskit GitHub discussions.