#!/usr/bin/env python3 """Integrity checks for this dataset, run against the canonical source, live. Usage: python validate.py Requires: pyarrow (pip install pyarrow) Every claim this file checks is a claim the dataset card makes. The point of the file is that a reader does not have to take the card's word for any of them. PACKAGING the parquet here is exactly what the old loading script produced: same six fields in the same order, same types, same rows, same order, all three splits. This is the whole basis on which the script was allowed to be deleted, so it is checked first. PROJECTION `created_by`, `created_on` and `is_pay` do not appear. The source records carry them; the published dataset never has. `created_by` resolves to 57 individual annotators and `is_pay` holds what each of them was paid per item, so this check is not cosmetic and is the reason the projection is reproduced rather than reinvented. INTEGRITY every answer span is exactly the slice of its context that it claims to be, and no article is split across two splits. Non-zero exit on any failure. """ import io import json import sys import urllib.request import zipfile import pyarrow as pa import pyarrow.parquet as pq SOURCE = ("https://github.com/iapp-technology/iapp-wiki-qa-dataset" "/raw/main/squad_format/data.zip") FIELDS = ["question_id", "article_id", "title", "context", "question", "answers"] WITHHELD = ["created_by", "created_on", "is_pay"] # split name -> (file in this repository, file inside the source archive, rows, articles) SPLITS = { "train": ("data/train-00000-of-00001.parquet", "train.jsonl", 5761, 1529), "validation": ("data/validation-00000-of-00001.parquet", "valid.jsonl", 742, 191), "test": ("data/test-00000-of-00001.parquet", "test.jsonl", 739, 192), } failures = [] def report(ok, label, detail=""): print(f" {'PASS' if ok else 'FAIL'} {label}{'' if ok else ' -- ' + detail}") if not ok: failures.append(label) def fetch_archive(): request = urllib.request.Request(SOURCE, headers={"User-Agent": "iapp-validate"}) with urllib.request.urlopen(request, timeout=300) as response: return zipfile.ZipFile(io.BytesIO(response.read())) def project(record): """The loading script's projection, reproduced. Six fields, in its order.""" return { "question_id": record["question_id"], "article_id": record["article_id"], "title": record["title"], "context": record["context"], "question": record["question"], "answers": { "text": record["answers"]["text"], "answer_start": record["answers"]["answer_start"], "answer_end": record["answers"]["answer_end"], }, } def source_rows(archive, filename): with archive.open(f"data/{filename}") as handle: for line in io.TextIOWrapper(handle, encoding="utf-8"): yield project(json.loads(line)) def main(): print("Fetching the canonical source from GitHub ...") try: archive = fetch_archive() except Exception as error: print(f" FAIL source archive unreachable: {error}") return 1 published = {} print("\nPACKAGING") for split, (path, filename, rows, _) in SPLITS.items(): try: table = pq.read_table(path) except Exception as error: report(False, f"{split}: file reads", str(error)) continue published[split] = table report(table.column_names == FIELDS, f"{split}: six fields, in order", str(table.column_names)) report(table.num_rows == rows, f"{split}: {rows} rows", f"found {table.num_rows}") # Compare the value types, not their string form: pyarrow names the child # field of a list `item` in some versions and `element` in others, and that # difference is not a difference in the data. answers = table.schema.field("answers").type shape = {} for i in range(answers.num_fields): field = answers.field(i) shape[field.name] = (str(field.type.value_type) if pa.types.is_list(field.type) else str(field.type)) expected = {"text": "string", "answer_start": "int32", "answer_end": "int32"} report(shape == expected, f"{split}: answers is a sequence of three lists", json.dumps(shape)) here = table.to_pylist() there = list(source_rows(archive, filename)) if len(here) == len(there): mismatched = [i for i, (a, b) in enumerate(zip(here, there)) if a != b] report(not mismatched, f"{split}: every row equals the projection of the source, in order", f"{len(mismatched)} rows differ, first at {mismatched[:5]}") else: report(False, f"{split}: row counts agree with the source", f"{len(here)} here, {len(there)} in the source") print("\nPROJECTION") for split, table in published.items(): leaked = [f for f in WITHHELD if f in table.column_names] report(not leaked, f"{split}: withholds annotator identity and payment fields", f"present: {leaked}") print("\nINTEGRITY") for split, table in published.items(): rows = table.to_pylist() bad = 0 for row in rows: answers = row["answers"] for text, start, end in zip(answers["text"], answers["answer_start"], answers["answer_end"]): if row["context"][start:end] != text: bad += 1 report(bad == 0, f"{split}: every answer span is its context slice", f"{bad} spans do not match") articles = {row["article_id"] for row in rows} report(len(articles) == SPLITS[split][3], f"{split}: {SPLITS[split][3]} distinct articles", f"found {len(articles)}") if len(published) == 3: seen = {split: {row["article_id"] for row in table.to_pylist()} for split, table in published.items()} overlap = ((seen["train"] & seen["validation"]) | (seen["train"] & seen["test"]) | (seen["validation"] & seen["test"])) report(not overlap, "no article appears in more than one split", f"{len(overlap)} shared article ids") print() if failures: print(f"{len(failures)} check(s) failed:") for failure in failures: print(f" {failure}") return 1 print("all checks passed") return 0 if __name__ == "__main__": sys.exit(main())