phylo2vec.utils.vector.get_node_depths

phylo2vec.utils.vector.get_node_depths#

phylo2vec.utils.vector.get_node_depths(vector_or_matrix) ndarray[source]#

Get the depths of all nodes in a Phylo2Vec tree.

The depth of a node is the length of the path from the root to that node (i.e., distance from root). This follows the BEAST/ETE convention.

The root has depth 0, and depths increase as you move toward the leaves.

For vectors, topological depth is returned (all branch lengths = 1). For matrices, actual branch lengths are used.

Parameters:

vector_or_matrix (numpy.ndarray) – Phylo2Vec vector (ndim == 1) or matrix (ndim == 2)

Returns:

Depths of all nodes (length = 2 * n_leaves - 1). Index i contains the depth of node i.

Return type:

numpy.ndarray

Examples

>>> import numpy as np
>>> from phylo2vec.utils.vector import get_node_depths
>>> # Tree: (0,(1,(2,3)4)5)6
>>> v = np.array([0, 1, 2])
>>> depths = get_node_depths(v)
>>> depths[6]  # Root has depth 0
0.0
>>> depths[0]  # Leaf 0 is 1 edge from root
1.0