|
| 1 | +import numpy as np |
| 2 | +import tensorflow as tf |
| 3 | + |
| 4 | +from cnsd import Dataset |
| 5 | +from cnsd.diagnosis.system import CNSD |
| 6 | +from validate_run import load_cwru, CWRU, TAXONOMY, headline_accuracy_by_verdict |
| 7 | + |
| 8 | + |
| 9 | +def main(): |
| 10 | + # Fix the random seeds to prevent INCONCLUSIVE rate drift between runs |
| 11 | + np.random.seed(42) |
| 12 | + tf.random.set_seed(42) |
| 13 | + |
| 14 | + print("=" * 68) |
| 15 | + print("CNSD THRESHOLD SWEEP (Held-out Calibration Protocol)") |
| 16 | + print("=" * 68) |
| 17 | + |
| 18 | + # 1. Load data |
| 19 | + X, y, cond = load_cwru() |
| 20 | + X = np.asarray(X, np.float32) |
| 21 | + y = np.asarray(y) |
| 22 | + cond = np.asarray(cond) |
| 23 | + |
| 24 | + # 2. Strict 3-way split to prevent test-set leakage |
| 25 | + # Train: Motor loads 0 and 1 |
| 26 | + # Calibration (Sweep): Motor load 2 (CNN never sees this) |
| 27 | + # Test: Motor load 3 |
| 28 | + train_mask = cond < 2 |
| 29 | + calib_mask = cond == 2 |
| 30 | + test_mask = cond == 3 |
| 31 | + |
| 32 | + train_data = Dataset.from_arrays( |
| 33 | + X[train_mask], y[train_mask], cond[train_mask], fs=12000, physics=CWRU, taxonomy=TAXONOMY, name='CWRU_Train_Loads_0_1' |
| 34 | + ) |
| 35 | + calib_data = Dataset.from_arrays( |
| 36 | + X[calib_mask], y[calib_mask], cond[calib_mask], fs=12000, physics=CWRU, taxonomy=TAXONOMY, name='CWRU_Calib_Load_2' |
| 37 | + ) |
| 38 | + test_data = Dataset.from_arrays( |
| 39 | + X[test_mask], y[test_mask], cond[test_mask], fs=12000, physics=CWRU, taxonomy=TAXONOMY, name='CWRU_Test_Load_3' |
| 40 | + ) |
| 41 | + |
| 42 | + print(f"\n[train_data] {train_data.summary()}") |
| 43 | + print(f"[calib_data] {calib_data.summary()}") |
| 44 | + print(f"[test_data] {test_data.summary()}") |
| 45 | + |
| 46 | + # 3. Train the model exclusively on Loads 0 and 1 |
| 47 | + print("\nTraining CNN on Motor Loads 0 and 1...") |
| 48 | + model = CNSD() |
| 49 | + model.fit(train_data, epochs=30) |
| 50 | + |
| 51 | + # 4. Perform the tau sweep on the unseen Calibration Set (Load 2) |
| 52 | + print("\n" + "-" * 68) |
| 53 | + print("SWEEPING TAU ON CALIBRATION SET (LOAD 2)") |
| 54 | + print("-" * 68) |
| 55 | + |
| 56 | + taus = np.arange(1.0, 4.1, 0.5) |
| 57 | + best_tau = None |
| 58 | + best_gap = -np.inf |
| 59 | + |
| 60 | + for tau in taus: |
| 61 | + # Hot-swap the threshold without retraining |
| 62 | + model.symbolic.tau = float(tau) |
| 63 | + report = model.diagnose(calib_data) |
| 64 | + |
| 65 | + hb = headline_accuracy_by_verdict(report, calib_data.y) |
| 66 | + |
| 67 | + conf = hb.get('CONFIRMED', {'cnn_accuracy': 0.0, 'n': 0}) |
| 68 | + cnfl = hb.get('CONFLICT', {'cnn_accuracy': 0.0, 'n': 0}) |
| 69 | + inc = hb.get('INCONCLUSIVE', {'cnn_accuracy': 0.0, 'n': 0}) |
| 70 | + |
| 71 | + gap = conf['cnn_accuracy'] - cnfl['cnn_accuracy'] |
| 72 | + |
| 73 | + # We need a meaningful yield rate so we aren't picking a threshold |
| 74 | + # that marks 99% of samples as INCONCLUSIVE. |
| 75 | + total_samples = len(calib_data.X) |
| 76 | + yield_rate = (conf['n'] + cnfl['n']) / total_samples |
| 77 | + |
| 78 | + print(f"tau={tau:4.1f} | gap={gap:+.3f} | yield={yield_rate:5.1%} | " |
| 79 | + f"CONF={conf['cnn_accuracy']:.3f}(n={conf['n']:4d}) " |
| 80 | + f"CNFL={cnfl['cnn_accuracy']:.3f}(n={cnfl['n']:4d}) " |
| 81 | + f"INC={inc['cnn_accuracy']:.3f}(n={inc['n']:4d})") |
| 82 | + |
| 83 | + if gap > best_gap and yield_rate > 0.05: |
| 84 | + best_gap = gap |
| 85 | + best_tau = float(tau) |
| 86 | + |
| 87 | + print(f"\n=> Optimal threshold found on calibration data: tau = {best_tau}") |
| 88 | + print("=> CNN is frozen. Threshold is frozen.") |
| 89 | + |
| 90 | + # 5. Final, rigorous test on the Test Set (Load 3) |
| 91 | + print("\n" + "=" * 68) |
| 92 | + print(f"FINAL RIGOROUS TEST ON LOAD 3 (Frozen tau={best_tau})") |
| 93 | + print("=" * 68) |
| 94 | + |
| 95 | + model.symbolic.tau = best_tau |
| 96 | + test_report = model.diagnose(test_data) |
| 97 | + test_hb = headline_accuracy_by_verdict(test_report, test_data.y) |
| 98 | + |
| 99 | + for v, d in test_hb.items(): |
| 100 | + print(f" {v:13}: acc={d['cnn_accuracy']:.3f} (n={d['n']})") |
| 101 | + |
| 102 | + if 'CONFIRMED' in test_hb and 'CONFLICT' in test_hb: |
| 103 | + final_gap = test_hb['CONFIRMED']['cnn_accuracy'] - test_hb['CONFLICT']['cnn_accuracy'] |
| 104 | + print(f" -> FINAL GAP: {final_gap:+.3f}") |
| 105 | + |
| 106 | + print("\n[Layer 2] Physics verification rate on Test Set:") |
| 107 | + vr = test_report.verification_rate() |
| 108 | + for k, v in vr.items(): |
| 109 | + print(f" {k:13}: {v:.1%}") |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + main() |
0 commit comments