File size: 2,542 Bytes
14d6235
3f5d257
14d6235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f5d257
 
14d6235
 
 
 
3f5d257
14d6235
 
 
 
 
 
 
 
3f5d257
14d6235
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Compare every emitted citation pair with an independent SQL source projection."""
import argparse
import json
from pathlib import Path

import duckdb


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("dataset_dir", type=Path)
    parser.add_argument("--source-dir", type=Path, required=True,
                        help="Directory containing the pinned data/all parquet shards")
    parser.add_argument("--temp-dir", type=Path,
                        help="Directory where DuckDB spills to disk")
    args = parser.parse_args()
    config = {"threads": 4}
    if args.temp_dir:
        config["temp_directory"] = str(args.temp_dir)
    c = duckdb.connect(config=config)
    c.read_parquet(str(args.source_dir / "*.parquet")).create_view("source")
    sql = (args.dataset_dir / "provenance/independent_projection.sql").read_text()
    c.execute("CREATE TEMP TABLE expected AS " + sql)
    c.read_json(str(args.dataset_dir / "data/*.jsonl.gz"),
                format="newline_delimited").create_view("records")
    unexpected_constants = c.execute("""SELECT count(*) FROM records
        WHERE enrichedValue.relatedIdentifierType IS DISTINCT FROM 'DOI'
           OR enrichedValue.relationType IS DISTINCT FROM 'IsCitedBy'""").fetchone()[0]
    c.execute("""CREATE TEMP TABLE actual AS
        SELECT doi, enrichedValue.relatedIdentifier AS related_identifier FROM records""")
    missing = c.execute("SELECT count(*) FROM (SELECT * FROM expected EXCEPT SELECT * FROM actual)").fetchone()[0]
    extra = c.execute("SELECT count(*) FROM (SELECT * FROM actual EXCEPT SELECT * FROM expected)").fetchone()[0]
    expected_count = c.execute("SELECT count(*) FROM expected").fetchone()[0]
    actual_count = c.execute("SELECT count(*) FROM actual").fetchone()[0]
    passed = missing == extra == unexpected_constants == 0 and expected_count == actual_count
    result = {"status": "passed" if passed else "failed",
              "independent_expected_citations": expected_count,
              "actual_citations": actual_count, "missing_citations": missing,
              "extra_citations": extra,
              "records_with_unexpected_constants": unexpected_constants}
    print(json.dumps(result, indent=2))
    assert passed
    path = args.dataset_dir / "validation_report.json"
    report = json.loads(path.read_text())
    report["independent_source_comparison"] = result
    path.write_text(json.dumps(report, indent=2) + "\n")


if __name__ == "__main__":
    main()