Skip to content

Commit b6364f8

Browse files
committed
Document numpy traversals
1 parent bc10f93 commit b6364f8

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

python/tskit/trees.py

+51-3
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ def get_num_tracked_samples(self, u=None):
21012101

21022102
def num_tracked_samples(self, u=None):
21032103
"""
2104-
Returns the number of samples in the set specified in the
2104+
Returns the numpy of samples in the set specified in the
21052105
``tracked_samples`` parameter of the :meth:`TreeSequence.trees` method
21062106
underneath the specified node. If the input node is not specified,
21072107
return the total number of tracked samples in the tree.
@@ -2116,15 +2116,51 @@ def num_tracked_samples(self, u=None):
21162116
u = self.virtual_root if u is None else u
21172117
return self._ll_tree.get_num_tracked_samples(u)
21182118

2119-
# TODO document these traversal arrays
2120-
# https://github.com/tskit-dev/tskit/issues/1788
21212119
def preorder(self, u=NULL):
2120+
"""
2121+
Returns a numpy array of node ids in preorder
2122+
<https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)>. If the node u
2123+
is specified the traversal is rooted at this node (and it will be the first
2124+
element in the returned array). Otherwise, all nodes reachable from the tree
2125+
roots will be returned. See :ref:tutorials:sec_analysing_trees_traversals for
2126+
examples.
2127+
2128+
:param int u: If specified, return all nodes in the subtree rooted at u
2129+
(including u) in traversal order.
2130+
:return: Array of node ids
2131+
:rtype: numpy.ndarray (dtype=np.int32)
2132+
"""
21222133
return self._ll_tree.get_preorder(u)
21232134

21242135
def postorder(self, u=NULL):
2136+
"""
2137+
Returns a numpy array of node ids in postorder
2138+
<https://en.wikipedia.org/wiki/Tree_traversal##Post-order_(LRN)>. If the node u
2139+
is specified the traversal is rooted at this node (and it will be the first
2140+
element in the returned array). Otherwise, all nodes reachable from the tree
2141+
roots will be returned. See :ref:tutorials:sec_analysing_trees_traversals for
2142+
examples.
2143+
2144+
:param int u: If specified, return all nodes in the subtree rooted at u
2145+
(including u) in traversal order.
2146+
:return: Array of node ids
2147+
:rtype: numpy.ndarray (dtype=np.int32)
2148+
"""
21252149
return self._ll_tree.get_postorder(u)
21262150

21272151
def timeasc(self, u=NULL):
2152+
"""
2153+
Returns a numpy array of node ids. Starting at `u`, returns the reachable
2154+
descendant nodes in order of increasing time (most recent first), falling back
2155+
to increasing ID if times are equal. Also see
2156+
:ref:`tutorials:sec_analysing_trees_traversals` for examples of how to use
2157+
traversals.
2158+
2159+
:param int u: If specified, return all nodes in the subtree rooted at u
2160+
(including u) in traversal order.
2161+
:return: Array of node ids
2162+
:rtype: numpy.ndarray (dtype=np.int32)
2163+
"""
21282164
nodes = self.preorder(u)
21292165
is_virtual_root = u == self.virtual_root
21302166
time = self.tree_sequence.tables.nodes.time
@@ -2138,6 +2174,18 @@ def timeasc(self, u=NULL):
21382174
return nodes[order]
21392175

21402176
def timedesc(self, u=NULL):
2177+
"""
2178+
Returns a numpy array of node ids. Starting at `u`, returns the reachable
2179+
descendant nodes in order of decreasing time (least recent first), falling back
2180+
to decreasing ID if times are equal. Also see
2181+
:ref:`tutorials:sec_analysing_trees_traversals` for examples of how to use
2182+
traversals.
2183+
2184+
:param int u: If specified, return all nodes in the subtree rooted at u
2185+
(including u) in traversal order.
2186+
:return: Array of node ids
2187+
:rtype: numpy.ndarray (dtype=np.int32)
2188+
"""
21412189
return self.timeasc(u)[::-1]
21422190

21432191
def _preorder_traversal(self, root):

0 commit comments

Comments
 (0)