← Back to Pull requests

fit: gauge-fix the log-brightness of a free-pixel Image #519

Open
unknown opened this pull request with 389 lines diff across 4 files
View on GitHub
unknown unknown commented on Oct 23, 2026 01:59 UTC
Author

What

fit gauge-fixes the log_brightness of every free-pixel Image to log of its pixel fluxes (Σ exp = 1), so the lit pixels no longer depend on rounding or platform.

Why

Image fluxes are softmax(log_brightness), so a constant added to log_brightness is an exact null direction of every loss. On virgil-validation's test_l_curve fixture (8×8, TSV, LM, float64, jax 0.12.0.1) the lit pixels sat at about 4e5 (3.6e10 on jax 0.11.2). L-BFGS and Adam, plain or precondition="diag", already stayed off the direction; _Metric's projection is unchanged.

Pixels whose optimum is zero flux have no finite log_brightness and legitimately fit towards -inf (a boundary optimum). They are not clamped (an earlier commit on this branch floored them; reverted on review of the vv investigation, virgil-validation#180). Gauging by log total flux keeps the lit pixels near 0 whatever the dark ones do. This is noted in Image's docstring.

How (src/virgil/fitting.py)

  • _recentre, applied to the start, each LM chunk and the result (every method): x ← x − logsumexp(x) over the supported finite pixels. -inf pixels stay -inf.

  • LM gets one extra residual per image, log Σ exp(x), which turns the constant null direction into a well-conditioned one. It is zero at the gauge, so the loss and the stopping test do not see it.

  • Only flat-prior plain-array images (_gauge_masks, as before). GaussianField latents and proper priors are untouched.

  • Documented in Image's docstring and design/fit_preconditioning.md (d); CHANGELOG line.
  • Checks

  • vv test_l_curve logic with this branch, by hand: κ−2 ≤ 2e-3 (limit 0.02), residual gradient ≤ 1.1e-7 (limit 1e-5), χ² equals lc.chi2 to 1e-8. The lit pixels sit at about -4; dark ones reach -1e9 (rounding), as expected.

  • New tests in tests/test_fit_precondition.py: _recentre changes no loss, fixes logsumexp = 0, keeps dark and -inf pixels; for LM, L-BFGS, L-BFGS+diag and Adam+diag the fit is finite, logsumexp(x) = 0, pixels brighter than 1e-8 of the peak are above -25, and the model's brightness is softmax(x); for LM, a start shifted by 1e5 gives the same loss, the same brightness (atol 1e-7) and the same log_brightness on pixels brighter than 1e-3 of the peak (atol 1e-4). The thresholds are justified in the test: a pixel's log gradient is proportional to its flux, so a stopped fit fixes it only to about gtol / flux. About 13 s.

  • Existing tests (fit, imaging, image, sparse, detection, evidence, radio, diagnose, Gauss–Newton, fields, compile-once, lint, docstrings): 706 passed. test_the_optimum_and_the_gradient_norm_are_unchanged asserted the mean log-brightness stays at 0; it now asserts the log flux is 0.

  • tests/test_ensemble.py::test_rotated_member_is_rendered_onto_the_common_grid_without_cropping fails with and without this change (a float32 sum); not touched.
  • Innovations register

    None: gauge fixing an invariant parameterization (log-softmax normalization) is established practice, so no entry is needed.

    🤖 Generated with Claude Code

    Diff: 389 lines (+291 / -98) across 4 files Reviewer: claude[bot] Review duration: 9m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.43) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 389 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.357
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #519"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.1% (MEDIUM) · Higher = larger blast radius
    43.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.6% · Safe to merge
    24.6%