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
36 changes: 32 additions & 4 deletions _profile_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,13 +1636,41 @@ def auto_simulate_if_missing(
imported lazily so this helper can sit in any module without forcing
the heavy import chain on every caller.
"""
import sys
import autolens as al

if al.util.dataset.should_simulate(str(dataset_path)):
_run_simulator(dataset_path, dataset_type, instrument, workspace_root)

if (dataset_type, instrument) == ("cluster", "simple"):
_validate_simple_cluster(dataset_path)


def _validate_simple_cluster(dataset_path: Path) -> None:
"""Reject legacy simple-cluster caches before their truth enters a fit."""
import autolens as al

recovery = (
"Move the old dataset directory aside and rerun the cluster cell "
"to regenerate it with scripts/misc/simulators/cluster.py."
)
members = dataset_path / "scaling_galaxies.csv"
if not members.is_file():
raise FileNotFoundError(f"Missing simple-cluster member table: {members}. {recovery}")

import autolens as al # noqa: F401 — imported lazily to defer side effects
truth = dataset_path / "tracer.json"
try:
tracer = al.from_json(file_path=truth)
except Exception as exc:
raise ValueError(
f"Cannot load simple-cluster truth from {truth}: {exc}. {recovery}"
) from exc
if not isinstance(tracer, al.Tracer):
raise ValueError(f"Expected a Tracer in {truth}, found {type(tracer).__name__}. {recovery}")

if not al.util.dataset.should_simulate(str(dataset_path)):
return

def _run_simulator(
dataset_path: Path, dataset_type: str, instrument: str, workspace_root: Path
) -> None:
simulator_script = workspace_root / "scripts" / "misc" / "simulators" / f"{dataset_type}.py"
if not simulator_script.exists():
raise FileNotFoundError(
Expand Down
34 changes: 34 additions & 0 deletions scripts/misc/cells/test/test_simple_cluster_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Cache gate regression for the legacy simple cluster in profiling issue 445."""

import sys
from pathlib import Path

import autolens as al
import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[4]))

from _profile_cli import auto_simulate_if_missing # noqa: E402


def test_simple_cluster_cache(tmp_path, monkeypatch):
monkeypatch.delenv("PYAUTO_SMALL_DATASETS", raising=False)
dataset = tmp_path / "dataset" / "cluster" / "simple"
dataset.mkdir(parents=True)

def check_cache():
auto_simulate_if_missing(
dataset, dataset_type="cluster", instrument="simple", workspace_root=tmp_path
)

with pytest.raises(FileNotFoundError, match="scaling_galaxies.csv"):
check_cache()

(dataset / "scaling_galaxies.csv").touch()
truth = dataset / "tracer.json"
truth.write_text("{invalid")
with pytest.raises(ValueError, match="tracer.json"):
check_cache()

al.output_to_json(obj=al.Tracer(galaxies=[al.Galaxy(redshift=0.5)]), file_path=truth)
check_cache()
Loading