Lengths¶
Overview¶
The lengths module provides functions for computing root length measurements from point coordinates. These are fundamental traits used across all pipelines.
Key functions:
- get_root_lengths - Calculate total root lengths
- get_curve_index - Measure root curvature
- get_max_length_pts - Find longest root path
When to use: - Computing primary or lateral root lengths - Measuring root curvature and tortuosity - Comparing root lengths across treatments
Quick Example¶
import sleap_roots as sr
import numpy as np
# Get root points
series = sr.Series.load("plant", primary_path="primary.slp")
pts = series.get_primary_points() # (plants, frames, nodes, 2)
# Compute lengths
lengths = sr.get_root_lengths(pts)
print(f"Primary root length: {lengths[0, 0]:.2f} pixels")
# Compute curvature
curve_idx = sr.get_curve_index(pts)
print(f"Curvature index: {curve_idx[0, 0]:.3f}")
API Reference¶
get_root_lengths¶
get_root_lengths ¶
Return root lengths for all roots in a frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pts
|
ndarray
|
Root landmarks as array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of root lengths of shape |
ndarray
|
is one point only (all of the rest of the points are NaNs), an array of NaNs |
ndarray
|
with shape (len(pts),) is returned. This is also the case for non-contiguous |
ndarray
|
points. |
Source code in sleap_roots/lengths.py
Example:
import sleap_roots as sr
import numpy as np
# Load and extract points
series = sr.Series.load("plant", primary_path="primary.slp")
primary_pts = series.get_primary_points()
# Calculate lengths
lengths = sr.get_root_lengths(primary_pts)
# Results shape: (plants, frames)
print(f"Shape: {lengths.shape}")
print(f"Frame 0 length: {lengths[0, 0]:.2f} px")
# Handle NaN (missing data)
valid_lengths = lengths[~np.isnan(lengths)]
print(f"Mean length: {np.mean(valid_lengths):.2f} px")
See Also: - get_curve_index - Root curvature - DicotPipeline - Uses this function
get_curve_index¶
get_curve_index ¶
get_curve_index(
lengths: Union[float, ndarray],
base_tip_dists: Union[float, ndarray],
) -> Union[float, ndarray]
Calculate the curvature index of a root.
The curvature index quantifies the curviness of the root's growth. A higher curvature index indicates a curvier root (less responsive to gravity), while a lower index indicates a straighter root (more responsive to gravity). The index is computed as the difference between the maximum root length and straight-line distance from the base to the tip of the root, normalized by the root length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lengths
|
Union[float, ndarray]
|
Maximum length of the root(s). Can be a scalar or a 1D numpy array
of shape |
required |
base_tip_dists
|
Union[float, ndarray]
|
The straight-line distance from the base to the tip of the
root(s). Can be a scalar or a 1D numpy array of shape |
required |
Returns:
| Type | Description |
|---|---|
Union[float, ndarray]
|
Curvature index of the root(s), quantifying its/their curviness. Will be a
scalar if input is scalar, or a 1D numpy array of shape |
Source code in sleap_roots/lengths.py
Example:
import sleap_roots as sr
series = sr.Series.load("plant", primary_path="primary.slp")
pts = series.get_primary_points()
# Compute curvature index
curve_idx = sr.get_curve_index(pts)
# Lower values = straighter root
# Higher values = more curved/tortuous root
print(f"Curvature index: {curve_idx[0, 0]:.3f}")
get_max_length_pts¶
get_max_length_pts ¶
Points of the root with maximum length (intended for primary root traits).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pts
|
ndarray
|
Root landmarks as array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Array of points with shape |
ndarray
|
length, or the input array unchanged if its shape is |
Source code in sleap_roots/lengths.py
Example:
import sleap_roots as sr
series = sr.Series.load("plant", lateral_path="lateral.slp")
lateral_pts_list = series.get_lateral_points()
# Find longest lateral root
max_pts = sr.get_max_length_pts(lateral_pts_list)
max_length = sr.get_root_lengths(max_pts[np.newaxis, ...])[0, 0]
print(f"Longest lateral: {max_length:.2f} px")
Related Modules¶
- Angles - Root angle measurements
- Network Length - Whole-plant length metrics
- Pipelines - Use length functions