Skip to content

Reject underflowing sigma_spatial in bilateral() (#1390)#1392

Merged
brendancol merged 1 commit into
mainfrom
issue-1390
Apr 30, 2026
Merged

Reject underflowing sigma_spatial in bilateral() (#1390)#1392
brendancol merged 1 commit into
mainfrom
issue-1390

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

Fixes #1390. bilateral() validated sigma_spatial > 0 only. Values small enough that 2 * sigma**2 underflows to a denormal or to zero made 1/(2*sigma**2) either +inf (exp(-dist2 * inf) = 0 * inf = NaN, all-NaN output) or raised ZeroDivisionError under numba. This is the MEDIUM-severity Cat 3 finding deferred from PR #1238.

The bound on both sigma_spatial and sigma_range is tightened to >= sqrt(np.finfo(float64).tiny) (~1.49e-154), the smallest value that keeps the squared term a normal float64. Realistic sigmas are unaffected; nonsense values now raise ValueError naming the parameter.

The same threshold is applied to sigma_range for symmetry, since the same underflow occurs in inv_2_sr.

Test plan

  • pytest xrspatial/tests/test_bilateral.py -- 22 passed (19 prior + 3 new)
  • test_bilateral_rejects_underflow_sigma_spatial covers sigma_spatial=1e-200 (would ZeroDivisionError), 1e-160 (would all-NaN), and sigma_range=1e-200
  • test_bilateral_rejects_zero_sigma regresses the previous > 0 rejection for sigma_spatial=0 and sigma_range=0
  • test_bilateral_accepts_normal_sigma confirms sigma_spatial=sigma_range=1.0 still produces a finite result

`bilateral()` validated `sigma_spatial > 0` only.  At values below
`sqrt(np.finfo(float64).tiny)` (~1.49e-154) the term `2 * sigma**2`
underflows to zero or a denormal, so `1/(2*sigma**2)` is `+inf` (or
raises ZeroDivisionError under numba) and `exp(-dist2 * inf) = 0 * inf
= NaN` propagates through the whole output.

Tighten the bound on `sigma_spatial` and `sigma_range` to
`>= sqrt(tiny)` and update the docstring.  Add three tests covering
the underflow-NaN and underflow-ZeroDivision cases, the zero-sigma
regression, and a realistic positive case.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 30, 2026
@brendancol brendancol merged commit 48da8e7 into main Apr 30, 2026
11 checks passed
@brendancol brendancol deleted the issue-1390 branch May 4, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bilateral: sigma_spatial underflow produces NaN at center pixel

1 participant