Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple dicot pipeline #71

Merged
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.h5 filter=lfs diff=lfs merge=lfs -text
*.slp filter=lfs diff=lfs merge=lfs -text
*.type filter=lfs diff=lfs merge=lfs -text
3 changes: 2 additions & 1 deletion sleap_roots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
TraitDef,
YoungerMonocotPipeline,
OlderMonocotPipeline,
MultipleDicotPipeline,
)
from sleap_roots.series import Series, find_all_series

# Define package version.
# This is read dynamically by setuptools in pyproject.toml to determine the release version.
__version__ = "0.0.6"
__version__ = "0.0.7"
46 changes: 38 additions & 8 deletions sleap_roots/lengths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,44 @@

import numpy as np
from typing import Union
from shapely.geometry import LineString


def get_max_length_pts(pts: np.ndarray) -> np.ndarray:
"""Points of the root with maximum length (intended for primary root traits).

Args:
pts: Root landmarks as array of shape `(instances, nodes, 2)`.
pts: Root landmarks as array of shape `(instances, nodes, 2)` or `(nodes, 2)`.

Returns:
np.ndarray: Array of points with shape `(nodes, 2)` from the root with maximum
length.
length, or the input array unchanged if its shape is `(nodes, 2)`.
"""
# Return the input array unchanged if its shape is (nodes, 2)
if pts.ndim == 2 and pts.shape[1] == 2:
return pts

# Return NaN points if the input array is empty
if len(pts) == 0:
return np.array([[np.nan, np.nan]])

# Check if pts has the correct shape, raise error if it does not
# Check if pts has the correct shape for processing multiple instances
if pts.ndim != 3 or pts.shape[2] != 2:
raise ValueError("Input array should have shape (instances, nodes, 2)")
raise ValueError(
"Input array should have shape (instances, nodes, 2) for multiple instances"
)

# Calculate the differences between consecutive points in each root
segment_diffs = np.diff(pts, axis=1)

# Calculate the length of each segment (the Euclidean distance between consecutive
# points)
# Calculate the length of each segment
segment_lengths = np.linalg.norm(segment_diffs, axis=-1)

# Sum the lengths of the segments for each root
total_lengths = np.nansum(segment_lengths, axis=-1)

# Handle roots where all segment lengths are NaN, recording NaN in place of the
# total length for these roots
# Handle roots where all segment lengths are NaN,
# recording NaN in place of the total length for these roots
total_lengths[np.isnan(segment_lengths).all(axis=-1)] = np.nan

# Return NaN points if all total lengths are NaN
Expand Down Expand Up @@ -128,3 +134,27 @@ def get_curve_index(
return curve_index.item()
else:
return curve_index


def get_min_distance_line_to_line(line1: LineString, line2: LineString) -> float:
"""Calculate the minimum distance between two LineString objects.

This function computes the shortest distance between any two points on the first
line segment and the second line segment. If the lines intersect, the minimum
distance is zero. The distance is calculated in the same units as the coordinates
of the LineStrings.

Args:
line1: The first LineString object representing a line segment.
line2: The second LineString object representing a line segment.

Returns:
The minimum distance between the two line segments.
"""
# Check if the inputs are LineString instances
if not isinstance(line1, LineString):
raise TypeError("The first argument must be a LineString object.")
if not isinstance(line2, LineString):
raise TypeError("The second argument must be a LineString object.")

return line1.distance(line2)
Loading
Loading