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

Implement older monocot pipeline #54

Merged
merged 50 commits into from
Mar 11, 2024
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3e841ec
Fix docstring and `get_network_distribution_ratio` arguments
eberrigan Aug 17, 2023
92dae9e
Add `join_pts` and `get_count` functions
eberrigan Aug 20, 2023
9325a1b
Refactor `get_network_distribution`
eberrigan Aug 21, 2023
1621efa
Generalize functions for concatenating and joining points
eberrigan Aug 21, 2023
cb0f13b
Refactor scanline function
eberrigan Aug 21, 2023
e23af0f
Make `OlderMonocotPipeline`
eberrigan Aug 21, 2023
4bc568f
Refactor tests and fix arguments
eberrigan Aug 21, 2023
ff1fb10
Refactor `get_network_length` to take arbitrary number of lengths
eberrigan Aug 23, 2023
57b8fed
Refactor `get_all_pts_array` to simplify
eberrigan Aug 23, 2023
25391b4
Refactor `Series` class to take a arbitrary number of labels
eberrigan Aug 23, 2023
dfabf07
Add 10 DO rice fixture
eberrigan Aug 23, 2023
890df7f
Test refactored functions
eberrigan Aug 23, 2023
4843f08
Add 10 DO rice fixture
eberrigan Aug 23, 2023
0c42ecb
Add test for `OlderMonocot_pipeline`
eberrigan Aug 27, 2023
f33f0e0
Modify `get_grav_index` to take float and array inputs
eberrigan Aug 31, 2023
797561b
Fix docstring in `Series` class
eberrigan Aug 31, 2023
aa59e2b
Test `OlderMonocotPipeline` and `get_grav_index`
eberrigan Aug 31, 2023
bd2621b
Lint
eberrigan Aug 31, 2023
045a7b4
Add folder of 10 do rice test data and test with `find_all_series`
eberrigan Sep 1, 2023
2364b89
Refactor series class and related functions to include crown labels
eberrigan Feb 20, 2024
a92be32
Refactor tests and fixtures
eberrigan Feb 20, 2024
1fceb01
Get rid of uncessary monocots flag
eberrigan Feb 21, 2024
1ab5a39
Refactor tips and bases
eberrigan Feb 21, 2024
11df884
Refactor tips and bases tests
eberrigan Feb 21, 2024
606af51
Merge main with `older_monocot_pipeline` branch
eberrigan Feb 21, 2024
3d1109f
Add standalone get root angle function
eberrigan Feb 27, 2024
57cfbd1
Fix typing
eberrigan Feb 27, 2024
c063a9f
Add checks to convex hull function
eberrigan Feb 27, 2024
29cdb7a
Get rid of obsolete function
eberrigan Feb 27, 2024
113ee3e
Fix network distribution
eberrigan Feb 27, 2024
534d9e6
Add points related functions
eberrigan Feb 27, 2024
b372567
Rename test data
eberrigan Feb 27, 2024
93cfe87
Test new angle functions
eberrigan Feb 27, 2024
7464680
Test new convex hull functions
eberrigan Feb 27, 2024
dd06872
Test new points functions
eberrigan Feb 27, 2024
2279ac0
Rename test data
eberrigan Feb 27, 2024
655d9cc
Fix tests
eberrigan Feb 27, 2024
ef271da
Match shapes
eberrigan Feb 27, 2024
a9054e3
Update trait pipelines
eberrigan Feb 28, 2024
d175680
Test trait pipelines
eberrigan Mar 1, 2024
5efb4fb
Test trait pipelines
eberrigan Mar 1, 2024
2416fd8
Update version
eberrigan Mar 1, 2024
9838314
Resolve conflicts
eberrigan Mar 1, 2024
0c4b4df
Resolve conflicts
eberrigan Mar 1, 2024
1e692b5
resolve
eberrigan Mar 1, 2024
10eb050
Merge branch 'main' into elizabeth/older_monocot_pipeline
eberrigan Mar 1, 2024
264dcca
Fix tips and bases and tests
eberrigan Mar 2, 2024
e38b289
Remove notebooks
eberrigan Mar 2, 2024
880817d
Reformat
eberrigan Mar 2, 2024
83b6ddd
Lint
eberrigan Mar 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 34 additions & 46 deletions sleap_roots/lengths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Get length-related traits."""
import numpy as np
from sleap_roots.bases import get_base_tip_dist
from typing import Optional
from typing import Union


def get_max_length_pts(pts: np.ndarray) -> np.ndarray:
Expand Down Expand Up @@ -114,57 +114,45 @@ def get_root_lengths_max(pts: np.ndarray) -> np.ndarray:


def get_grav_index(
primary_length: Optional[float] = None,
primary_base_tip_dist: Optional[float] = None,
pts: Optional[np.ndarray] = None,
) -> float:
"""Calculate the gravitropism index of a primary root.
lengths: Union[float, np.ndarray],
base_tip_dists: Union[float, np.ndarray],
) -> Union[float, np.ndarray]:
"""
Calculate the gravitropism index of a root.

The gravitropism index quantifies the curviness of the root's growth. A higher
gravitropism 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 primary root length and straight-line
distance from the base to the tip of the primary root, normalized by the root length.
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.

Args:
primary_length: Maximum length of the primary root. Used if `pts` is not
provided.
primary_base_tip_dist: The straight-line distance from the base to the tip of
the primary root. Used if `pts` is not provided.
pts: Landmarks of the primary root of shape `(instances, nodes, 2)`. If
provided, `primary_length` and `primary_base_tip_dist` are ignored.
lengths: Maximum length(s) of the root(s).
eberrigan marked this conversation as resolved.
Show resolved Hide resolved
base_tip_dists: The straight-line distance(s) from the base to the tip of the
root(s).

Returns:
float: Gravitropism index of the primary root, quantifying its curviness.
float or np.ndarray: Gravitropism index of the root(s), quantifying its (their)
curviness.
"""
# Use provided scalar values if available
if primary_length is not None and primary_base_tip_dist is not None:
max_primary_length = primary_length
max_base_tip_distance = primary_base_tip_dist

# Use provided pts array to compute required values if available
elif pts is not None:
if np.isnan(pts).all():
return np.nan
primary_length_max = get_root_lengths_max(pts=pts)
primary_base_tip_dist = get_base_tip_dist(pts=pts)
max_primary_length = np.nanmax(primary_length_max)
max_base_tip_distance = np.nanmax(primary_base_tip_dist)

else:
raise ValueError(
"Either both primary_length and primary_base_tip_dist, or pts"
"must be provided."
)

# Check for invalid values (NaN or zero lengths)
if (
np.isnan(max_primary_length)
or np.isnan(max_base_tip_distance)
or max_primary_length == 0
):
return np.nan

# Calculate and return gravitropism index
grav_index = (max_primary_length - max_base_tip_distance) / max_primary_length
return grav_index
# Convert inputs to NumPy arrays for element-wise operations
lengths = np.asarray(lengths)
base_tip_dists = np.asarray(base_tip_dists)

# Initialize an array to store the gravitropism index, filled with NaN
grav_index = np.full(lengths.shape, np.nan)

# Identify valid and invalid values
valid_values = ~np.isnan(lengths) & ~np.isnan(base_tip_dists) & (lengths != 0)

# Calculate gravitropism index only for valid values
grav_index[valid_values] = (
lengths[valid_values] - base_tip_dists[valid_values]
) / lengths[valid_values]

# If the input was a float, return a float; otherwise, return the NumPy array.
return (
grav_index
if grav_index.size > 1
else (grav_index.item() if valid_values else np.nan)
)