Skip to content

Improves 3D interpolation and adds KD-tree-based alternative to the existing Delaunay method - #149

Merged
aleaf merged 4 commits into
DOI-USGS:developfrom
ntdosch:speedup_interp_weights
Oct 27, 2026
Merged

aleaf merged 4 commits into
DOI-USGS:developfrom
ntdosch:speedup_interp_weights

Conversation

@ntdosch

@ntdosch ntdosch commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Improves 3D interpolation in mfsetup.interpolate and adds a KD-tree-based alternative to the existing Delaunay method, with accompanying tests and documentation. This is helpful for large 3D parent grids whose vertical dimension is small relative to their horizontal extent, where the Delaunay triangulation can become ill-conditioned.

Fixes #140

This PR implements the rescaling described in #140, but also goes beyond the original issue scope by also adding an optional KD-tree inverse-distance interpolation method as an alternative for the same high-aspect-ratio grids.

Changes

mfsetup/interpolate.py

  • 3D coordinate rescaling in interp_weights. For d == 3, coordinates are now centered at their mean and divided by their per-dimension peak-to-peak range (matching scipy.interpolate.griddata(rescale=True)) before the Delaunay triangulation. Dimensions with zero range are left unscaled to avoid division by zero. Because the same scaling is applied to both source and destination points, the interpolation remains equivalent while making the tessellation tractable. The 2D path is unchanged.
  • New interp_weights_kdtree(xyz, uvw, d, k). Computes weights from the k nearest source points using a scipy.spatial.cKDTree with inverse-distance weighting. k defaults to d + 1 (the vertex count of a simplex). Coordinates are rescaled the same way as above. Includes handling for exact matches (distance 0 gets full weight) and the k == 1 nearest-neighbor case. Returns indices and normalized weights that sum to 1 per row.
  • Interpolator options. Added weight_method ('delaunay' default, or 'kdtree') and k arguments that route interp_weights to the appropriate function, with weight caching unchanged.
  • interpolate docstring notes that fill_value is unused for KD-tree weights (all weights are positive).

mfsetup/tests/test_interpolation.py

  • test_interp_weights_3d_rescaling: verifies the 3D interp_weights output matches griddata(rescale=True) on a high-aspect-ratio point cloud, and confirms this differs from the unscaled (rescale=False) result so the assertion is meaningful.
  • KD-tree tests: validity of shapes/weights (default k = d + 1), the exact-match path, the k == 1 nearest-neighbor path, and routing Interpolator(weight_method='kdtree').

docs/source/concepts/perimeter-bcs.rst

  • New "Choosing an interpolation method" subsection documenting interpolation_method ('linear' vs 'kdtree') and interpolation_k, with a YAML example.
  • Note that interpolation_k: 1 gives pure nearest-neighbor interpolation and that interpolation_k must be a positive integer.
  • Updated the interpolation bullet in "Features and Limitations" to mention both methods.

Notes

  • Default behavior is unchanged: weight_method defaults to 'delaunay' and the 2D path is untouched, so existing usage should produce the same results.

@codecov-commenter

codecov-commenter commented Oct 20, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 97.24138% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.60%. Comparing base (8571531) to head (e680141).

Files with missing lines Patch % Lines
mfsetup/interpolate.py 92.59% 4 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #149      +/-   ##
===========================================
+ Coverage    89.48%   89.60%   +0.12%     
===========================================
  Files           67       67              
  Lines        13200    13339     +139     
===========================================
+ Hits         11812    11953     +141     
+ Misses        1388     1386       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aleaf
aleaf merged commit 92f4049 into DOI-USGS:develop Oct 27, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Normalize 3D coordinates in interp_weights to improve Delaunay triangulation performance

3 participants