chest-bench-example / 10_tools /validate_package.py
NeeyuHuynh's picture
Upload folder using huggingface_hub
1f965ea verified
Raw History Blame Contribute Delete
3.08 kB
#!/usr/bin/env python3
from pathlib import Path
import json, hashlib, sys
ROOT = Path(__file__).resolve().parents[1]
def sha256(p):
h=hashlib.sha256()
with open(p,"rb") as f:
for c in iter(lambda:f.read(1024*1024),b""): h.update(c)
return h.hexdigest()
errors=[]
# JSON/JSONL syntax
for p in ROOT.rglob("*.jsonl"):
with open(p,encoding="utf-8") as f:
for i,line in enumerate(f,1):
if not line.strip(): continue
try: json.loads(line)
except Exception as e: errors.append(f"{p.relative_to(ROOT)}:{i}: {e}")
for p in ROOT.rglob("*.json"):
try: json.loads(p.read_text(encoding="utf-8"))
except Exception as e: errors.append(f"{p.relative_to(ROOT)}: {e}")
# raw checksums remain immutable
chk=ROOT/"00_raw/checksums.sha256"
for line in chk.read_text().splitlines():
expected, rel=line.split(" ",1)
p=ROOT/rel
if not p.exists(): errors.append(f"missing {rel}")
elif sha256(p)!=expected: errors.append(f"hash mismatch {rel}")
# expected cases
case_map=(ROOT/"01_canonical/case_map.csv").read_text(encoding="utf-8")
for i in range(1,5):
cid=f"case_{i:04d}"
if cid not in case_map: errors.append(f"missing case map entry {cid}")
# QA <-> evidence contract cross references
qas=[json.loads(x) for x in (ROOT/"03_semantics/qa_pairs.jsonl").read_text(encoding="utf-8").splitlines() if x.strip()]
contracts=[json.loads(x) for x in (ROOT/"12_evidence_contract/evidence_contracts.jsonl").read_text(encoding="utf-8").splitlines() if x.strip()]
contract_by_id={x['evidence_contract_id']:x for x in contracts}
qa_ids={x['qa_id'] for x in qas}
for q in qas:
ec=q.get('evidence_contract_id')
if not ec or ec not in contract_by_id: errors.append(f"QA {q['qa_id']} missing valid evidence contract")
for c in contracts:
if c['qa_id'] not in qa_ids: errors.append(f"orphan evidence contract {c['evidence_contract_id']}")
# every QA gets exactly the five planned conditions
perts=[json.loads(x) for x in (ROOT/"13_perturbations/perturbation_manifest.jsonl").read_text(encoding="utf-8").splitlines() if x.strip()]
expected={'correct_image','question_only','same_modality_hard_mismatch','wrong_organ_or_modality','blank_or_target_occluded'}
for qid in qa_ids:
got={x['condition'] for x in perts if x['qa_id']==qid}
if got != expected: errors.append(f"{qid} perturbation conditions mismatch: {sorted(got)}")
# empirical fields must remain unset until a real run exists
for c in contracts:
em=c['empirical_measurement']
if em.get('status')=='not_run':
leaked=[k for k,v in em.items() if k!='status' and v is not None]
if leaked: errors.append(f"{c['qa_id']} has audit values despite status not_run: {leaked}")
if (ROOT/'VERSION').read_text().strip() != '0.2.0': errors.append('VERSION is not 0.2.0')
if errors:
print("VALIDATION FAILED")
print("\n".join(errors))
sys.exit(1)
print("VALIDATION PASS")
print(f"{len(qas)} QA items linked to {len(contracts)} evidence contracts; five-condition perturbation plans verified; raw hashes unchanged.")