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
4 changes: 3 additions & 1 deletion autolens/interop/coolest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def _coolest_modules():


def _path_no_ext(file_path: str) -> str:
# The coolest JSONSerializer rejects relative paths.
file_path = os.path.abspath(file_path)
if file_path.endswith(".json"):
return file_path[: -len(".json")]
return file_path
Expand Down Expand Up @@ -245,7 +247,7 @@ def to_coolest(
)

path_no_ext = _path_no_ext(file_path)
os.makedirs(os.path.dirname(os.path.abspath(path_no_ext)), exist_ok=True)
os.makedirs(os.path.dirname(path_no_ext), exist_ok=True)
JSONSerializer(path_no_ext, obj=root, check_external_files=False).dump_simple()

return f"{path_no_ext}.json"
Expand Down
16 changes: 16 additions & 0 deletions test_autolens/interop/test_coolest.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ 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__round_trip__relative_file_path(tmp_path, monkeypatch):
# The coolest JSONSerializer rejects relative paths — to_coolest /
# from_coolest must absolutize them.
monkeypatch.chdir(tmp_path)

file_path = interop_coolest.to_coolest(
galaxies=lens_model_galaxies(), file_path="template"
)

assert file_path == str(tmp_path / "template.json")

tracer_back = interop_coolest.from_coolest(file_path="template.json")

assert len(tracer_back.galaxies) == 3


def test__from_coolest__missing_point_estimate_raises(tmp_path):
pytest.importorskip("coolest")
from coolest.template.lazy import (
Expand Down
Loading