File size: 10,482 Bytes
715cc5a | 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 | 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()
|