File size: 5,883 Bytes
f1564a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Create and verify the public whitelist package for T2 v1."""

from __future__ import annotations

import hashlib
import json
import shutil
from pathlib import Path

import h5py
import numpy as np


ROOT = Path(__file__).resolve().parents[1]
DATA_DIR = ROOT / "data" / "t2_material_loading_memory_v1"
ARTIFACT_DIR = ROOT / "artifacts" / "t2_material_loading_memory_v1"
RELEASE_DIR = ROOT / "releases" / "t2_material_loading_memory_v1"
UPLOAD_DIR = RELEASE_DIR / "upload"


def _sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            digest.update(block)
    return digest.hexdigest()


def _copy(relative_source: str, relative_destination: str | None = None) -> Path:
    source = ROOT / relative_source
    destination = UPLOAD_DIR / (relative_destination or relative_source)
    destination.parent.mkdir(parents=True, exist_ok=True)
    shutil.copy2(source, destination)
    return destination


def build() -> dict[str, object]:
    manifest = json.loads((DATA_DIR / "manifest.json").read_text(encoding="utf-8"))
    quality = json.loads((ARTIFACT_DIR / "quality.json").read_text(encoding="utf-8"))
    baseline = json.loads(
        (ARTIFACT_DIR / "baseline_metrics.json").read_text(encoding="utf-8")
    )
    if quality["status"] != "accepted" or quality["quality_failure_count"] != 0:
        raise RuntimeError("Refusing to package a dataset that did not pass quality gates.")
    if baseline["status"] != "completed":
        raise RuntimeError("Baseline is incomplete.")
    if UPLOAD_DIR.exists():
        shutil.rmtree(UPLOAD_DIR)
    UPLOAD_DIR.mkdir(parents=True)
    copied = [
        _copy("data/t2_material_loading_memory_v1/README.md", "README.md"),
        _copy("data/t2_material_loading_memory_v1/design.jsonl"),
        _copy("data/t2_material_loading_memory_v1/index.jsonl"),
        _copy("data/t2_material_loading_memory_v1/manifest.json"),
        _copy("artifacts/t2_material_loading_memory_v1/QUALITY_REPORT.md"),
        _copy("artifacts/t2_material_loading_memory_v1/quality.json"),
        _copy("artifacts/t2_material_loading_memory_v1/hysteresis_preview.png"),
        _copy("artifacts/t2_material_loading_memory_v1/baseline_metrics.json"),
        _copy("artifacts/t2_material_loading_memory_v1/baseline_predictions.png"),
        _copy("artifacts/t2_material_loading_memory_v1/baseline_models.pt"),
        _copy("configs/t2_material_loading_memory_pilot.json"),
        _copy("configs/t2_material_loading_memory_v1.json"),
        _copy("src/t2_material_loading_memory.py"),
        _copy("src/t2_material_loading_memory_v1.py"),
        _copy("src/load_t2_material_loading_memory.py"),
        _copy("src/baseline_t2_material_loading_memory.py"),
        _copy("src/verify_t2_download.py"),
        _copy("src/package_t2_material_loading_memory_v1.py"),
        _copy("tests/test_t2_material_loading_memory.py"),
        _copy("tests/test_t2_material_loading_memory_v1.py"),
        _copy("handoff/t2_material_loading_memory_v1/START_HERE.md", "START_HERE.md"),
        _copy("handoff/t2_material_loading_memory_v1/REPRODUCE.md", "REPRODUCE.md"),
        _copy("handoff/t2_material_loading_memory_v1/PAPER_ROADMAP.md", "PAPER_ROADMAP.md"),
        _copy("handoff/t2_material_loading_memory_v1/KNOWN_LIMITATIONS.md", "KNOWN_LIMITATIONS.md"),
        _copy("handoff/t2_material_loading_memory_v1/environment-use.yml", "environment-use.yml"),
        _copy(
            "handoff/t2_material_loading_memory_v1/environment-reproduce.yml",
            "environment-reproduce.yml",
        ),
        _copy(
            "handoff/t2_material_loading_memory_v1/reproduce_t2_v1.sh",
            "reproduce_t2_v1.sh",
        ),
        _copy("releases/t1_layered_thermoelastic_v1/upload/DATA_LICENSE.md", "DATA_LICENSE.md"),
        _copy("releases/t1_layered_thermoelastic_v1/upload/CODE_LICENSE", "CODE_LICENSE"),
    ]
    for shard in manifest["shards"]:
        copied.append(_copy(shard["path"]))

    readme = UPLOAD_DIR / "README.md"
    readme.write_text(
        readme.read_text(encoding="utf-8").replace("../../artifacts/", "artifacts/"),
        encoding="utf-8",
    )

    sums_path = UPLOAD_DIR / "SHA256SUMS"
    lines = []
    for path in sorted(copied):
        relative = path.relative_to(UPLOAD_DIR)
        lines.append(f"{_sha256(path)}  {relative}")
    sums_path.write_text("\n".join(lines) + "\n", encoding="utf-8")

    expected_ids = [f"{index:05d}" for index in range(manifest["sample_count"])]
    observed_ids: list[str] = []
    for shard in manifest["shards"]:
        packaged = UPLOAD_DIR / shard["path"]
        if _sha256(packaged) != shard["sha256"]:
            raise RuntimeError(f"Packaged shard hash mismatch: {shard['path']}")
        with h5py.File(packaged, "r") as h5:
            observed_ids.extend(sorted(h5.keys()))
            for group in h5.values():
                if not all(np.isfinite(group[name][:]).all() for name in group.keys()):
                    raise RuntimeError("Packaged shard contains non-finite values.")
    if observed_ids != expected_ids:
        raise RuntimeError("Packaged sample IDs are incomplete or out of order.")
    result = {
        "status": "verified",
        "sample_count": manifest["sample_count"],
        "shard_count": len(manifest["shards"]),
        "file_count_including_sha256sums": len(copied) + 1,
        "bytes_excluding_sha256sums": sum(path.stat().st_size for path in copied),
        "upload_directory": str(UPLOAD_DIR),
    }
    RELEASE_DIR.mkdir(parents=True, exist_ok=True)
    (RELEASE_DIR / "release_check.json").write_text(
        json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8"
    )
    print(json.dumps(result, indent=2, sort_keys=True))
    return result


if __name__ == "__main__":
    build()