Spaces:
Sleeping
Sleeping
File size: 15,417 Bytes
a6bae97 4eb91d0 a6bae97 c813aa1 5d3ba70 a6bae97 4eb91d0 a6bae97 9011070 a6bae97 9011070 a6bae97 d109222 9011070 a6bae97 a2bea75 a6bae97 | 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | """Tests Sprint 86 β A.II.5 bout-en-bout : helpers runner +
rendu HTML.
Couvre :
1. ``compute_searchability_metrics`` adaptive masking.
2. ``aggregate_searchability_metrics`` micro-recall.
3. ``compute_numerical_sequence_metrics_adaptive`` masking.
4. ``aggregate_numerical_sequence_metrics`` somme par catΓ©gorie.
5. Champs ``DocumentResult.searchability_metrics`` et
``EngineReport.aggregated_searchability``.
6. Rendu HTML adaptive + anti-injection.
7. ComplΓ©tude i18n FR/EN.
"""
from __future__ import annotations
import json
from pathlib import Path
from picarones.evaluation.metrics.numerical_sequences_hooks import (
aggregate_numerical_sequence_metrics,
compute_numerical_sequence_metrics_adaptive,
)
from picarones.evaluation.metric_result import MetricsResult
from picarones.evaluation.benchmark_result import DocumentResult, EngineReport
def _stub_metrics() -> MetricsResult:
return MetricsResult(
cer=0.0, cer_nfc=0.0, cer_caseless=0.0,
wer=0.0, wer_normalized=0.0, mer=0.0, wil=0.0,
reference_length=0, hypothesis_length=0,
)
from picarones.evaluation.metrics.searchability_hooks import (
aggregate_searchability_metrics,
compute_searchability_metrics,
)
from picarones.reports.html.renderers.numerical_sequences import (
build_numerical_sequences_html,
)
from picarones.reports.html.renderers.searchability import (
build_searchability_summary_html,
)
def _load_labels(lang: str) -> dict:
p = (
Path(__file__).parent.parent.parent
/ "picarones" / "reports" / "i18n" / f"{lang}.json"
)
return json.loads(p.read_text(encoding="utf-8"))
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. Helpers searchability
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestSearchabilityRunner:
def test_empty_gt_returns_none(self) -> None:
assert compute_searchability_metrics("", "anything") is None
def test_normal(self) -> None:
r = compute_searchability_metrics("le roi", "le roy")
assert r is not None
assert r["recall"] == 1.0
assert r["n_gt_tokens"] == 2
def test_aggregate_micro_recall(self) -> None:
d1 = {"n_gt_tokens": 10, "n_searchable": 9, "missed_tokens": ["x"]}
d2 = {"n_gt_tokens": 20, "n_searchable": 15, "missed_tokens": ["y"]}
agg = aggregate_searchability_metrics([d1, d2])
assert agg is not None
assert agg["n_gt_tokens"] == 30
assert agg["n_searchable"] == 24
assert agg["recall"] == 24 / 30
assert agg["n_docs"] == 2
def test_aggregate_empty(self) -> None:
assert aggregate_searchability_metrics([None, None]) is None
assert aggregate_searchability_metrics([]) is None
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. Helpers numerical sequences
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestNumericalSequencesRunner:
def test_no_signal_returns_none(self) -> None:
# GT sans aucune sΓ©quence numΓ©rique
assert compute_numerical_sequence_metrics_adaptive(
"lorem ipsum dolor", "sit amet",
) is None
def test_signal_present(self) -> None:
r = compute_numerical_sequence_metrics_adaptive(
"an III, 1789", "an III, 1789",
)
assert r is not None
assert r["n_total"] >= 1
def test_aggregate_sums_per_category(self) -> None:
d1 = {
"n_total": 3,
"global_strict_score": 1.0,
"global_value_score": 1.0,
"per_category": {
"year": {"n_total": 2, "strict": 2, "value": 2,
"strict_score": 1.0, "value_score": 1.0,
"lost_items": []},
"roman": {"n_total": 1, "strict": 1, "value": 1,
"strict_score": 1.0, "value_score": 1.0,
"lost_items": []},
"foliation": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
"currency": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
"regnal": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
},
}
d2 = {
"n_total": 4,
"global_strict_score": 0.5,
"global_value_score": 0.5,
"per_category": {
"year": {"n_total": 4, "strict": 2, "value": 2,
"strict_score": 0.5, "value_score": 0.5,
"lost_items": ["1500", "1600"]},
"roman": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
"foliation": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
"currency": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
"regnal": {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []},
},
}
agg = aggregate_numerical_sequence_metrics([d1, d2])
assert agg["n_total"] == 7
assert agg["per_category"]["year"]["n_total"] == 6
assert agg["per_category"]["year"]["strict"] == 4
assert agg["per_category"]["year"]["strict_score"] == 4 / 6
# global = (2+1 + 2) / 7 = 5/7
assert agg["global_strict_score"] == 5 / 7
def test_aggregate_empty(self) -> None:
assert aggregate_numerical_sequence_metrics([None]) is None
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3. Champs results.py
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestResultsFields:
def test_document_result_serializes_searchability(self) -> None:
dr = DocumentResult(
doc_id="doc1", image_path="x.png",
ground_truth="hello", hypothesis="helo",
metrics=_stub_metrics(), duration_seconds=1.0,
searchability_metrics={"recall": 0.9},
numerical_sequence_metrics={"n_total": 1},
)
d = dr.as_dict()
assert d["searchability_metrics"] == {"recall": 0.9}
assert d["numerical_sequence_metrics"] == {"n_total": 1}
def test_document_result_omits_when_none(self) -> None:
dr = DocumentResult(
doc_id="doc1", image_path="x.png",
ground_truth="hello", hypothesis="helo",
metrics=_stub_metrics(), duration_seconds=1.0,
)
d = dr.as_dict()
assert "searchability_metrics" not in d
assert "numerical_sequence_metrics" not in d
def test_compact_clears_fields(self) -> None:
dr = DocumentResult(
doc_id="doc1", image_path="x.png",
ground_truth="hello", hypothesis="helo",
metrics=_stub_metrics(), duration_seconds=1.0,
searchability_metrics={"recall": 0.9},
numerical_sequence_metrics={"n_total": 1},
)
# Sprint A14-S1 β opt-in via drop_analyses=True.
dr.compact(drop_analyses=True)
assert dr.searchability_metrics is None
assert dr.numerical_sequence_metrics is None
def test_engine_report_serializes_aggregates(self) -> None:
er = EngineReport(
engine_name="t", engine_version="0",
engine_config={},
document_results=[],
pipeline_info=None,
aggregated_searchability={"recall": 0.85},
aggregated_numerical_sequences={"global_strict_score": 0.9},
)
d = er.as_dict()
assert d["aggregated_searchability"]["recall"] == 0.85
assert d["aggregated_numerical_sequences"]["global_strict_score"] == 0.9
def test_engine_report_omits_when_none(self) -> None:
er = EngineReport(
engine_name="t", engine_version="0",
engine_config={},
document_results=[],
pipeline_info=None,
)
d = er.as_dict()
assert "aggregated_searchability" not in d
assert "aggregated_numerical_sequences" not in d
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4. Rendu HTML
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestSearchabilityHtml:
def test_empty_returns_empty(self) -> None:
assert build_searchability_summary_html([]) == ""
def test_no_signal_returns_empty(self) -> None:
engines = [{"name": "t"}] # pas de aggregated_searchability
assert build_searchability_summary_html(engines) == ""
def test_renders_table_with_recall(self) -> None:
engines = [{
"name": "tess",
"aggregated_searchability": {
"recall": 0.92, "n_searchable": 92,
"n_gt_tokens": 100, "n_docs": 5,
},
}]
html = build_searchability_summary_html(
engines, _load_labels("fr"),
)
assert "<table" in html
assert "92.0%" in html
assert "92 / 100" in html
assert "tess" in html
def test_anti_injection(self) -> None:
engines = [{
"name": "<script>alert(1)</script>",
"aggregated_searchability": {
"recall": 0.5, "n_searchable": 5, "n_gt_tokens": 10,
"n_docs": 1,
},
}]
html = build_searchability_summary_html(
engines, _load_labels("fr"),
)
assert "<script>alert" not in html
assert "<script>" in html
def test_renders_in_english(self) -> None:
engines = [{
"name": "tess",
"aggregated_searchability": {
"recall": 0.95, "n_searchable": 95,
"n_gt_tokens": 100, "n_docs": 5,
},
}]
html = build_searchability_summary_html(
engines, _load_labels("en"),
)
assert "Fuzzy searchability" in html
class TestNumericalSequencesHtml:
def _engine(self, name="tess", **kwargs) -> dict:
per_cat_default = {
cat: {"n_total": 0, "strict": 0, "value": 0,
"strict_score": 0.0, "value_score": 0.0,
"lost_items": []}
for cat in ("year", "roman", "foliation", "currency", "regnal")
}
per_cat_default.update(kwargs.get("per_cat_overrides", {}))
return {
"name": name,
"aggregated_numerical_sequences": {
"global_strict_score": kwargs.get("strict", 0.5),
"global_value_score": kwargs.get("value", 0.5),
"n_total": kwargs.get("n_total", 1),
"n_docs": 1,
"per_category": per_cat_default,
},
}
def test_empty_returns_empty(self) -> None:
assert build_numerical_sequences_html([]) == ""
def test_no_signal_returns_empty(self) -> None:
engines = [{"name": "t"}]
assert build_numerical_sequences_html(engines) == ""
def test_omits_categories_without_signal(self) -> None:
# Seul 'year' a du signal
e = self._engine(per_cat_overrides={
"year": {"n_total": 5, "strict": 5, "value": 5,
"strict_score": 1.0, "value_score": 1.0,
"lost_items": []},
})
html = build_numerical_sequences_html([e], _load_labels("fr"))
assert "AnnΓ©e" in html
# Romain absent puisqu'aucun n_total > 0
assert "Romain" not in html
def test_renders_per_category_score(self) -> None:
e = self._engine(strict=0.8, value=0.9, n_total=20,
per_cat_overrides={
"year": {"n_total": 10, "strict": 8, "value": 9,
"strict_score": 0.8, "value_score": 0.9,
"lost_items": []},
})
html = build_numerical_sequences_html([e], _load_labels("fr"))
assert "80%" in html # year strict score
assert "n=20" in html or "n=10" in html
def test_anti_injection(self) -> None:
e = self._engine(name="<img/>", per_cat_overrides={
"year": {"n_total": 1, "strict": 1, "value": 1,
"strict_score": 1.0, "value_score": 1.0,
"lost_items": []},
})
html = build_numerical_sequences_html([e], _load_labels("fr"))
assert "<img/>" not in html
assert "<img" in html
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5. ComplΓ©tude i18n
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_KEYS = {
"search_title", "search_note", "search_engine", "search_recall",
"search_count", "search_docs",
"numseq_title", "numseq_note", "numseq_engine", "numseq_global",
"numseq_cat_year", "numseq_cat_roman", "numseq_cat_foliation",
"numseq_cat_currency", "numseq_cat_regnal",
}
class TestI18nCompleteness:
def test_fr_has_all(self) -> None:
d = _load_labels("fr")
missing = _KEYS - d.keys()
assert not missing, f"manque FR : {missing}"
def test_en_has_all(self) -> None:
d = _load_labels("en")
missing = _KEYS - d.keys()
assert not missing, f"manque EN : {missing}"
|