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
7 changes: 7 additions & 0 deletions autolens/interop/coolest.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def _parameters_from(coolest_profile) -> Dict:
def from_coolest(
file_path: str,
cosmology: Optional[ag.cosmo.LensingCosmology] = None,
intermediate: bool = False,
) -> Tracer:
"""
Build a ``Tracer`` from the analytic profiles of a COOLEST JSON template
Expand All @@ -290,6 +291,11 @@ def from_coolest(
The cosmology of the returned ``Tracer``. Defaults to a
``FlatLambdaCDM`` built from the template's H0 / Om0 (or
``ag.cosmo.Planck15`` if the template has none).
intermediate
If True, PEMD profiles are built as ``ag.mp.PowerLawIntermediate``
(whose ``einstein_radius`` is the template's ``theta_E`` unchanged)
instead of the default ``ag.mp.PowerLaw``. Both give the identical
mass distribution.
"""
_, _, _, JSONSerializer = _coolest_modules()

Expand Down Expand Up @@ -340,6 +346,7 @@ def from_coolest(
profile_type=coolest_profile.type,
parameters=_parameters_from(coolest_profile),
sigma_crit=sigma_crit,
intermediate=intermediate,
)

galaxies.append(
Expand Down
36 changes: 36 additions & 0 deletions test_autolens/interop/test_coolest.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,42 @@ def test__round_trip__nfw_uses_sigma_crit_from_cosmology(tmp_path):
assert nfw_default.kappa_s == pytest.approx(0.15, rel=1e-3)


def test__from_coolest__intermediate_returns_power_law_intermediate(tmp_path):
import autogalaxy as ag

galaxies = [
al.Galaxy(
redshift=0.5,
mass=al.mp.PowerLawIntermediate(
centre=(0.0, 0.0),
ell_comps=al.convert.ell_comps_from(axis_ratio=0.7, angle=45.0),
einstein_radius=1.2,
slope=2.1,
),
),
al.Galaxy(redshift=1.0, bulge=al.lp.SersicSph(intensity=0.5)),
]

file_path = interop_coolest.to_coolest(
galaxies=galaxies, file_path=str(tmp_path / "template")
)

tracer_back = interop_coolest.from_coolest(
file_path=file_path, intermediate=True
)

mass_back = [g for g in tracer_back.galaxies if g.redshift == 0.5][0].mass_0

assert isinstance(mass_back, ag.mp.PowerLawIntermediate)
assert mass_back.einstein_radius == pytest.approx(1.2, rel=1e-12)

grid = al.Grid2DIrregular([[1.0, 0.5], [-0.3, 0.7]])
tracer = al.Tracer(galaxies=galaxies)
assert np.asarray(
tracer_back.deflections_yx_2d_from(grid=grid)
) == pytest.approx(np.asarray(tracer.deflections_yx_2d_from(grid=grid)), rel=1e-8)


def test__round_trip__relative_file_path(tmp_path, monkeypatch):
# The coolest JSONSerializer rejects relative paths — to_coolest /
# from_coolest must absolutize them.
Expand Down
Loading