#!/usr/bin/env python3 """Validate the SciArgEU release (canonical, derived and auxiliary files). pip install jsonschema python scripts/validate_sciarg_eu_release.py # exit code 1 on any failure python scripts/validate_sciarg_eu_release.py --report # also write validation/validation_report.json Checks * every record of the canonical, topic and full files satisfies schemas/sciarg_eu_response.schema.json * response_id is unique within and across the canonical question files * each topic file and the full file contain exactly the records of the canonical files (same ids, same content) * the label of every record exists in its rubric; one rubric per rubric_id; it equals data/eu/rubrics/.json * every image_path points to an existing file * data/eu/metadata.json counts match the data """ import argparse, json, sys from collections import Counter from pathlib import Path try: import jsonschema except ImportError: sys.exit("jsonschema is required for schema validation: pip install jsonschema") ROOT = Path(__file__).resolve().parents[1] EU = ROOT / "data" / "eu" SCHEMA = ROOT / "schemas" / "sciarg_eu_response.schema.json" TOPIC_OF = {"sound": "sciarg_eu_sound", "biodiversity": "sciarg_eu_biodiversity", "txikipedia": "sciarg_eu_txikipedia"} def load(p): return [json.loads(l) for l in p.read_text(encoding="utf-8").splitlines() if l.strip()] def main(): ap = argparse.ArgumentParser() ap.add_argument("--report", action="store_true", help="write validation/validation_report.json") args = ap.parse_args() errors, validator = [], jsonschema.Draft202012Validator(json.loads(SCHEMA.read_text(encoding="utf-8"))) def check(path, rows): ids = [r["response_id"] for r in rows] if len(ids) != len(set(ids)): errors.append(f"{path.relative_to(ROOT)}: duplicate response_id") for i, r in enumerate(rows, 1): for e in validator.iter_errors(r): errors.append(f"{path.relative_to(ROOT)}: record {i} ({r.get('response_id')}): {e.message[:120]}") break print(f"{path.relative_to(ROOT)}: {len(rows)} records") canonical = {p: load(p) for p in sorted((EU / "questions").glob("*.jsonl"))} for p, rows in canonical.items(): check(p, rows) all_canon = [r for rows in canonical.values() for r in rows] by_id = {r["response_id"]: r for r in all_canon} if len(by_id) != len(all_canon): errors.append("canonical files: response_id repeated across files") for topic, stem in TOPIC_OF.items(): p = EU / "topics" / f"{stem}.jsonl" rows = load(p); check(p, rows) expected = {r["response_id"]: r for r in all_canon if r["topic"] == topic} if {r["response_id"]: r for r in rows} != expected: errors.append(f"{p.relative_to(ROOT)}: differs from the canonical records of topic '{topic}'") p = EU / "sciarg_eu_all.jsonl" rows = load(p); check(p, rows) if {r["response_id"]: r for r in rows} != by_id: errors.append(f"{p.relative_to(ROOT)}: differs from the union of the canonical question files") rubrics = {} for r in all_canon: if r["label"] not in {x["label"] for x in r["rubric"]}: errors.append(f"{r['response_id']}: label {r['label']!r} not in its rubric") if rubrics.setdefault(r["rubric_id"], r["rubric"]) != r["rubric"]: errors.append(f"{r['response_id']}: rubric differs within {r['rubric_id']}") if r["image_path"] and not (ROOT / r["image_path"]).is_file(): errors.append(f"{r['response_id']}: image_path {r['image_path']} does not exist") for rid, rub in rubrics.items(): f = EU / "rubrics" / f"{rid}.json" if not f.is_file(): errors.append(f"missing {f.relative_to(ROOT)}") elif json.loads(f.read_text(encoding="utf-8"))["labels"] != rub: errors.append(f"{f.relative_to(ROOT)}: labels differ from the rubric in the data") meta = json.loads((EU / "metadata.json").read_text(encoding="utf-8")) if meta["counts"]["responses"] != len(all_canon) or meta["counts"]["topics"] != dict(Counter(r["topic"] for r in all_canon)): errors.append("data/eu/metadata.json: counts do not match the data") report = {"passed": not errors, "total_records": len(all_canon), "topics": dict(Counter(r["topic"] for r in all_canon)), "question_ids": dict(Counter(r["question_id"] for r in all_canon)), "assessment_phase": {str(k): v for k, v in Counter(r["assessment_phase"] for r in all_canon).items()}, "group_type": {str(k): v for k, v in Counter(r["group_type"] for r in all_canon).items()}, "label_counts_by_rubric": {rid: dict(Counter(r["label"] for r in all_canon if r["rubric_id"] == rid)) for rid in rubrics}, "errors": errors} if args.report: out = ROOT / "validation" / "validation_report.json" out.parent.mkdir(exist_ok=True) out.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(f"report written to {out.relative_to(ROOT)}") if errors: print(f"\nValidation FAILED ({len(errors)} problem(s)):") for e in errors[:50]: print(" -", e) sys.exit(1) print("Validation passed.") if __name__ == "__main__": main()