File size: 6,292 Bytes
2e511b5 | 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 | import csv
from legex.analysis.report import build_report
def _write(path, header, rows):
with path.open("w", encoding="utf-8", newline="") as f:
w = csv.writer(f)
w.writerow(header)
w.writerows(rows)
def test_build_report_renders_sections_and_live_numbers(tmp_path) -> None:
iaa = tmp_path / "iaa"
iaa.mkdir()
analysis = tmp_path
_write(
iaa / "pairwise_agreement.csv",
["annotator_a", "annotator_b", "country", "field", "n",
"pct_exact", "pct_tolerant", "cohen_kappa"],
[
# categorical (fixed-vocab) -> kappa shown
["primary", "a1b2c3d4e5", "de", "plaintiff_no1_ISIC1_industry_category", "20", "0.5000", "0.5000", "0.4000"],
# monetary -> kappa computed in CSV but suppressed in the report
["primary", "a1b2c3d4e5", "de", "court_cost_awarded_nominal", "20", "0.6000", "0.6500", "0.3000"],
# free text -> percent only, kappa undefined
["primary", "a1b2c3d4e5", "de", "legal_subject_judgement", "20", "0.1000", "0.1000", ""],
],
)
# Reference-implementation output (scripts/alt_test_reference.py); an
# empty string marks an untestable (country, field, variant) cell.
_write(
iaa / "alt_test_reference_gpt-5.4-mini.csv",
["candidate", "country", "field",
"winning_rate", "advantage_probability", "passes",
"winning_rate_nontrivial", "advantage_probability_nontrivial",
"passes_nontrivial"],
[
["gpt-5.4-mini", "de", "plaintiffs_all_count",
"1.0", "0.95", "1", "1.0", "0.93", "1"],
["gpt-5.4-mini", "de", "dispute_value_nominal",
"0.3333", "0.7", "0", "", "", ""],
],
)
_write(
analysis / "per_column.csv",
["model", "column", "tp", "mismatch", "missed", "hallucinated", "tn",
"accuracy", "recall_when_filled", "precision_when_emitted",
"hallucination_rate", "miss_rate", "wrong_when_both_filled", "f1"],
[["gpt-5.4-mini", "plaintiffs_all_count", "8", "1", "1", "0", "10",
"0.9000", "0.8000", "0.8889", "0.0000", "0.1000", "0.1000", "0.8400"]],
)
md = build_report(iaa, analysis)
for header in (
"# Inter-Annotator Agreement",
"## 1. Scope",
"## 2. Human–human agreement",
"## 3. Alternative-annotator test",
"## 4. Headline extraction metrics",
"Landis–Koch",
):
assert header in md, header
assert "### 2.1 By variable" in md
assert "0.400" in md # ISIC (categorical) kappa is shown
assert "0.300" not in md # monetary kappa is suppressed, not displayed
assert "65.0%" in md # monetary tolerant percent agreement
assert "±" in md # uncertainty (±1 SE) is shown
assert "nominal" in md and "monetary" in md # measurement-level labels
assert "a1b2c3d4e5" in md # by-pair table
assert "80.0%" in md # recall_when_filled rendered as percent (section 4)
assert "1/2" in md # alt-test pass count (1 of 2 testable cells)
assert "1/1" in md # non-trivial pass count (untestable cell excluded)
assert "0.82" in md # mean advantage probability rho
assert "0.93" in md # non-trivial mean rho (only the testable cell)
# provenance: numbers come from the authors' reference implementation
assert "nitaytech/AltTest" in md
assert "alt_test_reference.py" in md
# per-field grids for every headline metric
assert "### 4.3 Precision by field" in md
assert "### 4.4 Hallucination rate by field" in md
assert "### 4.5 F1 by field" in md
assert "0.840" in md # F1 rendered as 0–1, not percent
# the alt-test formula block is always rendered, CSVs or not
assert "### 3.1 How ω and ρ are computed" in md
assert "rho_j = (1/n_j)" in md # advantage probability, with its >=
assert "(1 - epsilon) / 2 = 0.4" in md # the threshold epsilon actually buys
# without the decomposition CSV, §3.4 degrades to a pointer
assert "### 3.4 Substitutable vs. better" in md
assert "alt_test_decomposition.py" in md
def test_alt_test_decomposition_separates_substitutable_from_better(tmp_path) -> None:
"""§3.4 reports the win/tie/loss counts rho hides, and rho with ties split."""
iaa = tmp_path / "iaa"
iaa.mkdir()
_write(
iaa / "alt_test_decomposition.csv",
["candidate", "country", "variant", "n_comparisons",
"refs_disagree", "llm_better", "human_better",
"tie", "tie_same", "tie_diff", "tie_at_1", "tie_at_half", "tie_at_0",
"rho_alttest", "rho_tiebroken"],
[
# ties dominate: rho looks strong, but the human wins the decisive ones
["gpt-5.4-mini", "de", "all", "100",
"36", "10", "14", "76", "57", "19", "38", "26", "12", "0.86", "0.48"],
["gpt-5.4-mini", "de", "nontrivial", "80",
"30", "8", "8", "64", "48", "16", "32", "22", "10", "0.90", "0.50"],
],
)
md = build_report(iaa, tmp_path)
assert "### 3.4 Substitutable vs. better" in md
assert "76 (76%)" in md # tie count and share
assert "0.86" in md # rho as the alt-test computes it
assert "0.48" in md # rho once ties are split -> chance
# the tie bucket is itemised, both by score level and by same/different answer
assert "57 (75%)" in md # tie_same / tie
assert "19 (25%)" in md # tie_diff / tie -- ties that are real disagreements
assert "38 (50%)" in md # tie_at_1 / tie
assert "26 (34%)" in md # tie_at_half / tie -- only possible if refs conflict
assert "12 (16%)" in md # tie_at_0 / tie
assert "631" not in md # refs_disagree comes from the CSV, not hard-coded
assert "36 of 100 comparisons (36%;" in md # reference conflict rate
# the reading: mostly real agreement, but the human takes the decisive calls
assert "75% of those ties are real agreement" in md
assert "the human wins 58% (14 vs 10)" in md # 14 / (14 + 10) decisive
assert "0.50." in md # non-trivial rho (ties split) survives the filter
|