Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
408 lines (329 loc) · 16.6 KB
/
Copy pathevaluate.py
File metadata and controls
408 lines (329 loc) · 16.6 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""
Evaluate CUAD nbest predictions with a richer metric set.
Inputs (unchanged):
- test.json : SQuAD-style CUAD ground truth
- nbest_predictions_.json
: {qa_id: [{"text": str, "probability": float}, ...]}
Metrics:
- AUPR Area under the precision-recall curve
- Precision @ 80% recall Highest precision we can hit while keeping recall >= 0.8
- Precision @ 90% recall Same idea, stricter recall bar
- Best F1 Max F1 across all confidence thresholds
- Best F2 Max F2 (recall-weighted) across all thresholds
- Jaccard similarity Mean token-overlap between gt and best-matching pred
for a single chosen category (defaults to "License Grant")
A prediction matches a ground-truth answer when their token Jaccard is at least
IOU_THRESH=0.5 (the original CUAD paper's setting). The "Parties" category also
accepts a substring containment match because gt spans there are very short.
CLI:
python evaluate.py --model_path ./trained_models/qwen3-1.7b
python evaluate.py --model_path ./trained_models/qwen3-1.7b --category "License Grant"
python evaluate.py --model_path ./trained_models/qwen3-1.7b --category all
"""
import argparse
import json
import os
from pathlib import Path
import numpy as np
import pandas as pd
from sklearn import metrics
# Token-overlap threshold for calling a prediction a match. From the CUAD paper.
IOU_THRESH = 0.5
# "Complex" categories whose spans tend to be long, multi-clause, and worth
# scoring by Jaccard overlap rather than just precision/recall.
DEFAULT_JACCARD_CATEGORY = "License Grant"
# ---------------------------------------------------------------------------
# I/O helpers
# ---------------------------------------------------------------------------
def load_json(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def get_questions_from_csv(csv_path="category_descriptions.csv"):
"""Map category name -> description (kept for parity with the old script)."""
df = pd.read_csv(csv_path, encoding="utf-8-sig")
out = {}
for i in range(df.shape[0]):
category = df.iloc[i, 0].split("Category: ")[1]
description = df.iloc[i, 1].split("Description: ")[1]
out[category.title()] = description
return out
def get_answers(test_json_dict, contract_ids=None):
"""Flatten SQuAD-style ground truth into {qa_id: [answer_text, ...]}.
If `contract_ids` is given (a list of contract titles, full or substring),
only QAs belonging to matching contracts are kept. Each qa id has the form
"<contract title>__<Category>", so we match on the contract's title.
"""
results = {}
for contract in test_json_dict["data"]:
title = contract["title"]
if contract_ids and not any(cid in title for cid in contract_ids):
continue
for para in contract["paragraphs"]:
for qa in para["qas"]:
results[qa["id"]] = [a["text"] for a in qa["answers"]]
return results
def list_contract_ids(test_json_dict):
"""Return the list of contract titles (== contract IDs) in the test set."""
return [contract["title"] for contract in test_json_dict["data"]]
def get_preds(nbest_preds_dict, conf):
"""Filter the nbest dict to predictions whose probability > conf."""
results = {}
for qid, preds in nbest_preds_dict.items():
kept = {}
for p in preds:
if p["text"]: # the empty-string entry stands for "no answer"
kept[p["text"]] = p["probability"]
results[qid] = [t for t, prob in kept.items() if prob > conf]
return results
# ---------------------------------------------------------------------------
# Core matching primitives
# ---------------------------------------------------------------------------
def get_jaccard(gt: str, pred: str) -> float:
"""Token-level Jaccard. Lowercases, strips light punctuation, splits on
whitespace. Splits on *any* whitespace run (str.split()) so that
non-breaking spaces (U+00A0) and other unicode whitespace — common in
text extracted from PDFs — tokenize the same as ordinary spaces."""
for token in [".", ",", ";", ":"]:
gt = gt.replace(token, "")
pred = pred.replace(token, "")
gt = gt.lower().replace("/", " ")
pred = pred.lower().replace("/", " ")
gt_words = set(gt.split())
pred_words = set(pred.split())
if not gt_words and not pred_words:
return 0.0
return len(gt_words & pred_words) / len(gt_words | pred_words)
def _norm_ws(s: str) -> str:
"""Collapse any whitespace run (incl. non-breaking spaces) to single spaces."""
return " ".join(s.split())
def _is_match(ans: str, pred: str, substr_ok: bool) -> bool:
if substr_ok and _norm_ws(ans) in _norm_ws(pred):
return True
return get_jaccard(ans, pred) >= IOU_THRESH
def compute_counts(gt_dict, preds_dict, category=None):
"""Count tp/fp/fn across all (or one) categories and return them."""
tp = fp = fn = 0
for key, answers in gt_dict.items():
if category and not key.endswith(f"__{category}"):
continue
substr_ok = "Parties" in key
preds = preds_dict.get(key, [])
if not answers:
# No ground truth -> every prediction is a false positive.
fp += len(preds)
continue
# True positives: each gt that has any matching prediction.
for ans in answers:
if any(_is_match(ans, p, substr_ok) for p in preds):
tp += 1
else:
fn += 1
# False positives: each prediction that matches no gt answer.
for p in preds:
if not any(_is_match(a, p, substr_ok) for a in answers):
fp += 1
return tp, fp, fn
def compute_precision_recall(gt_dict, preds_dict, category=None):
"""Return (precision, recall) for the given confidence-thresholded preds."""
tp, fp, fn = compute_counts(gt_dict, preds_dict, category=category)
precision = tp / (tp + fp) if (tp + fp) else np.nan
recall = tp / (tp + fn) if (tp + fn) else np.nan
return precision, recall
# ---------------------------------------------------------------------------
# PR curve and derived metrics
# ---------------------------------------------------------------------------
def process_precisions(precisions):
"""Monotone-decreasing precision envelope (standard PR-curve smoothing)."""
rev = list(precisions[::-1])
for i in range(1, len(rev)):
rev[i] = max(rev[i - 1], rev[i])
return rev[::-1]
def get_precisions_recalls(pred_dict, gt_dict, category=None):
"""Sweep confidence thresholds high -> low to trace out a PR curve."""
precisions, recalls, confs = [1.0], [0.0], []
for conf in list(np.arange(0.99, 0, -0.01)) + [0.001, 0.0]:
thresholded = get_preds(pred_dict, conf)
p, r = compute_precision_recall(gt_dict, thresholded, category=category)
# NaN occurs when there are no predictions at this threshold; treat as
# precision=1, recall=0 so the curve still extends cleanly.
precisions.append(0.0 if np.isnan(p) else p)
recalls.append(0.0 if np.isnan(r) else r)
confs.append(conf)
return precisions, recalls, confs
def get_aupr(precisions, recalls):
aupr = metrics.auc(recalls, process_precisions(precisions))
return 0.0 if np.isnan(aupr) else aupr
def get_prec_at_recall(precisions, recalls, recall_thresh):
"""Highest precision among points whose recall >= the threshold."""
smoothed = process_precisions(precisions)
for prec, recall in zip(smoothed, recalls):
if recall >= recall_thresh:
return prec
return 0.0
def get_best_fbeta(precisions, recalls, confs, beta):
"""Walk the PR curve and return the best F-beta score plus its threshold."""
best_f, best_conf = 0.0, 0.0
smoothed = process_precisions(precisions)
# confs is one element shorter (no conf for the (P=1, R=0) anchor).
for p, r, c in zip(smoothed[1:], recalls[1:], confs):
denom = (beta ** 2) * p + r
if denom == 0:
continue
f = (1 + beta ** 2) * p * r / denom
if f > best_f:
best_f, best_conf = f, c
return best_f, best_conf
# ---------------------------------------------------------------------------
# Jaccard for a single (complex) category
# ---------------------------------------------------------------------------
def compute_jaccard_for_category(pred_dict, gt_dict, category, conf=0.0):
"""Mean best-match Jaccard for one category.
For each QA in the category that has at least one ground-truth answer,
take max_p Jaccard(answer, p) for every answer, then average. A QA with
answers but no predictions contributes 0 (worst overlap).
"""
thresholded = get_preds(pred_dict, conf)
scores = []
for key, answers in gt_dict.items():
if not key.endswith(f"__{category}") or not answers:
continue
preds = thresholded.get(key, [])
for ans in answers:
scores.append(max((get_jaccard(ans, p) for p in preds), default=0.0))
return float(np.mean(scores)) if scores else 0.0
def compute_jaccard_per_category(pred_dict, gt_dict, category=None, conf=0.0):
"""Mean best-match Jaccard for *every* category -> {category: score}.
Same per-answer logic as compute_jaccard_for_category, but reported once
per category instead of for a single chosen one. If `category` is given,
only that category is included (so it honours the --category filter).
"""
thresholded = get_preds(pred_dict, conf)
per_cat = {}
for key, answers in gt_dict.items():
if not answers:
continue
cat = key.rsplit("__", 1)[1]
if category and cat != category:
continue
bucket = per_cat.setdefault(cat, [])
preds = thresholded.get(key, [])
for ans in answers:
bucket.append(max((get_jaccard(ans, p) for p in preds), default=0.0))
return {cat: float(np.mean(s)) for cat, s in sorted(per_cat.items())}
# ---------------------------------------------------------------------------
# Top-level driver
# ---------------------------------------------------------------------------
def evaluate(pred_dict, gt_dict, category=None, jaccard_category=None):
"""Compute every metric for one (category | all) selection.
`category` = None evaluates over all categories; a string evaluates only QAs
whose id ends with "__<category>". `jaccard_category` controls which single
category the Jaccard metric is computed for (defaults to `category` if set,
otherwise to DEFAULT_JACCARD_CATEGORY).
"""
precisions, recalls, confs = get_precisions_recalls(pred_dict, gt_dict, category)
jaccard_cat = jaccard_category or category or DEFAULT_JACCARD_CATEGORY
best_f1, best_f1_conf = get_best_fbeta(precisions, recalls, confs, beta=1)
best_f2, _ = get_best_fbeta(precisions, recalls, confs, beta=2)
# TP/FP/FN are threshold-dependent; report them at the confidence threshold
# that maximises F1 (the operating point implied by "best_f1").
thresholded = get_preds(pred_dict, best_f1_conf)
tp, fp, fn = compute_counts(gt_dict, thresholded, category=category)
return {
"category": category or "all",
"aupr": get_aupr(precisions, recalls),
"prec_at_80_recall": get_prec_at_recall(precisions, recalls, 0.80),
"prec_at_90_recall": get_prec_at_recall(precisions, recalls, 0.90),
"best_f1": best_f1,
"best_f2": best_f2,
"best_f1_conf": best_f1_conf,
"true_positives": tp,
"false_positives": fp,
"false_negatives": fn,
"jaccard_category": jaccard_cat,
"jaccard_similarity": compute_jaccard_for_category(pred_dict, gt_dict, jaccard_cat),
"jaccard_per_category": compute_jaccard_per_category(pred_dict, gt_dict, category),
}
def get_results(model_path, gt_dict, category=None, jaccard_category=None, verbose=False):
"""Load a model's nbest file and run `evaluate` against the gt."""
pred_path = os.path.join(model_path, "nbest_predictions_.json")
pred_dict = load_json(pred_path)
# Align prediction keys to the gt keyspace: missing -> empty, extra -> drop.
for qid in set(gt_dict) - set(pred_dict):
pred_dict[qid] = []
pred_dict = {k: v for k, v in pred_dict.items() if k in gt_dict}
results = evaluate(pred_dict, gt_dict, category, jaccard_category)
results["name"] = os.path.basename(os.path.normpath(model_path))
if verbose:
print_results(results)
return results
def print_results(results: dict):
print(f"\n== {results['name']} [category: {results['category']}] ==")
print(f" AUPR {results['aupr']:.4f}")
print(f" Precision @ 80% recall {results['prec_at_80_recall']:.4f}")
print(f" Precision @ 90% recall {results['prec_at_90_recall']:.4f}")
print(f" Best F1 {results['best_f1']:.4f} (@ conf {results['best_f1_conf']:.2f})")
print(f" Best F2 {results['best_f2']:.4f}")
print(f" True Positives {results['true_positives']}")
print(f" False Positives {results['false_positives']}")
print(f" False Negatives {results['false_negatives']}")
print(f" Jaccard ({results['jaccard_category']}){' ' * max(1, 20 - len(results['jaccard_category']))}"
f"{results['jaccard_similarity']:.4f}")
per_cat = results.get("jaccard_per_category") or {}
if per_cat:
print("\n Jaccard per category:")
width = max(len(c) for c in per_cat)
for cat, score in per_cat.items():
print(f" {cat:<{width}} {score:.4f}")
print(f" {'-' * width} ------")
print(f" {'MEAN (macro)':<{width}} {np.mean(list(per_cat.values())):.4f}")
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--model_path", default="./trained_models/qwen3-1.7b",
help="Folder containing nbest_predictions_.json")
ap.add_argument("--data", default="./data/test.json",
help="CUAD test set in SQuAD format")
ap.add_argument("--save_dir", default="./results",
help="Where to write the result JSON")
ap.add_argument("--category", default="all",
help='Category name (e.g. "License Grant") or "all"')
ap.add_argument("--jaccard_category", default=None,
help="Category used for the Jaccard metric. Defaults to "
f"--category when specific, else '{DEFAULT_JACCARD_CATEGORY}'.")
ap.add_argument("--contract_ids", nargs="+", default=None,
help="One or more contract IDs (titles, full or substring) to "
"evaluate. Omit to evaluate all 102 contracts. "
'Example: --contract_ids LohaCompanyltd "Supply Agreement"')
ap.add_argument("--list_contracts", action="store_true",
help="Print all available contract IDs and exit.")
args = ap.parse_args()
test_data = load_json(args.data)
if args.list_contracts:
for cid in list_contract_ids(test_data):
print(cid)
raise SystemExit(0)
Path(args.save_dir).mkdir(parents=True, exist_ok=True)
gt_dict = get_answers(test_data, contract_ids=args.contract_ids)
if not gt_dict:
raise SystemExit(
f"No contracts matched {args.contract_ids!r}. "
"Use --list_contracts to see valid IDs."
)
if args.contract_ids:
n_matched = len({k.rsplit('__', 1)[0] for k in gt_dict})
print(f"Evaluating {n_matched} contract(s) matching {args.contract_ids}")
category = None if args.category.lower() == "all" else args.category
results = get_results(args.model_path, gt_dict,
category=category,
jaccard_category=args.jaccard_category,
verbose=True)
cat_suffix = "all" if category is None else category.replace(" ", "_")
contract_suffix = "" if not args.contract_ids else "__" + str(len(
{k.rsplit('__', 1)[0] for k in gt_dict})) + "contracts"
suffix = cat_suffix + contract_suffix
save_path = os.path.join(args.save_dir, f"{results['name']}__{suffix}.json")
with open(save_path, "w", encoding="utf-8") as f:
json.dump(results, f, indent=2)
print(f"\nSaved results to {save_path}")