From 6cd42cc7aa13d1be01e9fdcb5dbbcce3aa1b36bb Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sat, 29 Aug 2026 15:52:52 -0400 Subject: [PATCH 1/2] test: Richardson-step regression now asserts (PyAutoGalaxy#591) `test__bug_480_configuration__numpy_richardson_hessian_step_is_too_coarse` was a strict xfail pinning the defect: `LensCalc`'s NumPy Hessian at the last plane of the PyAutoLens#480 configuration returned [-0.00694, -0.00221, 0.00139, 0.00246] against the [0.04508, 0.01099, -0.08602, -0.01118] that exact JAX autodiff and the ray-traced Jacobian agree on. PyAutoGalaxy#591 made the Richardson step adapt per point, so the xfail now XPASSes. It becomes a positive assertion against the ray-traced Jacobian at the same rtol=1e-3 used everywhere else in this module - the tolerance was not widened when the test recorded a disagreement and is not tightened now that it records an agreement. Renamed to `..._numpy_richardson_hessian_agrees_at_the_last_plane`, since the old name states a claim that is no longer true. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01WzKP1RJKhPrtFmsHSm6EEA --- .../lens/test_multi_plane_cross_validation.py | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/test_autolens/lens/test_multi_plane_cross_validation.py b/test_autolens/lens/test_multi_plane_cross_validation.py index b38dcd295..2276cc40b 100644 --- a/test_autolens/lens/test_multi_plane_cross_validation.py +++ b/test_autolens/lens/test_multi_plane_cross_validation.py @@ -1123,25 +1123,22 @@ def test__bug_480_configuration__richardson_hessian_agrees_at_the_intermediate_p np.testing.assert_allclose(magnification_hessian, magnification_traced, rtol=1e-6) -@pytest.mark.xfail( - strict=True, - reason="lenscalc_numpy_hessian_step_is_too_coarse: LensCalc._hessian_via_richardson uses a " - "hardcoded buffer=0.01 arcsec, which is far too coarse where a ray passes ~4e-4 arcsec from a " - "compact deflector's centre. On the PyAutoLens#480 configuration it returns " - "[-0.00694, -0.00221, 0.00139, 0.00246] where exact JAX autodiff and the ray-traced Jacobian " - "both give [0.04508, 0.01099, -0.08602, -0.01118]: wrong by ~100-120% with a flipped sign on " - "all four points. Tracked in PyAutoMind/draft/bug/autogalaxy/" - "lenscalc_numpy_hessian_step_is_too_coarse.md. When that is fixed this xfail turns into an " - "XPASS and must be removed.", -) -def test__bug_480_configuration__numpy_richardson_hessian_step_is_too_coarse(): +def test__bug_480_configuration__numpy_richardson_hessian_agrees_at_the_last_plane(): """ - The known, filed defect, pinned rather than tolerated. + Regression test for PyAutoGalaxy#591. ``LensCalc.magnification_2d_via_hessian_from`` at the last plane must agree with the ray-traced - Jacobian, which the test above shows reproduces exact autodiff on this configuration. It does - not, and this test records exactly by how much. The tolerance is the same 1e-3 used everywhere - else in this module: it is not widened to make the disagreement go away. + Jacobian, which the test above shows reproduces exact autodiff on this configuration. It did + not while ``LensCalc._hessian_via_richardson`` used a hardcoded ``buffer=0.01`` arcsec step: + where a ray passes ~4e-4 arcsec from the compact z=1.0 deflector's centre that step straddles + the whole deflector, and the NumPy path returned + ``[-0.00694, -0.00221, 0.00139, 0.00246]`` against the ``[0.04508, 0.01099, -0.08602, + -0.01118]`` that exact JAX autodiff and the ray-traced Jacobian agree on -- wrong by + ~100-120% with a flipped sign on all four points. PyAutoGalaxy#591 made the Richardson step + adapt per point, and this test now asserts the agreement rather than pinning the defect. + + The tolerance is the same 1e-3 used everywhere else in this module: it was not widened when the + test recorded a disagreement, and it is not tightened now that it records an agreement. """ tracer = _bug_480_tracer() From 24cec3cbf59e269f9cf44c2ca3605c37474ab976 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sat, 29 Aug 2026 15:59:07 -0400 Subject: [PATCH 2/2] test: relax theta_for_beta root tolerance to 1e-12 for the adaptive Hessian (PyAutoGalaxy#591) `TestTensorVsScalarWeighting`'s ground-truth ray-trace solved `beta_of_theta(theta) == target_beta` with `scipy.optimize.root(method="hybr", tol=1e-14)` and asserted `solution.success`. The jacobian precision tensor's off-diagonal is numerically zero there (1.08e-06 against diagonals 2.04e+06 and 1.00e+04), so the low-precision eigenvector is `(5.3e-13, -1)`; PyAutoGalaxy#591's more accurate Hessian moves those last bits, which moves `target_beta` and with it scipy's termination path. The solver then returns status 3, "xtol too small, no further improvement", at a residual of 8.4e-17 - the root is found to machine precision, only the flag differs. The test's three physics assertions (tensor ordering, scalar ordering, and the true image-plane distances 0.010000 < 0.013505) hold either way. 1e-12 is still far tighter than the 1e-3 scale the test discriminates on. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01WzKP1RJKhPrtFmsHSm6EEA --- test_autolens/point/fit/test_solved.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test_autolens/point/fit/test_solved.py b/test_autolens/point/fit/test_solved.py index 8cd2d5af9..4a8c23ea6 100644 --- a/test_autolens/point/fit/test_solved.py +++ b/test_autolens/point/fit/test_solved.py @@ -306,7 +306,10 @@ def theta_for_beta(target_beta): lambda theta: beta_of_theta(theta) - target_beta, x0=np.array(observed_theta), method="hybr", - tol=1.0e-14, + # 1e-12, not 1e-14: with the adaptive Hessian (PyAutoGalaxy#591) scipy reports + # status 3 ("xtol too small") at residual ~1e-16 - a termination-path artefact, + # not a physics change. + tol=1.0e-12, ) assert solution.success return solution.x