File size: 3,649 Bytes
5ca0182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Validate the standardized DeepCFD dataset package."""

from __future__ import annotations

import argparse
import hashlib
import json
import pickle
import sys
from pathlib import Path

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 = ("dataX.pkl", "dataY.pkl")
EXPECTED_SHAPE = (981, 3, 172, 79)
EXPECTED_DTYPE = np.float32


def fail(message: str) -> None:
    print(f"[FAIL] {message}")
    raise SystemExit(1)


def ok(message: str) -> None:
    print(f"[OK] {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 validate_structure() -> None:
    if not DATA_ROOT.is_dir():
        fail(f"dataset data root does not exist: {DATA_ROOT}")
    actual = sorted(path.name for path in DATA_ROOT.iterdir() if path.is_file())
    if actual != sorted(EXPECTED_FILES):
        fail(f"unexpected data file set: expected {sorted(EXPECTED_FILES)}, got {actual}")
    ok("dataset file names and directory layout are valid")


def load_pickle(path: Path) -> np.ndarray:
    with path.open("rb") as handle:
        arr = np.asarray(pickle.load(handle))
    return arr


def validate_arrays(sample_values: bool) -> None:
    arrays = {}
    for name in EXPECTED_FILES:
        path = DATA_ROOT / name
        arr = load_pickle(path)
        arrays[name] = arr
        if arr.shape != EXPECTED_SHAPE:
            fail(f"data/{name} shape mismatch: expected {EXPECTED_SHAPE}, got {arr.shape}")
        if arr.dtype != EXPECTED_DTYPE:
            fail(f"data/{name} dtype mismatch: expected {EXPECTED_DTYPE}, got {arr.dtype}")
        if sample_values and not np.isfinite(arr[0]).all():
            fail(f"data/{name} first sample contains non-finite values")
    if arrays["dataX.pkl"].shape != arrays["dataY.pkl"].shape:
        fail("dataX.pkl and dataY.pkl shapes are inconsistent")
    ok("dataset PKL files are readable and match expected shape/dtype")


def verify_checksums(full_hash: bool) -> None:
    if not CHECKSUM_PATH.exists():
        fail(f"checksum manifest is not present: {CHECKSUM_PATH}")
    records = [json.loads(line) for line in CHECKSUM_PATH.read_text(encoding="utf-8").splitlines() if line.strip()]
    if len(records) != len(EXPECTED_FILES):
        fail(f"checksum manifest must contain {len(EXPECTED_FILES)} records, got {len(records)}")
    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")
    parser.add_argument("--skip-value-sample", action="store_true")
    args = parser.parse_args()
    validate_structure()
    validate_arrays(sample_values=not args.skip_value_sample)
    verify_checksums(full_hash=args.full_hash)
    ok("dataset validation completed")
    return 0


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