Skip to content

Given a tree sequence, draw marginal trees emphasising similarity between neighbouring trees #389

Closed

Description

@brianzhang01

Currently we have two main tree sequence drawing methods: text and SVG. Each of these has a method to draw the entire tree sequence, which ends up drawing the marginal trees. Alternatively, one can iterate through the trees and call the appropriate draw method on the marginal trees.

However, the current methods of drawing do not emphasise the similarity between neighbouring trees. I have a proposal for this which is sketched out in this repo. If you run python sort.py, you get the current output of drawing an msprime-generated tree sequence (displaying as a screenshot due to #189 ).

Screen Shot 2019-10-16 at 4 28 31 PM

Going from the first to the second tree, all that has changed is that the edge from 2 to 14 has disappeared, and 2 has instead coalesced onto the edge from 15 to 20 to create a new node, 17. However, many other things are shifting around in the visualisation, obscuring the fundamental diff. Similarly, from the second to the third tree, all that has happened is that node 20 has been replaced by a node 19 of more recent time. The order of the samples could have stayed the same, but instead the nodes 18 and 17 are reversed in order, shifting the samples underneath.

Running the Python code (python sort.py), we get an ordering on the samples of each of the marginal trees that has much better display properties.

[0, 4, 1, 2, 7, 3, 5, 6, 9, 8]
[0, 4, 1, 2, 3, 7, 5, 6, 9, 8]
[0, 4, 1, 2, 3, 7, 5, 6, 9, 8]

If you imagine these sample orderings being used to draw the marginal trees, then the SPR (subtree pruning and regrafting) from tree 1 to 2 can be seen more clearly, and from tree 2 to 3, one can easily see that there is no change in topology, only a change in the height of the root node.

The operation I propose is to find the ordering of samples, consistent with the tree topology, that is minimum in terms of lexicographic ordering. The details of the implementation are in the Github repo.

@jeromekelleher has pointed out that the most efficient way to add this to the tskit draw methods is to add an iterator to tskit.Tree.nodes() here. I propose we start by just adding one called minlex_postorder, with minlex standing for "minimum lexicographic". We would then change the tree drawing code in these three places to use minlex_postorder instead of postorder, either changing the default or adding an option to choose.

Activity

  1. brianzhang01 commented on Oct 16, 2019

    @brianzhang01
    MemberAuthor

    A few more notes that are a bit less important to the issue description.

    First, my algorithm sketch assumes that samples = leaves. Since samples can also be internal nodes, the algorithm might need a bit more generality to handle that case.

    Second, the minimum lexicographic order is one of many orderings that can be used. The idea is to have an unambiguous way to resolve which of a node's children should be shown first (leftmost) in the visualisation. The minimum lexicographic order ends up ensuring that sample 0 is always at the left of the tree visualisation. Later on we might want to discuss other orderings.

    Third, once we introduce this ordering, it may be worthwhile / easier to of showing changes from one tree to the next. This could be done with an SVG animation or something like #385 .

    Also, just to clarify the current ordering that is used: starting from the root node, each node has its children shown in the order that they occur in the tuple Tree.children(u). In the above example, node 20 has children (18, 17), but then node 19 has children (17, 18), so the subtrees are switched.

  2. jeromekelleher commented on Oct 16, 2019

    @jeromekelleher
    Member

    This sounds amazing @brianzhang01, let me know how I can help! (Perhaps worth calling up to the BDI to chat?)

  3. brianzhang01 commented on Nov 21, 2019

    @brianzhang01
    MemberAuthor

    First, my algorithm sketch assumes that samples = leaves. Since samples can also be internal nodes, the algorithm might need a bit more generality to handle that case.

    I've now decided that the right way to do this is to output a minimum lexicographic order by leaves, not samples, so this is resolved. However, another issue is that sometimes there can apparently be multiple roots for TreeSequence / Tree objects. Right now I only do minlex ordering within each root, and the roots are iterated over using for u in roots: (trees.py, nodes() function). Should we add separate logic to also go over the roots in a minlex order?

    As we prepare to modify the tree drawing code itself, you can play around with a visualization that shows the effect of minlex sorting vs. without here. There's a command-line flag called sort, and most of the times I like the sorted version better. However, the README.md also mentions an extreme example for which the sorting ends up modifying a lot of stuff (run with python make_example.py to make the TreeSequence).

  4. jeromekelleher commented on Nov 22, 2019

    @jeromekelleher
    Member

    I've now decided that the right way to do this is to output a minimum lexicographic order by leaves, not samples, so this is resolved. However, another issue is that sometimes there can apparently be multiple roots for TreeSequence / Tree objects. Right now I only do minlex ordering within each root, and the roots are iterated over using for u in roots: (trees.py, nodes() function). Should we add separate logic to also go over the roots in a minlex order?

    I think I've convinced myself elsewhere that we shouldn't be treating this root-by-root, but it should be a global order. (But, let's see what happens when we try to draw the trees)

  5. added this to the Python 0.2.4 milestone on Apr 21, 2020
  6. jeromekelleher commented on Apr 21, 2020

    @jeromekelleher
    Member

    Now that #411 is merged, we're in a position to improve our drawing code. I think there's two options:

    1. Change the order that we draw trees to use the minlex ordering.
    2. Add an option leaf_order to the various drawing functions, which defaults to None (the current behaviour) and can optionally be "minlex".

    I'm in favour of (2), since this will break no existing code, and implementing it will require touching only a few places and adding a handful of tests.

    Any thoughts?

  7. petrelharp commented on Apr 21, 2020

    @petrelharp
    Contributor

    Any thoughts?

    (a) this is wonderful
    (b) I don't think we need to worry about changing the default ordering to something better: so why not (2) except default is "minlex"?

  8. benjeffery commented on Apr 21, 2020

    @benjeffery
    Member

    If it is just for drawing then it isn't serious breakage. I say default to minlex as it's so much better.

  9. brianzhang01 commented on Apr 21, 2020

    @brianzhang01
    MemberAuthor

    I would prefer Peter's suggestion as well, but I'm open to other views. As a reminder, there's already a version here if anyone wants to play around with visualising trees with minlex vs. no minlex. You need to install PyGame, which Yan had some trouble with, but then you can use the visualisation with --sort 0 vs. --sort 1. If there are any issues getting it to work, feel free to report as an issue in that repo.

    Note that as a consequence of minlex ordering, sample 0 is always going to be at the very left. But sample n - 1 isn't always at the very right -- for instance if 0 and n - 1 coalesce right away, then n - 1 is immediately to the right of 0. So there's some asymmetry.

    I also mention this in the linked README: "As an extreme example where minimum lexicographic (minlex) sorting is not that helpful, run python make_example.py to produce an example.trees file, then run python viz.py --file example.trees to display the example." (I would add that you can do --sort 0 vs. --sort 1 to compare.) However, based on playing around with several msprime trees, I've found minlex to be a good ordering.

  10. jeromekelleher commented on Apr 22, 2020

    @jeromekelleher
    Member

    OK, great, let's default to leaf_order="minlex" then. What would we call the standard order that comes out of postorder? How about "natural" maybe? I don't really like that - any better ideas?

  11. brianzhang01 commented on Apr 22, 2020

    @brianzhang01
    MemberAuthor

    The current order is to recursively traverse in the order returned by Tree.children(node). I don't understand what currently goes into determining that order. Some suggestions: leaf_order="normal", "naive", "standard", "children_order".

  12. jeromekelleher commented on Apr 22, 2020

    @jeromekelleher
    Member

    Well there is a sense of left-to-rightness in the trees, as each node has a left_child, right_child, left_sib and right_sib. The order we get then is the result of traversing in this order. Maybe "tree_order" gets this across?

  13. jeromekelleher commented on Apr 22, 2020

    @jeromekelleher
    Member

    Or just leaf_order="tree"?

  14. brianzhang01 commented on Apr 22, 2020

    @brianzhang01
    MemberAuthor

    leaf_order="tree" sounds great to me. And thanks for explaining left_child / right_child / left_sib / right_sib -- it makes more sense to me now.

    This also stood out from the documentation: "The left-to-right ordering of children is arbitrary and should not be depended on; see the data model section for details." And "The left-to-right ordering of nodes is determined by the order in which edges are inserted into the tree during iteration along the sequence."

    Edit: leaf_order="left_to_right" also works, though is more verbose.

  15. jeromekelleher commented on Apr 22, 2020

    @jeromekelleher
    Member

    Thanks @brianzhang01, let's go with leaf_order=tree unless anyone complains. I'll code this up when I get a chance.

  16. added 2 commits that reference this issue on Apr 30, 2020
    3ad5290
    d8d20c1
  17. added 2 commits that reference this issue on May 7, 2020
    751eed3
    ce366cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions