File size: 2,517 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pathlib
import struct
import subprocess
import sys

root = pathlib.Path.cwd()


def _py_sources():
    _skip = {"tests", "logs", "output", "__pycache__", "node_modules", ".git"}
    return [
        p
        for p in root.rglob("*.py")
        if p.is_file()
        and not (set(p.relative_to(root).parts) & _skip)
        and p.name != "validate_outputs.py"
    ]


def run_candidate() -> None:
    scripts = [root / 'solve.py']
    assert scripts, "missing candidate python source (e.g. solve.py)"
    failures = []
    for script in scripts[:2]:
        proc = subprocess.run(
            [sys.executable, str(script)],
            cwd=root,
            capture_output=True,
            text=True,
            timeout=60,
        )
        if proc.returncode == 0:
            return
        failures.append((script.name, proc.returncode, proc.stdout, proc.stderr))
    raise AssertionError(
        "candidate script(s) failed to execute:\n"
        + "\n".join(
            f"{name}: rc={rc}\nstdout={out}\nstderr={err}" for name, rc, out, err in failures
        )
    )


def validate_outputs() -> None:
    report_path = root / "report.html"
    sample_path = root / "mnist_samples.png"
    for p in (report_path, sample_path):
        assert p.exists() and p.stat().st_size > 0, f"missing {p.name}"

    img_path = root / "train-images-idx3-ubyte"
    label_path = root / "train-labels-idx1-ubyte"
    magic, count, rows, cols = struct.unpack(">IIII", img_path.read_bytes()[:16])
    lmagic, lcount = struct.unpack(">II", label_path.read_bytes()[:8])
    assert (magic, count, rows, cols) == (2051, 10, 8, 8), "unexpected tiny MNIST header"
    assert (lmagic, lcount) == (2049, 10), "unexpected tiny label header"
    assert count == lcount == 10, "image count and label count should match"

    report = report_path.read_text(encoding="utf-8", errors="ignore").lower()
    assert "mnist" in report, "report missing mnist"
    assert "mlp" in report or "network" in report, "report missing mlp/network"
    assert "80" in report and "20" in report, "report should mention 80/20 split"
    assert any(word in report for word in ('transform', 'normalize', 'flip', '变换', '归一化')), "report missing transform discussion"
    assert any(word in report for word in ('layer', 'hidden', 'network', '隐层', '网络')), "report missing network structure discussion"


run_candidate()
validate_outputs()

print("validate_outputs OK: task_049 checks the packaged IDX fixture and MNIST task outputs")