BUG: fix NaN in ND linear interpolation outside convex hull#969
BUG: fix NaN in ND linear interpolation outside convex hull#969YuriCastroDev wants to merge 4 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:
|
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. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
phmbressan
left a comment
There was a problem hiding this comment.
Good implementation. Much nicer than my initial idea of computing the interpolation and, with a mask, forwarding any existing NaNs to extrapolate. This is more mathematically sound and likely faster.
About the linter issue, it seems a false positive so I would simply disable that line:
from scipy.spatial import Delaunay # pylint: disable=no-name-in-module|
I'm available to keep contributing, if you have any open issues or tasks that need help, I'd appreciate any guidance on where to focus next! |
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.