Tree graph¶
- class dendrotweaks.morphology.trees.Node(idx: int | str, parent_idx: int | str)[source]¶
Bases:
object
Represents 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.
Examples –
>>> node = Node(0, -1) >>> node •0
- property parent¶
- 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
- 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:
object
A 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.