fix: analysis log_likelihood_function preserves the cause behind FitException#639
Merged
Merged
Conversation
…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>
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.
Summary
The numpy-path
log_likelihood_functionin all three lens analyses wrapped fitfailures in
except Exception as e: raise af.exc.FitExceptionand discardede. TheFitExceptionis correct (PR#607 path-parity: a numpy non-PDLinAlgError→FitException→ the sampler resamples, matching the JAXNaN-resample), but the bare re-raise masked the true error — the traceback
showed only
FitExceptionatanalysis.py, and a genuine code bug wasindistinguishable from a pathological-model resample. Surfaced by
autolens_workspace#308/#309 (interferometer-Delaunay marker re-diagnosis).
Fix: a shared
raise_fit_exceptionhelper chains the cause (raise ... from e)and honours an opt-in
PYAUTO_RAISE_ANALYSIS_EXCEPTIONS=1to re-raise theoriginal exception unwrapped for debugging. Default behaviour is unchanged —
production searches still resample on
FitException.API Changes
Changed Behaviour
AnalysisImaging/AnalysisInterferometer/AnalysisPointlog_likelihood_function(numpy path): a fit failure is still re-raised asaf.exc.FitException, but now with the original exception preserved as__cause__(raise ... from e). No change to when/whetherFitExceptionisraised; searches resample exactly as before.
Added
PYAUTO_RAISE_ANALYSIS_EXCEPTIONS=1(default off /"0"): re-raisesthe original exception unwrapped instead of wrapping it — a debug aid for
surfacing masked failures (e.g. under
PYAUTO_TEST_MODE, where a singleevaluation is not absorbed by a sampler).
autolens.analysis.exceptions.raise_fit_exception(exception)— internalhelper implementing the above (not part of the user-facing modelling API).
Removed / Renamed / Changed Signature
Migration
difference is a richer traceback (
__cause__) on an already-raisedFitException.Test Plan
test_autolens/analysis/test_exceptions.py(3 tests): wraps + preserves__cause__; env flag re-raises original;"0"still wraps (no truthy-string trap).Refs #638.
Generated by the PyAutoLabs agent workflow.