File size: 4,736 Bytes
a9ed09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""Validate the standardized ANI-1x dataset package."""

from __future__ import annotations

import argparse
import hashlib
import json
from pathlib import Path

import h5py


EXPECTED_SPLITS = {
    "ANI1x_cc_DFT_rc5_train": 8,
    "ANI1x_cc_DFT_rc5_val": 8,
    "ANI1x_cc_DFT_rc5_test": 8,
}
EXPECTED_FILES = {
    "ANI1x_cc_DFT_rc5_statistics.json",
    "ani1x_cc_dft.xyz",
    "ani1x_train.xyz",
    "ani1x_test.xyz",
}
REQUIRED_SAMPLE_KEYS = (
    "atomic_numbers",
    "cell",
    "pbc",
    "positions",
    "properties/energy",
    "properties/forces",
)


def sha256_file(path: Path) -> str:
    h = hashlib.sha256()
    with path.open("rb") as f:
        for chunk in iter(lambda: f.read(1024 * 1024), b""):
            h.update(chunk)
    return h.hexdigest()


def validate_h5(path: Path) -> None:
    with h5py.File(path, "r") as f:
        batches = sorted(f.keys())
        if not batches:
            raise SystemExit(f"[FAIL] HDF5 无 batch: {path}")
        configs = sorted(f[batches[0]].keys())
        if not configs:
            raise SystemExit(f"[FAIL] HDF5 batch 无 config: {path}")
        sample = f[batches[0]][configs[0]]
        for key in REQUIRED_SAMPLE_KEYS:
            if key not in sample:
                raise SystemExit(f"[FAIL] HDF5 缺少字段 {key}: {path}")
        atomic_numbers = sample["atomic_numbers"]
        positions = sample["positions"]
        forces = sample["properties/forces"]
        if atomic_numbers.dtype.kind not in ("i", "u"):
            raise SystemExit(f"[FAIL] atomic_numbers dtype 非整数: {path}")
        if positions.dtype.kind != "f" or positions.shape[-1] != 3:
            raise SystemExit(f"[FAIL] positions dtype/shape 异常: {path}")
        if forces.dtype.kind != "f" or forces.shape != positions.shape:
            raise SystemExit(f"[FAIL] forces dtype/shape 异常: {path}")


def validate_checksum_manifest(dataset_root: Path, manifest_path: Path) -> None:
    if not manifest_path.exists():
        raise SystemExit(f"[FAIL] 校验清单不存在: {manifest_path}")
    checked = 0
    with manifest_path.open("r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            digest, rel = line.split(maxsplit=1)
            path = dataset_root / rel
            if not path.exists():
                raise SystemExit(f"[FAIL] 清单文件不存在: {path}")
            actual = sha256_file(path)
            if actual != digest:
                raise SystemExit(f"[FAIL] SHA256 不一致: {rel}")
            checked += 1
    if checked == 0:
        raise SystemExit("[FAIL] 校验清单为空")
    print(f"[OK] SHA256 清单验证通过: {checked} 个文件")


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataset-root", default="data/ani1x")
    parser.add_argument("--checksum-manifest", default="metadata/sha256_manifest.txt")
    args = parser.parse_args()

    repo_root = Path.cwd()
    dataset_root = Path(args.dataset_root)
    if not dataset_root.is_absolute():
        dataset_root = repo_root / dataset_root
    checksum_manifest = Path(args.checksum_manifest)
    if not checksum_manifest.is_absolute():
        checksum_manifest = repo_root / checksum_manifest

    if not dataset_root.exists():
        raise SystemExit(f"[FAIL] 数据目录不存在: {dataset_root}")

    for rel in EXPECTED_FILES:
        path = dataset_root / rel
        if not path.exists():
            raise SystemExit(f"[FAIL] 缺少文件: {path}")
        if path.stat().st_size <= 0:
            raise SystemExit(f"[FAIL] 文件为空: {path}")
        print(f"[OK] 文件存在: {rel}")

    stats_path = dataset_root / "ANI1x_cc_DFT_rc5_statistics.json"
    with stats_path.open("r", encoding="utf-8") as f:
        stats = json.load(f)
    for key in ("atomic_energies", "avg_num_neighbors", "mean", "std", "atomic_numbers", "r_max"):
        if key not in stats:
            raise SystemExit(f"[FAIL] statistics 缺少字段: {key}")
    print("[OK] statistics JSON 可读")

    for split, expected_count in EXPECTED_SPLITS.items():
        split_dir = dataset_root / split
        h5_files = sorted(split_dir.glob("*.h5"))
        if len(h5_files) != expected_count:
            raise SystemExit(f"[FAIL] {split} 分片数异常: {len(h5_files)} != {expected_count}")
        validate_h5(h5_files[0])
        print(f"[OK] {split}: {len(h5_files)} 个 h5 分片,样本结构可读")

    validate_checksum_manifest(dataset_root, checksum_manifest)
    print("[OK] ANI-1x 数据集读取验证通过")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())