Skip to content

Commit 7ff7a17

Browse files
benjefferymergify-bot
authored andcommitted
Document numpy traversals
1 parent 8bb1b5d commit 7ff7a17

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

python/tskit/trees.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,15 +2102,51 @@ def num_tracked_samples(self, u=None):
21022102
u = self.virtual_root if u is None else u
21032103
return self._ll_tree.get_num_tracked_samples(u)
21042104

2105-
# TODO document these traversal arrays
2106-
# https://github.com/tskit-dev/tskit/issues/1788
21072105
def preorder(self, u=NULL):
2106+
"""
2107+
Returns a numpy array of node ids in preorder
2108+
<https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)>. If the node u
2109+
is specified the traversal is rooted at this node (and it will be the first
2110+
element in the returned array). Otherwise, all nodes reachable from the tree
2111+
roots will be returned. See :ref:`tutorials:sec_analysing_trees_traversals` for
2112+
examples.
2113+
2114+
:param int u: If specified, return all nodes in the subtree rooted at u
2115+
(including u) in traversal order.
2116+
:return: Array of node ids
2117+
:rtype: numpy.ndarray (dtype=np.int32)
2118+
"""
21082119
return self._ll_tree.get_preorder(u)
21092120

21102121
def postorder(self, u=NULL):
2122+
"""
2123+
Returns a numpy array of node ids in postorder
2124+
<https://en.wikipedia.org/wiki/Tree_traversal##Post-order_(LRN)>. If the node u
2125+
is specified the traversal is rooted at this node (and it will be the last
2126+
element in the returned array). Otherwise, all nodes reachable from the tree
2127+
roots will be returned. See :ref:`tutorials:sec_analysing_trees_traversals` for
2128+
examples.
2129+
2130+
:param int u: If specified, return all nodes in the subtree rooted at u
2131+
(including u) in traversal order.
2132+
:return: Array of node ids
2133+
:rtype: numpy.ndarray (dtype=np.int32)
2134+
"""
21112135
return self._ll_tree.get_postorder(u)
21122136

21132137
def timeasc(self, u=NULL):
2138+
"""
2139+
Returns a numpy array of node ids. Starting at `u`, returns the reachable
2140+
descendant nodes in order of increasing time (most recent first), falling back
2141+
to increasing ID if times are equal. Also see
2142+
:ref:`tutorials:sec_analysing_trees_traversals` for examples of how to use
2143+
traversals.
2144+
2145+
:param int u: If specified, return all nodes in the subtree rooted at u
2146+
(including u) in traversal order.
2147+
:return: Array of node ids
2148+
:rtype: numpy.ndarray (dtype=np.int32)
2149+
"""
21142150
nodes = self.preorder(u)
21152151
is_virtual_root = u == self.virtual_root
21162152
time = self.tree_sequence.tables.nodes.time
@@ -2124,6 +2160,18 @@ def timeasc(self, u=NULL):
21242160
return nodes[order]
21252161

21262162
def timedesc(self, u=NULL):
2163+
"""
2164+
Returns a numpy array of node ids. Starting at `u`, returns the reachable
2165+
descendant nodes in order of decreasing time (least recent first), falling back
2166+
to decreasing ID if times are equal. Also see
2167+
:ref:`tutorials:sec_analysing_trees_traversals` for examples of how to use
2168+
traversals.
2169+
2170+
:param int u: If specified, return all nodes in the subtree rooted at u
2171+
(including u) in traversal order.
2172+
:return: Array of node ids
2173+
:rtype: numpy.ndarray (dtype=np.int32)
2174+
"""
21272175
return self.timeasc(u)[::-1]
21282176

21292177
def _preorder_traversal(self, root):

0 commit comments

Comments
 (0)