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
21 changes: 21 additions & 0 deletions autolens/analysis/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,33 @@ def positions_likelihood_from(
Outside test mode this branch is never taken — bad positions still surface as
the original error, so production fits are not silently masked.

Skip-checks safeguard (``PYAUTO_SKIP_CHECKS``): when validation checks are
skipped *and* test mode is active, we return a ``PositionsLH`` built from the
same synthetic ``[(1.0, 0.0), (-1.0, 0.0)]`` pair (with threshold set to
``minimum_threshold`` or 0.5 if unset). This avoids the expensive
multiple-image-position computation while still giving callers a usable
``PositionsLH``, so workspace scripts that chain ``.positions`` continue to
work end-to-end. With skip-checks alone (no test mode) the function still
returns ``None`` — the production opt-out behaviour is unchanged.

Returns
-------
The `PositionsLH` object used to apply a likelihood penalty or resample the positions.
"""

if skip_checks():
if is_test_mode():
synthetic_positions = aa.Grid2DIrregular(
values=[(1.0, 0.0), (-1.0, 0.0)]
)
synthetic_threshold = (
minimum_threshold if minimum_threshold is not None else 0.5
)
return PositionsLH(
positions=synthetic_positions,
threshold=synthetic_threshold,
plane_redshift=plane_redshift,
)
return

positions = (
Expand Down
32 changes: 32 additions & 0 deletions test_autolens/analysis/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,38 @@ def test__positions_likelihood_from(analysis_imaging_7x7):
assert positions_likelihood.threshold == pytest.approx(0.2, 1.0e-4)


def test__positions_likelihood_from__skip_checks_returns_none_outside_test_mode(
monkeypatch, analysis_imaging_7x7,
):
monkeypatch.setenv("PYAUTO_SKIP_CHECKS", "1")
monkeypatch.delenv("PYAUTO_TEST_MODE", raising=False)

samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=al.Tracer(galaxies=[]))
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.positions_likelihood_from(factor=0.1, minimum_threshold=0.2) is None


def test__positions_likelihood_from__skip_checks_returns_synthetic_in_test_mode(
monkeypatch, analysis_imaging_7x7,
):
monkeypatch.setenv("PYAUTO_SKIP_CHECKS", "1")
monkeypatch.setenv("PYAUTO_TEST_MODE", "2")

samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=al.Tracer(galaxies=[]))
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

positions_likelihood = result.positions_likelihood_from(
factor=0.1, minimum_threshold=0.2
)

assert isinstance(positions_likelihood, al.PositionsLH)
assert positions_likelihood.threshold == pytest.approx(0.2, 1.0e-4)
assert len(positions_likelihood.positions) == 2
assert positions_likelihood.positions[0] == pytest.approx((1.0, 0.0))
assert positions_likelihood.positions[1] == pytest.approx((-1.0, 0.0))


def test__positions_likelihood_from__test_mode_fallback(
monkeypatch, analysis_imaging_7x7,
):
Expand Down
Loading