BUG: fix NaN in ND linear interpolation outside convex hull#969
Open
YuriCastroDev wants to merge 2 commits into
Open
BUG: fix NaN in ND linear interpolation outside convex hull#969YuriCastroDev wants to merge 2 commits into
YuriCastroDev wants to merge 2 commits into
Conversation
Use Delaunay.find_simplex() instead of bounding box check in __get_value_opt_nd to correctly detect out-of-domain points. Closes RocketPy-Team#926
Use Delaunay.find_simplex() instead of bounding box check in __get_value_opt_nd to correctly detect out-of-domain points. Closes RocketPy-Team#926
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #969 +/- ##
===========================================
+ Coverage 80.27% 81.12% +0.85%
===========================================
Files 104 113 +9
Lines 12769 14560 +1791
===========================================
+ Hits 10250 11812 +1562
- Misses 2519 2748 +229 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in rocketpy.mathutils.Function where N-D interpolation="linear" could return NaN for query points that are inside the axis-aligned bounding box but outside the Delaunay convex hull, by using the triangulation to correctly route those points to extrapolation.
Changes:
- Build and cache a
scipy.spatial.Delaunaytriangulation for N-D linear interpolation and reuse it for convex-hull membership checks. - Update N-D fast evaluation to detect “outside hull” points via
find_simplex(...) < 0instead of bounding-box checks. - Add a regression unit test for the NaN case and document the fix in the changelog.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
rocketpy/mathutils/function.py |
Cache Delaunay triangulation for N-D linear interpolation and use it to decide interpolation vs extrapolation. |
tests/unit/mathutils/test_function.py |
Adds regression test ensuring no NaN is returned for bounding-box-but-outside-hull queries. |
CHANGELOG.md |
Notes the bug fix under Unreleased/Fixed. |
Comment on lines
+833
to
+837
| if hasattr(self, "_nd_triangulation"): | ||
| extrap = self._nd_triangulation.find_simplex(args) < 0 | ||
| else: | ||
| lower, upper = args < min_domain, args > max_domain | ||
| extrap = np.logical_or(lower.any(axis=1), upper.any(axis=1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request type
Checklist
black rocketpy/ tests/) has passed locallypytest tests -m slow --runslow) have passed locallyCHANGELOG.mdhas been updated (if relevant)Current behavior
Closes #926.
When using
Function(..., interpolation="linear")with N-D data,querying a point inside the axis-aligned bounding box but outside
the convex hull silently returns
nanwith no warning.New behavior
f(0.3, 0.3)no longer returnsnan. Points outside the convexhull are correctly identified and routed to the extrapolation method.
Breaking change
Additional information
__get_value_opt_ndused per-axis min/max (bounding box) to decidebetween interpolation and extrapolation.
LinearNDInterpolatoroperates on the Delaunay triangulation (convex hull), not the
bounding box — points in the gap between the two were silently
sent to the interpolator and came back as
nan.Fix: compute
Delaunay(self._domain)once at setup, pass it toLinearNDInterpolator(reuses the triangulation at no extra cost),and store it as
self._nd_triangulation. In__get_value_opt_nd,replace the bounding box check with
find_simplex(args) < 0.Shepard, RBF and other ND interpolators are unaffected.