From 561c17d0edd75f27093fc296918bd674d32cece1 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Tue, 24 Nov 2026 08:26:37 +0000 Subject: [PATCH] Reject stale truth files in the simple cluster cache --- _profile_cli.py | 36 ++++++++++++++++--- .../cells/test/test_simple_cluster_cache.py | 34 ++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 scripts/misc/cells/test/test_simple_cluster_cache.py diff --git a/_profile_cli.py b/_profile_cli.py index 808a635db..a47662175 100644 --- a/_profile_cli.py +++ b/_profile_cli.py @@ -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( diff --git a/scripts/misc/cells/test/test_simple_cluster_cache.py b/scripts/misc/cells/test/test_simple_cluster_cache.py new file mode 100644 index 000000000..49f49ebd2 --- /dev/null +++ b/scripts/misc/cells/test/test_simple_cluster_cache.py @@ -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()