File size: 6,924 Bytes
2b3f8d7
 
 
0f4ef83
2b3f8d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f4ef83
 
 
 
 
 
 
 
 
 
 
 
 
 
2b3f8d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
from pathlib import Path
import json
import re
import sys


ROOT = Path(__file__).resolve().parents[1]

REQUIRED_FILES = [
    "README.md",
    "DATASHEET.md",
    "METHODS.md",
    "PRIVACY_AND_DISCLOSURE.md",
    "EVALUATION_REPORT.md",
    "EVALUATION_METRICS.json",
    "SAFE_USE_POLICY.md",
    "MODEL_AND_DATA_GOVERNANCE.md",
    "CITATION.cff",
    "ZENODO_METADATA.json",
    "datacite_metadata.json",
    "release_manifest.json",
    "RELEASE_CHECKLIST.md",
    "docs/DOI_RELEASE_PLAN.md",
    "docs/ANNOUNCEMENT_POST.md",
    "docs/REVIEW_NOTES.md",
]

REQUIRED_ARTIFACTS = [
    "persona_core",
    "household_core",
    "persona_views",
    "actor_state_init",
    "benchmark_tasks",
    "source_registry",
    "field_provenance",
]

REQUIRED_README_STRINGS = [
    "Package architecture",
    "Responsible use",
    "not observed microdata",
    "Synthetic personas are not real people",
]


def read_json(rel):
    path = ROOT / rel
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except Exception as exc:
        raise ValueError(f"{rel} is not valid JSON: {exc}") from exc


def is_lfs_pointer(path):
    try:
        head = path.read_bytes()[:120]
    except OSError:
        return False
    return head.startswith(b"version https://git-lfs.github.com/spec/v1")


def main():
    errors = []
    warnings = []

    for rel in REQUIRED_FILES:
        if not (ROOT / rel).exists():
            errors.append(f"Missing required file: {rel}")

    if errors:
        for err in errors:
            print("ERROR:", err)
        return 1

    try:
        manifest = read_json("release_manifest.json")
        metrics = read_json("EVALUATION_METRICS.json")
        zenodo = read_json("ZENODO_METADATA.json")
        datacite = read_json("datacite_metadata.json")
    except ValueError as exc:
        print("ERROR:", exc)
        return 1

    readme = (ROOT / "README.md").read_text(encoding="utf-8", errors="ignore")
    if not readme.startswith("---"):
        errors.append("README.md does not start with Hugging Face YAML front matter")
    for needle in REQUIRED_README_STRINGS:
        if needle not in readme:
            errors.append(f"README.md missing required text: {needle}")

    citation = (ROOT / "CITATION.cff").read_text(encoding="utf-8", errors="ignore")
    for needle in [
        "cff-version: 1.2.0",
        "type: dataset",
        "license: \"CC-BY-4.0\"",
    ]:
        if needle not in citation:
            errors.append(f"CITATION.cff missing required text: {needle}")

    doi_pattern = re.compile(r"^10\.\d{4,9}/[-._;()/:A-Za-z0-9]+$")
    zenodo_doi = zenodo.get("doi")
    datacite_doi = datacite.get("identifier", {}).get("identifier")
    citation_doi_match = re.search(r'^doi:\s+"([^"]+)"', citation, re.MULTILINE)
    citation_doi = citation_doi_match.group(1) if citation_doi_match else None
    for label, doi in [
        ("ZENODO_METADATA.json doi", zenodo_doi),
        ("datacite_metadata.json identifier", datacite_doi),
        ("CITATION.cff doi", citation_doi),
    ]:
        if doi != "10.XXXX/zenodo.XXXXXXX" and not (isinstance(doi, str) and doi_pattern.match(doi)):
            errors.append(f"{label} is not a DOI placeholder or valid DOI: {doi!r}")
    if zenodo_doi and zenodo_doi not in readme and "10.XXXX/zenodo.XXXXXXX" not in readme:
        errors.append("README.md does not include the Zenodo DOI or placeholder")

    artifacts = manifest.get("artifacts", {})
    for artifact in REQUIRED_ARTIFACTS:
        entry = artifacts.get(artifact)
        if not entry:
            errors.append(f"release_manifest.json missing artifact: {artifact}")
            continue
        parquet = entry.get("parquet")
        if not parquet:
            errors.append(f"release_manifest.json artifact {artifact} missing parquet path")
            continue
        if not (ROOT / parquet).exists():
            errors.append(f"Missing artifact file declared in manifest: {parquet}")

    release = metrics.get("release", {})
    manifest_pairs = [
        ("total_personas", "personas"),
        ("total_households", "households"),
        ("total_persona_views", "persona_views"),
        ("release_date", "release_date"),
        ("population_reference_date", "population_reference_date"),
    ]
    for manifest_key, metric_key in manifest_pairs:
        if manifest.get(manifest_key) != release.get(metric_key):
            errors.append(
                f"Manifest/metrics mismatch for {manifest_key}/{metric_key}: "
                f"{manifest.get(manifest_key)!r} != {release.get(metric_key)!r}"
            )

    expected_total_rows = sum(
        int(artifacts[name]["rows"])
        for name in REQUIRED_ARTIFACTS
        if name in artifacts and "rows" in artifacts[name]
    )
    if expected_total_rows != 8889089:
        warnings.append(f"Total package rows from manifest are {expected_total_rows:,}, expected v0.1 value is 8,889,089")

    safe_use = (ROOT / "SAFE_USE_POLICY.md").read_text(encoding="utf-8", errors="ignore")
    for needle in ["political microtargeting", "individual-level persuasion", "automated eligibility decisions"]:
        if needle not in safe_use:
            errors.append(f"SAFE_USE_POLICY.md missing prohibition: {needle}")

    try:
        import pyarrow.parquet as pq
    except Exception as exc:
        warnings.append(f"pyarrow not available; skipped Parquet row inspection: {exc}")
    else:
        for artifact in REQUIRED_ARTIFACTS:
            entry = artifacts.get(artifact, {})
            parquet_name = entry.get("parquet")
            if not parquet_name:
                continue
            path = ROOT / parquet_name
            if is_lfs_pointer(path):
                warnings.append(f"{parquet_name} is a Git LFS pointer; skipped Parquet row inspection")
                continue
            try:
                pf = pq.ParquetFile(path)
                actual_rows = pf.metadata.num_rows
            except Exception as exc:
                errors.append(f"Could not inspect {parquet_name}: {exc}")
                continue
            expected_rows = int(entry["rows"])
            if actual_rows != expected_rows:
                errors.append(f"{parquet_name} row count mismatch: {actual_rows:,} != {expected_rows:,}")

    print("Release:", manifest.get("id"))
    print("Release date:", manifest.get("release_date"))
    print("Population reference date:", manifest.get("population_reference_date"))
    print("Manifest package rows:", f"{expected_total_rows:,}")

    if warnings:
        print("\nWarnings:")
        for warning in warnings:
            print("-", warning)

    if errors:
        print("\nValidation errors:")
        for err in errors:
            print("-", err)
        return 1

    print("\nOK: release documentation and metadata validation passed")
    return 0


if __name__ == "__main__":
    sys.exit(main())