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
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<domain>/` (folder replaced, so no stale
figures); after running a producer by hand, `python _viz_cli.py scratch <domain>` does the same.
Unset = no-op (CI never sets it). Nothing is committed from the scratch folder.

Each producer wipes its own `scripts/<domain>/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.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<domain>/` 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/<domain>/images/` together with `GALLERY.md` and
`gallery/viz_manifest.yaml` (every figure's producer, domain, source type, path, byte size and
Expand Down
51 changes: 51 additions & 0 deletions _viz_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<domain>/images/`` PNGs to the opt-in
``$PYAUTO_EYES_SCRATCH/<lib>/<domain>/`` 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::

Expand All @@ -29,6 +32,7 @@

from __future__ import annotations

import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -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>/<domain>/``.
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/<EYES_LIB>/<domain>/`` is
deleted and refilled with every PNG under ``scripts/<domain>/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")
9 changes: 9 additions & 0 deletions gallery/gallery_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/<lib>/<domain>/ via `python _viz_cli.py scratch <domain>`
# (folder replaced each time; a mirror failure never fails the run). Unset = no-op.

set -uo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
Expand Down Expand Up @@ -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"
Expand Down
74 changes: 74 additions & 0 deletions scripts/misc/test/test_eyes_scratch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""Hermetic tests for ``_viz_cli.mirror_to_scratch`` (the opt-in Eyes scratch mirror).

A fabricated ``scripts/<domain>/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/<domain>/<subpath below images/>``, 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()
Loading