Tree graph¶
- class dendrotweaks.morphology.trees.Node(idx: int | str, parent_idx: int | str)[source]¶
Bases:
objectRepresents a node in a tree.
A node can be a 3D point in a neuron morphology, a segment, a section, or even a tree.
- Parameters:
idx (Union[int, str]) – The index of the node.
parent_idx (Union[int, str]) – The index of the parent node.
- property parent¶
- property is_root: bool¶
Check if the node is the root of the tree.
- Returns:
True if the node is the root, False otherwise.
- Return type:
bool
- property topological_type: str¶
The topological type of the node based on the number of children.
- Returns:
The topological type of the node: ‘continuation’, ‘bifurcation’, or ‘termination’.
- Return type:
str
- property subtree: list¶
Gets the subtree of the node (including the node itself) using an iterative depth-first traversal.
- Returns:
A list of nodes in the subtree.
- Return type:
list
- property subtree_size¶
Gets the size of the subtree of the node.
- Returns:
The size of the subtree of the node.
- Return type:
int
- property depth¶
Computes the depth of the node in the tree iteratively.
- property siblings¶
Gets the siblings of the node.
- Returns:
A list of nodes that share the same parent as the node.
- Return type:
list
- property nearest_neighbours¶
Gets the nearest neighbours of the node.
- Returns:
A list of nodes that share the same parent or children as the node.
- Return type:
list
- property ancestors¶
Get all ancestors of this node up to the root.
- path_to_ancestor(ancestor=None, include_self=True, include_ancestor=True)[source]¶
Get the path from this node to a given ancestor node.
- Parameters:
ancestor (Node, optional) – The ancestor node to get the path to. If None, the path to the root is returned.
include_self (bool, optional) – Whether to include this node in the path. Defaults to True.
include_ancestor (bool, optional) – Whether to include the ancestor node in the path. Defaults to True.
- Returns:
A list of nodes representing the path from this node to the ancestor node.
- Return type:
list
- path(other, include_self=True, include_other=True, include_ancestor=True)[source]¶
Get the path between this node and another node.
- Parameters:
other (Node) – The other node to get the path to.
include_self (bool) – Whether to include this node in the path.
include_other (bool) – Whether to include the other node in the path.
include_ancestor (bool) – Whether to include the lowest common ancestor (LCA) in the path. Only relevant when self and other are in parallel subtrees, ignored otherwise.
- Returns:
A list of nodes representing the path from this node to the other node.
- Return type:
list
- connect_to_parent(parent)[source]¶
Attach the node to a parent node.
Warning
This method should not be used directly when working with trees as it doesn’t add the node to the tree’s list of nodes. Use the Tree class to insert nodes into the tree.
- Args:
parent (Node): The parent node to attach the node to.
- class dendrotweaks.morphology.trees.Tree(nodes: list)[source]¶
Bases:
objectA class to represent a tree data structure.
A tree graph is a hierarchical data structure that consists of nodes connected by edges.
- Parameters:
nodes (list) – A list of nodes in the tree.
- property is_connected¶
Whether the tree is connected i.e. each node can be reached from the root.
- Returns:
bool – True if the root node’s subtree contains exactly the same nodes
as the entire tree. False otherwise.
- property is_sorted¶
Whether the nodes in the tree are sorted by index.
- Returns:
True if the nodes are sorted by index. False otherwise.
- Return type:
bool
- property bifurcations¶
The bifurcation nodes in the tree.
- Returns:
A list of bifurcation nodes in the tree.
- Return type:
list
- property terminations¶
- property edges: list¶
Returns a list of edges in the tree.
- traverse(root=None)[source]¶
Iterate over the nodes in the tree using a stack-based depth-first traversal.
- Parameters:
root (Node, optional) – The root node to start the traversal from. Defaults to None.
- sort(sort_children=True, force=False)[source]¶
Sort the nodes in the tree using a stack-based depth-first traversal.
- Parameters:
sort_children (bool, optional) – Whether to sort the children of each node based on the number of bifurcations in their subtrees. Defaults to True.
force (bool, optional) – Whether to force the sorting of the tree even if it is already sorted. Defaults to False.
- remove_node(node)[source]¶
Remove a node from the tree.
- Parameters:
node (Node) – The node to remove.
- Raises:
ValueError – If the tree is not sorted.
- remove_subtree(node)[source]¶
Remove a subtree from the tree.
- Parameters:
node (Node) – The root node of the subtree to remove.