diff --git a/autolens/analysis/positions.py b/autolens/analysis/positions.py index 21e1fe7b7..b137ee458 100644 --- a/autolens/analysis/positions.py +++ b/autolens/analysis/positions.py @@ -88,14 +88,27 @@ def __init__( for a double source plane lens system is being used where the specific plane is required. """ + # A position that is not finite cannot be traced: a single (inf, inf) row makes + # the maximum source-plane separation nan, `nan > threshold` is False, and the + # penalty is zero for every model -- silently, since `output_positions_info` + # uses a nan-safe maximum and reports the true separation. `Result. + # positions_likelihood_from` passes exactly that: the point solver's + # fixed-length output, padded with (inf, inf). Drop such rows. + values = np.asarray(positions.array if hasattr(positions, "array") else positions) + finite = np.isfinite(values).all(axis=1) + + if not finite.all(): + positions = aa.Grid2DIrregular(values[finite]) + self.positions = positions self.threshold = threshold self.plane_redshift = plane_redshift - if len(positions) == 1: + if len(positions) < 2: raise exc.PositionsException( - f"The positions input into the PositionsLikelihood object have length one " - f"(e.g. it is only one (y,x) coordinate and therefore cannot be compared with other images).\n\n" + f"The positions input into the PositionsLikelihood object have {len(positions)} finite " + f"(y,x) coordinate(s) ({int((~finite).sum())} non-finite dropped), so there is nothing " + f"to compare them with.\n\n" "Please input more positions into the Positions." ) diff --git a/test_autolens/analysis/test_positions.py b/test_autolens/analysis/test_positions.py index 7f5d7efaf..c5223840f 100644 --- a/test_autolens/analysis/test_positions.py +++ b/test_autolens/analysis/test_positions.py @@ -1,5 +1,6 @@ import os from pathlib import Path +import numpy as np import pytest from autonerves.dictable import output_to_json, from_json, from_dict @@ -20,6 +21,46 @@ def test__check_positions_on_instantiation(): al.PositionsLH(positions=al.Grid2DIrregular([(1.0, 2.0)]), threshold=0.1) +def _penalty(positions_lh, einstein_radius): + """The penalty for an isothermal lens of the given Einstein radius.""" + lens = al.Galaxy( + redshift=0.5, mass=al.mp.Isothermal(centre=(0.0, 0.0), einstein_radius=einstein_radius) + ) + tracer = al.Tracer(galaxies=[lens, al.Galaxy(redshift=1.0)]) + + class MockAnalysis: + def tracer_via_instance_from(self, instance): + return tracer + + return float( + positions_lh.log_likelihood_penalty_from(instance=None, analysis=MockAnalysis()) + ) + + +def test__non_finite_positions_are_dropped_and_the_penalty_still_fires(): + # `Result.positions_likelihood_from` pads the point solver's images with + # (inf, inf). One such row used to make the max separation nan, so the + # penalty was zero for every model. + inf = np.inf + positions = al.Grid2DIrregular([(0.0, 1.0), (inf, inf), (0.0, -1.0)] + [(inf, inf)] * 5) + + positions_lh = al.PositionsLH(positions=positions, threshold=0.1) + + assert len(positions_lh.positions) == 2 + # Images at +/-1" trace together only for theta_E = 1"; theta_E = 0.3" splits them. + assert _penalty(positions_lh, einstein_radius=1.0) == 0.0 + assert _penalty(positions_lh, einstein_radius=0.3) > 0.0 + + +def test__fewer_than_two_finite_positions_raises(): + inf = np.inf + + with pytest.raises(exc.PositionsException): + al.PositionsLH( + positions=al.Grid2DIrregular([(0.0, 1.0), (inf, inf), (inf, inf)]), threshold=0.1 + ) + + def test__output_positions_info(): output_path = Path(__file__).resolve().parent / "files"