skbio.tree.TreeNode.to_array

TreeNode.to_array(attrs=None)[source]

Return an array representation of self

Parameters:

attrs : list 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.

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 the array

If length is an attribute, this will also record the length off the root which is nan. Take care when summing.

Examples

>>> from six import StringIO
>>> from skbio import TreeNode
>>> t = TreeNode.read(StringIO('(((a:1,b:2,c:3)x:4,(d:5)y:6)z:7);'))
>>> res = t.to_array()
>>> res.keys()
['child_index', 'length', 'name', 'id_index', 'id']
>>> res['child_index']
[(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)