Skip to content

fix: analysis log_likelihood_function preserves the cause behind FitException - #639

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/analysis-fitexception-masks-cause
Jul 21, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/analysis-fitexception-masks-cause

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

The numpy-path log_likelihood_function in all three lens analyses wrapped fit
failures in except Exception as e: raise af.exc.FitException and discarded
e
. The FitException is correct (PR#607 path-parity: a numpy non-PD
LinAlgError → FitException → the sampler resamples, matching the JAX
NaN-resample), but the bare re-raise masked the true error — the traceback
showed only FitException at analysis.py, and a genuine code bug was
indistinguishable from a pathological-model resample. Surfaced by
autolens_workspace#308/#309 (interferometer-Delaunay marker re-diagnosis).

Fix: a shared raise_fit_exception helper chains the cause (raise ... from e)
and honours an opt-in PYAUTO_RAISE_ANALYSIS_EXCEPTIONS=1 to re-raise the
original exception unwrapped for debugging. Default behaviour is unchanged —
production searches still resample on FitException.

API Changes

Changed Behaviour

  • AnalysisImaging / AnalysisInterferometer / AnalysisPoint
    log_likelihood_function (numpy path): a fit failure is still re-raised as
    af.exc.FitException, but now with the original exception preserved as
    __cause__ (raise ... from e). No change to when/whether FitException is
    raised; searches resample exactly as before.

Added

  • Env var PYAUTO_RAISE_ANALYSIS_EXCEPTIONS=1 (default off / "0"): re-raises
    the original exception unwrapped instead of wrapping it — a debug aid for
    surfacing masked failures (e.g. under PYAUTO_TEST_MODE, where a single
    evaluation is not absorbed by a sampler).
  • autolens.analysis.exceptions.raise_fit_exception(exception) — internal
    helper implementing the above (not part of the user-facing modelling API).

Removed / Renamed / Changed Signature

  • None.

Migration

  • None required. Existing code and configs are unaffected; the only observable
    difference is a richer traceback (__cause__) on an already-raised
    FitException.

Test Plan

  • New test_autolens/analysis/test_exceptions.py (3 tests): wraps + preserves
    __cause__; env flag re-raises original; "0" still wraps (no truthy-string trap).
  • Existing analysis/model tests pass (16) — no regression.

Refs #638.

Generated by the PyAutoLabs agent workflow.

…tion

The numpy-path log_likelihood_function in the imaging, interferometer and point
analyses wrapped fit failures in `except Exception as e: raise af.exc.FitException`,
discarding e. The FitException (path-parity with the JAX NaN-resample so real
fits resample rather than crash) is correct, but the bare re-raise masked the
true error — a genuine bug was indistinguishable from a pathological-model
resample, and the traceback showed only FitException at analysis.py.

Chain the cause via a shared raise_fit_exception helper (raise ... from e), and
add an opt-in PYAUTO_RAISE_ANALYSIS_EXCEPTIONS=1 to re-raise the original
exception unwrapped for debugging (e.g. under PYAUTO_TEST_MODE where a single
eval is not absorbed by a sampler). Default behaviour unchanged; production
searches still resample.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Jul 21, 2026
@Jammy2211
Jammy2211 merged commit 60a2c48 into main Jul 21, 2026
5 checks passed
@Jammy2211
Jammy2211 deleted the feature/analysis-fitexception-masks-cause branch July 21, 2026 17:35
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant