Download verify_source.py from cometadata/crossref-datacite-citations-datacite-enrichment-format: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/datasets/cometadata/crossref-datacite-citations-datacite-enrichment-format/resolve/main/verify_source.py
- Command line
-
hf download hf://datasets/cometadata/crossref-datacite-citations-datacite-enrichment-format/verify_source.py
-
curl -L -o verify_source.py https://huggingface.co/datasets/cometadata/crossref-datacite-citations-datacite-enrichment-format/resolve/main/verify_source.py
2.54 kB
| #!/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() | |