polymerist.genutils.trees.treecopy
Tools for copying parts and wholes of trees, at various levels of resolution
Functions
|
Return a dict of all attributes set on a Node |
|
Create a copy of a Node with matching attributes but no ancestors or descendents |
|
Create a copy of an anytree Node hierarchy. Can provide filters and stop criteria to exclude nodes or whole branches |
|
Produces a networkx.DiGraph representation on an anytree Tree |
Module Contents
- polymerist.genutils.trees.treecopy.get_node_attrs(node: anytree.node.Node, attr_filter: polymerist.genutils.filters.Filter[str] | None = None, include_name: bool = False) dict[str, Any][source]
Return a dict of all attributes set on a Node
- Parameters:
node (Node) – An anytree.node.Node object
attr_filter (Filter[str] (optional), default lambda x : True) – An optional criterion to decide whether an attribute should be kept Should be a function which accepts a single string arg and returns a bool
Return value of True will include an attribute in the return, while value or False will exclude it If None, will default to always True (i.e. no attributes will be screened out)
include_name (bool, deafult False) – Whether to include the required “name” attribute of a Node in the returned dict Useful to exclude when copying nodes to avoid redundancy By default False (i.e. “name” will be excluded from the returned attributes)
- Returns:
node_attrs – A dictionary keyed by attribute name whose values are the values set for repective Node attributes
- Return type:
dict[str, Any]
- polymerist.genutils.trees.treecopy.copy_node_unbound(node: anytree.node.Node, attr_filter: polymerist.genutils.filters.Filter[str] | None = None) anytree.node.Node[source]
Create a copy of a Node with matching attributes but no ancestors or descendents
- Parameters:
node (Node) – An anytree.node.Node object
attr_filter (Filter[str] (optional), default lambda x : True) – An optional criterion to decide whether an attribute should be kept Should be a function which accepts a single string arg and returns a bool
Return value of True will include an attribute in the return, while value or False will exclude it If None, will default to always True (i.e. no attributes will be screened out)
- Returns:
node_copy – A new Node object which has the same attributes as the original node but none of the ancestors or descendants
- Return type:
Node
- polymerist.genutils.trees.treecopy.copy_tree(root: anytree.node.Node, stop: polymerist.genutils.filters.Filter[anytree.node.Node] | None = None, attr_filter: polymerist.genutils.filters.Filter[str] | None = None) anytree.node.Node[source]
Create a copy of an anytree Node hierarchy. Can provide filters and stop criteria to exclude nodes or whole branches
- Parameters:
root (Node) – An anytree.node.Node object which is the root of a tree-like hierarchy
stop (Filter[Node] (optional), default None) – An optional criterion to decide when to stop traversing the tree to be copied Should be a function which accepts a single Node arg and returns a bool
This criterion is inclusive, i.e. a Node matching this criterion will NOT be included in the copied tree, nor will any of its children or their children, recursively
Return value of True will exclude all subsequent nodes on a branch, while value of True will proceed with iteration and copying If None, will default to always False (i.e. no extra stop conditions, full tree will be copied)
attr_filter (Filter[str] (optional), default lambda x : True) – An optional criterion to decide whether an attribute should be kept Should be a function which accepts a single string arg and returns a bool
Return value of True will include an attribute in the return, while value or False will exclude it If None, will default to always True (i.e. no attributes will be screened out)
- Returns:
root_new – The root Node of the copied tree structure
- Return type:
Node
- polymerist.genutils.trees.treecopy.tree_to_networkx(root: anytree.node.Node, stop: polymerist.genutils.filters.Filter[anytree.node.Node] | None = None, attr_filter: polymerist.genutils.filters.Filter[str] | None = None) anytree.node.Node[source]
Produces a networkx.DiGraph representation on an anytree Tree
- Parameters:
root (Node) – An anytree.node.Node object which is the root of a tree-like hierarchy
stop (Filter[Node] (optional), default None) – An optional criterion to decide when to stop traversing the tree to be copied Should be a function which accepts a single Node arg and returns a bool
Return value of True will exclude all subsequent nodes on a branch, while value of True will proceed with iteration and copying If None, will default to always False (i.e. no extra stop conditions, full tree will be copied)
This criterion is inclusive, i.e. a Node matching this criterion will NOT be included in the copied tree, nor will any of its children or their children, recursively
attr_filter (Filter[str] (optional), default lambda x : True) – An optional criterion to decide whether an attribute should be kept Should be a function which accepts a single string arg and returns a bool
Return value of True will include an attribute in the return, while value or False will exclude it If None, will default to always True (i.e. no attributes will be screened out)
- Returns:
nx_tree – A networkx directed graph object
- Return type:
diGraph