File size: 3,956 Bytes
712756b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Machine-readable split, overlap, and sealed-test audit for DENIM data."""

from __future__ import annotations

import hashlib
import json
from collections import Counter, defaultdict
from pathlib import Path

import numpy as np

from src.evaluate_t2_denim_sealed_test_v1 import load_trajectories as load_sealed
from src.train_t2_denim_capability_model_v4 import load_v4_trajectories
from src.train_t2_denim_conditional_v2 import load_all_trajectories


ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / "artifacts" / "t2_denim_protocol_audit_v1.json"


def _hash(*arrays: np.ndarray) -> str:
    digest = hashlib.sha256()
    for value in arrays:
        selected = np.ascontiguousarray(value)
        digest.update(str(selected.shape).encode())
        digest.update(selected.view(np.uint8))
    return digest.hexdigest()


def run() -> dict[str, object]:
    old = load_all_trajectories()
    new, _ = load_v4_trajectories()
    sealed, _ = load_sealed()
    all_items = old + new + sealed
    role_counts = Counter(item.role for item in all_items)
    role_families: dict[str, set[str]] = defaultdict(set)
    full_input_roles: dict[str, set[str]] = defaultdict(set)
    input_target_roles: dict[str, set[str]] = defaultdict(set)
    strain_roles: dict[str, set[str]] = defaultdict(set)
    for item in all_items:
        role_families[item.role].add(item.family)
        descriptors = np.concatenate((item.descriptor.numpy(), np.asarray((item.young, item.poisson, item.yield_stress))))
        full_input_roles[_hash(item.strain.numpy(), descriptors)].add(item.role)
        input_target_roles[_hash(item.strain.numpy(), descriptors, item.stress.numpy())].add(item.role)
        strain_roles[_hash(item.strain.numpy())].add(item.role)

    def collisions(values: dict[str, set[str]]) -> int:
        return sum(len(roles) > 1 for roles in values.values())

    training_families = role_families["train"]
    report = {
        "schema": "agentfem.denim-ml-protocol-audit.v1",
        "trajectory_count": len(all_items),
        "role_counts": dict(sorted(role_counts.items())),
        "exact_cross_role_collisions": {
            "model_input_plus_target": collisions(input_target_roles),
            "full_model_input": collisions(full_input_roles),
            "strain_only": collisions(strain_roles),
        },
        "family_overlap_with_training": {
            role: sorted(families & training_families) for role, families in sorted(role_families.items())
        },
        "sealed_test": {
            "trajectory_count": len(sealed),
            "family_count": len(role_families["sealed_test"]),
            "families": sorted(role_families["sealed_test"]),
            "family_overlap_with_training": sorted(role_families["sealed_test"] & training_families),
            "eligible_for_training_or_selection": False,
        },
        "interpretation": {
            "strain_only_collisions": "Expected transfer pairs can share strain histories across different material descriptors and targets; they are not direct input/target leakage.",
            "existing_test_policy": "The 1,349 pre-existing test trajectories are development benchmarks because prior aggregate results informed later model design.",
            "sealed_policy": "sealed_test_v1 is the first prospective frozen-model test and must never be reused for training or checkpoint selection.",
        },
    }
    report["passed"] = bool(
        report["exact_cross_role_collisions"]["model_input_plus_target"] == 0
        and report["exact_cross_role_collisions"]["full_model_input"] == 0
        and not report["sealed_test"]["family_overlap_with_training"]
        and len(sealed) == 128
    )
    OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    OUTPUT.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    print(json.dumps(report, indent=2, sort_keys=True))
    return report


if __name__ == "__main__":
    run()