From 1181501fb3782121fb364f4d1613bd1e81849495 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 18 Sep 2026 21:57:28 +0100 Subject: [PATCH] maintenance: derive the demo dataset and canonical-output paths, never name them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `scripts/reduce_cosmos_web_ring.py` hardcoded /home/jammy/Code/PyAutoLabs/autolens_assistant/dataset/... — a cross-repo reference that breaks when autolens_assistant moves under lens/, and that was already machine-specific: nobody else's checkout is at /home/jammy. It deliberately does NOT reach for PyAutoBrain's resolver. PyAutoReduce is a released PyPI distribution: someone who `pip install pyautoreduce`s and clones this repo to run scripts/ has no Brain checkout, no .pyauto-root marker and no workspace at all, so an import-by-file-path of a developer-box organ would be dead code on every machine but one — the exact failure class that resolver's own docstring exists to kill. It would also invert the dependency direction, a science library reaching back into the orchestration layer, and it answers a question one level up from the one asked: the script needs one sibling's dataset subtree, not the workspace root. Instead: $AUTOREDUCE_DEMO_ROOT, then a bounded sibling probe, then an unverified guess. The probe checks one and two levels above the repo, each directly and one family subdirectory deeper — the full cross-product of "is THIS repo in a family folder" x "is autolens_assistant". No walk to /, no marker, stdlib only. It calls only is_dir()/glob() so it cannot raise at import, and the not-found branch returns the flat-layout path so the eventual failure names something plausible. A --demo-root flag completes the set: env var for a shell, flag for a one-off, probe for everyone else. The four prototypes' _CAND fallbacks guessed where the canonical checkout is, in order to read reduced outputs that are gitignored and so absent from a worktree. Git already knows: a worktree's .git is a FILE reading `gitdir: /...`. The guess is replaced by that fact — stdlib, no subprocess at import, .parent chaining rather than parents[2] so a short gitdir degrades instead of raising. In an ordinary clone .git is a directory, MAIN == ROOT and the candidates collapse harmlessly; verified the resulting lists are byte-identical to the originals. Resolution demonstrated across six layouts including the actual phase-3 one (lens/autolens_assistant). 299 passed, 3 skipped. Part of PyAutoBrain#393 (phase 2). No directory layout changes. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_0161NLKVzbBMzhaBfjpGogor --- prototypes/starred_epsf_spike.py | 14 ++++++- prototypes/starred_vs_epsf_comparison.py | 14 ++++++- prototypes/starred_vs_epsf_m92.py | 14 ++++++- prototypes/starred_vs_epsf_omegacen.py | 14 ++++++- scripts/reduce_cosmos_web_ring.py | 52 ++++++++++++++++++++---- 5 files changed, 96 insertions(+), 12 deletions(-) diff --git a/prototypes/starred_epsf_spike.py b/prototypes/starred_epsf_spike.py index 8708556..605c88a 100644 --- a/prototypes/starred_epsf_spike.py +++ b/prototypes/starred_epsf_spike.py @@ -31,11 +31,21 @@ ROOT = Path(__file__).resolve().parents[1] # Reduced outputs are gitignored and live only in the canonical checkout, not -# in the task worktree — resolve the field there, worktree-first. +# in a task worktree — resolve the field there too, worktree-first. A worktree's +# `.git` is a FILE pointing back into `/.git/worktrees/`, so +# the canonical checkout is derived from git rather than from a workspace path, +# which differs per machine and moves when the workspace is regrouped. In an +# ordinary clone `.git` is a directory and this collapses to ROOT. +_DOTGIT = ROOT / ".git" +MAIN = ( + Path(_DOTGIT.read_text().split(":", 1)[-1].strip()).parent.parent.parent + if _DOTGIT.is_file() + else ROOT +) _FIELD_NAME = "cosmos_web_ring_f115w" # richest clean-point-source count of the reduced fields _CANDIDATES = [ ROOT / "scripts" / "output" / _FIELD_NAME, - Path.home() / "Code/PyAutoLabs/PyAutoReduce/scripts/output" / _FIELD_NAME, + MAIN / "scripts" / "output" / _FIELD_NAME, ] FIELD = next((p for p in _CANDIDATES if (p / "data.fits").exists()), _CANDIDATES[0]) OUT = ROOT / "prototypes" / "output" / "starred_spike" diff --git a/prototypes/starred_vs_epsf_comparison.py b/prototypes/starred_vs_epsf_comparison.py index 324d1c6..81afad1 100644 --- a/prototypes/starred_vs_epsf_comparison.py +++ b/prototypes/starred_vs_epsf_comparison.py @@ -26,9 +26,21 @@ import numpy as np ROOT = Path(__file__).resolve().parents[1] +# Reduced outputs are gitignored and live only in the canonical checkout, not +# in a task worktree — resolve the field there too, worktree-first. A worktree's +# `.git` is a FILE pointing back into `/.git/worktrees/`, so +# the canonical checkout is derived from git rather than from a workspace path, +# which differs per machine and moves when the workspace is regrouped. In an +# ordinary clone `.git` is a directory and this collapses to ROOT. +_DOTGIT = ROOT / ".git" +MAIN = ( + Path(_DOTGIT.read_text().split(":", 1)[-1].strip()).parent.parent.parent + if _DOTGIT.is_file() + else ROOT +) _FIELD = "cosmos_web_ring_f115w" _CAND = [ROOT / "scripts/output" / _FIELD, - Path.home() / "Code/PyAutoLabs/PyAutoReduce/scripts/output" / _FIELD] + MAIN / "scripts/output" / _FIELD] FIELD = next((p for p in _CAND if (p / "data.fits").exists()), _CAND[0]) OUT = ROOT / "prototypes" / "output" / "starred_vs_epsf" NPZ = OUT / "compare.npz" diff --git a/prototypes/starred_vs_epsf_m92.py b/prototypes/starred_vs_epsf_m92.py index 7e01e8c..807a20e 100644 --- a/prototypes/starred_vs_epsf_m92.py +++ b/prototypes/starred_vs_epsf_m92.py @@ -24,10 +24,22 @@ import numpy as np ROOT = Path(__file__).resolve().parents[1] +# Reduced outputs are gitignored and live only in the canonical checkout, not +# in a task worktree — resolve the field there too, worktree-first. A worktree's +# `.git` is a FILE pointing back into `/.git/worktrees/`, so +# the canonical checkout is derived from git rather than from a workspace path, +# which differs per machine and moves when the workspace is regrouped. In an +# ordinary clone `.git` is a directory and this collapses to ROOT. +_DOTGIT = ROOT / ".git" +MAIN = ( + Path(_DOTGIT.read_text().split(":", 1)[-1].strip()).parent.parent.parent + if _DOTGIT.is_file() + else ROOT +) _FIELD = os.environ.get("M92_FIELD", "m92_f150w") # m92_f150w (SW) | m92_f277w (LW) _CAND = [ ROOT / "scripts/output" / _FIELD, - Path.home() / "Code/PyAutoLabs/PyAutoReduce/scripts/output" / _FIELD, + MAIN / "scripts/output" / _FIELD, ] FIELD = next((p for p in _CAND if (p / "data.fits").exists()), _CAND[0]) OUT = ROOT / "prototypes" / "output" / f"starred_vs_epsf_{_FIELD}" diff --git a/prototypes/starred_vs_epsf_omegacen.py b/prototypes/starred_vs_epsf_omegacen.py index e65468f..6353c50 100644 --- a/prototypes/starred_vs_epsf_omegacen.py +++ b/prototypes/starred_vs_epsf_omegacen.py @@ -20,10 +20,22 @@ import numpy as np ROOT = Path(__file__).resolve().parents[1] +# Reduced outputs are gitignored and live only in the canonical checkout, not +# in a task worktree — resolve the field there too, worktree-first. A worktree's +# `.git` is a FILE pointing back into `/.git/worktrees/`, so +# the canonical checkout is derived from git rather than from a workspace path, +# which differs per machine and moves when the workspace is regrouped. In an +# ordinary clone `.git` is a directory and this collapses to ROOT. +_DOTGIT = ROOT / ".git" +MAIN = ( + Path(_DOTGIT.read_text().split(":", 1)[-1].strip()).parent.parent.parent + if _DOTGIT.is_file() + else ROOT +) _FIELD = "omegacen_f606w" _CAND = [ ROOT / "scripts/output" / _FIELD, - Path.home() / "Code/PyAutoLabs/PyAutoReduce/scripts/output" / _FIELD, + MAIN / "scripts/output" / _FIELD, ] FIELD = next((p for p in _CAND if (p / "data.fits").exists()), _CAND[0]) OUT = ROOT / "prototypes" / "output" / "starred_vs_epsf_omegacen" diff --git a/scripts/reduce_cosmos_web_ring.py b/scripts/reduce_cosmos_web_ring.py index 44d6927..32d0797 100644 --- a/scripts/reduce_cosmos_web_ring.py +++ b/scripts/reduce_cosmos_web_ring.py @@ -18,6 +18,7 @@ import argparse import json +import os import sys from pathlib import Path @@ -30,14 +31,45 @@ RA, DEC = 150.10048, 1.89301 CACHE_ROOT = REPO / "scripts" / "cache" OUTPUT_ROOT = REPO / "scripts" / "output" -DEMO_ROOT = Path( - "/home/jammy/Code/PyAutoLabs/autolens_assistant/dataset/imaging/" - "cosmos_web_ring/wavebands" -) + +# The parity reference is a *sibling checkout*, not part of this repo and not +# shipped in the `pyautoreduce` distribution: autolens_assistant's demo +# cutouts. Resolve it, never name it — an absolute path is right on exactly one +# machine, and the checkouts beside us get grouped into family subdirectories +# (autolens_assistant -> lens/autolens_assistant) as the workspace grows. +DEMO_REL = Path("autolens_assistant/dataset/imaging/cosmos_web_ring/wavebands") BANDS = ("F115W", "F150W", "F277W", "F444W") +def default_demo_root() -> Path: + """Where autolens_assistant's parity cutouts are, resolved beside us. + + ``$AUTOREDUCE_DEMO_ROOT`` wins if set; otherwise look for the sibling + checkout one and two levels above this repo, at each level directly and one + family subdirectory deeper. That covers the flat workspace, the regrouped + one, and a bundle of task worktrees, without this library knowing anything + about the workspace tooling — `pyautoreduce` is released to users who have + no workspace at all, so it must not import the developer-box organs that + could answer this question. + + Only ever probes for directories, so it cannot raise: a missing demo + dataset stays an error for `compare` at run time, never an import failure, + and the guess returned when nothing is found names a plausible path. + """ + env = os.environ.get("AUTOREDUCE_DEMO_ROOT") + if env: + return Path(env).expanduser() + for root in (REPO.parent, REPO.parent.parent): + for cand in (root / DEMO_REL, *sorted(root.glob(f"*/{DEMO_REL}"))): + if cand.is_dir(): + return cand + return REPO.parent / DEMO_REL + + +DEMO_ROOT = default_demo_root() + + def spec_for(band: str, psf_backend: str = "epsf") -> TargetSpec: adapter = nircam_adapter_for_filter(band) # Demo cutouts: SW 419x419 @0.03 (12.57"), LW 209x209 @0.06 (12.54") — @@ -60,14 +92,14 @@ def spec_for(band: str, psf_backend: str = "epsf") -> TargetSpec: ) -def compare(band: str, out_dir: Path) -> dict: +def compare(band: str, out_dir: Path, demo_root: Path | None = None) -> dict: from astropy.io import fits from autoreduce.validation import registered_ratios new_data = fits.getdata(out_dir / "data.fits").astype(float) new_noise = fits.getdata(out_dir / "noise_map.fits").astype(float) - demo_dir = DEMO_ROOT / band + demo_dir = (demo_root or DEMO_ROOT) / band demo_data = fits.getdata(demo_dir / "data.fits").astype(float) demo_noise = fits.getdata(demo_dir / "noise_map.fits").astype(float) return { @@ -84,6 +116,12 @@ def main(): help="PSF back-end: photutils Tier-1 (default) or STARRED Tier-1b (#35; " "needs the pyautoreduce[starred] extra)", ) + parser.add_argument( + "--demo-root", type=Path, default=None, + help="autolens_assistant parity cutouts (default: the sibling checkout " + f"resolved beside this repo, currently {DEMO_ROOT}; or " + "$AUTOREDUCE_DEMO_ROOT)", + ) args = parser.parse_args() bands = BANDS if args.band == "all" else (args.band,) @@ -98,7 +136,7 @@ def main(): "correlated_noise_factor": record["noise"]["correlated_noise_factor"], "sky_over_err_floor": record["noise"].get("sky_over_err_floor"), "psf": record["psf"], - "parity": compare(band, out_dir), + "parity": compare(band, out_dir, args.demo_root), } print(f"[{band}] ---- validation ----") print(json.dumps(summary, indent=2))