Commit d91500d
committed
fix: form the reconstruction covariance via Cholesky, not elementwise sqrt
`reconstruction_noise_map_with_covariance` applied np.sqrt elementwise to the
whole inverse of curvature_reg_matrix. The off-diagonals of a covariance
matrix are covariances and are routinely negative, so every one was NaN by
construction -- for any matrix, however well-conditioned -- and each call
emitted "RuntimeWarning: invalid value encountered in sqrt". The property's
docstring promised a matrix accounting for "the covariance of the noise
between pixels"; the entries carrying that covariance were the broken ones.
The 1D reconstruction_noise_map was NOT affected: np.sqrt is elementwise, so
it commutes with taking the diagonal.
Replaced with reconstruction_covariance_matrix, which returns C directly and
forms it from a Cholesky factorization. An A/B against np.linalg.inv refuted
two hypotheses and confirmed one:
- inv gives negative diagonals on well-formed SPD matrices: REFUTED
(0 across cond 1e3-1e15, n=400, 20 trials each)
- inv is materially less accurate on the diagonal: REFUTED
(matches cho_solve; at cond 1e15 inv was marginally better)
- near-coincident mesh vertices degrade the inverse: REFUTED
(regularization keeps the matrix PD, cond ~6.8e7 at duplicated columns)
- inv returns asymmetric output: CONFIRMED
(5.2e-7 at cond 1e12, vs 2.6e-16 for cho_solve)
- inv silently succeeds on indefinite matrices: CONFIRMED
The last is the substantive one. cho_factor raises LinAlgError on a matrix
with a negative eigenvalue; inv succeeds and returns a plausible-looking
covariance. At eigenvalue -1e-8 all 300 diagonals came back negative; at -1.0,
zero did -- no NaN, no warning, no error, and wrong numbers. A covariance is
only defined for a positive-definite matrix, so this adds a definiteness check
the code lacked entirely. Both existing call sites already catch LinAlgError
(inversion_plots.py:169 and :395), verified rather than assumed.
reconstruction_noise_map now computes sqrt(diag(C)) directly. It was correct
only incidentally before, via the elementwise sqrt; the invariant is now
stated, and it cannot silently become a variance if the matrix changes.
The old name stays as a deprecated alias. Its values do change -- diagonal
from std-dev to variance, off-diagonals from NaN to covariances -- so the
warning says so explicitly.
Tests: the only prior assertion checked [0, 0], a diagonal element, which is
why this shipped. Added off-diagonal finiteness under -W error::RuntimeWarning,
exact symmetry, the sqrt(diag(C)) invariant, the indefinite-matrix raise, and
the deprecation. Repointed two plotter monkeypatch sites to the new property.
Full suite: 1150 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 a6b07cd commit d91500d
3 files changed
Lines changed: 160 additions & 23 deletions
File tree
- autoarray/inversion/inversion
- test_autoarray/inversion
- inversion
- plot
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
836 | 837 | | |
837 | 838 | | |
838 | 839 | | |
839 | | - | |
| 840 | + | |
840 | 841 | | |
841 | | - | |
842 | | - | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
843 | 883 | | |
844 | | - | |
845 | | - | |
846 | | - | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
847 | 888 | | |
848 | | - | |
849 | | - | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
850 | 893 | | |
851 | | - | |
852 | | - | |
| 894 | + | |
| 895 | + | |
853 | 896 | | |
854 | 897 | | |
855 | 898 | | |
856 | | - | |
857 | | - | |
| 899 | + | |
858 | 900 | | |
859 | | - | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
860 | 912 | | |
861 | 913 | | |
862 | 914 | | |
| |||
870 | 922 | | |
871 | 923 | | |
872 | 924 | | |
873 | | - | |
874 | | - | |
| 925 | + | |
| 926 | + | |
875 | 927 | | |
876 | 928 | | |
877 | 929 | | |
878 | 930 | | |
879 | 931 | | |
880 | 932 | | |
881 | | - | |
| 933 | + | |
882 | 934 | | |
883 | 935 | | |
884 | 936 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
681 | 683 | | |
682 | 684 | | |
683 | 685 | | |
684 | | - | |
685 | | - | |
686 | | - | |
| 686 | + | |
687 | 687 | | |
688 | 688 | | |
689 | 689 | | |
690 | 690 | | |
691 | 691 | | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 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 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
692 | 777 | | |
693 | 778 | | |
694 | 779 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
83 | | - | |
| 82 | + | |
| 83 | + | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
111 | | - | |
| 110 | + | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| |||
0 commit comments