File size: 3,802 Bytes
b8ea052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""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