Commit f0aefa8
committed
fix: raise LinAlgError on non-finite input, symmetrize before Cholesky
Addresses an adversarial review of the previous commit.
1. MAJOR regression, now fixed. scipy's cho_factor/cho_solve default to
check_finite=True and raise ValueError -- not LinAlgError -- on a NaN/inf
matrix. Both callers (inversion_plots.py:169 and :397) catch only
LinAlgError, and the CSV writer's docstring explicitly promises that a
failure there may not abort the enclosing model-fit. So a NaN-contaminated
curvature matrix would have killed a fit that previously wrote a nan column
and continued. Verified before and after: the old code returned [nan, nan];
the new code raised builtins.ValueError past the guards.
Fixed by checking finiteness explicitly and raising LinAlgError with a
message naming the cause, rather than by broadening the callers' except
clauses -- the property's contract is now "raises LinAlgError for any input
that has no covariance", which holds downstream too. check_finite=False is
then passed to scipy, so the explicit check costs nothing net.
2. cho_factor reads only the upper triangle, so an asymmetric input was
silently inverted as though its lower triangle matched. Verified: for
[[2.0, 0.5], [0.1, 2.0]] it gave diag 0.5333 against the true inverse's
0.5063. The output symmetrization does not address this -- it symmetrizes
the result, not the input -- so the input is now symmetrized too.
curvature_reg_matrix is F + H and symmetric by construction, so this is
defensive, but the silent wrong answer is not an acceptable failure mode.
3. Softened the "no value change" claim for reconstruction_noise_map. The two
forms are algebraically identical but only numerically equivalent, since the
covariance is now formed by Cholesky rather than LU: ~7e-15 relative at
cond 1e3 rising to ~4e-5 at cond 1e13. Neither is the more correct result.
4. Tests. The symmetry test was tautological -- 0.5 * (C + C.T) is bitwise
symmetric for any C, so it could only fail if the line were deleted. It now
also asserts accuracy against an exactly-constructed ground truth
(A = Q diag(w) Q.T), with the tolerance set from the measured error (~3e-9
at cond 1e9) and a comment recording that np.linalg.inv is marginally more
accurate on that matrix, not less. Added tests for the non-finite raise and
the asymmetric-input symmetrization. Fixed a stale test name that said
"asymmetric_curvature_reg_matrix" over a symmetric matrix.
Downstream check for the value-changing deprecated alias: no consumer of
reconstruction_noise_map_with_covariance exists in PyAutoGalaxy (3ca31bf),
PyAutoLens (87e5827) or autolens_workspace -- zero hits for "with_covariance"
in any of them. reconstruction_noise_map IS used, in autolens_workspace
source_science.py scripts computing signal_to_noise_map = reconstruction /
reconstruction_noise_map, and its values are unchanged.
Full suite: 1152 passed, 1 skipped. The 3 test_transformer.py pynufft
failures are pre-existing on a6b07cd and unrelated.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133X4XhMV91SFjzV2mK4Ejh1 parent d91500d commit f0aefa8
2 files changed
Lines changed: 73 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
862 | 862 | | |
863 | 863 | | |
864 | 864 | | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
865 | 873 | | |
866 | 874 | | |
867 | 875 | | |
| |||
874 | 882 | | |
875 | 883 | | |
876 | 884 | | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
877 | 896 | | |
878 | | - | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
879 | 900 | | |
880 | 901 | | |
881 | 902 | | |
| |||
925 | 946 | | |
926 | 947 | | |
927 | 948 | | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
928 | 955 | | |
929 | 956 | | |
930 | 957 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
678 | 678 | | |
679 | 679 | | |
680 | 680 | | |
681 | | - | |
| 681 | + | |
682 | 682 | | |
683 | 683 | | |
684 | 684 | | |
| |||
713 | 713 | | |
714 | 714 | | |
715 | 715 | | |
716 | | - | |
717 | | - | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
718 | 723 | | |
719 | 724 | | |
720 | | - | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
721 | 728 | | |
722 | 729 | | |
| 730 | + | |
| 731 | + | |
723 | 732 | | |
724 | 733 | | |
725 | 734 | | |
726 | 735 | | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
727 | 739 | | |
728 | 740 | | |
729 | 741 | | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
730 | 771 | | |
731 | 772 | | |
732 | 773 | | |
| |||
0 commit comments