okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5 fccdb48a3 (image noise switch, step 1 only) into f-own-int150c2394f

main
2.0 KiB50 linesraw
1"""tools/image/gate/check_limits.py - TEST-ONLY (OWN-5 gate harness): apply a limit from limits.py to a result file.
2
3    python -I -B check_limits.py <limit name> <cmp json>            compare.py's <pics>/cmp/<label>.json (pictures)
4    python -I -B check_limits.py <limit name> <stage json> <stage>   stage_cmp.py's <root>/cmp/stage_*.json, one stage
5
6Prints PASS or FAIL with the violations; exit 0 on pass, 1 on fail, 2 on a usage or input error."""
7import json
8import math
9import sys
10from pathlib import Path
11
12sys.path.insert(0, str(Path(__file__).resolve().parent))
13import limits  # noqa: E402
14
15
16def from_pictures(summary: dict) -> dict:
17    inf = math.inf
18    return {"identical_all": summary.get("bytes_equal") == summary.get("pairs") and summary.get("pairs", 0) > 0,
19            "psnr_min": summary.get("psnr_min", inf), "ssim_min": summary.get("ssim_min", 1.0),
20            "psnr_mean_min": summary.get("psnr_mean", inf), "ssim_mean_min": summary.get("ssim_mean", 1.0),
21            "psnr_min_min": summary.get("psnr_min", inf)}
22
23
24def from_stage(summary: dict) -> dict:
25    return {"identical_all": summary.get("identical") == summary.get("n") and summary.get("n", 0) > 0,
26            "rel_l2_max": summary.get("rel_l2_max", 0.0), "cos_min": summary.get("cos_min", 1.0),
27            "token_cos_min": summary.get("token_cos_min", 1.0)}
28
29
30def main() -> int:
31    a = sys.argv[1:]
32    if len(a) not in (2, 3) or a[0] not in limits.LIMITS:
33        print(__doc__)
34        print("limit names:", ", ".join(limits.LIMITS))
35        return 2
36    data = json.loads(Path(a[1]).read_text(encoding="utf-8"))
37    if len(a) == 3:
38        if a[2] not in data:
39            print(f"no stage {a[2]!r} in {a[1]}")
40            return 2
41        got = from_stage(data[a[2]]["summary"])
42    else:
43        got = from_pictures(data["summary"])
44    bad = limits.passes(a[0], got)
45    print(("FAIL " + "; ".join(bad)) if bad else f"PASS {a[0]}")
46    return 1 if bad else 0
47
48
49if __name__ == "__main__":
50    sys.exit(main())