polymerist.genutils.trees.treecopy ================================== .. py:module:: polymerist.genutils.trees.treecopy .. autoapi-nested-parse:: Tools for copying parts and wholes of trees, at various levels of resolution Functions --------- .. autoapisummary:: polymerist.genutils.trees.treecopy.get_node_attrs polymerist.genutils.trees.treecopy.copy_node_unbound polymerist.genutils.trees.treecopy.copy_tree polymerist.genutils.trees.treecopy.tree_to_networkx Module Contents --------------- .. py:function:: get_node_attrs(node: anytree.node.Node, attr_filter: Optional[polymerist.genutils.filters.Filter[str]] = None, include_name: bool = False) -> dict[str, Any] 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 :rtype: dict[str, Any] .. py:function:: copy_node_unbound(node: anytree.node.Node, attr_filter: Optional[polymerist.genutils.filters.Filter[str]] = None) -> anytree.node.Node 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 :rtype: Node .. py:function:: copy_tree(root: anytree.node.Node, stop: Optional[polymerist.genutils.filters.Filter[anytree.node.Node]] = None, attr_filter: Optional[polymerist.genutils.filters.Filter[str]] = None) -> anytree.node.Node 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 :rtype: Node .. py:function:: tree_to_networkx(root: anytree.node.Node, stop: Optional[polymerist.genutils.filters.Filter[anytree.node.Node]] = None, attr_filter: Optional[polymerist.genutils.filters.Filter[str]] = None) -> anytree.node.Node 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 :rtype: diGraph