Datasets:
Download code/src/pvr_eval/contracts/compiler.py from wenjieshu/solveedit_benchmark_2728: direct link, hf CLI and curl.
- Browser
- Download file 15.8 kB
-
https://huggingface.co/datasets/wenjieshu/solveedit_benchmark_2728/resolve/main/code/src/pvr_eval/contracts/compiler.py
- Command line
-
hf download hf://datasets/wenjieshu/solveedit_benchmark_2728/code/src/pvr_eval/contracts/compiler.py
-
curl -L -o compiler.py https://huggingface.co/datasets/wenjieshu/solveedit_benchmark_2728/resolve/main/code/src/pvr_eval/contracts/compiler.py
15.8 kB
| """Compile route contracts into formal atomic-check contracts.""" | |
| from __future__ import annotations | |
| from copy import deepcopy | |
| from pathlib import Path | |
| from typing import Any | |
| def _rect_to_params(roi: dict[str, Any] | str | None) -> dict[str, Any]: | |
| if isinstance(roi, dict) and {"x0", "y0", "x1", "y1"} <= set(roi): | |
| return {"roi": deepcopy(roi)} | |
| if isinstance(roi, str): | |
| return {"roi": roi} | |
| return {} | |
| def _normalize_roi(roi: dict[str, Any] | str | None, *, fallback: dict[str, Any] | None = None) -> dict[str, Any]: | |
| if isinstance(roi, dict) and {"x0", "y0", "x1", "y1"} <= set(roi): | |
| return deepcopy(roi) | |
| if fallback is not None: | |
| return deepcopy(fallback) | |
| return {"type": "rect_ratio", "x0": 0.0, "y0": 0.0, "x1": 1.0, "y1": 1.0} | |
| def _make_check( | |
| *, | |
| check_id: str, | |
| checker: str, | |
| dimension: str, | |
| weight: float, | |
| description: str, | |
| params: dict[str, Any], | |
| hard_fail: bool = False, | |
| ) -> dict[str, Any]: | |
| return { | |
| "id": check_id, | |
| "dimension": dimension, | |
| "weight": weight, | |
| "checker": checker, | |
| "description": description, | |
| "params": params, | |
| "hard_fail": hard_fail, | |
| } | |
| def compile_case_checker_contract(case: dict[str, Any]) -> dict[str, Any]: | |
| """Compile one case_checker contract into a formal EvalContract payload.""" | |
| out = { | |
| "case_id": case["case_key"], | |
| "instruction": case.get("instruction", ""), | |
| "task_family": case.get("task_family", ""), | |
| "eval_type": case.get("eval_type", "contract_v1"), | |
| "input_image": case.get("input_image", "images/input.png"), | |
| "quality_gate": case.get("quality_gate", {}), | |
| "manual_facts": case.get("manual_facts", {}), | |
| "zones": case.get("zones", {}), | |
| "metadata": { | |
| "domain": case.get("domain", ""), | |
| "subdomain": case.get("subdomain", ""), | |
| "task_key": case.get("task_key", ""), | |
| "gt_policy": case.get("gt_policy", ""), | |
| "reference_image": case.get("reference_gt_image", case.get("reference_image", "images/gt.png")), | |
| "production_question": case.get("production_question", ""), | |
| "production_pass_criteria": case.get("production_pass_criteria", ""), | |
| "fail_criteria": case.get("fail_criteria", ""), | |
| "decision_contract": deepcopy(case.get("decision_contract", {})), | |
| "primary_checker": case.get("primary_checker", ""), | |
| "secondary_checkers": list(case.get("secondary_checkers", [])), | |
| }, | |
| "atomic_checks": [], | |
| } | |
| checks: list[dict[str, Any]] = [] | |
| case_key = case["case_key"] | |
| family = str(case.get("task_family", "")) | |
| # Zone rules. | |
| for idx, rule in enumerate(case.get("zone_rules", [])): | |
| labels = [str(rule.get("entity", ""))] if rule.get("entity") else [] | |
| zones = rule.get("zones", []) | |
| if not labels or not zones: | |
| continue | |
| relation = str(rule.get("relation", "inside_any")) | |
| checker = "object_zone_checker" | |
| params = { | |
| "labels": labels, | |
| "relation": relation if relation in {"inside_any", "must_exist", "count_inside_min", "count_outside_min"} else "inside_any", | |
| "zones": zones, | |
| "min_score": float(rule.get("min_score", 0.3)), | |
| "min_count": int(rule.get("min_count", 1)), | |
| "min_bbox_containment": float(rule.get("min_bbox_containment", 0.15)), | |
| } | |
| checks.append(_make_check( | |
| check_id=f"zone_rule_{idx+1:02d}", | |
| checker=checker, | |
| dimension="target_completion", | |
| weight=float(rule.get("weight", 0.25)), | |
| description=f"Zone rule for {case_key}", | |
| params=params, | |
| hard_fail=True, | |
| )) | |
| # OCR rules. | |
| for idx, rule in enumerate(case.get("ocr_rules", [])): | |
| relation = str(rule.get("relation", "must_be_readable")) | |
| if relation == "must_be_readable": | |
| checks.append(_make_check( | |
| check_id=f"ocr_rule_{idx+1:02d}", | |
| checker="text_ocr_checker", | |
| dimension="attribute_correctness", | |
| weight=float(rule.get("weight", 0.18)), | |
| description=f"OCR readability rule for {case_key}", | |
| params={"relation": "must_be_readable", "roi": rule.get("roi"), "min_chars": int(rule.get("min_chars", 2))}, | |
| )) | |
| elif relation == "label_set_readable": | |
| checks.append(_make_check( | |
| check_id=f"ocr_rule_{idx+1:02d}", | |
| checker="text_ocr_checker", | |
| dimension="attribute_correctness", | |
| weight=float(rule.get("weight", 0.2)), | |
| description=f"OCR label-set rule for {case_key}", | |
| params={ | |
| "relation": "label_set_readable", | |
| "roi": rule.get("roi"), | |
| "expected_all": rule.get("expected_any", []), | |
| "min_match": int(rule.get("min_match", 1)), | |
| }, | |
| )) | |
| elif relation == "text_or_symbol_matches_expected": | |
| checks.append(_make_check( | |
| check_id=f"ocr_rule_{idx+1:02d}", | |
| checker="text_ocr_checker", | |
| dimension="attribute_correctness", | |
| weight=float(rule.get("weight", 0.2)), | |
| description=f"OCR text/symbol rule for {case_key}", | |
| params={"relation": "must_be_readable", "roi": rule.get("roi"), "min_chars": int(rule.get("min_chars", 2))}, | |
| )) | |
| # Symbol rules. | |
| for idx, rule in enumerate(case.get("symbol_rules", [])): | |
| relation = str(rule.get("relation", "")) | |
| if relation == "path_must_exist": | |
| checks.append(_make_check( | |
| check_id=f"symbol_rule_{idx+1:02d}", | |
| checker="route_checker", | |
| dimension="spatial_relation", | |
| weight=float(rule.get("weight", 0.25)), | |
| description=f"Route/path rule for {case_key}", | |
| params={ | |
| "color": rule.get("color", "green"), | |
| "diff_threshold": int(rule.get("diff_threshold", 32)), | |
| "min_pixels": int(rule.get("min_pixels", 1000)), | |
| "roi": rule.get("roi"), | |
| "detector_labels": rule.get("detector_labels", ["path", "trajectory", "route", "line", "arrow"]), | |
| }, | |
| hard_fail=True, | |
| )) | |
| elif relation == "final_state_matches_reference": | |
| checks.append(_make_check( | |
| check_id=f"symbol_rule_{idx+1:02d}", | |
| checker="gt_state_or_reference_checker", | |
| dimension="attribute_correctness", | |
| weight=float(rule.get("weight", 0.25)), | |
| description=f"Reference symbol-state rule for {case_key}", | |
| params={ | |
| "relation": "gt_state_patch_matches_reference", | |
| "roi": rule.get("roi"), | |
| "max_mae_vs_gt": float(rule.get("max_mae_vs_gt", 0.08)), | |
| "partial_mae_vs_gt": float(rule.get("partial_mae_vs_gt", 0.15)), | |
| }, | |
| )) | |
| elif relation == "checked_state_matches_expected": | |
| checks.append(_make_check( | |
| check_id=f"symbol_rule_{idx+1:02d}", | |
| checker="symbol_checker", | |
| dimension="attribute_correctness", | |
| weight=float(rule.get("weight", 0.22)), | |
| description=f"Checked-state rule for {case_key}", | |
| params={"relation": "must_be_readable", "roi": rule.get("roi"), "min_chars": 1}, | |
| )) | |
| elif relation == "checkmarks_match_reference": | |
| checks.append(_make_check( | |
| check_id=f"symbol_rule_{idx+1:02d}", | |
| checker="gt_state_or_reference_checker", | |
| dimension="attribute_correctness", | |
| weight=0.25, | |
| description=f"Reference checkmark rule for {case_key}", | |
| params={ | |
| "relation": "gt_state_patch_matches_reference", | |
| "roi": rule.get("roi"), | |
| "max_mae_vs_gt": float(rule.get("max_mae_vs_gt", 0.045)), | |
| "partial_mae_vs_gt": float(rule.get("partial_mae_vs_gt", 0.09)), | |
| "max_changed_24_vs_gt": float(rule.get("max_changed_24_vs_gt", 0.05)), | |
| "partial_changed_24_vs_gt": float(rule.get("partial_changed_24_vs_gt", 0.12)), | |
| }, | |
| )) | |
| # State rules. | |
| for idx, rule in enumerate(case.get("state_rules", [])): | |
| relation = str(rule.get("relation", "")) | |
| if relation == "gt_state_patch_matches_reference": | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="gt_state_or_reference_checker", | |
| dimension="attribute_correctness", | |
| weight=0.30, | |
| description=f"Reference-based state rule for {case_key}", | |
| params={ | |
| "relation": relation, | |
| "roi": rule.get("roi"), | |
| "reference_image": rule.get("reference_image") or case.get("reference_gt_image") or case.get("reference_image") or "images/gt.png", | |
| "max_mae_vs_gt": float(rule.get("max_mae_vs_gt", 0.08)), | |
| "partial_mae_vs_gt": float(rule.get("partial_mae_vs_gt", 0.14)), | |
| "max_changed_24_vs_gt": float(rule.get("max_changed_24_vs_gt", 0.15)), | |
| "partial_changed_24_vs_gt": float(rule.get("partial_changed_24_vs_gt", 0.25)), | |
| "allow_partial": bool(rule.get("allow_partial", True)), | |
| }, | |
| hard_fail=False, | |
| )) | |
| elif relation == "lock_inserted_key_colors": | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="state_image_proxy_checker", | |
| dimension="attribute_correctness", | |
| weight=0.28, | |
| description=f"Lock color-state rule for {case_key}", | |
| params=rule, | |
| )) | |
| elif relation == "ocr_terms_in_roi_zones": | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="state_checker", | |
| dimension="attribute_correctness", | |
| weight=0.24, | |
| description=f"Structured state rule for {case_key}", | |
| params={**rule, "state_key": "state"}, | |
| )) | |
| elif relation in {"roi_must_change_from_input", "forbid_loose_colored_keys"}: | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="state_image_proxy_checker", | |
| dimension="invariant_preservation" if relation != "slot_color_presence" else "target_completion", | |
| weight=0.18, | |
| description=f"Image proxy state rule for {case_key}", | |
| params=rule, | |
| )) | |
| elif relation == "slot_color_presence": | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="state_checker", | |
| dimension="target_completion", | |
| weight=0.20, | |
| description=f"Slot color presence rule for {case_key}", | |
| params=rule, | |
| )) | |
| elif relation in {"connectivity_matches_expected", "must_form_connected_path", "final_state_matches_expected"}: | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="gt_state_or_reference_checker", | |
| dimension="attribute_correctness", | |
| weight=float(rule.get("weight", 0.30)), | |
| description=f"State/reference rule for {case_key}", | |
| params={ | |
| "relation": "gt_state_patch_matches_reference", | |
| "roi": rule.get("roi"), | |
| "reference_image": rule.get("reference_image") or case.get("reference_gt_image") or case.get("reference_image") or "images/gt.png", | |
| "max_mae_vs_gt": float(rule.get("max_mae_vs_gt", 0.08)), | |
| "partial_mae_vs_gt": float(rule.get("partial_mae_vs_gt", 0.14)), | |
| "max_changed_24_vs_gt": float(rule.get("max_changed_24_vs_gt", 0.15)), | |
| "partial_changed_24_vs_gt": float(rule.get("partial_changed_24_vs_gt", 0.25)), | |
| "allow_partial": True, | |
| }, | |
| )) | |
| elif relation == "must_exist": | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="object_presence_checker", | |
| dimension="entity_integrity", | |
| weight=float(rule.get("weight", 0.25)), | |
| description=f"Presence rule for {case_key}", | |
| params={"labels": [str(rule.get("entity", ""))], "min_count": 1, "min_score": 0.2}, | |
| )) | |
| else: | |
| checks.append(_make_check( | |
| check_id=f"state_rule_{idx+1:02d}", | |
| checker="gt_state_or_reference_checker", | |
| dimension="attribute_correctness", | |
| weight=0.25, | |
| description=f"Fallback state rule for {case_key}", | |
| params={"relation": "gt_state_patch_matches_reference", "roi": rule.get("roi"), "allow_partial": True}, | |
| )) | |
| # Invariant rules. | |
| for idx, rule in enumerate(case.get("invariant_rules", [])): | |
| relation = str(rule.get("relation", "must_remain_consistent")) | |
| if relation == "must_remain_present": | |
| checks.append(_make_check( | |
| check_id=f"invariant_rule_{idx+1:02d}", | |
| checker="preservation_checker", | |
| dimension="invariant_preservation", | |
| weight=0.20, | |
| description=f"Invariant presence rule for {case_key}", | |
| params={"zones": {"roi": _normalize_roi(rule.get("roi"))}, "pass_max_changed": 0.04, "partial_max_changed": 0.12}, | |
| )) | |
| else: | |
| checks.append(_make_check( | |
| check_id=f"invariant_rule_{idx+1:02d}", | |
| checker="preservation_checker", | |
| dimension="invariant_preservation", | |
| weight=0.18, | |
| description=f"Invariant consistency rule for {case_key}", | |
| params={"roi": _normalize_roi(rule.get("roi")), "pass_max_changed": 0.05, "partial_max_changed": 0.12}, | |
| )) | |
| if not checks: | |
| checks.append(_make_check( | |
| check_id="fallback_observation_check", | |
| checker="observation", | |
| dimension="target_completion", | |
| weight=1.0, | |
| description=f"Fallback observation check for {case_key}", | |
| params={}, | |
| )) | |
| out["atomic_checks"] = checks | |
| return out | |
| def inject_case_paths(contract: dict[str, Any], *, case_dir: str | None = None) -> dict[str, Any]: | |
| """Inject absolute input/reference image paths if a concrete case directory is known.""" | |
| if not case_dir: | |
| return contract | |
| case_root = case_dir | |
| contract["input_image"] = str(Path(case_root) / "images" / "input.png") | |
| contract.setdefault("metadata", {}) | |
| contract["metadata"]["reference_image"] = str(Path(case_root) / "images" / "gt.png") | |
| for check in contract.get("atomic_checks", []): | |
| params = check.get("params") | |
| if isinstance(params, dict) and params.get("reference_image") in {None, "", "images/gt.png"}: | |
| params["reference_image"] = str(Path(case_root) / "images" / "gt.png") | |
| return contract | |