"""Study folders shared by inference writers, metric writers and readers. Run IDs and artifact bytes are stable; only their containing directory changes. Explicit external output directories keep their flat layout for portable bundles. """ from pathlib import Path import re from utils.paths import PROJECT_ROOT STUDIES = { "pilot": "Early pilot and parser recovery runs", "multipass-pilot": "Initial Gemma one-pass and multipass experiments", "benchmark302": "Frozen 302-image benchmark and qualifications", "benchmark1200": "Frozen 1,200-image benchmark and hosted extension", "gemma-strategy": "Gemma domain and split-pass comparison", "repeatability": "Repeated runs at the original generation settings", "sampling": "Separate temperature-0.3 campaign", "final-gold11534": "Frozen 11,534-image synthetic benchmark", "adhoc": "New runs without a named study", } def study_for_run(name: str) -> str: """Classify a run or one of its sidecars using stable campaign identifiers.""" if "final-gold11534" in name or name.startswith(("final-gold-", "261004-full-glm53-flash")): return "final-gold11534" if name.startswith("260924-temperature03-"): return "sampling" if name.startswith("260924-benchmark1200-repeat"): return "repeatability" if name.startswith("260910-gemma12b-domain1200-"): return "gemma-strategy" if "benchmark1200" in name: return "benchmark1200" if re.match(r"26090[345]-.*302", name): return "benchmark302" if name.startswith("260902-gemma12b-"): return "multipass-pilot" if re.match(r"2608\d{2}_|260903_pilot-recovery_", name): return "pilot" return "adhoc" def result_path(directory: Path, filename: str) -> Path: """Group files in repository result roots; leave explicit bundle dirs flat.""" directory = Path(directory) if Path(filename).name != filename or filename in {"", ".", ".."}: raise ValueError(f"Expected an artifact filename: {filename!r}") if directory.resolve() in { PROJECT_ROOT / "results/inference", PROJECT_ROOT / "results/metrics" }: return directory / study_for_run(filename) / filename return directory / filename def inference_path(run_id: str, suffix: str = ".jsonl", *, directory=None) -> Path: return result_path( Path(directory) if directory is not None else PROJECT_ROOT / "results/inference", run_id + suffix, ) def find_result(directory: Path, filename: str) -> Path: """Read either a flat transfer bundle or the grouped repository layout.""" directory = Path(directory) preferred = result_path(directory, filename) candidates = {preferred, directory / filename, directory / study_for_run(filename) / filename} present = [p for p in candidates if p.is_file()] if len(present) > 1: raise ValueError(f"Ambiguous result copies for {filename}: {present}") return present[0] if present else preferred def metric_path(run_id: str, suffix: str = "_met.json", *, directory=None) -> Path: return result_path( Path(directory) if directory is not None else PROJECT_ROOT / "results/metrics", run_id + suffix, ) def relocated_result(path: Path | str) -> Path: """Resolve a historical flat result reference without rewriting its ledger. Only the two known repository roots are eligible. Existing paths and references into frozen packages or other directories are returned unchanged. """ path = Path(path) if path.exists(): return path return result_path(path.parent, path.name)