Skip to content

mmetrics

Get child->parent distances for skeleton nodes.

PARAMETER DESCRIPTION
x

TYPE: TreeNeuron | node table

root_dist
    `parent_dist` for the root's row. Set to `None`, to leave
    at `NaN` or e.g. to `0` to set to 0.

TYPE: int | None DEFAULT: None

RETURNS DESCRIPTION
np.ndarray

Array with distances in same order and size as node table.

Source code in navis/morpho/mmetrics.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def parent_dist(
    x: Union["core.TreeNeuron", pd.DataFrame], root_dist: Optional[int] = None
) -> None:
    """Get child->parent distances for skeleton nodes.

    Parameters
    ----------
    x :         TreeNeuron | node table
    root_dist : int | None
                `parent_dist` for the root's row. Set to `None`, to leave
                at `NaN` or e.g. to `0` to set to 0.

    Returns
    -------
    np.ndarray
                Array with distances in same order and size as node table.

    """
    if isinstance(x, core.TreeNeuron):
        nodes = x.nodes
    elif isinstance(x, pd.DataFrame):
        nodes = x
    else:
        raise TypeError(f'Need TreeNeuron or DataFrame, got "{type(x)}"')

    if not utils.fastcore:
        # Extract node coordinates
        tn_coords = nodes[["x", "y", "z"]].values

        # Get parent coordinates
        parent_coords = (
            nodes.set_index("node_id")
            .reindex(nodes.parent_id.values)[["x", "y", "z"]]
            .values
        )

        # Calculate distances between nodes and their parents
        w = np.sqrt(np.sum((tn_coords - parent_coords) ** 2, axis=1))

        # Replace root dist (nan by default)
        w[np.isnan(w)] = root_dist
    else:
        w = utils.fastcore.dag.parent_dist(
            x.nodes.node_id.values,
            x.nodes.parent_id.values,
            x.nodes[["x", "y", "z"]].values,
            root_dist=root_dist,
        )

    return w