| |
| """Validate the standardized PDENNEval dataset package.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import hashlib |
| import json |
| import math |
| import sys |
| from pathlib import Path |
|
|
| import h5py |
| import numpy as np |
|
|
|
|
| REPO_ROOT = Path(__file__).resolve().parents[1] |
| DATA_ROOT = REPO_ROOT / "data" |
| CHECKSUM_PATH = REPO_ROOT / "files_sha256.jsonl" |
|
|
| EXPECTED_FILES = { |
| "1D_Burgers_Sols_Nu0.001.hdf5": { |
| "datasets": { |
| "tensor": {"ndim": 3, "dtype": "float32"}, |
| "x-coordinate": {"ndim": 1, "dtype": "float32"}, |
| "t-coordinate": {"ndim": 1, "dtype": "float32"}, |
| }, |
| "attrs": {"Nu": 0.001}, |
| }, |
| "2D_DarcyFlow_beta0.1_Train.hdf5": { |
| "datasets": { |
| "tensor": {"ndim": 4, "dtype": "float32"}, |
| "nu": {"ndim": 3, "dtype": "float32"}, |
| "x-coordinate": {"ndim": 1, "dtype": "float32"}, |
| "y-coordinate": {"ndim": 1, "dtype": "float32"}, |
| }, |
| "attrs": {"beta": 0.1}, |
| }, |
| "1D_Advection_Sols_beta1.0.hdf5": { |
| "datasets": { |
| "tensor": {"ndim": 3, "dtype": "float32"}, |
| "x-coordinate": {"ndim": 1, "dtype": "float32"}, |
| "t-coordinate": {"ndim": 1, "dtype": "float32"}, |
| }, |
| "attrs": {}, |
| }, |
| } |
|
|
|
|
| def fail(message: str) -> None: |
| print(f"[FAIL] {message}") |
| raise SystemExit(1) |
|
|
|
|
| def ok(message: str) -> None: |
| print(f"[OK] {message}") |
|
|
|
|
| def warn(message: str) -> None: |
| print(f"[WARN] {message}") |
|
|
|
|
| def sha256_file(path: Path) -> str: |
| digest = hashlib.sha256() |
| with path.open("rb") as handle: |
| for chunk in iter(lambda: handle.read(1024 * 1024), b""): |
| digest.update(chunk) |
| return digest.hexdigest() |
|
|
|
|
| def as_float(value: object) -> float | None: |
| try: |
| return float(value) |
| except (TypeError, ValueError): |
| return None |
|
|
|
|
| def validate_hdf5_file(path: Path, spec: dict[str, object]) -> None: |
| if not path.is_file(): |
| fail(f"missing expected HDF5 file: {path}") |
| with h5py.File(path, "r") as handle: |
| for attr_name, expected in spec.get("attrs", {}).items(): |
| actual = as_float(handle.attrs.get(attr_name)) |
| if actual is None or not math.isclose(actual, float(expected), rel_tol=1e-6, abs_tol=1e-12): |
| fail(f"{path.name} attr {attr_name!r} expected {expected}, got {handle.attrs.get(attr_name)!r}") |
| for dataset_name, dataset_spec in spec["datasets"].items(): |
| if dataset_name not in handle: |
| fail(f"{path.name} missing dataset {dataset_name!r}") |
| dataset = handle[dataset_name] |
| if dataset.ndim != dataset_spec["ndim"]: |
| fail(f"{path.name}/{dataset_name} ndim expected {dataset_spec['ndim']}, got {dataset.ndim}") |
| if str(dataset.dtype) != dataset_spec["dtype"]: |
| fail(f"{path.name}/{dataset_name} dtype expected {dataset_spec['dtype']}, got {dataset.dtype}") |
| if any(dim <= 0 for dim in dataset.shape): |
| fail(f"{path.name}/{dataset_name} has invalid shape {dataset.shape}") |
| probe = np.asarray(dataset[0]) |
| if not np.isfinite(probe).all(): |
| fail(f"{path.name}/{dataset_name} first slice contains non-finite values") |
| ok(f"{path.name} HDF5 schema is readable") |
|
|
|
|
| def verify_checksums(full_hash: bool) -> None: |
| if not CHECKSUM_PATH.exists(): |
| warn(f"checksum manifest is not present: {CHECKSUM_PATH}") |
| return |
| records = [json.loads(line) for line in CHECKSUM_PATH.read_text(encoding="utf-8").splitlines() if line.strip()] |
| if not records: |
| fail("checksum manifest is empty") |
| for record in records: |
| path = REPO_ROOT / record["path"] |
| if not path.is_file(): |
| fail(f"checksum entry points to missing file: {path}") |
| size = path.stat().st_size |
| if size != record["size"]: |
| fail(f"size mismatch for {path}: expected {record['size']}, got {size}") |
| if full_hash: |
| digest = sha256_file(path) |
| if digest != record["sha256"]: |
| fail(f"sha256 mismatch for {path}") |
| mode = "size+sha256" if full_hash else "size" |
| ok(f"checksum manifest verified in {mode} mode: {len(records)} files") |
|
|
|
|
| def main() -> int: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--full-hash", action="store_true", help="verify SHA256 for all large HDF5 files") |
| args = parser.parse_args() |
|
|
| if not DATA_ROOT.is_dir(): |
| fail(f"dataset data root does not exist: {DATA_ROOT}") |
| for filename, spec in EXPECTED_FILES.items(): |
| validate_hdf5_file(DATA_ROOT / filename, spec) |
| verify_checksums(args.full_hash) |
| ok("dataset validation completed") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|