Datasets:
Languages:
English
Size:
1K<n<10K
ArXiv:
Tags:
temporal-reasoning
knowledge-graph
question-answering
benchmark
retrieval-augmented-generation
DOI:
License:
Add TRP/CCR scorers and a quickstart; state train-split, template and point-in-time scope
Browse files- MANIFEST.json +6 -1
- README.md +139 -0
- code/tempbench_eval.py +204 -0
MANIFEST.json
CHANGED
|
@@ -79,6 +79,11 @@
|
|
| 79 |
"bytes": 15074,
|
| 80 |
"why": "Wikidata QID/PID -> human-readable label resolution"
|
| 81 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
{
|
| 83 |
"path": "annotation/pilot_low_confidence.jsonl",
|
| 84 |
"bytes": 63043,
|
|
@@ -141,7 +146,7 @@
|
|
| 141 |
},
|
| 142 |
{
|
| 143 |
"path": "v1.0.1-addendum.md",
|
| 144 |
-
"bytes":
|
| 145 |
"why": "known issues + evaluation protocol; the interval-leak fix the paper cites"
|
| 146 |
}
|
| 147 |
]
|
|
|
|
| 79 |
"bytes": 15074,
|
| 80 |
"why": "Wikidata QID/PID -> human-readable label resolution"
|
| 81 |
},
|
| 82 |
+
{
|
| 83 |
+
"path": "code/tempbench_eval.py",
|
| 84 |
+
"bytes": 8213,
|
| 85 |
+
"why": "TRP / CCR scorers -- evaluate your own retriever without the reference implementation"
|
| 86 |
+
},
|
| 87 |
{
|
| 88 |
"path": "annotation/pilot_low_confidence.jsonl",
|
| 89 |
"bytes": 63043,
|
|
|
|
| 146 |
},
|
| 147 |
{
|
| 148 |
"path": "v1.0.1-addendum.md",
|
| 149 |
+
"bytes": 9334,
|
| 150 |
"why": "known issues + evaluation protocol; the interval-leak fix the paper cites"
|
| 151 |
}
|
| 152 |
]
|
README.md
CHANGED
|
@@ -49,6 +49,91 @@ answers are years and a same-`(s,r,o)`-other-time variant is ill-defined.
|
|
| 49 |
**Per-question flags ship in `benchmark/functional_negatives.jsonl`** — restrict
|
| 50 |
negative-dependent evaluation to the functional subset.
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
## Contents
|
| 53 |
|
| 54 |
| path | what |
|
|
@@ -58,6 +143,7 @@ negative-dependent evaluation to the functional subset.
|
|
| 58 |
| `benchmark/functional_negatives.jsonl` | per-question functional-negative flags |
|
| 59 |
| `benchmark/labels.tsv`, `ids.txt` | Wikidata label dump and id list |
|
| 60 |
| `code/` | the **deterministic construction pipeline** — indexer, 6-stage benchmark builder, label resolver, and the design-decisions document. Stdlib only; `python build_benchmark.py --smoke_test` verifies it |
|
|
|
|
| 61 |
| `annotation/` | the annotation protocol (EN governing, IT translation) and validation-sample provenance |
|
| 62 |
| `annotation/pilot_low_confidence.jsonl` | per-question **low-confidence flags** for the 500-question IAA pilot: 452 consensus, 48 flagged, with which judgment was disputed |
|
| 63 |
| `baselines/` | reference-baseline evaluation outputs (see below) |
|
|
@@ -93,6 +179,59 @@ corrected protocol are in `v1.0.1-addendum.md`.
|
|
| 93 |
**Naturalness ratings are not reliable between annotators** and should not be used
|
| 94 |
as a quality signal; see the paper's Human Validation section.
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
## Provenance and licence
|
| 97 |
|
| 98 |
Built from `tkgl-smallpedia` in [TGB 2.0](https://arxiv.org/abs/2406.09639)
|
|
|
|
| 49 |
**Per-question flags ship in `benchmark/functional_negatives.jsonl`** — restrict
|
| 50 |
negative-dependent evaluation to the functional subset.
|
| 51 |
|
| 52 |
+
## Quickstart
|
| 53 |
+
|
| 54 |
+
Three steps, standard library only, no install. Scoring your own retriever
|
| 55 |
+
against TempBench does **not** require the reference system.
|
| 56 |
+
|
| 57 |
+
**1. Load.** Each line of `benchmark/benchmark_labelled.jsonl` is one question
|
| 58 |
+
carrying its gold subgraph `S*` and its two typed negatives:
|
| 59 |
+
|
| 60 |
+
```python
|
| 61 |
+
import json
|
| 62 |
+
|
| 63 |
+
test = [json.loads(l) for l in open('benchmark/benchmark_labelled.jsonl',
|
| 64 |
+
encoding='utf-8')]
|
| 65 |
+
test = [q for q in test if q['split'] == 'test'] # 1,743 questions
|
| 66 |
+
|
| 67 |
+
q = test[0]
|
| 68 |
+
q['question'] # 'In 1994, what was ... ?'
|
| 69 |
+
q['t_query'] # 1994.0 -- the time the question is asked about
|
| 70 |
+
q['S_star'] # [{'s':..., 'r':..., 'o':..., 't_start':..., 't_end':...}, ...]
|
| 71 |
+
q['S_dist'] # same (s,r), wrong object
|
| 72 |
+
q['S_stale'] # same (s,r,o), wrong time
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
**2. Retrieve** with your own system. Return an iterable of triples per
|
| 76 |
+
question — dicts with `s`/`r`/`o`/`t_start`/`t_end`, or 5-tuples in that order.
|
| 77 |
+
Truncate to your own `k`; TRP is a precision quantity and is not truncated for
|
| 78 |
+
you.
|
| 79 |
+
|
| 80 |
+
**3. Score** with `code/tempbench_eval.py`:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
from tempbench_eval import score_question, aggregate
|
| 84 |
+
|
| 85 |
+
rows = [score_question(q, my_retriever(q['question'], q['t_query']))
|
| 86 |
+
for q in test]
|
| 87 |
+
print(aggregate(rows))
|
| 88 |
+
# {'n_questions': 1743, 'coverage': ..., 'TRP_macro': ..., 'CCR': ...,
|
| 89 |
+
# 'by_complexity': {...}, 'by_operator': {...}}
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
`python code/tempbench_eval.py` runs a self-check on synthetic data and needs
|
| 93 |
+
no files.
|
| 94 |
+
|
| 95 |
+
### What the two metrics mean
|
| 96 |
+
|
| 97 |
+
Both are **answer-independent** — they score retrieved evidence, not the
|
| 98 |
+
generated string, which is the whole point of the resource. A system can emit
|
| 99 |
+
the right answer from a stale fact, and exact-match cannot see it.
|
| 100 |
+
|
| 101 |
+
- **TRP** — of the triples you retrieved, the fraction that are in `S*` *and*
|
| 102 |
+
valid at `t_query`. Macro-averaged over questions that retrieved anything.
|
| 103 |
+
- **CCR** — 1 if you retrieved *every* triple of `S*`, all time-valid; else 0.
|
| 104 |
+
Averaged over all questions, empty retrievals included.
|
| 105 |
+
|
| 106 |
+
A triple is time-valid when `t_start <= t_query <= t_end`. TRP scores against
|
| 107 |
+
1–3-triple gold chains, so its absolute value is low by construction: read the
|
| 108 |
+
gap between systems and the per-complexity profile, not the raw number. The two
|
| 109 |
+
are not redundant — the reference retriever scores TRP 0.203 against CCR 0.014
|
| 110 |
+
at 3+-hop, meaning partial evidence arrives routinely and the full chain almost
|
| 111 |
+
never.
|
| 112 |
+
|
| 113 |
+
Always report `coverage` alongside them. A system that returns nothing on hard
|
| 114 |
+
questions inflates its own TRP, since undefined TRP is excluded rather than
|
| 115 |
+
scored zero.
|
| 116 |
+
|
| 117 |
+
### The one trap
|
| 118 |
+
|
| 119 |
+
**Restrict negative-dependent analysis to the functional subset.** Not every
|
| 120 |
+
question's negatives genuinely differ from its gold. Scoring the stale subgraph
|
| 121 |
+
directly on the 1-hop test slice returns TRP 0.141 — which looks like a
|
| 122 |
+
time-aware retriever leaking, and is not:
|
| 123 |
+
|
| 124 |
+
```python
|
| 125 |
+
flags = {json.loads(l)['id']: json.loads(l)
|
| 126 |
+
for l in open('benchmark/functional_negatives.jsonl', encoding='utf-8')}
|
| 127 |
+
sub = [q for q in test if flags[q['id']]['stale_functional']]
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
Restricted to functional negatives, the same measurement returns **TRP 0.000 /
|
| 131 |
+
CCR 0.000**, as the construction implies. The 0.141 was entirely
|
| 132 |
+
non-functional negatives.
|
| 133 |
+
|
| 134 |
+
Read `v1.0.1-addendum.md` before evaluating: interval questions leak their
|
| 135 |
+
answer under the original prompt protocol.
|
| 136 |
+
|
| 137 |
## Contents
|
| 138 |
|
| 139 |
| path | what |
|
|
|
|
| 143 |
| `benchmark/functional_negatives.jsonl` | per-question functional-negative flags |
|
| 144 |
| `benchmark/labels.tsv`, `ids.txt` | Wikidata label dump and id list |
|
| 145 |
| `code/` | the **deterministic construction pipeline** — indexer, 6-stage benchmark builder, label resolver, and the design-decisions document. Stdlib only; `python build_benchmark.py --smoke_test` verifies it |
|
| 146 |
+
| `code/tempbench_eval.py` | **the TRP and CCR scorers** — score your own retriever without re-implementing the definitions. Stdlib only; `python tempbench_eval.py` self-checks |
|
| 147 |
| `annotation/` | the annotation protocol (EN governing, IT translation) and validation-sample provenance |
|
| 148 |
| `annotation/pilot_low_confidence.jsonl` | per-question **low-confidence flags** for the 500-question IAA pilot: 452 consensus, 48 flagged, with which judgment was disputed |
|
| 149 |
| `baselines/` | reference-baseline evaluation outputs (see below) |
|
|
|
|
| 179 |
**Naturalness ratings are not reliable between annotators** and should not be used
|
| 180 |
as a quality signal; see the paper's Human Validation section.
|
| 181 |
|
| 182 |
+
**Only the test split is human-validated.** Validation covers the 500-question
|
| 183 |
+
pilot plus a 120-item blind round (116 scored) drawn from the test split. The
|
| 184 |
+
6,096-question training split carries automatically generated labels that no
|
| 185 |
+
human has checked. This is defensible for the benchmark's intended use — every
|
| 186 |
+
number in the paper is computed on test, and none of the reference baselines
|
| 187 |
+
trains on the released split — but if you fine-tune on `train`, you are training
|
| 188 |
+
on unaudited labels. Treat the pipeline's construction guarantees, not human
|
| 189 |
+
review, as what backs that split.
|
| 190 |
+
|
| 191 |
+
**Question surface forms come from nine templates** — three for point-in-time,
|
| 192 |
+
two each for before/after, interval and sequence — parameterised over anchor
|
| 193 |
+
entity, relation chain and reference year. Linguistic diversity is therefore
|
| 194 |
+
low by construction, and TempBench measures temporal *retrieval*, not robustness
|
| 195 |
+
to paraphrase. Do not read a score here as evidence about natural-language
|
| 196 |
+
variation. (Full template inventory and parameters in
|
| 197 |
+
`code/benchmark-design-decisions.md`.)
|
| 198 |
+
|
| 199 |
+
**The source KG is point-in-time, so `valid_at` reduces to exact-year
|
| 200 |
+
equality.** `tkgl-smallpedia` carries discrete-timestamp facts
|
| 201 |
+
(`t_start == t_end`), which means the composability operator ⊕ is exercised here
|
| 202 |
+
in its degenerate case: checking that each hop is valid at the query year. The
|
| 203 |
+
operator is defined for interval facts and admits chains that a plain interval
|
| 204 |
+
intersection rejects, but **the released benchmark does not test that generality**
|
| 205 |
+
— a validity-window TKG (YAGO3, ICEWS) would. Treat results here as evidence
|
| 206 |
+
about time-valid retrieval on point-in-time graphs, and not yet as evidence
|
| 207 |
+
about general temporal-chain reasoning.
|
| 208 |
+
|
| 209 |
+
## Open questions this release does not answer
|
| 210 |
+
|
| 211 |
+
Stated plainly, because they bound what a number on TempBench means.
|
| 212 |
+
|
| 213 |
+
**Whether the benchmark discriminates across retriever families is not yet
|
| 214 |
+
established.** Every system evaluated in the paper is a variant of one
|
| 215 |
+
BFS + BM25 retriever — the same graph-traversal family used to *construct* `S*`
|
| 216 |
+
by shortest-path retrieval under temporal constraints. High CCR may therefore
|
| 217 |
+
partly reflect that methodological alignment rather than retrieval quality, and
|
| 218 |
+
no heterogeneous system has been run: no dense retriever, no published
|
| 219 |
+
temporal-RAG system, no parametric-LLM baseline.
|
| 220 |
+
|
| 221 |
+
This is the most important open question about the resource, and it is
|
| 222 |
+
squarely future work. The metrics ship here (`code/tempbench_eval.py`)
|
| 223 |
+
specifically so that anyone can run a system from a different family and
|
| 224 |
+
report TRP/CCR without going through the reference implementation — which is
|
| 225 |
+
the cheapest path to settling it. Results from an unrelated architecture are
|
| 226 |
+
more informative about the benchmark than anything the reference retriever can
|
| 227 |
+
produce, and contributions are welcome.
|
| 228 |
+
|
| 229 |
+
**A validity-window edition (v2).** Extending construction to interval-fact TKGs
|
| 230 |
+
would exercise ⊕ in its general form and test whether the retrieval findings
|
| 231 |
+
survive outside exact-year matching. When porting, check the source data's
|
| 232 |
+
closed-interval convention against `valid_at`'s semantics first — the two do not
|
| 233 |
+
always agree.
|
| 234 |
+
|
| 235 |
## Provenance and licence
|
| 236 |
|
| 237 |
Built from `tkgl-smallpedia` in [TGB 2.0](https://arxiv.org/abs/2406.09639)
|
code/tempbench_eval.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""TempBench retrieval-quality metrics: TRP and Chain Consistency Rate.
|
| 2 |
+
|
| 3 |
+
Scores *your* retriever against TempBench's per-question gold subgraphs. You
|
| 4 |
+
supply the evidence your system retrieved; this module supplies the metrics the
|
| 5 |
+
paper reports, so numbers are comparable without re-implementing the
|
| 6 |
+
definitions.
|
| 7 |
+
|
| 8 |
+
Both metrics are answer-independent: they score the evidence, not the generated
|
| 9 |
+
string. That is the point of the benchmark -- a system can produce the right
|
| 10 |
+
answer from stale evidence, and end-task exact-match cannot see it.
|
| 11 |
+
|
| 12 |
+
TRP (Temporal Retrieval Precision)
|
| 13 |
+
per question: |retrieved triples that are in S* AND valid at t_query|
|
| 14 |
+
/ |retrieved triples|
|
| 15 |
+
Macro-averaged over questions that retrieved anything. Undefined (and
|
| 16 |
+
excluded) when a system returns nothing.
|
| 17 |
+
|
| 18 |
+
CCR (Chain Consistency Rate)
|
| 19 |
+
per question: 1 if every triple of S* was retrieved and time-valid,
|
| 20 |
+
else 0. Averaged over all questions, including empty retrievals.
|
| 21 |
+
|
| 22 |
+
A triple counts as time-valid when t_start <= t_query <= t_end. On a
|
| 23 |
+
point-in-time KG (t_start == t_end, as in the released benchmark) that reduces
|
| 24 |
+
to exact-year equality.
|
| 25 |
+
|
| 26 |
+
TRP is a precision-at-k quantity against 1-3-triple gold chains, so its absolute
|
| 27 |
+
scale is low by construction; read the gap between retrievers and the
|
| 28 |
+
per-complexity profile, not the raw value. TRP and CCR are not redundant: at
|
| 29 |
+
3+-hop the reference retriever scores TRP 0.203 against CCR 0.014 -- partial
|
| 30 |
+
gold evidence is routinely retrieved, the full chain almost never.
|
| 31 |
+
|
| 32 |
+
Stdlib only. Python 3.9+.
|
| 33 |
+
|
| 34 |
+
Usage
|
| 35 |
+
-----
|
| 36 |
+
import json
|
| 37 |
+
from tempbench_eval import score_question, aggregate
|
| 38 |
+
|
| 39 |
+
rows = []
|
| 40 |
+
for line in open('benchmark/benchmark_labelled.jsonl', encoding='utf-8'):
|
| 41 |
+
q = json.loads(line)
|
| 42 |
+
if q['split'] != 'test':
|
| 43 |
+
continue
|
| 44 |
+
evidence = my_retriever(q['question'], q['t_query']) # your system
|
| 45 |
+
rows.append(score_question(q, evidence))
|
| 46 |
+
|
| 47 |
+
print(aggregate(rows))
|
| 48 |
+
|
| 49 |
+
`evidence` is whatever your retriever returned, as an iterable of triples. Each
|
| 50 |
+
may be a dict with keys s/r/o/t_start/t_end (label space, matching
|
| 51 |
+
`benchmark_labelled.jsonl`), a dict with s_id/r_id/o_id (Wikidata id space,
|
| 52 |
+
matching `benchmark.jsonl`), or a 5-tuple (s, r, o, t_start, t_end). Mixing
|
| 53 |
+
spaces within one run will silently score zero, so pick one and stay in it --
|
| 54 |
+
`key='auto'` infers it from the first item.
|
| 55 |
+
|
| 56 |
+
Restrict negative-dependent analysis to the functional subset: see
|
| 57 |
+
`benchmark/functional_negatives.jsonl`, and read `v1.0.1-addendum.md` before
|
| 58 |
+
evaluating -- interval questions leak their answer under the original prompt
|
| 59 |
+
protocol.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
from collections import defaultdict
|
| 63 |
+
|
| 64 |
+
__all__ = ['score_question', 'aggregate', 'as_triple']
|
| 65 |
+
|
| 66 |
+
_LABEL_KEYS = ('s', 'r', 'o')
|
| 67 |
+
_ID_KEYS = ('s_id', 'r_id', 'o_id')
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def as_triple(item, key='auto'):
|
| 71 |
+
"""Normalise one retrieved item to (s, r, o, t_start, t_end).
|
| 72 |
+
|
| 73 |
+
key: 'label' to match on surface labels, 'id' to match on Wikidata ids,
|
| 74 |
+
'auto' to use ids when the item carries them and labels otherwise.
|
| 75 |
+
"""
|
| 76 |
+
if isinstance(item, (tuple, list)):
|
| 77 |
+
if len(item) != 5:
|
| 78 |
+
raise ValueError('tuple evidence must be (s, r, o, t_start, t_end),'
|
| 79 |
+
' got %d fields' % len(item))
|
| 80 |
+
s, r, o, ts, te = item
|
| 81 |
+
elif isinstance(item, dict):
|
| 82 |
+
use_id = (key == 'id' or
|
| 83 |
+
(key == 'auto' and all(k in item for k in _ID_KEYS)))
|
| 84 |
+
ks = _ID_KEYS if use_id else _LABEL_KEYS
|
| 85 |
+
missing = [k for k in ks if k not in item]
|
| 86 |
+
if missing:
|
| 87 |
+
raise KeyError('evidence dict is missing %s; it has %s'
|
| 88 |
+
% (missing, sorted(item)))
|
| 89 |
+
s, r, o = (item[k] for k in ks)
|
| 90 |
+
ts, te = item.get('t_start'), item.get('t_end')
|
| 91 |
+
if ts is None or te is None:
|
| 92 |
+
raise KeyError('evidence dict needs t_start and t_end to be scored '
|
| 93 |
+
'for temporal validity')
|
| 94 |
+
else:
|
| 95 |
+
raise TypeError('evidence items must be dicts or 5-tuples, got %r'
|
| 96 |
+
% type(item).__name__)
|
| 97 |
+
return (s, r, o, float(ts), float(te))
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def score_question(question, evidence, key='auto'):
|
| 101 |
+
"""Score one question's retrieved evidence. Returns a per-question row.
|
| 102 |
+
|
| 103 |
+
`question` is a record from benchmark_labelled.jsonl (or benchmark.jsonl).
|
| 104 |
+
`evidence` is what your retriever returned for it, already truncated to
|
| 105 |
+
your k -- TRP is a precision quantity, so this module does not truncate for
|
| 106 |
+
you.
|
| 107 |
+
"""
|
| 108 |
+
retrieved = [as_triple(e, key) for e in evidence]
|
| 109 |
+
tq = float(question['t_query'])
|
| 110 |
+
|
| 111 |
+
gold = question['S_star']
|
| 112 |
+
use_id = (key == 'id' or
|
| 113 |
+
(key == 'auto' and all(k in gold[0] for k in _ID_KEYS)))
|
| 114 |
+
gk = _ID_KEYS if use_id else _LABEL_KEYS
|
| 115 |
+
gold_ids = {tuple(g[k] for k in gk) for g in gold}
|
| 116 |
+
|
| 117 |
+
hits = [t for t in retrieved
|
| 118 |
+
if t[:3] in gold_ids and t[3] <= tq <= t[4]]
|
| 119 |
+
covered = {t[:3] for t in hits}
|
| 120 |
+
n_ret = len(retrieved)
|
| 121 |
+
|
| 122 |
+
return {
|
| 123 |
+
'id': question.get('id'),
|
| 124 |
+
'complexity': question.get('complexity'),
|
| 125 |
+
'operator': question.get('operator_type'),
|
| 126 |
+
'n_ret': n_ret,
|
| 127 |
+
'hits': len(hits),
|
| 128 |
+
'n_gold': len(gold_ids),
|
| 129 |
+
'trp': (len(hits) / n_ret) if n_ret else None,
|
| 130 |
+
'ccr': 1 if (gold_ids and covered == gold_ids) else 0,
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _cell(rows):
|
| 135 |
+
scored = [r for r in rows if r['n_ret'] > 0]
|
| 136 |
+
trp = [r['trp'] for r in scored]
|
| 137 |
+
return {
|
| 138 |
+
'n': len(rows),
|
| 139 |
+
'TRP': (sum(trp) / len(trp)) if trp else 0.0,
|
| 140 |
+
'CCR': (sum(r['ccr'] for r in rows) / len(rows)) if rows else 0.0,
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def aggregate(rows, by=('complexity', 'operator')):
|
| 145 |
+
"""Corpus-level metrics, plus the per-slice breakdowns the paper reports.
|
| 146 |
+
|
| 147 |
+
TRP is macro-averaged over questions that retrieved something; CCR is
|
| 148 |
+
averaged over all of them. `coverage` is the fraction that retrieved
|
| 149 |
+
anything -- report it, because a system that returns nothing on hard
|
| 150 |
+
questions otherwise inflates its own TRP.
|
| 151 |
+
"""
|
| 152 |
+
if not rows:
|
| 153 |
+
return {'n_questions': 0}
|
| 154 |
+
scored = [r for r in rows if r['n_ret'] > 0]
|
| 155 |
+
total_ret = sum(r['n_ret'] for r in rows)
|
| 156 |
+
out = {
|
| 157 |
+
'n_questions': len(rows),
|
| 158 |
+
'coverage': len(scored) / len(rows),
|
| 159 |
+
'TRP_macro': (sum(r['trp'] for r in scored) / len(scored)) if scored else 0.0,
|
| 160 |
+
'TRP_micro': (sum(r['hits'] for r in rows) / total_ret) if total_ret else 0.0,
|
| 161 |
+
'CCR': sum(r['ccr'] for r in rows) / len(rows),
|
| 162 |
+
}
|
| 163 |
+
for field in by:
|
| 164 |
+
buckets = defaultdict(list)
|
| 165 |
+
for r in rows:
|
| 166 |
+
buckets[r.get(field)].append(r)
|
| 167 |
+
out['by_' + field] = {k: _cell(v) for k, v in sorted(
|
| 168 |
+
buckets.items(), key=lambda kv: str(kv[0]))}
|
| 169 |
+
return out
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
if __name__ == '__main__':
|
| 173 |
+
# Self-check on a synthetic question: a perfect retrieval, a stale-fact
|
| 174 |
+
# retrieval, and an empty one. Needs no data files.
|
| 175 |
+
q = {
|
| 176 |
+
'id': 'demo', 'complexity': '1hop', 'operator_type': 'point_in_time',
|
| 177 |
+
't_query': 1994.0,
|
| 178 |
+
'S_star': [{'s': 'A', 'r': 'rel', 'o': 'B',
|
| 179 |
+
't_start': 1994.0, 't_end': 1994.0}],
|
| 180 |
+
}
|
| 181 |
+
gold = {'s': 'A', 'r': 'rel', 'o': 'B', 't_start': 1994.0, 't_end': 1994.0}
|
| 182 |
+
stale = dict(gold, t_start=1991.0, t_end=1991.0)
|
| 183 |
+
noise = {'s': 'A', 'r': 'rel', 'o': 'C',
|
| 184 |
+
't_start': 1994.0, 't_end': 1994.0}
|
| 185 |
+
|
| 186 |
+
perfect = score_question(q, [gold])
|
| 187 |
+
diluted = score_question(q, [gold, noise])
|
| 188 |
+
stale_only = score_question(q, [stale])
|
| 189 |
+
empty = score_question(q, [])
|
| 190 |
+
|
| 191 |
+
assert (perfect['trp'], perfect['ccr']) == (1.0, 1)
|
| 192 |
+
assert (diluted['trp'], diluted['ccr']) == (0.5, 1), diluted
|
| 193 |
+
assert (stale_only['trp'], stale_only['ccr']) == (0.0, 0), stale_only
|
| 194 |
+
assert empty['trp'] is None and empty['ccr'] == 0
|
| 195 |
+
|
| 196 |
+
agg = aggregate([perfect, diluted, stale_only, empty])
|
| 197 |
+
assert agg['coverage'] == 0.75, agg
|
| 198 |
+
assert agg['CCR'] == 0.5, agg
|
| 199 |
+
print('tempbench_eval self-check OK')
|
| 200 |
+
print(' perfect ', perfect)
|
| 201 |
+
print(' +1 distractor', diluted)
|
| 202 |
+
print(' stale only ', stale_only)
|
| 203 |
+
print(' aggregate ', {k: v for k, v in agg.items()
|
| 204 |
+
if not k.startswith('by_')})
|