Step 5: data dictionary and Croissant metadata

- data/dictionary.yaml: every published file, and every column of runs_clean.csv
  with its type, meaning and allowed values
- collection/publish_metadata.py writes data/croissant.json (Croissant 1.0) from
  it, with a sha256 per file; `npm run metadata`, run by the daily update
- tests/core/test_dictionary.py: every column and value described, types hold,
  croissant.json up to date and checksums match
- attempt is now written as a whole number (was 1.0, 2.0); no statistic changes
- the site ships both files and the Methods text links them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
This commit is contained in:
ekcSchiano
2026-11-04 01:37:10 +00:00
co-authored by Claude Opus 5.5
parent 11b4a0398e
commit 63b5d40e54
12 changed files with 4395 additions and 3284 deletions
+78
View File
@@ -0,0 +1,78 @@
"""The data dictionary describes every published column and value, and data/croissant.json matches it."""
import csv
import importlib.util
import json
import re
import unittest
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parents[2]
spec = importlib.util.spec_from_file_location("publish_metadata", ROOT / "collection" / "publish_metadata.py")
publish_metadata = importlib.util.module_from_spec(spec)
spec.loader.exec_module(publish_metadata)
with open(ROOT / "data" / "dictionary.yaml", encoding="utf-8") as f:
DICT = yaml.safe_load(f)
with open(ROOT / "data" / "derived" / "runs_clean.csv", encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
HEADER = reader.fieldnames
ROWS = list(reader)
CHECKS = {
"integer": lambda v: re.fullmatch(r"-?\d+", v),
"boolean": lambda v: v in ("True", "False"),
"date": lambda v: re.fullmatch(r"\d{4}-\d{2}-\d{2}", v),
"text": lambda v: True,
}
class Dictionary(unittest.TestCase):
def test_every_column_described_in_order(self):
self.assertEqual(HEADER, list(DICT["columns"]))
def test_every_column_has_type_and_description(self):
for name, col in DICT["columns"].items():
self.assertIn(col.get("type"), CHECKS, name)
self.assertTrue(col.get("description"), name)
def test_values_have_the_declared_type(self):
for name, col in DICT["columns"].items():
bad = {r[name] for r in ROWS if r[name] and not CHECKS[col["type"]](r[name])}
self.assertFalse(bad, f"{name}: not {col['type']}: {sorted(bad)[:5]}")
def test_every_value_is_listed(self):
for name, col in DICT["columns"].items():
if "values" not in col:
continue
allowed = col["values"]
# YAML reads bare Yes/No as booleans: a listed value must be written as text
self.assertTrue(all(isinstance(v, str) for v in allowed), f"{name}: quote every value")
bad = {r[name] for r in ROWS if r[name] and r[name] not in allowed}
self.assertFalse(bad, f"{name}: values not in the dictionary: {sorted(bad)}")
def test_every_published_file_exists(self):
for key, f in DICT["files"].items():
self.assertTrue((ROOT / f["path"]).exists(), key)
class Croissant(unittest.TestCase):
def test_croissant_is_up_to_date(self):
expected = publish_metadata.render(publish_metadata.build(DICT))
actual = (ROOT / "data" / "croissant.json").read_text(encoding="utf-8")
self.assertEqual(actual, expected, "run python3 collection/publish_metadata.py")
def test_fields_and_checksums(self):
doc = json.loads((ROOT / "data" / "croissant.json").read_text(encoding="utf-8"))
names = [f["name"] for f in doc["recordSet"][0]["field"]]
self.assertEqual(names, HEADER)
for obj in doc["distribution"]:
path = ROOT / DICT["files"][obj["@id"]]["path"]
self.assertEqual(obj["sha256"], publish_metadata.sha256(path), obj["@id"])
# the route PDF is not redistributed, so it is not a source of the published data
self.assertNotIn("pdf", json.dumps(doc["isBasedOn"]).lower())
if __name__ == "__main__":
unittest.main()