Point tree

class dendrotweaks.morphology.point_trees.Point(idx: str, type_idx: int, x: float, y: float, z: float, r: float, parent_idx: str)[source]

Bases: Node

A class representing a single point in a morphological reconstruction.

Parameters:
  • idx (str) – The unique identifier of the node.

  • type_idx (int) – The type of the node according to the SWC specification (e.g. soma-1, axon-2, dendrite-3).

  • x (float) – The x-coordinate of the node.

  • y (float) – The y-coordinate of the node.

  • z (float) – The z-coordinate of the node.

  • r (float) – The radius of the node.

  • parent_idx (str) – The identifier of the parent node.

idx

The unique identifier of the node.

Type:

str

type_idx

The type of the node according to the SWC specification (e.g. soma-1, axon-2, dendrite-3).

Type:

int

x

The x-coordinate of the node.

Type:

float

y

The y-coordinate of the node.

Type:

float

z

The z-coordinate of the node.

Type:

float

r

The radius of the node.

Type:

float

parent_idx

The identifier of the parent node.

Type:

str

property domain

The morphological or functional domain of the node.

property distance_to_parent

The Euclidean distance from this node to its parent.

path_distance(within_domain=False, ancestor=None)[source]

Compute the distance from this node to an ancestor node.

Parameters:
  • within_domain (bool) – If True, stops when domain changes.

  • ancestor (Node, optional) – If provided, stops at this specific ancestor.

Returns:

The accumulated distance.

Return type:

float

property df

Return a DataFrame representation of the node.

info()[source]

Print information about the node.

copy()[source]

Create a copy of the node.

Returns:

A copy of the node with the same attributes.

Return type:

Point

overlaps_with(other, **kwargs) bool[source]

Check if the coordinates of this node overlap with another node.

Parameters:
  • other (Point) – The other node to compare with.

  • kwargs – Additional keyword arguments passed to np.allclose.

Returns:

True if the coordinates overlap, False otherwise.

Return type:

bool

connect_to_parent(parent)

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.

property depth

Computes the depth of the node in the tree iteratively.

disconnect_from_parent()

Detach the node from its parent.

Examples

for child in node.children: child.disconnect_from_parent()

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 parent
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 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 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

class dendrotweaks.morphology.point_trees.PointTree(nodes: list[Point])[source]

Bases: Tree

A class representing a tree graph of points in a morphological reconstruction.

Parameters:

nodes (list[Point]) – A list of points in the tree.

property points

The list of points in the tree. An alias for self._nodes.

property soma_points

The list of points representing the soma (type 1).

property soma_center

The center of the soma as the average of the coordinates of the soma points.

property apical_center

The center of the apical dendrite as the average of the coordinates of the apical points.

property soma_notation

The type of soma notation used in the tree. - ‘1PS’: One-point soma - ‘2PS’: Two-point soma - ‘3PS’: Three-point soma - ‘contour’: Soma represented as a contour

property df

A DataFrame representation of the tree.

change_soma_notation(notation)[source]

Convert the soma to 3PS notation.

round_coordinates(decimals=8)[source]

Round the coordinates of all nodes to the specified number of decimals.

Parameters:

decimals (int, optional) – The number of decimals to round to, by default

shift_coordinates_to_soma_center()[source]

Shift all coordinates so that the soma center is at the origin (0, 0, 0).

rotate(**kwargs)
align_apical_dendrite(axis='Y', facing='up')[source]

Align the apical dendrite with the specified axis.

Parameters:
  • axis (str, optional) – The axis to align the apical dendrite with (‘X’, ‘Y’, or ‘Z’), by default ‘Y’.

  • facing (str, optional) – The direction the apical dendrite should face (‘up’ or ‘down’), by default ‘up’.

remove_overlaps()[source]

Remove overlapping nodes from the tree.

extend_sections()[source]

Extend each section by adding a node in the beginning overlapping with the parent node for geometrical continuity.

to_swc(path_to_file)[source]

Save the tree to an SWC file.

plot(ax=None, show_nodes=True, show_edges=True, show_domains=True, annotate=False, projection='XY', highlight_nodes=None, focus_nodes=None)[source]

Plot a 2D projection of the tree.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – The axes to plot on, by default None

  • show_nodes (bool, optional) – Whether to plot the nodes, by default True

  • show_edges (bool, optional) – Whether to plot the edges, by default True

  • show_domains (bool, optional) – Whether to color the nodes based on their domains, by default True

  • annotate (bool, optional) – Whether to annotate the nodes with their indices, by default False

  • projection (str, optional) – The projection plane (‘XY’, ‘XZ’, or ‘YZ’), by default ‘XY’

  • highlight_nodes (list, optional) – A list of nodes to highlight, by default None

  • focus_nodes (list, optional) – A list of nodes to focus on, by default None

add_subtree(node, parent)

Add a subtree to the tree.

Parameters:
  • node (Node) – The root node of the subtree to add.

  • parent (Node) – The parent node to attach the subtree to.

property bifurcations

The bifurcation nodes in the tree.

Returns:

A list of bifurcation nodes in the tree.

Return type:

list

property edges: list

Returns a list of edges in the tree.

Returns:

A list of edges in the tree.

Return type:

list[tuple[Node, Node]]

insert_node_after(new_node, existing_node)

Insert a node after a given node in the tree.

Parameters:
  • new_node (Node) – The new node to insert.

  • existing_node (Node) – The existing node after which to insert the new node.

insert_node_before(new_node, existing_node)

Insert a node before a given node in the tree.

Parameters:
  • new_node (Node) – The new node to insert.

  • existing_node (Node) – The existing node before which to insert the new node.

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

plot_radii_distribution(ax=None, highlight=None, domains=True, show_soma=False)[source]
remove_node(node)

Remove a node from the tree.

Parameters:

node (Node) – The node to remove.

Raises:

ValueError – If the tree is not sorted.

remove_subtree(node)

Remove a subtree from the tree.

Parameters:

node (Node) – The root node of the subtree to remove.

reposition_subtree(node, new_parent_node, origin=None)

Reposition a subtree in the tree.

Parameters:
  • node (Node) – The root node of the subtree to reposition.

  • new_parent_node (Node) – The new parent node of the subtree.

  • origin (Node, optional) – The origin node to use as the reference point for the repositioning. Defaults to None.

Note

Treats differently the children of the root node.

sort(sort_children=True, force=False)

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.

property terminations
topology()

Print the topology of the tree with a visual tree structure.

traverse(root=None)

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.