From 4bce43dd8fb3e2f5fdeb905cc0391cba92d6ab21 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 21 Oct 2026 12:21:25 +0100 Subject: [PATCH] feat: mirror rendered figures to an opt-in Eyes scratch folder When PYAUTO_EYES_SCRATCH is set, gallery/gallery_run.sh copies each successful producer's domain PNGs to $PYAUTO_EYES_SCRATCH/autogalaxy// (folder replaced, no stale figures) for on-the-fly review before any PR or CI. Also exposed as `python _viz_cli.py scratch [domain ...]`. Unset = silent no-op. Refs PyAutoLabs/autogalaxy_visualization#4 Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 6 +++ README.md | 3 ++ _viz_cli.py | 52 ++++++++++++++++++ gallery/gallery_run.sh | 9 ++++ scripts/misc/test/test_eyes_scratch.py | 74 ++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 scripts/misc/test/test_eyes_scratch.py diff --git a/AGENTS.md b/AGENTS.md index bebdaa8..bc604b9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,6 +67,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/autogalaxy//` (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 55446c5..3a7f977 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,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/autogalaxy//` 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 8f1d211..0ad15d8 100644 --- a/_viz_cli.py +++ b/_viz_cli.py @@ -12,6 +12,9 @@ - ``dataset_path(dataset_type, instrument)`` — ``/dataset//``. - ``auto_simulate_if_missing(...)`` — shell out to ``scripts/misc/simulators/.py --instrument `` when a dataset is absent. +- ``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:: @@ -27,6 +30,8 @@ from __future__ import annotations +import os +import shutil import subprocess import sys from pathlib import Path @@ -98,3 +103,50 @@ def auto_simulate_if_missing( ], check=True, ) + + +# 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 = "autogalaxy" +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..a254e02 --- /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/autogalaxy//``, a re-mirror leaves +no stale figure, and ``misc`` is never mirrored. + +Run:: + + cd autogalaxy_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("autogalaxy_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 / "autogalaxy" + 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()