Repository navigation
Expand file tree
/
Copy pathtest_citations.py
More file actions
424 lines (364 loc) · 14 KB
/
Copy pathtest_citations.py
File metadata and controls
424 lines (364 loc) · 14 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import importlib
import inspect
import pkgutil
import re
import time
import xml.etree.ElementTree as ET
from collections import defaultdict
from collections.abc import Callable
from typing import Any, cast
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import saps
import saps.benchmarks
from saps import Author, Ref
_DOI_RE = re.compile(r"\b10\.\d{4,9}/[-._;()/:A-Z0-9]+", re.IGNORECASE)
# The `arxiv` package's Client queries export.arxiv.org/api/query, whose
# id_list lookups (unlike common cached search_query lookups) currently hang
# or 500 rather than responding, and the package's session.get() call omits
# a timeout so a stall blocks forever (see arxiv/__init__.py:567 in
# arxiv==4.0.0, unchanged as of the project's current master). arXiv's own
# docs recommend the OAI-PMH interface for metadata harvesting instead; it
# hits a different backend (oaipmh.arxiv.org) and has proven reliable where
# the legacy query API has not, so we use it directly rather than going
# through the `arxiv` package.
_OAI_NS = {
"oai": "http://www.openarchives.org/OAI/2.0/",
"arxiv": "http://arxiv.org/OAI/arXiv/",
}
# New-style arXiv IDs (post-2007) encode their original submission year and
# month as YYMM, e.g. "1508.03619" -> 2015-08. This is the v1 submission
# year, matching what benchmarks cite. The OAI record's created/updated
# dates are NOT a substitute: they track the latest revision (e.g. for
# 1508.03619 they reflect a 2017 revision, four years after the 2015 v1).
_NEW_STYLE_ARXIV_ID_RE = re.compile(r"^(\d{2})(\d{2})\.\d{4,5}$")
def _arxiv_submission_year(arxiv_id: str) -> int | None:
match = _NEW_STYLE_ARXIV_ID_RE.match(arxiv_id)
return 2000 + int(match.group(1)) if match else None
def _oai_arxiv_ref(session: requests.Session, arxiv_id: str) -> Ref | None:
bare_id = re.sub(r"v\d+$", "", arxiv_id)
response = session.get(
"https://export.arxiv.org/oai2",
params={
"verb": "GetRecord",
"identifier": f"oai:arXiv.org:{bare_id}",
"metadataPrefix": "arXiv",
},
timeout=10,
)
response.raise_for_status()
root = ET.fromstring(response.content)
if root.find("oai:error", _OAI_NS) is not None:
return None
record = root.find(".//arxiv:arXiv", _OAI_NS)
if record is None:
return None
authors = []
for author in record.findall("arxiv:authors/arxiv:author", _OAI_NS):
name = " ".join(
part
for part in [
author.findtext("arxiv:forenames", namespaces=_OAI_NS),
author.findtext("arxiv:keyname", namespaces=_OAI_NS),
]
if part
)
if name:
authors.append(Author(name))
year = _arxiv_submission_year(bare_id)
if year is None:
created = record.findtext("arxiv:created", default="", namespaces=_OAI_NS)
year = int(created[:4]) if created[:4].isdigit() else None
return Ref(
title=(
record.findtext("arxiv:title", default="", namespaces=_OAI_NS) or ""
).strip(),
authors=authors,
journal="Arxiv",
volume=f"arXiv:{arxiv_id}",
year=year,
url=f"https://arxiv.org/abs/{arxiv_id}",
)
def _clean_reference_token(value: str) -> str:
return value.rstrip(".,);]")
def _reference_doi(reference: str) -> str | None:
match = _DOI_RE.search(reference)
if match is None:
return None
return _clean_reference_token(match.group(0))
def _arxiv_id_from_url(url: str | None) -> str | None:
if not url:
return None
match = re.search(
r"arxiv\.org/(?:abs|pdf|html)/(?P<id>[^?#\s/]+)",
url,
flags=re.IGNORECASE,
)
if match is None:
return None
return re.sub(r"\.pdf$", "", match.group("id"), flags=re.IGNORECASE)
def _normalize_citation_text(value: str | None) -> str:
text = re.sub(r"\s+", " ", value or "").strip()
text = re.sub(r"\s*:\s*", ":", text)
return text.rstrip(".").casefold()
def _author_key(author: Author) -> str:
words = re.findall(r"[0-9A-Za-zÀ-ÖØ-öø-ÿ]+", author.name.casefold())
return words[-1] if words else ""
def _first(values) -> str | None:
if isinstance(values, list) and values:
return values[0]
if isinstance(values, str):
return values
return None
def _crossref_year(message: dict) -> int | None:
for key in ("published-print", "published-online", "published", "issued"):
date_parts = message.get(key, {}).get("date-parts")
if date_parts and date_parts[0]:
return date_parts[0][0]
return None
def _crossref_authors(message: dict) -> list[Author]:
authors = []
for author in message.get("author", []):
if not isinstance(author, dict):
continue
name = " ".join(
part
for part in [author.get("given"), author.get("family")]
if isinstance(part, str) and part
)
if not name and isinstance(author.get("name"), str):
name = author["name"]
if name:
authors.append(Author(name))
return authors
def _crossref_ref(message: dict) -> Ref:
ref_type = message.get("type")
container_title = _first(message.get("container-title"))
kwargs = {}
if ref_type == "journal-article" and container_title:
kwargs["journal"] = container_title
elif ref_type == "proceedings-article" and container_title:
kwargs["booktitle"] = container_title
elif container_title:
kwargs["publisher"] = container_title
if message.get("publisher") and "publisher" not in kwargs:
kwargs["publisher"] = message["publisher"]
return Ref(
title=_first(message.get("title")) or "",
authors=_crossref_authors(message),
volume=message.get("volume"),
number=message.get("issue"),
pages=message.get("page"),
year=_crossref_year(message),
url=message.get("URL"),
doi=message.get("DOI"),
**kwargs,
)
def _ref_constructor(ref: Ref) -> str:
fields = [
("title", ref.title),
("authors", ref.authors),
("journal", ref.journal),
("conference", ref.conference),
("booktitle", ref.booktitle),
("publisher", ref.publisher),
("institution", ref.institution),
("volume", ref.volume),
("number", ref.number),
("pages", ref.pages),
("city", ref.city),
("year", ref.year),
("url", ref.url),
("doi", ref.doi),
]
lines = ["Ref("]
for name, value in fields:
if value is None or value == []:
continue
if name == "authors":
authors_value = cast(list[Author], value)
authors = ", ".join(f"Author({author.name!r})" for author in authors_value)
lines.append(f" authors=[{authors}],")
else:
lines.append(f" {name}={value!r},")
lines.append(")")
return "\n".join(lines)
def _effective_ref_doi(ref: Ref) -> str | None:
if ref.doi:
return ref.doi.lower()
if ref.url:
return _reference_doi(ref.url)
return None
def _ref_mismatches(actual: Ref, expected: Ref) -> list[str]:
mismatches = []
if _normalize_citation_text(actual.title) != _normalize_citation_text(
expected.title
):
mismatches.append("title")
if expected.year is not None and actual.year != expected.year:
mismatches.append("year")
actual_doi = _effective_ref_doi(actual)
expected_doi = _effective_ref_doi(expected)
if expected_doi and actual_doi and actual_doi.lower() != expected_doi.lower():
mismatches.append("doi")
actual_authors = [_author_key(author) for author in actual.authors]
expected_authors = [_author_key(author) for author in expected.authors]
if expected_authors and actual_authors != expected_authors:
mismatches.append("authors")
return mismatches
def _missing_fields(ref: Ref) -> list[str]:
"""The fields a citation can't do without that `ref` leaves out: what the
work is, who made it, when, and where to find it."""
missing = []
if not ref.title.strip():
missing.append("title")
if not ref.authors:
missing.append("authors")
if ref.year is None:
missing.append("year")
where = (ref.url, ref.doi, ref.journal, ref.conference, ref.booktitle)
if not any(where + (ref.publisher, ref.institution)):
missing.append("url, doi, or venue")
return missing
def _crossref_call(call: Callable[..., Any], **kwargs: Any) -> Any:
"""Crossref answers 429 when queried too fast, so back off and retry."""
from httpx2 import HTTPStatusError
for attempt in range(6):
try:
return call(**kwargs)
except HTTPStatusError as exc:
status = exc.response.status_code
if status not in (429, 500, 502, 503, 504) or attempt == 5:
raise
retry_after = exc.response.headers.get("Retry-After", "")
time.sleep(int(retry_after) if retry_after.isdigit() else 2**attempt)
raise AssertionError("unreachable")
def _crossref_search(crossref_client: Any, ref: Ref) -> Ref | None:
"""Crossref's record of the work `ref` cites, if it has one with the same
title and first author."""
if not ref.title.strip() or not ref.authors:
return None
response = _crossref_call(
crossref_client.works,
query_bibliographic=ref.title,
query_author=ref.authors[0].name,
limit=5,
)
for message in response["message"]["items"]:
candidate = _crossref_ref(message)
if (
candidate.authors
and _normalize_citation_text(candidate.title)
== _normalize_citation_text(ref.title)
and _author_key(candidate.authors[0]) == _author_key(ref.authors[0])
):
return candidate
return None
def _benchmark_instances():
seen = set()
for module_info in pkgutil.iter_modules(saps.benchmarks.__path__):
module = importlib.import_module(f"saps.benchmarks.{module_info.name}")
for _name, cls in inspect.getmembers(module, inspect.isclass):
if cls in seen or inspect.isabstract(cls):
continue
if not issubclass(cls, saps.Benchmark) or cls is saps.Benchmark:
continue
seen.add(cls)
try:
yield cls()
except (TypeError, ValueError):
continue
def _references_by_owner() -> dict[str, tuple[Ref, list[str]]]:
refs: dict[str, Ref] = {}
owners = defaultdict(list)
for benchmark in _benchmark_instances():
for ref in benchmark.references:
refs.setdefault(str(ref), ref)
owners[str(ref)].append(benchmark.name)
for generator in benchmark.generators:
for ref in generator.references:
refs.setdefault(str(ref), ref)
owners[str(ref)].append(f"{benchmark.name} / {generator.name}")
return {
ref_string: (refs[ref_string], ref_owners)
for ref_string, ref_owners in owners.items()
}
def test_citations_are_complete():
failures = [
f"Missing {', '.join(missing)}: {ref}\nowners={owners}"
for ref, owners in _references_by_owner().values()
if (missing := _missing_fields(ref))
]
assert not failures, "\n\n".join(failures)
def test_citations_match_crossref_or_arxiv():
"""Every reference with a DOI or arXiv URL matches that record, and every
other reference cites a work Crossref doesn't have (else it needs its DOI)."""
from habanero import Crossref
from habanero.exceptions import RequestError
from httpx2 import HTTPStatusError
crossref_client = Crossref(mailto="ahrens@gatech.edu", timeout=10)
arxiv_session = requests.Session()
# arXiv's OAI endpoint sometimes stalls or answers 503 with Retry-After
# under load, so retry timeouts and busy responses before failing.
arxiv_session.mount(
"https://",
HTTPAdapter(
max_retries=Retry(
total=4,
backoff_factor=2,
status_forcelist=(429, 500, 502, 503, 504),
allowed_methods=("GET",),
respect_retry_after_header=True,
)
),
)
failures = []
for ref, owners in _references_by_owner().values():
doi = ref.doi or _reference_doi(ref.url or "")
arxiv_id = _arxiv_id_from_url(ref.url)
try:
if doi:
expected = _crossref_ref(
_crossref_call(crossref_client.works, ids=doi)["message"]
)
elif arxiv_id:
expected = _oai_arxiv_ref(arxiv_session, arxiv_id)
if expected is None:
failures.append(
f"arXiv has no record of {arxiv_id}: {ref}\nowners={owners}"
)
continue
else:
found = _crossref_search(crossref_client, ref)
if found is not None:
failures.append(
"\n".join(
[
f"Crossref has this work, add its DOI: {ref}",
f"owners={owners}",
_ref_constructor(found),
]
)
)
continue
except (
HTTPStatusError,
RequestError,
requests.exceptions.RequestException,
ET.ParseError,
) as exc:
failures.append(f"Could not fetch {ref}\nowners={owners}\n{exc}")
continue
mismatches = _ref_mismatches(ref, expected)
if mismatches:
failures.append(
"\n".join(
[
f"Mismatch ({', '.join(mismatches)}): {ref}",
f"owners={owners}",
_ref_constructor(expected),
]
)
)
assert not failures, "\n\n".join(failures)