Datasets:
Download code/src/pvr_eval/checks.py from wenjieshu/solveedit_benchmark_2728: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/datasets/wenjieshu/solveedit_benchmark_2728/resolve/main/code/src/pvr_eval/checks.py
- Command line
-
hf download hf://datasets/wenjieshu/solveedit_benchmark_2728/code/src/pvr_eval/checks.py
-
curl -L -o checks.py https://huggingface.co/datasets/wenjieshu/solveedit_benchmark_2728/resolve/main/code/src/pvr_eval/checks.py
3.8 kB
| """Atomic check evaluation from eval specs and observation files.""" | |
| from __future__ import annotations | |
| from typing import Any | |
| from pvr_eval.schema import CHECK_DIMENSION_BY_TYPE | |
| STATUS_TO_SCORE = { | |
| "pass": 1.0, | |
| "partial": 0.5, | |
| "fail": 0.0, | |
| "uncertain": 0.0, | |
| } | |
| def dimension_for_check(check: dict[str, Any]) -> str: | |
| if check.get("dimension"): | |
| return str(check["dimension"]) | |
| return CHECK_DIMENSION_BY_TYPE.get(str(check.get("type")), "instruction") | |
| def evaluate_atomic_check(check: dict[str, Any], observations: dict[str, Any]) -> dict[str, Any]: | |
| check_id = str(check["id"]) | |
| observed = observations.get("atomic_checks", {}).get(check_id) | |
| dimension = dimension_for_check(check) | |
| if observed is None: | |
| status = "uncertain" | |
| score = 0.0 | |
| confidence = 0.0 | |
| evidence = {"reason": "missing_observation"} | |
| elif isinstance(observed, bool): | |
| status = "pass" if observed else "fail" | |
| score = STATUS_TO_SCORE[status] | |
| confidence = 1.0 | |
| evidence = {"source": "boolean_observation"} | |
| elif isinstance(observed, (int, float)): | |
| score = max(0.0, min(1.0, float(observed))) | |
| status = "pass" if score >= 0.99 else "partial" if score > 0 else "fail" | |
| confidence = 1.0 | |
| evidence = {"source": "numeric_observation"} | |
| elif isinstance(observed, dict): | |
| status = str(observed.get("status", "uncertain")) | |
| score = observed.get("score") | |
| if score is None: | |
| score = STATUS_TO_SCORE.get(status, 0.0) | |
| score = max(0.0, min(1.0, float(score))) | |
| confidence = float(observed.get("confidence", 0.0 if status == "uncertain" else 1.0)) | |
| evidence = dict(observed.get("evidence", {})) | |
| else: | |
| status = "uncertain" | |
| score = 0.0 | |
| confidence = 0.0 | |
| evidence = {"reason": f"unsupported_observation_type:{type(observed).__name__}"} | |
| return { | |
| "check_id": check_id, | |
| "type": check.get("type"), | |
| "dimension": dimension, | |
| "status": status, | |
| "score": score, | |
| "weight": float(check.get("weight", 1.0)), | |
| "hard_fail": bool(check.get("hard_fail", False)), | |
| "confidence": max(0.0, min(1.0, confidence)), | |
| "evidence": evidence, | |
| } | |
| def aggregate_dimension_scores( | |
| check_results: list[dict[str, Any]], | |
| observation_dimension_scores: dict[str, Any] | None = None, | |
| ) -> dict[str, float]: | |
| scores: dict[str, float] = {} | |
| weights: dict[str, float] = {} | |
| for result in check_results: | |
| if str(result.get("status", "uncertain")) == "uncertain" and float(result.get("confidence", 0.0)) <= 0.0: | |
| continue | |
| dimension = str(result["dimension"]) | |
| weight = float(result.get("weight", 1.0)) | |
| scores[dimension] = scores.get(dimension, 0.0) + float(result["score"]) * weight | |
| weights[dimension] = weights.get(dimension, 0.0) + weight | |
| dimension_scores = {dimension: scores[dimension] / weights[dimension] for dimension in scores if weights[dimension]} | |
| statuses_by_dimension: dict[str, list[str]] = {} | |
| for result in check_results: | |
| dimension = str(result["dimension"]) | |
| statuses_by_dimension.setdefault(dimension, []).append(str(result.get("status", "uncertain"))) | |
| for dimension, value in (observation_dimension_scores or {}).items(): | |
| if not isinstance(value, (int, float)): | |
| continue | |
| check_statuses = statuses_by_dimension.get(dimension, []) | |
| atomic_dimension_is_unresolved = bool(check_statuses) and all(status == "uncertain" for status in check_statuses) | |
| if dimension not in dimension_scores or atomic_dimension_is_unresolved: | |
| dimension_scores[dimension] = max(0.0, min(1.0, float(value))) | |
| return dimension_scores | |