Skip to content

Fix NaN in spectral TV regularizer on flat maps (epsilon_tv=0)#311

Merged
achael merged 1 commit into
dev-backendfrom
fix/reg-tv-spec-nan-safe
Jun 26, 2026
Merged

Fix NaN in spectral TV regularizer on flat maps (epsilon_tv=0)#311
achael merged 1 commit into
dev-backendfrom
fix/reg-tv-spec-nan-safe

Conversation

@rohandahale

Copy link
Copy Markdown
Collaborator

What

reg_tv_spec / reggrad_tv_spec produced NaN on a flat spectral map when epsilon_tv == 0 (the default since #310). A flat coefficient map is exactly the multifrequency imaging start — add_const_mf initialises a constant spectral index, so every neighbour difference is zero and the naive sqrt(0) leaves a 0/0:

  • in the value (the autodiff path under JAX), and
  • in the analytic gradient (the NumPy path).

Downstream this killed the optimizer: an HL Tau mf_order=1 run gave J: nan and reconstructed a flat blob instead of the disk. With the fix it converges to chi2_vis ~ 1 and nxcorr 0.999 vs the exact v1.2.5 result.

Fix

Double-where guard in both functions (autodiff-safe; a flat pixel gets gradient 0, not 0/0). Minimal and self-contained — only multifreq_imager_utils.py is touched, no new shared helper.

Test

New TestSpectralTVFlatMapFinite in test_gradients.py: every tv_* spectral regularizer stays finite on a flat map at epsilon_tv=0, checked on both the NumPy analytic gradient and jax.grad of the value. The existing S5 FD-parity suite uses a structured map with epsilon_tv != 0, which is why it never caught this.

Origin

Regression from #310, which flipped the epsilon_tv default from 1e-12 to 0. Backend-touching. May warrant a release cherry-pick like the #240 stv_pol_grad fix.

@rohandahale rohandahale requested a review from achael June 23, 2026 05:35
@rohandahale rohandahale self-assigned this Jun 23, 2026
@rohandahale rohandahale added this to the 2.0 milestone Jun 23, 2026
@rohandahale rohandahale marked this pull request as ready for review June 23, 2026 05:36
@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 47.44%. Comparing base (20e604e) to head (e8bd8eb).

Additional details and impacted files
@@             Coverage Diff              @@
##           dev-backend     #311   +/-   ##
============================================
  Coverage        47.43%   47.44%           
============================================
  Files               55       55           
  Lines            26979    26984    +5     
  Branches          4599     4599           
============================================
+ Hits             12798    12803    +5     
  Misses           12689    12689           
  Partials          1492     1492           

☔ 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.

@achael achael left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good -- main still has default epsilon=1e-12, so I think we just need to cherry pick this into dev as well.

@achael achael merged commit 8c5a439 into dev-backend Jun 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants