Skip to content

fix: un-flake interferometer Delaunay test-mode FitException (+ NNLS NaN root cause) #640

Closed

Description

@Jammy2211

Overview

autolens_workspace/scripts/interferometer/features/pixelization/delaunay.py intermittently hard-fails CI smoke: the same commit (dce04672) failed one py3.12 run (ValueError: Points cannot contain NaN from the Delaunay/qhull triangulation) and passed another ~1 min apart, with no library merge in between — a true nondeterministic flake. The masked underlying cause is a NaN/non-PD in the interferometer Delaunay path; analysis.py:175-182 wraps ANY numpy-path error as af.exc.FitException (PR#607 parity guard so the sampler resamples), but under PYAUTO_TEST_MODE=2 there is no sampler, so a single unlucky eval hard-fails the script. The entry is currently parked in autolens_workspace/config/build/no_run.yaml.

Plan

  • Phase 1 — un-flake test-mode (PyAutoLens + autolens_workspace): make the TEST_MODE bypass tolerate a single-eval FitException the way a real sampler does (resample / sentinel low FOM, logged so it stays visible) instead of hard-failing. This un-flakes all FitException-prone pixelization scripts in test-mode without masking real breakage in a real fit. Then un-park the interferometer Delaunay no_run.yaml entry and add the script to smoke_tests.txt (imaging Delaunay was added in autolens_workspace#307).
  • Phase 2 — fix the numerical producer (PyAutoArray, separate PR): the smoking gun is fnnls.py:134 (alpha = np.min(d[q] / (d[q] - s_chol[q]))) — 45/250 local draws emit divide-by-zero/invalid RuntimeWarnings, usually benign but a candidate source of the fatal NaN/non-PD on CI. Guard/repair at the producer (no silent guards — fix the math) and/or the traced-mesh NaN conditioning.
Detailed implementation plan

Affected Repositories

  • PyAutoLens (primary — Phase 1 test-mode tolerance, interferometer/model/analysis.py)
  • autolens_workspace (Phase 1 — un-park marker + smoke)
  • PyAutoArray (Phase 2 — util/fnnls.py producer fix)

Branch Survey

Repository Current Branch Dirty?
./PyAutoLens docs/citations-exhaustive (other session) dirty
./PyAutoArray main clean
./autolens_workspace main clean

Suggested branch: interferometer-delaunay-flaky-fitexception

Reproduction evidence (2026-07-21)

  • True flake, not a library race: Feature/multi image visualization #307's two CI runs (same commit dce04672) ~1 min apart, one failed (qhull NaN) one passed, no PyAutoArray/PyAutoLens main merge between them.
  • Local repro hard: 250/250 random prior-instance fit_from draws clean on current main — CI-runner thread/BLAS-dependent.
  • Producer: 45/250 draws emit RuntimeWarning divide-by-zero at fnnls.py:134. Confirm which solver the script's modeling uses (use_positive_only_solver=False vs default positive/fnnls path).

Phase 1 steps

  1. PyAutoLens/autolens/interferometer/model/analysis.py:175-182 — in the TEST_MODE path, catch the single-eval FitException, log it, return a sentinel low figure-of-merit instead of propagating (mirror the real-sampler resample semantics). Check for a shared base-analysis test-mode hook so imaging/pixelization scripts benefit too.
  2. autolens_workspace/config/build/no_run.yaml — remove the interferometer Delaunay NEEDS_FIX entry.
  3. autolens_workspace/smoke_tests.txt — add interferometer/features/pixelization/delaunay.py.
  4. Verify: run the script in the exact CI smoke env repeatedly; run library unit tests; smoke.

Key Files

  • PyAutoLens/autolens/interferometer/model/analysis.py — test-mode FitException tolerance
  • PyAutoArray/autoarray/util/fnnls.py — Phase 2 divide-by-zero producer
  • autolens_workspace/config/build/no_run.yaml, smoke_tests.txt — un-park + guard

Testing

Library unit tests (numpy-only) for the test-mode tolerance path; repeated CI-env script runs to confirm the flake is gone; PyAutoHeart vitals GREEN/YELLOW before ship. Library-first: PyAutoLens PR merges before the autolens_workspace un-park.

Original Prompt

Click to expand

Filed as PyAutoMind/draft/bug/autoarray/interferometer_delaunay_intermittent_qhull_nan.md. Surfaced 2026-07-21 while un-parking the Delaunay cluster (autolens_workspace#307, follow-up to #300/#301). Two masked signatures (qhull NaN vertices + non-PD inversion), both wrapped as FitException by analysis.py:175-182. Refs (all closed): autolens_workspace#300/#307/#308/#309.

Activity

  1. Jammy2211 commented on Jul 21, 2026

    @Jammy2211
    CollaboratorAuthor

    Phase 1 library PR created: PyAutoLabs/PyAutoFit#1408 (pending-release).

    Re-scoped from PyAutoLens to PyAutoFit after the full CI traceback showed the failure runs through autofit's _fit_bypass_test_mode (abstract_search.py), not PyAutoLens analysis — source_pix_2 (Hilbert adaptive mesh) → bypass single-eval → log_likelihood_function → Delaunay interpolator → scipy_delaunay NaN. The fix makes the TEST_MODE=2 bypass tolerate a single-eval FitException (keep the -1e99 sentinel, log the cause) — central, fixes all analyses; only FitException is caught so real errors still fail loudly. Complements PyAutoLens#639.

    Remaining (this task):

    • Phase 1b (autolens_workspace, library-first — after #1408 merges): un-park the interferometer Delaunay no_run.yaml entry + add to smoke_tests.txt.
    • Phase 2 (PyAutoArray, separate): fix the NaN/non-PD producer (fnnls.py:134 divide-by-zero / degenerate Hilbert-mesh vertices) so the fit is correct, not merely tolerated.
  2. Jammy2211 commented on Jul 21, 2026

    @Jammy2211
    CollaboratorAuthor

    Phase 1a MERGED (PyAutoFit#1408) and Phase 1b PR opened (autolens_workspace#311): un-park the interferometer Delaunay + add to smoke, now safe on the merged fix. Local run_smoke.py = 11/11 passed (interferometer delaunay 25.5s). Library-first satisfied. Remaining: Phase 2 — fix the underlying NaN/non-PD producer (fnnls.py:134 / degenerate Hilbert-mesh vertices) so the fit is correct, not merely tolerated.

  3. Jammy2211 commented on Jul 21, 2026

    @Jammy2211
    CollaboratorAuthor

    ✅ Phase 1 complete (both PRs merged)

    • Phase 1a — PyAutoFit#1408 MERGED: TEST_MODE=2 bypass tolerates a single-eval FitException (keep -1.0e99 sentinel + log), mirroring fitness.py:256's resample-to-reject. +2 tests; CI green 3.12/3.13.
    • Phase 1b — autolens_workspace#311 MERGED: un-parked the interferometer Delaunay no_run.yaml entry + added to smoke_tests.txt. CI green on 3.12 and 3.13 against the merged fix — the flake no longer hard-fails.

    User-facing problem resolved: the intermittent CI failure is fixed; any future FitException-raising instance in test-mode is now tolerated (all analyses benefit, not just this script).

    Remaining — Phase 2 (open, non-urgent)

    Fix the underlying producer in PyAutoArray so the fit is correct, not merely tolerated: the NaN Delaunay-mesh vertices in source_pix_2 (Hilbert adaptive mesh — the actual failing stage) and/or the fnnls.py:134 divide-by-zero (45/250 local draws hit it). This is harder — the flake is CI-thread-dependent and not locally reproducible on the Overlay stage (250 draws clean), so it needs a source_pix_2 Hilbert-stage reproduction first. Keeping this issue open for Phase 2.

  4. Jammy2211 commented on Aug 7, 2026

    @Jammy2211
    CollaboratorAuthor

    Phase 2: root cause re-pointed, fix pushed

    Phase 2 is implemented on claude/interferometer-delaunay-phase-2-1p53vk in PyAutoArray (9e9a3360). No PR opened yet.

    fnnls.py:134 is not the producer

    The plan above names it as "the smoking gun". It isn't. I exercised it 599 times across 600 constraint-binding draws with zero degenerate denominators. The 45/250 RuntimeWarnings recorded in phase 1 are a downstream symptom, not the cause. Line 134 is unchanged by this fix.

    The producer is cholesky_funcs.py → cholinsertlast

    S[index, index] = s22 = math.sqrt(x[index] - S12.dot(S12))   # Schur complement, unchecked

    Two (near-)coincident source-mesh vertices give (near-)identical mapping-matrix columns, so the normal-equations matrix is singular to working precision and that Schur complement is zero to within rounding — measured at ±1.776e-15 (= 8·eps). Which side of zero it lands on is decided purely by floating-point summation order, i.e. by the BLAS thread count. That is the CI-vs-local thread dependence, and it is why 250 local draws came back clean.

    The same degenerate matrix therefore failed three different ways (60 exactly-singular draws):

    Schur lands Old behaviour Raised?
    tiny negative ValueError: math domain error yes
    exactly 0.0 (27/60) zero pivot → cho_solve divides by it no
    tiny positive (19/60) pivot ~4e-8, amplified by cho_solve no

    Why it surfaced in source_pix_2, a stage away from the cause

    inversion_util.py:365 guards the solver with except (RuntimeError, LinAlgError, ValueError) — a NaN raises none of those. So the NaN escaped as a valid-looking reconstruction → poisoned adapt_data → hilbert.py:275-284 places mesh vertices from adapt_data with no NaN guard → scipy.spatial.Delaunay: "Points cannot contain NaN". That closes the loop from producer to failing stage.

    The fix

    Reject a non-positive Schur complement (np.linalg.LinAlgError), plus a finiteness assertion at the fnnls solver boundary as a backstop. inversion_util.py needed no edit — LinAlgError subclasses ValueError, so its existing except-tuple already catches it.

    Likelihood evaluations are unchanged — verified, not assumed

    The test is schur > 0 and nothing stricter. A relative-tolerance version was written first and discarded: it converted 7/300 finite reconstructions (max 0.26–0.46) into exceptions, which would have changed likelihoods.

    • 800 old-vs-new problems: 708 bitwise identical, 92 raise in both, 0 finite→raise, 0 numerically different
    • jitter-sweep clean counts identical pre/post: 115 / 82 / 71 / 71 / 120
    • full test_autoarray: 855 → 887 passed, same 16 pre-existing failures (env: py3.11 + missing optional deps) — +32 new tests, zero regressions

    schur < 0 already raised; schur == 0 yields NaN with certainty so there is no finite result to preserve; any positive Schur complement passes through and returns a bitwise identical pivot.

    Limit worth stating

    The repro is unit-level and deterministic (thread-independent) — stronger than the CI-only flake for regression purposes, and it is committed as tests. But I did not execute source_pix_2 end-to-end: this container has no autolens stack and runs Python 3.11 against a >=3.12 requirement. The source_pix_2 link is established by code reading, not by a run.


    Generated by Claude Code

  5. Jammy2211 commented on Aug 7, 2026

    @Jammy2211
    CollaboratorAuthor

    Closing — both phases merged

    Phase 1 (already merged): PyAutoFit#1408 (TEST_MODE=2 bypass tolerates a single-eval FitException as a resample-to-reject sentinel) + autolens_workspace#311 (un-park the script, add it to smoke). Made the flake non-fatal.

    Phase 2 (merged today): PyAutoArray#436 → efaf3041. CI green first run on both matrix legs (3.12 + 3.13). Fixes the producer, so the fit is correct rather than merely tolerated.

    Completion record: PyAutoMind/complete/2026/08/interferometer-delaunay-flaky-fitexception.md.

    Two things to carry forward

    The root cause named in this issue was wrong. fnnls.py:134 is not the producer — 599 executions across 600 constraint-binding draws, zero degenerate denominators. The real producer is cholesky_funcs.py → cholinsertlast, taking math.sqrt of an unchecked Schur complement that, on a near-singular matrix, is zero to within rounding (±1.776e-15 = 8·eps) and lands on either side of zero by floating-point summation order — i.e. by BLAS thread count. That is the entire explanation for the CI-vs-local dependence.

    The first fix would have changed likelihood evaluations. A relative-tolerance guard converted 7/300 finite reconstructions (max 0.26–0.46) into exceptions. Discarded in favour of schur > 0 and nothing stricter, verified bitwise: 708/800 identical, 92 raise in both, 0 finite→raise, 0 numerically different.

    Residual limit

    The repro is unit-level and deterministic (committed as 32 tests), but was never executed end-to-end through source_pix_2 — that link is established by code reading, not a run. Whether the workspace script still ever flakes is now only provable by CI over time; phase 1's tolerance means any residual occurrence would be non-fatal rather than a hard failure. Reopen if the smoke entry flakes again.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions