Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions autolens/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,12 @@ def save_results(self, paths: af.DirectoryPaths, result: ResultDataset):
obj=result.max_log_likelihood_tracer,
file_path=paths._files_path / "tracer.json",
)
except AttributeError:
pass
except (AttributeError, af.exc.SamplesException, af.exc.FitException) as e:
# Building the tracer requires materializing the maximum log likelihood sample as a model
# instance, which the model may reject (e.g. `ell_comps` outside the unit disk). Writing an
# extra output file must never kill a completed fit before `paths.completed()` is called
# (PyAutoFit #1535), so the failure is logged and the fit finishes without `tracer.json`.
logger.warning(
f"The maximum log likelihood tracer could not be written to tracer.json, "
f"the model-fit is otherwise unaffected:\n{e}"
)
38 changes: 38 additions & 0 deletions test_autolens/analysis/analysis/test_analysis_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,41 @@ def test__save_attributes__dataset_fits_output_for_aggregator(analysis_imaging_7
assert ext_names[:4] == ["MASK", "DATA", "NOISE_MAP", "PSF"]

os.remove(dataset_fits_path)


class _RaisingTracerResult:
"""
Result double whose tracer cannot be built, because materializing the maximum log
likelihood sample as a model instance fails.
"""

def __init__(self, error):
self._error = error

@property
def max_log_likelihood_tracer(self):
raise self._error


@pytest.mark.parametrize(
"error",
[
AttributeError("no tracer on this result"),
af.exc.SamplesException("stored parameters cannot be reconstructed"),
af.exc.FitException("ell_comps must satisfy e0**2+e1**2 < 1"),
],
)
def test__save_results__tracer_failure_never_kills_the_fit(analysis_imaging_7x7, error):
"""
`save_results` runs after the search has finished but before `paths.completed()`, so a
failure writing the (optional) `tracer.json` must be logged and swallowed rather than
losing the run its `.completed` marker (PyAutoFit #1535).
"""
paths = af.DirectoryPaths()

analysis_imaging_7x7.save_results(
paths=paths,
result=_RaisingTracerResult(error),
)

assert not (paths._files_path / "tracer.json").exists()
Loading