Changes in root order of trees. #2075
-
Preface: I'm posting this first as a discussion as I'm not sure it is an issue ("bug"). Some of the recent changelog content alludes to this, but I find it a bit cryptic and am unsure that it is related. The order of root traversal has changed from Python 0.3.7 to 0.4.0, presumably due to the introduction of "virtual roots" in C API 0.99.15. Take the following code: import tskit
tc = tskit.TableCollection(1000.)
tc.nodes.add_row(time=2.)
tc.nodes.add_row(time=1.)
for _ in range(4):
tc.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0.)
tc.edges.add_row(500., 1000., 0, 1)
tc.edges.add_row(0., 500., 0, 2)
tc.edges.add_row(0., 1000., 0, 3)
tc.edges.add_row(500., 1000., 1, 2)
tc.edges.add_row(0., 1000., 1, 4)
tc.edges.add_row(0., 1000., 1, 5)
tc.sort()
ts = tc.tree_sequence()
print(ts.draw_text())
for t in ts.trees():
for r in t.roots:
print(r) The tree sequence is:
With tskit 0.3.7, the root orders are:
With 0.4.0, the root orders are:
If one digs around more (data not shown), the different outputs are due to the different definitions of the left root of a tree that I believe arises from the latest C API. If this is expected, yay! If not... I ran into this writing various tests of preorder traversal and got confused, and the preorder output for the first tree differs between the two python versions as one would expect given the different order of visiting roots. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Well-spotted. FWIW, I don't think we've written anywhere that roots of trees will come in any particular order (and I'd argue against painting ourselves into that corner unless there's a good reason for it?), so I don't think tihs is a bug. |
Beta Was this translation helpful? Give feedback.
-
This is expected - basically the old ordering was arbitrary, but now roots are treated in the same way as all other nodes and the ordering arises in the same way the left-to-right ordering for any other node. So, it's documented as arbitrary (but very unlikely to change, in practise) but does depend on which direction you've built the tree from (i.e., whether you've iterated from the left or the right). I thought we'd got that into the changelog, but it must have gotten lost somewhere. |
Beta Was this translation helpful? Give feedback.
This is expected - basically the old ordering was arbitrary, but now roots are treated in the same way as all other nodes and the ordering arises in the same way the left-to-right ordering for any other node. So, it's documented as arbitrary (but very unlikely to change, in practise) but does depend on which direction you've built the tree from (i.e., whether you've iterated from the left or the right).
I thought we'd got that into the changelog, but it must have gotten lost somewhere.