Skip to content

Iterate over edges within a tree #556

Closed

Description

@hyanwong

It is occasionally useful, given a Tree object, to iterate over all the edges in that tree. Would it be easy to add an edges() iterator to the Tree class? For consistency with the Tree.nodes() iterator, I guess this should return the integer ID of the edge, rather than an edge object.

Activity

  1. petrelharp commented on Apr 25, 2020

    @petrelharp
    Contributor

    Hm, I could see this being useful. Since we never refer to edges by ID elsewhere, I vote for not starting to do that here, though? It could be, very simply:

    def edges(t):
      for n in t.nodes():
         if n not in t.roots:
           pn = t.parent(n)
           yield (n, pn, t.time(pn) - t.time(n))
    

    ... but is that what's useful? or is the times of the two nodes more useful? Or what? Or, maybe this is easy enough to get out that we don't need it?

  2. hyanwong commented on Apr 25, 2020

    @hyanwong
    MemberAuthor

    No, I meant the actual edge ID, not the contents of the edge. For example, when displaying multiple trees, we might want to highlight one edge. The edge might appear in multiple trees, so we need to identify it by ID.

    Similarly, when considering 2 different trees, we might want to check if they share an edge. We could do this by comparing the whole edge object, including left & right positions (as presumably we can have edges with the same parent & child that are not the same, because they are in different positions), but easier just to compare edge IDs, no?

  3. petrelharp commented on Apr 25, 2020

    @petrelharp
    Contributor

    Since we never refer to edges by ID elsewhere, I vote for not starting to do that here, though?

    The edge might appear in multiple trees, so we need to identify it by ID.

    Oh, that makes sense. Hm, is ID the right way to identify them? For instance, what if you've got a -> b on [0, 1] and then a -> c -> b on [1, 2] and then a -> b again on [2,3]. Arguably the "edge" a->b should extend over [0,3], but if not, it still seems like you'd want it to identify the edge on [0,1] and the edge on [2,3]? For this the (parent, child) pair is the right labeling?

  4. jeromekelleher commented on Apr 27, 2020

    @jeromekelleher
    Member

    If we want to see the structure of the tree sequence itself, I think Yan's right and the simplest/most useful thing to do is return the edge IDs/objects. I can see how we might want to highlight how a particular edge spans across multiple trees as a useful viz tool.

    Unfortunately I don't think it's something we have easy access to. We'd need to change the tree update algorithm to have an edge array that is updated at the same time as the parent array. We could make this an option to the Tree() class so that we don't incur this cost every time.

    However, this is sufficiently involved that someone would need to be quite motivated to do it. I'm not sure a "nice to have" is enough here.

  5. hyanwong commented on Apr 27, 2020

    @hyanwong
    MemberAuthor

    I agree, and see this as a "punt down the line" issue, but thought it was worth raising. Not sure if we want a "nice to have, but not v important" flag for tskit issues?

  6. jeromekelleher commented on Apr 27, 2020

    @jeromekelleher
    Member

    Maybe the "enhancement" tag?

  7. added
    enhancement
    on Apr 27, 2020
  8. added
    Python API
    on Sep 29, 2020
  9. jeromekelleher commented on Sep 29, 2020

    @jeromekelleher
    Member

    I'm putting this into the "Random access for trees" project as it's the type of functionality we should be able to support with a point query interface for the edge table.

  10. jeromekelleher commented on Dec 11, 2022

    @jeromekelleher
    Member

    Closing this we can now easily access edge information on trees.

  11. hyanwong commented on Dec 12, 2022

    @hyanwong
    MemberAuthor

    In particular, we can now do

    tree = ts.first()
    for u in tree.nodes():
        edge_id = tree.edge(u)
        if edge_id != tskit.NULL:
            print(ts.edge(edge_id))
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