fix: repair ConstantZeroth dead code (eye(P) shape bug + missing neighbors_sizes) - #449
Merged
Merged
Conversation
…hbors_sizes) Two independent defects made al.reg.ConstantZeroth unusable through its class API (issue #448, found in the wsdev#104 reg-logdet investigation): - constant_zeroth_regularization_matrix_from built the zeroth-order term as eye(P) where P is the neighbor-column count, not S the mesh pixel count, so `const + zeroth` raised a broadcast ValueError whenever S != P. The zeroth term is now the full S x S scaled identity, matching zeroth.py's semantics. - ConstantZeroth.regularization_matrix_from omitted the required neighbors_sizes argument, raising TypeError before the shape bug was even reached. It now threads linear_obj.neighbors.sizes, mirroring Constant. Siblings adapt_split_zeroth.py and brightness_zeroth.py were checked and do not carry the pattern (their zeroth terms are diag over per-pixel weights). Adds numpy-only unit tests: matrix shape (S, S) with S != P, no null mode (smallest eigenvalue >= lambda_z^2), reduction to Constant's matrix + lambda_z^2 * I, and the class-API path returning without raising. Constant/Adapt/AdaptSplit numerics untouched. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013frRFAHyKN1EpE8sQHTogB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #448. Phase 8C of the inference-methods programme (autolens_profiling#134,
results/notes/inference/PROGRAMME.mdon its PR #135).What was broken
al.reg.ConstantZerothwas dead code presenting as a feature — two independent defects, both reproduced on cleanmain(7d1906e) before any fix (tracebacks pasted on #448):eye(P)shape bug —constant_zeroth_regularization_matrix_frombuilt the zeroth-order term asxp.eye(P)whereP = neighbors.shape[1]is the neighbor-column count (e.g. 4), notSthe mesh pixel count, soconst + zerothraisedValueError: operands could not be broadcast together with shapes (9,9) (4,4)wheneverS != P.neighbors_sizesat the class API —ConstantZeroth.regularization_matrix_fromomitted the required argument, raisingTypeErrorbefore the shape bug was even reached.The fix
S x Sscaled identity (xp.eye(S) * coefficient_zeroth**2), matchingzeroth.py's semantics (eye(pixels)).neighbors_sizes=linear_obj.neighbors.sizes, mirroringConstant.regularization_matrix_from.adapt_split_zeroth.pyandbrightness_zeroth.pydo not carry the copy-paste pattern — their zeroth terms arexp.diagover per-pixel weights; no change made there.Constant/Adapt/AdaptSplitnumerics,autoarray/__init__.py(version-stamp branch owns it). Nothing else bundled.Null-mode-lift verification (spectrum before/after)
On the 9-pixel rectangular MockMapper fixture with
lambda_neighbor = lambda_zeroth = 1.0, through the class API:ConstantConstantZeroth(fixed)The zeroth-order term lifts the null mode exactly as intended:
max |(CZ − C) − lambda_z^2·I| = 0.0element-wise, and the smallest eigenvalue rises from the 1e-8 conditioning floor tolambda_z^2. This fix makes the scheme WORK — it does not recommend it over AdaptSplit (ConstantZeroth is an alternative regularization model; target-changing relative to AdaptSplit).Tests
New
test_autoarray/inversion/regularizations/test_constant_zeroth.py(numpy-only per repo policy):(S, S)on a fixture withS != P(the shape that previously raised),min(eigvalsh) >= lambda_z^2,Constant's matrix+ lambda_z^2 * Iwhere the schemes overlap,8.0000001+lambda_z^2 = 9).Full suite:
pytest test_autoarray/→ 988 passed, 59 skipped, 3 failed — the 3 failures aretest_transformer.py::test__nufft_pynufft__*(scipy.linalg.pinv2removed in modern scipy, inside pynufft), identical on cleanmainin this environment and unrelated to this change.Merge stays with @Jammy2211.
Generated by Claude Code