okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
1"""tools/image/gate/check_limits.py - TEST-ONLY (OWN-5 gate harness): apply a limit from limits.py to a result file. 2 3python -I -B check_limits.py <limit name> <cmp json> compare.py's <pics>/cmp/<label>.json (pictures) 4python -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 : 17inf = math .inf 18return {"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 : 25return {"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 : 31a = sys .argv [1 :] 32if len (a )not in (2 ,3 )or a [0 ]not in limits .LIMITS : 33__doc__ ) 34"limit names:" ,", " .join (limits .LIMITS )) 35return 2 36data = json .loads (Path (a [1 ]).read_text (encoding = "utf-8" )) 37if len (a )== 3 : 38if a [2 ]not in data : 39f"no stage { a [ 2 ]!r } in { a [ 1 ] } " ) 40return 2 41got = from_stage (data [a [2 ]]["summary" ]) 42else : 43got = from_pictures (data ["summary" ]) 44bad = limits .passes (a [0 ],got ) 45"FAIL " + "; " .join (bad ))if bad else f"PASS { a [ 0 ] } " ) 46return 1 if bad else 0 47 48 49if __name__ == "__main__" : 50sys .exit (main ())