fix: prevent floating-point truncation in spatial location indexing - #122
fix: prevent floating-point truncation in spatial location indexing#122tee2s wants to merge 1 commit into
Conversation
Floating-point precision errors in (location + x).long() can produce values like -0.99999994 instead of -1.0. Because PyTorch's .long() truncates toward zero, index values shift by an entire grid cell. Decouple integer grid indices from the floating-point distance calculations by computing locations via torch.arange(...) + int(location).
|
Hello @tee2s and thank you for this clear explanation of the problem and clean fix. I hope that the bug did not cause problems for you. I am in the final stages of major revision of Deepwave. One of the areas that has received the most attention is this location interpolation feature. In addition to issues such as the one you identified, I think the current implementation was also unnecessarily complicated for users, especially for the staggered grid propagators (acoustic and elastic). This issue is already fixed in the revision, but I will add your new test, which I am grateful for. Thank you again. I hope that you continue to find Deepwave useful. |
|
Hi @ar4 , thanks for the fast reply! Glad to hear this is already resolved in the new revision. I really appreciate your work on Deepwave, it’s been a great tool to use. Also, this bug didn't cause any major issues for me! |
Summary
Fixes a spatial grid indexing bug where floating-point rounding errors cause location indices to drop/shift by an entire grid cell during interpolation.
Root Cause
In location_interpolation.py, locations is derived directly from floating-point distances via (location + x).long(). Floating-point inaccuracies can yield values like -0.99999994 instead of -1.0. Because PyTorch’s .long() truncates toward zero rather than flooring, the resulting index truncates to 0 instead of -1.
Fix
Decouple integer grid indices from the floating-point distance tensor x. Calculate locations directly using integer offsets via torch.arange(...) + int(location).
Minimal Reproduction