| |
| """Validate AlphaGenome dataset layout, checksums, and minimal readability.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import gzip |
| import hashlib |
| from pathlib import Path |
| import sys |
|
|
| import yaml |
|
|
|
|
| def sha256_file(path: Path) -> str: |
| digest = hashlib.sha256() |
| with path.open("rb") as f: |
| for chunk in iter(lambda: f.read(1024 * 1024), b""): |
| digest.update(chunk) |
| return digest.hexdigest() |
|
|
|
|
| def load_manifest(path: Path) -> dict: |
| return yaml.safe_load(path.read_text(encoding="utf-8")) |
|
|
|
|
| def validate_entry(root: Path, entry: dict, check_sha256: bool) -> None: |
| rel_path = entry["path"] |
| path = root / rel_path |
| if not path.is_file(): |
| raise FileNotFoundError(f"missing file: {rel_path}") |
| size = path.stat().st_size |
| if size != int(entry["size_bytes"]): |
| raise ValueError(f"size mismatch for {rel_path}: {size} != {entry['size_bytes']}") |
| expected = entry.get("sha256") |
| if check_sha256 and expected: |
| actual = sha256_file(path) |
| if actual != expected: |
| raise ValueError(f"sha256 mismatch for {rel_path}: {actual} != {expected}") |
| if rel_path.endswith(".gz.tfrecord"): |
| with gzip.open(path, "rb") as f: |
| if not f.read(1): |
| raise ValueError(f"gzip TFRecord is unreadable: {rel_path}") |
|
|
|
|
| def main() -> int: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--root", default=".", help="dataset package root") |
| parser.add_argument( |
| "--manifest", |
| default="metadata/file_manifest.yaml", |
| help="file manifest relative to --root", |
| ) |
| parser.add_argument( |
| "--check-sha256", |
| action="store_true", |
| help="verify full SHA256 for every listed file", |
| ) |
| args = parser.parse_args() |
|
|
| root = Path(args.root).resolve() |
| manifest = load_manifest(root / args.manifest) |
| entries = manifest["files"] |
| for entry in entries: |
| validate_entry(root, entry, args.check_sha256) |
|
|
| print("dataset_validation_ok: true") |
| print(f"checked_files: {len(entries)}") |
| print(f"total_size_bytes: {sum(int(e['size_bytes']) for e in entries)}") |
| print("full_sha256_checked:", bool(args.check_sha256)) |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| try: |
| raise SystemExit(main()) |
| except Exception as exc: |
| print(f"dataset_validation_ok: false\nerror: {exc}", file=sys.stderr) |
| raise SystemExit(1) |
|
|