Skip to content

Commit b6fdb44

Browse files
Numpy versions of timeasc and timedesc
1 parent 5f1ca95 commit b6fdb44

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

c/tests/test_trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2019 Tskit Developers
4+
* Copyright (c) 2019-2021 Tskit Developers
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

python/tests/test_topology.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6459,8 +6459,9 @@ def verify(self, ts):
64596459
assert tree.is_internal(tree.virtual_root)
64606460
assert not tree.is_leaf(tree.virtual_root)
64616461
assert not tree.is_sample(tree.virtual_root)
6462-
# The mrca of the virtual_root and anything except itself is -1
6463-
assert tree.mrca(0, tree.virtual_root) == tskit.NULL
6462+
# The mrca of the virtual_root and anything is itself
6463+
assert tree.mrca(0, tree.virtual_root) == tree.virtual_root
6464+
assert tree.mrca(tree.virtual_root, 0) == tree.virtual_root
64646465
assert tree.mrca(tree.virtual_root, tree.virtual_root) == tree.virtual_root
64656466
# The virtual_root is a descendant of nothing other than itself
64666467
assert not tree.is_descendant(0, tree.virtual_root)

python/tskit/trees.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,22 @@ def preorder(self, u=NULL):
20972097
def postorder(self, u=NULL):
20982098
return self._ll_tree.get_postorder(u)
20992099

2100+
def timeasc(self, u=NULL):
2101+
nodes = self.preorder(u)
2102+
is_virtual_root = u == self.virtual_root
2103+
time = self.tree_sequence.tables.nodes.time
2104+
if is_virtual_root:
2105+
# We could avoid creating this array if we wanted to, but
2106+
# it's not that often people will be using this with the
2107+
# virtual_root as an argument, so doesn't seem worth
2108+
# the complexity
2109+
time = np.append(time, [np.inf])
2110+
order = np.lexsort([nodes, time[nodes]])
2111+
return nodes[order]
2112+
2113+
def timedesc(self, u=NULL):
2114+
return self.timeasc(u)[::-1]
2115+
21002116
def _preorder_traversal(self, root):
21012117
return map(int, self.preorder(root))
21022118

@@ -2137,16 +2153,13 @@ def _timeasc_traversal(self, root):
21372153
"""
21382154
Sorts by increasing time but falls back to increasing ID for equal times.
21392155
"""
2140-
# TODO implement with numpy?
2141-
yield from sorted(self.nodes(root), key=lambda u: (self.time(u), u))
2156+
yield from self.timeasc(root)
21422157

21432158
def _timedesc_traversal(self, root):
21442159
"""
21452160
The reverse of timeasc.
21462161
"""
2147-
yield from sorted(
2148-
self.nodes(root), key=lambda u: (self.time(u), u), reverse=True
2149-
)
2162+
yield from self.timedesc(root)
21502163

21512164
def _minlex_postorder_traversal(self, root):
21522165
"""

0 commit comments

Comments
 (0)