Datasets:
Download validator/vendor/physics.py from RegalFire/Agent-Failure-Recovery-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 5.71 kB
-
https://huggingface.co/datasets/RegalFire/Agent-Failure-Recovery-Benchmark/resolve/main/validator/vendor/physics.py
- Command line
-
hf download hf://datasets/RegalFire/Agent-Failure-Recovery-Benchmark/validator/vendor/physics.py
-
curl -L -o physics.py https://huggingface.co/datasets/RegalFire/Agent-Failure-Recovery-Benchmark/resolve/main/validator/vendor/physics.py
5.71 kB
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import math | |
| from collections import Counter | |
| from pathlib import Path | |
| DT = 0.1 | |
| G = 9.81 | |
| FAMILIES = { | |
| "gravity_fall", | |
| "projectile_motion", | |
| "elastic_collision_1d", | |
| "inelastic_collision_1d", | |
| "friction_slide", | |
| "bounce", | |
| "constant_velocity_occlusion", | |
| "support_removal", | |
| "containment", | |
| "momentum_transfer", | |
| } | |
| def canon(x): | |
| return json.dumps(x, sort_keys=True, separators=(",", ":"), ensure_ascii=False) | |
| def sha(x): | |
| return hashlib.sha256(canon(x).encode()).hexdigest() | |
| def close(a,b,tol=2e-5): | |
| return abs(float(a)-float(b)) <= tol | |
| def truth(row): | |
| s = row["state_t0"] | |
| a = row["action"] | |
| fam = row["family"] | |
| if fam == "gravity_fall": | |
| y = max(0.0, s["y"] + s["vy"]*DT - .5*G*DT*DT) | |
| vy = 0.0 if y == 0 else s["vy"]-G*DT | |
| return {"y":y,"vy":vy} | |
| if fam == "projectile_motion": | |
| y = max(0.0, s["y"] + s["vy"]*DT - .5*G*DT*DT) | |
| return { | |
| "x":s["x"]+s["vx"]*DT, | |
| "y":y, | |
| "vx":s["vx"], | |
| "vy":0.0 if y == 0 else s["vy"]-G*DT | |
| } | |
| if fam == "elastic_collision_1d": | |
| m1,m2,u1,u2 = s["m1"],s["m2"],s["u1"],s["u2"] | |
| return { | |
| "v1":((m1-m2)/(m1+m2))*u1 + ((2*m2)/(m1+m2))*u2, | |
| "v2":((2*m1)/(m1+m2))*u1 + ((m2-m1)/(m1+m2))*u2 | |
| } | |
| if fam == "inelastic_collision_1d": | |
| m1,m2,u1,u2 = s["m1"],s["m2"],s["u1"],s["u2"] | |
| v=(m1*u1+m2*u2)/(m1+m2) | |
| return {"v1":v,"v2":v} | |
| if fam == "friction_slide": | |
| v=s["velocity"] | |
| decel=s["mu"]*G*DT | |
| nv=0.0 if abs(v)<=decel else math.copysign(abs(v)-decel,v) | |
| return {"velocity":nv} | |
| if fam == "bounce": | |
| return { | |
| "y":0.0, | |
| "vy":-s["vy"]*s["restitution"] | |
| } | |
| if fam == "constant_velocity_occlusion": | |
| return { | |
| "x":s["x"]+s["vx"]*s["frames_hidden"]*DT, | |
| "vx":s["vx"], | |
| "visible":False | |
| } | |
| if fam == "support_removal": | |
| return { | |
| "y":max(0.0,s["y"]-.5*G*DT*DT), | |
| "vy":-G*DT | |
| } | |
| if fam == "containment": | |
| nx=s["x"]+a["dx"] | |
| ny=s["y"]+a["dy"] | |
| cx=s["container_x"] | |
| cy=s["container_y"] | |
| radius=s["radius"] | |
| d=math.hypot(nx-cx,ny-cy) | |
| if d>radius: | |
| scale=radius/d | |
| nx=cx+(nx-cx)*scale | |
| ny=cy+(ny-cy)*scale | |
| return {"x":nx,"y":ny,"contained":True} | |
| if fam == "momentum_transfer": | |
| m1,m2=s["m1"],s["m2"] | |
| u1,u2=s["u1"],s["u2"] | |
| e=s["restitution"] | |
| return { | |
| "v1":(m1*u1+m2*u2-m2*e*(u1-u2))/(m1+m2), | |
| "v2":(m1*u1+m2*u2+m1*e*(u1-u2))/(m1+m2) | |
| } | |
| raise ValueError(fam) | |
| def compare(calc, stored): | |
| if set(calc) != set(stored): | |
| return False | |
| for k,v in calc.items(): | |
| sv=stored[k] | |
| if isinstance(v,bool): | |
| if v is not sv: | |
| return False | |
| elif not close(v,sv): | |
| return False | |
| return True | |
| def validate(path): | |
| ids=set() | |
| fps=set() | |
| families=Counter() | |
| splits=Counter() | |
| count=0 | |
| with path.open("r",encoding="utf-8") as f: | |
| for line in f: | |
| if not line.strip(): | |
| continue | |
| r=json.loads(line) | |
| count += 1 | |
| if r["family"] not in FAMILIES: | |
| raise ValueError("unknown family") | |
| if r["id"] in ids: | |
| raise ValueError("duplicate id") | |
| if r["fingerprint_sha256"] in fps: | |
| raise ValueError("duplicate fingerprint") | |
| ids.add(r["id"]) | |
| fps.add(r["fingerprint_sha256"]) | |
| body={ | |
| k:v for k,v in r.items() | |
| if k not in {"id","fingerprint_sha256"} | |
| } | |
| if sha(body)[:32] != r["id"]: | |
| raise ValueError(f"id mismatch {r['id']}") | |
| with_id=dict(body) | |
| with_id["id"]=r["id"] | |
| if sha(with_id) != r["fingerprint_sha256"]: | |
| raise ValueError(f"fingerprint mismatch {r['id']}") | |
| calc=truth(r) | |
| if not compare(calc,r["expected_state_t1"]): | |
| raise ValueError(f"oracle mismatch {r['id']}") | |
| changed=[ | |
| k for k in r["expected_state_t1"] | |
| if r["expected_state_t1"][k] != r["counterfactual_state_t1"][k] | |
| ] | |
| if changed != [r["counterfactual_changed_field"]]: | |
| raise ValueError(f"counterfactual mismatch {r['id']}") | |
| if compare(calc,r["counterfactual_state_t1"]): | |
| raise ValueError(f"counterfactual accidentally valid {r['id']}") | |
| p=r["provenance"] | |
| if not p["synthetic"] or p["copied_records"] != 0 or p["contains_pii"]: | |
| raise ValueError("provenance") | |
| if p["llm_generated_ground_truth"]: | |
| raise ValueError("ground truth provenance") | |
| families[r["family"]] += 1 | |
| splits[r["split"]] += 1 | |
| return { | |
| "status":"PASS", | |
| "validated":count, | |
| "unique_ids":len(ids), | |
| "unique_fingerprints":len(fps), | |
| "families":dict(families), | |
| "splits":dict(splits) | |
| } | |
| def main(): | |
| ap=argparse.ArgumentParser() | |
| ap.add_argument("dataset",type=Path) | |
| ap.add_argument("--report",type=Path) | |
| a=ap.parse_args() | |
| report=validate(a.dataset) | |
| if a.report: | |
| a.report.write_text( | |
| json.dumps(report,indent=2), | |
| encoding="utf-8" | |
| ) | |
| print(json.dumps(report)) | |
| if __name__=="__main__": | |
| main() | |