-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix new older monocot traits for edge cases #77
Changes from all commits
35272e7
93741fc
dd4efaf
5033de5
5a51eab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,7 +4,10 @@ | |||||
from scipy.spatial import ConvexHull | ||||||
from scipy.spatial.distance import pdist | ||||||
from typing import Tuple, Optional, Union | ||||||
from sleap_roots.points import get_line_equation_from_points | ||||||
from sleap_roots.points import ( | ||||||
extract_points_from_geometry, | ||||||
get_line_equation_from_points, | ||||||
) | ||||||
from shapely import box, LineString, normalize, Polygon | ||||||
|
||||||
|
||||||
|
@@ -382,13 +385,9 @@ | |||||
# Find the intersection between the hull perimeter and the extended line | ||||||
intersection = extended_line.intersection(hull_perimeter) | ||||||
|
||||||
# Add intersection points to both lists | ||||||
# Compute the intersection points and add to lists | ||||||
if not intersection.is_empty: | ||||||
intersect_points = ( | ||||||
np.array([[point.x, point.y] for point in intersection.geoms]) | ||||||
if intersection.geom_type == "MultiPoint" | ||||||
else np.array([[intersection.x, intersection.y]]) | ||||||
) | ||||||
intersect_points = extract_points_from_geometry(intersection) | ||||||
above_line.extend(intersect_points) | ||||||
below_line.extend(intersect_points) | ||||||
|
||||||
|
@@ -452,6 +451,12 @@ | |||||
Raises: | ||||||
ValueError: If pts does not have the expected shape. | ||||||
""" | ||||||
if r0_pts.ndim == 1 or rn_pts.ndim == 1 or pts.ndim == 2: | ||||||
print( | ||||||
"Not enough instances or incorrect format to compute convex hull intersections." | ||||||
) | ||||||
return (np.array([[np.nan, np.nan]]), np.array([[np.nan, np.nan]])) | ||||||
|
||||||
# Check for valid pts input | ||||||
if not isinstance(pts, np.ndarray) or pts.ndim != 3 or pts.shape[-1] != 2: | ||||||
raise ValueError("pts must be a numpy array of shape (instances, nodes, 2).") | ||||||
Comment on lines
451
to
462
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ensure proper input validation. - if r0_pts.ndim == 1 or rn_pts.ndim == 1 or pts.ndim == 2:
+ if r0_pts.ndim != 2 or rn_pts.ndim != 2 or pts.ndim != 3: The condition for checking the dimensions of the input arrays should ensure that they meet the expected dimensions explicitly, rather than checking for what they should not be. This makes the validation more robust and understandable. |
||||||
|
@@ -460,7 +465,7 @@ | |||||
raise ValueError("rn_pts must be a numpy array of shape (instances, 2).") | ||||||
# Ensure r0_pts is a numpy array of shape (instances, 2) | ||||||
if not isinstance(r0_pts, np.ndarray) or r0_pts.ndim != 2 or r0_pts.shape[-1] != 2: | ||||||
raise ValueError("r0_pts must be a numpy array of shape (instances, 2).") | ||||||
raise ValueError(f"r0_pts must be a numpy array of shape (instances, 2).") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary f-string. - raise ValueError(f"r0_pts must be a numpy array of shape (instances, 2).")
+ raise ValueError("r0_pts must be a numpy array of shape (instances, 2).") The f-string in this line does not contain any placeholders, making it unnecessary. Replace it with a regular string to avoid confusion and potential errors in future modifications. Committable suggestion
Suggested change
|
||||||
|
||||||
# Flatten pts to 2D array and remove NaN values | ||||||
flattened_pts = pts.reshape(-1, 2) | ||||||
|
@@ -481,6 +486,9 @@ | |||||
|
||||||
# Ensuring r0_pts does not contain NaN values | ||||||
r0_pts_valid = r0_pts[~np.isnan(r0_pts).any(axis=1)] | ||||||
# Expect two vectors in the end | ||||||
if len(r0_pts_valid) < 2: | ||||||
return (np.array([[np.nan, np.nan]]), np.array([[np.nan, np.nan]])) | ||||||
|
||||||
# Get the vertices of the convex hull | ||||||
hull_vertices = hull.points[hull.vertices] | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,14 @@ | ||||||||||||||||||
import numpy as np | ||||||||||||||||||
import pytest | ||||||||||||||||||
from shapely.geometry import LineString | ||||||||||||||||||
from shapely.geometry import Point, MultiPoint, LineString, GeometryCollection | ||||||||||||||||||
from sleap_roots import Series | ||||||||||||||||||
from sleap_roots.lengths import get_max_length_pts | ||||||||||||||||||
from sleap_roots.points import filter_plants_with_unexpected_ct, get_count, join_pts | ||||||||||||||||||
from sleap_roots.points import ( | ||||||||||||||||||
extract_points_from_geometry, | ||||||||||||||||||
filter_plants_with_unexpected_ct, | ||||||||||||||||||
get_count, | ||||||||||||||||||
join_pts, | ||||||||||||||||||
) | ||||||||||||||||||
from sleap_roots.points import ( | ||||||||||||||||||
get_all_pts_array, | ||||||||||||||||||
get_nodes, | ||||||||||||||||||
|
@@ -738,3 +743,55 @@ def test_filter_plants_with_unexpected_ct_incorrect_input_types(): | |||||||||||||||||
expected_count = "not a float" | ||||||||||||||||||
with pytest.raises(ValueError): | ||||||||||||||||||
filter_plants_with_unexpected_ct(primary_pts, lateral_pts, expected_count) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_point(): | ||||||||||||||||||
point = Point(1, 2) | ||||||||||||||||||
expected = [np.array([1, 2])] | ||||||||||||||||||
assert np.array_equal(extract_points_from_geometry(point), expected) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_multipoint(): | ||||||||||||||||||
multipoint = MultiPoint([(1, 2), (3, 4)]) | ||||||||||||||||||
expected = [np.array([1, 2]), np.array([3, 4])] | ||||||||||||||||||
results = extract_points_from_geometry(multipoint) | ||||||||||||||||||
assert all(np.array_equal(result, exp) for result, exp in zip(results, expected)) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_linestring(): | ||||||||||||||||||
linestring = LineString([(0, 0), (1, 1), (2, 2)]) | ||||||||||||||||||
expected = [np.array([0, 0]), np.array([1, 1]), np.array([2, 2])] | ||||||||||||||||||
results = extract_points_from_geometry(linestring) | ||||||||||||||||||
assert all(np.array_equal(result, exp) for result, exp in zip(results, expected)) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_geometrycollection(): | ||||||||||||||||||
geom_collection = GeometryCollection([Point(1, 2), LineString([(0, 0), (1, 1)])]) | ||||||||||||||||||
expected = [np.array([1, 2]), np.array([0, 0]), np.array([1, 1])] | ||||||||||||||||||
results = extract_points_from_geometry(geom_collection) | ||||||||||||||||||
assert all(np.array_equal(result, exp) for result, exp in zip(results, expected)) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_empty_multipoint(): | ||||||||||||||||||
empty_multipoint = MultiPoint() | ||||||||||||||||||
expected = [] | ||||||||||||||||||
assert extract_points_from_geometry(empty_multipoint) == expected | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_empty_linestring(): | ||||||||||||||||||
empty_linestring = LineString() | ||||||||||||||||||
expected = [] | ||||||||||||||||||
assert extract_points_from_geometry(empty_linestring) == expected | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_unsupported_type(): | ||||||||||||||||||
with pytest.raises(NameError): | ||||||||||||||||||
extract_points_from_geometry( | ||||||||||||||||||
Polygon([(0, 0), (1, 1), (1, 0)]) | ||||||||||||||||||
) # Polygon is unsupported | ||||||||||||||||||
Comment on lines
+787
to
+791
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle unsupported geometry type correctly. - extract_points_from_geometry(Polygon([(0, 0), (1, 1), (1, 0)])) # Polygon is unsupported
+ # Polygon is unsupported, consider adding support or handling this case differently. Committable suggestion
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def test_extract_from_empty_geometrycollection(): | ||||||||||||||||||
empty_geom_collection = GeometryCollection() | ||||||||||||||||||
expected = [] | ||||||||||||||||||
assert extract_points_from_geometry(empty_geom_collection) == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused imports detected.
Remove the unused imports
box
,normalize
, andPolygon
to clean up the code and avoid potential confusion about their necessity.Committable suggestion