| 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"], |
| [ |
| |
| ["primary", "a1b2c3d4e5", "de", "plaintiff_no1_ISIC1_industry_category", "20", "0.5000", "0.5000", "0.4000"], |
| |
| ["primary", "a1b2c3d4e5", "de", "court_cost_awarded_nominal", "20", "0.6000", "0.6500", "0.3000"], |
| |
| ["primary", "a1b2c3d4e5", "de", "legal_subject_judgement", "20", "0.1000", "0.1000", ""], |
| ], |
| ) |
| |
| |
| _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 |
| assert "0.300" not in md |
| assert "65.0%" in md |
| assert "±" in md |
| assert "nominal" in md and "monetary" in md |
| assert "a1b2c3d4e5" in md |
| assert "80.0%" in md |
| assert "1/2" in md |
| assert "1/1" in md |
| assert "0.82" in md |
| assert "0.93" in md |
| |
| assert "nitaytech/AltTest" in md |
| assert "alt_test_reference.py" in md |
| |
| 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 |
| |
| assert "### 3.1 How ω and ρ are computed" in md |
| assert "rho_j = (1/n_j)" in md |
| assert "(1 - epsilon) / 2 = 0.4" in md |
| |
| 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"], |
| [ |
| |
| ["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 |
| assert "0.86" in md |
| assert "0.48" in md |
| |
| assert "57 (75%)" in md |
| assert "19 (25%)" in md |
| assert "38 (50%)" in md |
| assert "26 (34%)" in md |
| assert "12 (16%)" in md |
| assert "631" not in md |
| assert "36 of 100 comparisons (36%;" in md |
| |
| assert "75% of those ties are real agreement" in md |
| assert "the human wins 58% (14 vs 10)" in md |
| assert "0.50." in md |
|
|