Cache attributes on internal nodes of the tree
Parameters: | func : function
cache_attrname : str
cache_type : {set, frozenset, list}
|
---|---|
Raises: | TypeError
|
Notes
This method is particularly useful if you need to frequently look up attributes that would normally require a traversal of the tree.
WARNING: any cache created by this method will be invalidated if the topology of the tree changes (e.g., if TreeNode.invalidate_caches is called).
Examples
Cache the tip names of the tree on its internal nodes
>>> from six import StringIO
>>> from skbio import TreeNode
>>> tree = TreeNode.read(StringIO("((a,b,(c,d)e)f,(g,h)i)root;"))
>>> f = lambda n: [n.name] if n.is_tip() else []
>>> tree.cache_attr(f, 'tip_names')
>>> for n in tree.traverse(include_self=True):
... print("Node name: %s, cache: %r" % (n.name, n.tip_names))
Node name: root, cache: ['a', 'b', 'c', 'd', 'g', 'h']
Node name: f, cache: ['a', 'b', 'c', 'd']
Node name: a, cache: ['a']
Node name: b, cache: ['b']
Node name: e, cache: ['c', 'd']
Node name: c, cache: ['c']
Node name: d, cache: ['d']
Node name: i, cache: ['g', 'h']
Node name: g, cache: ['g']
Node name: h, cache: ['h']