| from __future__ import annotations |
|
|
| import argparse |
| from pathlib import Path |
| from typing import Any |
|
|
| from src.analysis.compare_predictions import load_prediction_map, row_id |
| from src.data.io_utils import read_jsonl, write_csv |
|
|
|
|
| SPECS = { |
| "vifactcheck": { |
| "label": "ViFactCheck", |
| "split": "test", |
| "baseline_pred": Path("outputs/baselines/vifactcheck/encoder_verifier/xlm-roberta-large/seed_13/predictions_test.jsonl"), |
| "wikikg_pred": Path("outputs/verifier/vifactcheck/wikikg_fact/xlm-roberta-large_wikikg_text_only_diag_top5/seed_13/predictions_test.jsonl"), |
| "retrieval_topk": Path("outputs/retrieval/vifactcheck/wikikg_topk_test.jsonl"), |
| "verified_subgraphs": Path("outputs/kg/vifactcheck/verified_claim_subgraphs_test.jsonl"), |
| "unsupported_triples": Path("outputs/kg/vifactcheck/unsupported_triples_test.jsonl"), |
| }, |
| "averitec": { |
| "label": "AVeriTeC", |
| "split": "local_test", |
| "baseline_pred": Path("outputs/llm_baselines/averitec/gemma4_31b_q4/local_test_predictions.jsonl"), |
| "wikikg_pred": Path("outputs/llm_baselines/averitec/gemma4_31b_q4_qa_wikikg_paths_only_top10_kg5/local_test_predictions.jsonl"), |
| "retrieval_topk": Path("outputs/retrieval/averitec/wikikg_topk_local_test.jsonl"), |
| "verified_subgraphs": Path("outputs/kg/averitec/verified_claim_subgraphs_local_test.jsonl"), |
| "unsupported_triples": Path("outputs/kg/averitec/unsupported_triples_local_test.jsonl"), |
| }, |
| "healthver": { |
| "label": "HealthVer", |
| "split": "test", |
| "baseline_pred": Path("outputs/baselines/healthver/encoder_verifier/microsoft__BiomedNLP-BiomedBERT-base-uncased-abstract-fulltext/seed_13/predictions_test.jsonl"), |
| "wikikg_pred": Path("outputs/verifier/healthver/wikikg_fact/microsoft__BiomedNLP-BiomedBERT-base-uncased-abstract-fulltext_wikikg_top5/seed_13/predictions_test.jsonl"), |
| "retrieval_topk": Path("outputs/retrieval/healthver/wikikg_topk_test.jsonl"), |
| "verified_subgraphs": Path("outputs/kg/healthver/verified_claim_subgraphs_test.jsonl"), |
| "unsupported_triples": Path("outputs/kg/healthver/unsupported_triples_test.jsonl"), |
| }, |
| } |
|
|
| BIOMEDICAL_RELATIONS = {"TREATS", "PREVENTS", "CAUSES", "INCREASES_RISK", "DECREASES_RISK"} |
|
|
|
|
| def load_map(path: Path) -> dict[str, dict[str, Any]]: |
| return {row_id(row): row for row in read_jsonl(path)} |
|
|
|
|
| def group_rows(path: Path) -> dict[str, list[dict[str, Any]]]: |
| grouped: dict[str, list[dict[str, Any]]] = {} |
| for row in read_jsonl(path): |
| grouped.setdefault(str(row.get("claim_id", "")), []).append(row) |
| return grouped |
|
|
|
|
| def is_wrong_relation(row: dict[str, Any]) -> bool: |
| flags = set(row.get("removal_reasons") or []) | set(row.get("rule_notes") or []) |
| return "relation_demoted_to_associated" in flags or bool(row.get("relation_original")) or row.get("nli_label") == "entailment" |
|
|
|
|
| def build_claim_rows(dataset: str) -> list[dict[str, Any]]: |
| spec = SPECS[dataset] |
| baseline = load_prediction_map(spec["baseline_pred"]) |
| wikikg = load_prediction_map(spec["wikikg_pred"]) |
| topk = load_map(spec["retrieval_topk"]) |
| subgraphs = load_map(spec["verified_subgraphs"]) |
| unsupported = group_rows(spec["unsupported_triples"]) |
|
|
| rows: list[dict[str, Any]] = [] |
| for claim_id in sorted(wikikg): |
| pred = wikikg[claim_id] |
| base = baseline.get(claim_id, {}) |
| subgraph = subgraphs.get(claim_id, {}) |
| metrics = (topk.get(claim_id) or {}).get("metrics", {}) |
| unsupported_rows = unsupported.get(claim_id, []) |
| correct = str(pred.get("prediction")) == str(pred.get("gold")) |
| retrieval_miss = False |
| if dataset == "healthver": |
| retrieval_miss = (not correct) and int(subgraph.get("num_triples") or 0) == 0 and int(subgraph.get("num_facts") or 0) == 0 |
| else: |
| retrieval_miss = (not correct) and not bool(metrics.get("gold_at_10")) |
| wrong_relation = (not correct) and any(is_wrong_relation(row) for row in unsupported_rows) |
| overclaim_biomedical = (not correct) and dataset == "healthver" and any( |
| row.get("relation") in BIOMEDICAL_RELATIONS |
| or row.get("relation_original") in BIOMEDICAL_RELATIONS |
| or "relation_demoted_to_associated" in set(row.get("removal_reasons") or []) | set(row.get("rule_notes") or []) |
| for row in unsupported_rows |
| ) |
| gold = str(pred.get("gold", "")) |
| prediction = str(pred.get("prediction", "")) |
| nei_confusion = (not correct) and ("NEI" in {gold, prediction}) |
| conflicting_confusion = dataset == "averitec" and (not correct) and ("CONFLICTING" in {gold, prediction}) |
| weak_explanation = correct and int(subgraph.get("num_triples") or 0) == 0 and int(subgraph.get("num_facts") or 0) == 0 |
| good_kg_path_wrong_verdict = (not correct) and ( |
| int(subgraph.get("num_triples") or 0) > 0 or int(subgraph.get("num_facts") or 0) > 0 |
| ) |
| rows.append( |
| { |
| "dataset": dataset, |
| "split": spec["split"], |
| "claim_id": claim_id, |
| "gold": gold, |
| "baseline_prediction": base.get("prediction", ""), |
| "wikikg_prediction": prediction, |
| "baseline_correct": str(base.get("prediction", "")) == gold if base else "", |
| "wikikg_correct": correct, |
| "retrieval_miss": int(retrieval_miss), |
| "wrong_kg_relation": int(wrong_relation), |
| "overclaim_biomedical_relation": int(overclaim_biomedical), |
| "nei_confusion": int(nei_confusion), |
| "conflicting_confusion": int(conflicting_confusion), |
| "correct_label_weak_explanation": int(weak_explanation), |
| "good_kg_path_wrong_verdict": int(good_kg_path_wrong_verdict), |
| "num_verified_facts": int(subgraph.get("num_facts") or 0), |
| "num_verified_triples": int(subgraph.get("num_triples") or 0), |
| "num_unsupported_triples": len(unsupported_rows), |
| } |
| ) |
| return rows |
|
|
|
|
| def first_example(rows: list[dict[str, Any]], field: str) -> str: |
| for row in rows: |
| if int(row[field]) == 1: |
| return row["claim_id"] |
| return "" |
|
|
|
|
| def summarize(rows: list[dict[str, Any]]) -> list[dict[str, Any]]: |
| by_dataset: dict[str, list[dict[str, Any]]] = {} |
| for row in rows: |
| by_dataset.setdefault(row["dataset"], []).append(row) |
|
|
| def count(dataset: str, field: str) -> int: |
| return sum(int(row[field]) for row in by_dataset.get(dataset, [])) |
|
|
| def example(field: str, note: str) -> str: |
| ids = [] |
| for dataset in ("vifactcheck", "averitec", "healthver"): |
| claim_id = first_example(by_dataset.get(dataset, []), field) |
| if claim_id: |
| ids.append(f"{SPECS[dataset]['label']}:{claim_id}") |
| suffix = "; ".join(ids[:2]) |
| return note if not suffix else f"{note}; e.g. {suffix}" |
|
|
| return [ |
| { |
| "Error type": "Retrieval miss", |
| "ViFactCheck": count("vifactcheck", "retrieval_miss"), |
| "AVeriTeC": count("averitec", "retrieval_miss"), |
| "HealthVer": count("healthver", "retrieval_miss"), |
| "Example / interpretation": example("retrieval_miss", "evidence absent or still ranked too low"), |
| }, |
| { |
| "Error type": "Wrong KG relation", |
| "ViFactCheck": count("vifactcheck", "wrong_kg_relation"), |
| "AVeriTeC": count("averitec", "wrong_kg_relation"), |
| "HealthVer": count("healthver", "wrong_kg_relation"), |
| "Example / interpretation": example("wrong_kg_relation", "relation semantics remain too strong or misaligned"), |
| }, |
| { |
| "Error type": "Overclaim biomedical relation", |
| "ViFactCheck": "n/a", |
| "AVeriTeC": "n/a", |
| "HealthVer": count("healthver", "overclaim_biomedical_relation"), |
| "Example / interpretation": example("overclaim_biomedical_relation", "biomedical claims still risk over-strong causal wording"), |
| }, |
| { |
| "Error type": "NEI confusion", |
| "ViFactCheck": count("vifactcheck", "nei_confusion"), |
| "AVeriTeC": count("averitec", "nei_confusion"), |
| "HealthVer": count("healthver", "nei_confusion"), |
| "Example / interpretation": example("nei_confusion", "insufficient evidence still flips into support or refute"), |
| }, |
| { |
| "Error type": "CONFLICTING confusion", |
| "ViFactCheck": "n/a", |
| "AVeriTeC": count("averitec", "conflicting_confusion"), |
| "HealthVer": "n/a", |
| "Example / interpretation": example("conflicting_confusion", "cherry-picking cases remain the hardest label"), |
| }, |
| { |
| "Error type": "Correct label, weak explanation", |
| "ViFactCheck": count("vifactcheck", "correct_label_weak_explanation"), |
| "AVeriTeC": count("averitec", "correct_label_weak_explanation"), |
| "HealthVer": count("healthver", "correct_label_weak_explanation"), |
| "Example / interpretation": example("correct_label_weak_explanation", "prediction correct despite little retained KG support"), |
| }, |
| { |
| "Error type": "Good KG path, wrong verdict", |
| "ViFactCheck": count("vifactcheck", "good_kg_path_wrong_verdict"), |
| "AVeriTeC": count("averitec", "good_kg_path_wrong_verdict"), |
| "HealthVer": count("healthver", "good_kg_path_wrong_verdict"), |
| "Example / interpretation": example("good_kg_path_wrong_verdict", "useful path exists but the verifier still misclassifies"), |
| }, |
| ] |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--analysis-output", type=Path, default=Path("outputs/analysis/error_analysis.csv")) |
| parser.add_argument("--table-output", type=Path, default=Path("outputs/tables/T15_error_analysis.csv")) |
| args = parser.parse_args() |
|
|
| rows: list[dict[str, Any]] = [] |
| for dataset in ("vifactcheck", "averitec", "healthver"): |
| rows.extend(build_claim_rows(dataset)) |
| write_csv(args.analysis_output, rows) |
| write_csv(args.table_output, summarize(rows)) |
| print(f"Wrote {len(rows)} per-claim error rows to {args.analysis_output}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|