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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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