skbio.tree.TreeNode.to_array#

TreeNode.to_array(attrs=None, nan_length_value=None)[source]#

Return an array representation of self.

Parameters:
attrslist of tuple or None

The attributes and types to return. The expected form is [(attribute_name, type)]. If None, then name, length, and id are returned.

nan_length_valuefloat, optional

If provided, replaces any nan in the branch length vector (i.e., result['length']) with this value. nan branch lengths can arise from an edge not having a length (common for the root node parent edge), which can making summing problematic.

Returns:
dict of array
{id_index: {id: TreeNode},

child_index: ((node_id, left_child_id, right_child_id)), attr_1: array(…), … attr_N: array(…)}

Notes

Attribute arrays are in index order such that TreeNode.id can be used as a lookup into the array.

Examples

>>> from skbio import TreeNode
>>> t = TreeNode.read(['(((a:1,b:2,c:3)x:4,(d:5)y:6)z:7);'])
>>> res = t.to_array()
>>> sorted(res.keys())
['child_index', 'id', 'id_index', 'length', 'name']
>>> res['child_index'] 
array([[4, 0, 2],
       [5, 3, 3],
       [6, 4, 5],
       [7, 6, 6]]...
>>> for k, v in res['id_index'].items():
...     print(k, v)
...
0 a:1.0;

1 b:2.0;

2 c:3.0;

3 d:5.0;

4 (a:1.0,b:2.0,c:3.0)x:4.0;

5 (d:5.0)y:6.0;

6 ((a:1.0,b:2.0,c:3.0)x:4.0,(d:5.0)y:6.0)z:7.0;

7 (((a:1.0,b:2.0,c:3.0)x:4.0,(d:5.0)y:6.0)z:7.0);

>>> res['id']
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> res['name']
array(['a', 'b', 'c', 'd', 'x', 'y', 'z', None], dtype=object)