Spaces:
Sleeping
Sleeping
File size: 6,051 Bytes
a91323c | 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Tuple, Iterable, Set, Any
import math
from .alignment import AlignmentEngine
from .models import StrategicObjective, ActionTask
@dataclass
class EvalConfig:
top_k: int = 5
def precision_recall_at_k(
pred_ids: List[str], truth_ids: Set[str], k: int
) -> Tuple[float, float]:
preds = pred_ids[:k]
hits = sum(1 for pid in preds if pid in truth_ids)
precision = hits / max(1, len(preds))
recall = hits / max(1, len(truth_ids))
return precision, recall
def average_precision(pred_ids: List[str], truth_ids: Set[str]) -> float:
hits = 0
ap_sum = 0.0
for i, pid in enumerate(pred_ids, start=1):
if pid in truth_ids:
hits += 1
ap_sum += hits / i
return ap_sum / max(1, hits)
def ndcg_at_k(pred_ids: List[str], truth_ids: Set[str], k: int) -> float:
# Relevance is binary: 1 if in truth, else 0
dcg = 0.0
for i, pid in enumerate(pred_ids[:k], start=1):
rel = 1.0 if pid in truth_ids else 0.0
dcg += rel / math.log2(i + 1)
# Ideal DCG assumes all relevant items are ranked first
ideal_rel_count = min(len(truth_ids), k)
idcg = sum(1.0 / math.log2(i + 1) for i in range(1, ideal_rel_count + 1))
return dcg / idcg if idcg > 0 else 0.0
@dataclass
class StrategyEval:
strategy_id: str
precision_at_k: float
recall_at_k: float
ap: float
ndcg: float
@dataclass
class EvalSummary:
top_k: int
macro_precision: float
macro_recall: float
map: float
mean_ndcg: float
per_strategy: List[StrategyEval]
similarity_summary: Dict[str, float] | None = None
def evaluate_alignment(
engine: AlignmentEngine,
strategies: Iterable[StrategicObjective],
actions: Iterable[ActionTask],
ground_truth: Dict[str, List[str]],
config: EvalConfig | None = None,
) -> EvalSummary:
cfg = config or EvalConfig()
# Run alignment retrieval
result = engine.align(
strategies=list(strategies), actions=list(actions), top_k=cfg.top_k
)
per_strategy: List[StrategyEval] = []
p_list: List[float] = []
r_list: List[float] = []
ap_list: List[float] = []
ndcg_list: List[float] = []
for sres in result["strategy_results"]:
sid = sres["strategy_id"]
preds = [m["action_id"] for m in sres.get("top_matches", [])]
truth = set(ground_truth.get(sid, []))
p, r = precision_recall_at_k(preds, truth, cfg.top_k)
ap = average_precision(preds, truth)
nd = ndcg_at_k(preds, truth, cfg.top_k)
per_strategy.append(
StrategyEval(
strategy_id=sid,
precision_at_k=p,
recall_at_k=r,
ap=ap,
ndcg=nd,
)
)
p_list.append(p)
r_list.append(r)
ap_list.append(ap)
ndcg_list.append(nd)
summary = EvalSummary(
top_k=cfg.top_k,
macro_precision=sum(p_list) / max(1, len(p_list)),
macro_recall=sum(r_list) / max(1, len(r_list)),
map=sum(ap_list) / max(1, len(ap_list)),
mean_ndcg=sum(ndcg_list) / max(1, len(ndcg_list)),
per_strategy=per_strategy,
similarity_summary=None,
)
return summary
def precision_at_k(pred_ids: List[str], truth_ids: Set[str], k: int) -> float:
p, _ = precision_recall_at_k(pred_ids, truth_ids, k)
return p
def recall_at_k(pred_ids: List[str], truth_ids: Set[str], k: int) -> float:
_, r = precision_recall_at_k(pred_ids, truth_ids, k)
return r
def run_evaluation(
alignment_result: Dict[str, Any], ground_truth_path: str | None, top_k: int = 5
) -> Dict[str, Any]:
"""Compute Precision@K, Recall@K and similarity summaries given alignment results.
Ground truth format: {"S1": ["A3","A9"], "S2": ["A2"], ...}
"""
import json
from pathlib import Path
truth_map: Dict[str, List[str]] = {}
if ground_truth_path:
p = Path(ground_truth_path)
if p.exists():
with p.open("r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
truth_map = {str(k): list(v or []) for k, v in data.items()}
per_strategy: List[Dict[str, Any]] = []
p_list: List[float] = []
r_list: List[float] = []
retrieved_sims: List[float] = []
relevant_sims: List[float] = []
for sres in alignment_result.get("strategy_results", []):
sid = sres.get("strategy_id")
preds = [m.get("action_id") for m in sres.get("top_matches", [])]
sims = [float(m.get("similarity", 0.0)) for m in sres.get("top_matches", [])]
truth = set(truth_map.get(str(sid), []))
p, r = precision_recall_at_k(preds, truth, top_k)
ap = average_precision(preds, truth)
nd = ndcg_at_k(preds, truth, top_k)
per_strategy.append(
{
"strategy_id": sid,
"precision_at_k": p,
"recall_at_k": r,
"ap": ap,
"ndcg": nd,
}
)
p_list.append(p)
r_list.append(r)
# Similarity summaries
retrieved_sims.extend(sims)
# Relevant sims: similarity of matches that are in ground truth
for m in sres.get("top_matches", []):
if m.get("action_id") in truth:
relevant_sims.append(float(m.get("similarity", 0.0)))
eval_summary = {
"top_k": top_k,
"macro_precision": sum(p_list) / max(1, len(p_list)),
"macro_recall": sum(r_list) / max(1, len(r_list)),
"per_strategy": per_strategy,
"similarity_summary": {
"retrieved_mean": (sum(retrieved_sims) / max(1, len(retrieved_sims)))
if retrieved_sims
else 0.0,
"relevant_mean": (sum(relevant_sims) / max(1, len(relevant_sims)))
if relevant_sims
else 0.0,
},
}
return eval_summary
|