Skip to content

Commit

Permalink
Simpler root traversal (can now use virtual root)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyanwong authored and mergify[bot] committed Oct 7, 2024
1 parent e52eac7 commit 9ec83a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
16 changes: 8 additions & 8 deletions python/tests/data/svg/internal_sample_ts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 11 additions & 11 deletions python/tests/data/svg/ts_multiroot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 11 additions & 12 deletions python/tskit/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,19 +1686,18 @@ def assign_x_coordinates(self):
# Set up x positions for nodes
node_x_coord = {}
leaf_x = 0 # First leaf starts at x=1, to give some space between Y axis & leaf
for root in self.tree.roots:
for u in self.tree.nodes(root, order=self.traversal_order):
if self.tree.is_leaf(u):
leaf_x += 1
node_x_coord[u] = leaf_x
for u in self.tree.nodes(order=self.traversal_order):
if self.tree.is_leaf(u):
leaf_x += 1
node_x_coord[u] = leaf_x
else:
child_coords = [node_x_coord[c] for c in self.tree.children(u)]
if len(child_coords) == 1:
node_x_coord[u] = child_coords[0]
else:
child_coords = [node_x_coord[c] for c in self.tree.children(u)]
if len(child_coords) == 1:
node_x_coord[u] = child_coords[0]
else:
a = min(child_coords)
b = max(child_coords)
node_x_coord[u] = a + (b - a) / 2
a = min(child_coords)
b = max(child_coords)
node_x_coord[u] = a + (b - a) / 2
# Now rescale to the plot width: leaf_x is the maximum value of the last leaf
if len(node_x_coord) > 0:
scale = self.plotbox.width / leaf_x
Expand Down

0 comments on commit 9ec83a5

Please sign in to comment.