From e300e4795c3f47caa11104054cfbec60cca1d85e Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 21 Oct 2026 12:38:07 +0100 Subject: [PATCH] feat: mirror rendered figures to an opt-in Eyes scratch folder Port of phase 1 (autogalaxy_visualization#5): mirror_to_scratch() and the `python _viz_cli.py scratch [domain ...]` entry point copy scripts//images/ PNGs to $PYAUTO_EYES_SCRATCH/autolens// (folder replaced, misc skipped, no-op when unset); gallery_run.sh mirrors each successful producer. Hermetic tests in scripts/misc/test/test_eyes_scratch.py; AGENTS.md/README notes. Refs #8 Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 6 +++ README.md | 3 ++ _viz_cli.py | 51 ++++++++++++++++++ gallery/gallery_run.sh | 9 ++++ scripts/misc/test/test_eyes_scratch.py | 74 ++++++++++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 scripts/misc/test/test_eyes_scratch.py diff --git a/AGENTS.md b/AGENTS.md index 4ebace3..57e95f4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -94,6 +94,12 @@ python gallery/gallery_build.py # rebuild GALLERY.md + gallery/viz_mani python gallery/gallery_build.py --check # fail if GALLERY.md or the manifest is stale vs the PNGs on disk ``` +**On-the-fly review (Eyes scratch).** Set `PYAUTO_EYES_SCRATCH` to any folder +you can browse and `gallery_run.sh` mirrors each successful +producer's domain to `$PYAUTO_EYES_SCRATCH/autolens//` (folder replaced, so no stale +figures); after running a producer by hand, `python _viz_cli.py scratch ` does the same. +Unset = no-op (CI never sets it). Nothing is committed from the scratch folder. + Each producer wipes its own `scripts//images/visualization/` tree first, so the committed PNG set is exactly what the last run produced. Commit the PNGs, `GALLERY.md` and `gallery/viz_manifest.yaml` together. diff --git a/README.md b/README.md index 57218e2..d8e5200 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ source activate.sh # library checkouts on PYTHONPATH (see the f bash gallery/gallery_run.sh --all # run every producer, rebuild GALLERY.md + manifest, --check ``` +Set `PYAUTO_EYES_SCRATCH` to also mirror each re-rendered domain to +`$PYAUTO_EYES_SCRATCH/autolens//` for review before any PR (see `AGENTS.md`). + Or one domain: `python scripts/imaging/visualization.py`, then `python gallery/gallery_build.py`. Commit the regenerated PNGs under `scripts//images/` together with `GALLERY.md` and `gallery/viz_manifest.yaml` (every figure's producer, domain, source type, path, byte size and diff --git a/_viz_cli.py b/_viz_cli.py index 647cc76..1ec67b0 100644 --- a/_viz_cli.py +++ b/_viz_cli.py @@ -14,6 +14,9 @@ - ``DATASETS`` / ``ensure_dataset(key)`` — the registry of every tracked dataset (its directory, simulator and the sentinel file proving it exists) and the one-call "path, simulating it first if absent" helper the newer producers use. +- ``mirror_to_scratch(domains)`` — copy ``scripts//images/`` PNGs to the opt-in + ``$PYAUTO_EYES_SCRATCH///`` for on-the-fly review (no-op when unset); + also ``python _viz_cli.py scratch [domain ...]``. ``gallery/gallery_run.sh`` calls it. Typical use at the top of a producer:: @@ -29,6 +32,7 @@ from __future__ import annotations +import os import shutil import subprocess import sys @@ -207,3 +211,50 @@ def keep_dataset_and_fit( else: p.unlink() return kept + + +# The PyAutoEyes dashboard names this repo's section by its library import name, and the +# scratch mirror reuses that name: ``$PYAUTO_EYES_SCRATCH///``. +EYES_LIB = "autolens" +EYES_SCRATCH_ENV = "PYAUTO_EYES_SCRATCH" + + +def mirror_to_scratch(domains: list[str] | None = None) -> Path | None: + """Copy freshly rendered PNGs to the opt-in Eyes scratch folder; return its lib folder. + + For on-the-fly figure review before any PR or CI. When ``$PYAUTO_EYES_SCRATCH`` is + unset this is a silent no-op returning ``None`` (so CI and other machines are + untouched). Otherwise each domain's ``$PYAUTO_EYES_SCRATCH///`` is + deleted and refilled with every PNG under ``scripts//images/`` (subpaths + below ``images/`` kept), so no stale figure survives a re-render. ``domains=None`` + mirrors every domain that has an ``images/`` folder (``misc`` never does). + """ + scratch = os.environ.get(EYES_SCRATCH_ENV) + if not scratch: + return None + scripts = repo_root() / "scripts" + if domains is None: + domains = sorted(p.parent.name for p in scripts.glob("*/images") if p.parent.name != "misc") + lib_dir = Path(scratch).expanduser() / EYES_LIB + for domain in domains: + images = scripts / domain / "images" + if not images.is_dir(): + print(f" [eyes-scratch] skip {domain}: no {images.relative_to(scripts.parent)}") + continue + dest = lib_dir / domain + shutil.rmtree(dest, ignore_errors=True) + pngs = sorted(images.rglob("*.png")) + for png in pngs: + target = dest / png.relative_to(images) + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(png, target) + print(f" [eyes-scratch] {len(pngs)} PNGs -> {dest}") + return lib_dir + + +if __name__ == "__main__": + # ``python _viz_cli.py scratch [domain ...]`` — mirror rendered PNGs to the Eyes scratch. + if len(sys.argv) < 2 or sys.argv[1] != "scratch": + sys.exit("usage: python _viz_cli.py scratch [domain ...]") + if mirror_to_scratch(sys.argv[2:] or None) is None: + print(f" [eyes-scratch] ${EYES_SCRATCH_ENV} is unset; nothing mirrored") diff --git a/gallery/gallery_run.sh b/gallery/gallery_run.sh index 60efe17..56bdbad 100755 --- a/gallery/gallery_run.sh +++ b/gallery/gallery_run.sh @@ -12,6 +12,11 @@ # bash gallery/gallery_run.sh --all # same (explicit; used by CI) # bash gallery/gallery_run.sh imaging # one domain + build + check # bash gallery/gallery_run.sh --build-only # skip runs, just build + check +# +# Eyes scratch mirror (opt-in, for on-the-fly review before any PR / CI): when +# PYAUTO_EYES_SCRATCH is set, each successful producer's domain is mirrored to +# $PYAUTO_EYES_SCRATCH/// via `python _viz_cli.py scratch ` +# (folder replaced each time; a mirror failure never fails the run). Unset = no-op. set -uo pipefail cd "$(dirname "${BASH_SOURCE[0]}")/.." @@ -42,6 +47,10 @@ for script in "${run_list[@]}"; do mkdir -p output/gallery_logs if python "$script" > "$log" 2>&1; then echo " ok ($((SECONDS - start))s)" + if [[ -n "${PYAUTO_EYES_SCRATCH:-}" ]]; then + domain="${script#scripts/}" + python _viz_cli.py scratch "${domain%%/*}" || echo " (eyes scratch mirror failed; continuing)" + fi else echo " FAILED ($((SECONDS - start))s) — traceback in $log" tail -n 30 "$log" diff --git a/scripts/misc/test/test_eyes_scratch.py b/scripts/misc/test/test_eyes_scratch.py new file mode 100644 index 0000000..da67da1 --- /dev/null +++ b/scripts/misc/test/test_eyes_scratch.py @@ -0,0 +1,74 @@ +"""Hermetic tests for ``_viz_cli.mirror_to_scratch`` (the opt-in Eyes scratch mirror). + +A fabricated ``scripts//images/`` tree in a tmp dir stands in for real +producer output (``repo_root`` is monkeypatched to it), so no figures are rendered and +the real ``scripts/`` tree is never read. The points: unset env var = no-op, the layout +is ``$PYAUTO_EYES_SCRATCH/autolens//``, a re-mirror leaves +no stale figure, and ``misc`` is never mirrored. + +Run:: + + cd autolens_visualization + python -m pytest scripts/misc/test/test_eyes_scratch.py +""" + +from __future__ import annotations + +import importlib.util +import sys as _sys +from pathlib import Path as _Path + +import pytest + + +def _repo_root() -> _Path: + for _p in _Path(__file__).resolve().parents: + if (_p / "ruff.toml").exists(): + return _p + raise RuntimeError("autolens_visualization root (ruff.toml) not found") + + +_spec = importlib.util.spec_from_file_location("viz_cli_under_test", _repo_root() / "_viz_cli.py") +viz = importlib.util.module_from_spec(_spec) +_sys.modules[_spec.name] = viz +_spec.loader.exec_module(viz) + + +@pytest.fixture +def fake_repo(tmp_path, monkeypatch): + root = tmp_path / "repo" + for rel in ("alpha/images/visualization/a.png", "alpha/images/visualization/sub/b.png"): + (root / "scripts" / rel).parent.mkdir(parents=True, exist_ok=True) + (root / "scripts" / rel).write_bytes(b"png") + (root / "scripts" / "beta" / "images" / "visualization").mkdir(parents=True) + (root / "scripts" / "beta" / "images" / "visualization" / "c.png").write_bytes(b"png") + (root / "scripts" / "misc" / "images").mkdir(parents=True) + (root / "scripts" / "misc" / "images" / "ignored.png").write_bytes(b"png") + monkeypatch.setattr(viz, "repo_root", lambda: root) + return root + + +def test_unset_env_is_a_no_op(fake_repo, monkeypatch): + monkeypatch.delenv(viz.EYES_SCRATCH_ENV, raising=False) + assert viz.mirror_to_scratch() is None + + +def test_mirror_layout_and_no_stale_figures(fake_repo, tmp_path, monkeypatch): + scratch = tmp_path / "scratch" + monkeypatch.setenv(viz.EYES_SCRATCH_ENV, str(scratch)) + + lib_dir = viz.mirror_to_scratch() + + assert lib_dir == scratch / "autolens" + got = sorted(p.relative_to(lib_dir).as_posix() for p in lib_dir.rglob("*.png")) + assert got == [ + "alpha/visualization/a.png", + "alpha/visualization/sub/b.png", + "beta/visualization/c.png", + ] + + (fake_repo / "scripts" / "alpha" / "images" / "visualization" / "a.png").unlink() + viz.mirror_to_scratch(["alpha"]) + assert not (lib_dir / "alpha" / "visualization" / "a.png").exists() + assert (lib_dir / "alpha" / "visualization" / "sub" / "b.png").exists() + assert (lib_dir / "beta" / "visualization" / "c.png").exists()