|
| 1 | +import os |
| 2 | +import glob |
| 3 | +import numpy as np |
| 4 | +import scipy.io as sio |
| 5 | + |
| 6 | +from cnsd import Dataset |
| 7 | +from cnsd.diagnosis.system import CNSD |
| 8 | +from cnsd.physics import PhysicsConfig |
| 9 | + |
| 10 | + |
| 11 | +def load_pu(data_dir=r'E:\301\PU-dataset', window_size=2048): |
| 12 | + """ |
| 13 | + Loads authentic Paderborn University dataset .mat files. |
| 14 | + K00x = Healthy (0) |
| 15 | + KAxx = Outer Race Fault (1) |
| 16 | + KIxx = Inner Race Fault (2) |
| 17 | + """ |
| 18 | + X = [] |
| 19 | + y = [] |
| 20 | + cond = [] |
| 21 | + |
| 22 | + categories = [('K0*', 0), ('KA*', 1), ('KI*', 2)] |
| 23 | + |
| 24 | + for prefix, label in categories: |
| 25 | + pattern = os.path.join(data_dir, prefix, '*.mat') |
| 26 | + files = glob.glob(pattern) |
| 27 | + for fpath in files: |
| 28 | + fname = os.path.basename(fpath) |
| 29 | + key = fname.replace('.mat', '') |
| 30 | + |
| 31 | + # N15 -> 1500 RPM, N09 -> 900 RPM |
| 32 | + rpm_code = fname.split('_')[0] |
| 33 | + rpm = float(rpm_code[1:]) * 100.0 if rpm_code.startswith('N') else 1500.0 |
| 34 | + |
| 35 | + try: |
| 36 | + mat = sio.loadmat(fpath) |
| 37 | + if key not in mat: |
| 38 | + continue |
| 39 | + |
| 40 | + y_struct = mat[key]['Y'][0, 0] |
| 41 | + |
| 42 | + # Find the vibration_1 channel |
| 43 | + vib_idx = -1 |
| 44 | + for i in range(y_struct['Name'].shape[1]): |
| 45 | + if 'vibration' in str(y_struct['Name'][0, i][0]).lower(): |
| 46 | + vib_idx = i |
| 47 | + break |
| 48 | + |
| 49 | + if vib_idx == -1: |
| 50 | + continue |
| 51 | + |
| 52 | + sig = y_struct['Data'][0, vib_idx].flatten() |
| 53 | + |
| 54 | + # Segment signal |
| 55 | + for i in range(0, len(sig) - window_size, window_size): |
| 56 | + segment = sig[i : i + window_size] |
| 57 | + segment = (segment - np.mean(segment)) / (np.std(segment) + 1e-8) |
| 58 | + X.append(segment) |
| 59 | + y.append(label) |
| 60 | + cond.append(rpm) |
| 61 | + |
| 62 | + except Exception as e: |
| 63 | + print(f'Error loading {fname}: {e}') |
| 64 | + |
| 65 | + return np.array(X, dtype=np.float32), np.array(y), np.array(cond) |
| 66 | + |
| 67 | + |
| 68 | +def headline_accuracy_by_verdict(report, y_true): |
| 69 | + pred = np.array([r['predicted_class'] for r in report.records]) |
| 70 | + correct = pred == np.asarray(y_true) |
| 71 | + verdicts = np.array([r['physics_verdict'] for r in report.records]) |
| 72 | + out = {} |
| 73 | + for v in ('CONFIRMED', 'CONFLICT', 'INCONCLUSIVE'): |
| 74 | + m = verdicts == v |
| 75 | + if m.any(): |
| 76 | + out[v] = {'n': int(m.sum()), 'cnn_accuracy': float(correct[m].mean())} |
| 77 | + return out |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + print('Loading Authentic PU dataset (Real Fatigue Damages)...') |
| 82 | + X, y, cond = load_pu() |
| 83 | + |
| 84 | + print(f'Total samples extracted: {len(X)}') |
| 85 | + |
| 86 | + # Stratified split: 60% Train, 20% Calib, 20% Test |
| 87 | + indices = np.arange(len(y)) |
| 88 | + np.random.shuffle(indices) |
| 89 | + |
| 90 | + train_size = int(len(y) * 0.6) |
| 91 | + calib_size = int(len(y) * 0.2) |
| 92 | + |
| 93 | + train_idx = indices[:train_size] |
| 94 | + calib_idx = indices[train_size : train_size + calib_size] |
| 95 | + test_idx = indices[train_size + calib_size :] |
| 96 | + |
| 97 | + X_train, y_train, cond_train = X[train_idx], y[train_idx], cond[train_idx] |
| 98 | + X_calib, y_calib, cond_calib = X[calib_idx], y[calib_idx], cond[calib_idx] |
| 99 | + X_test, y_test, cond_test = X[test_idx], y[test_idx], cond[test_idx] |
| 100 | + |
| 101 | + print(f'Data split: Train={len(y_train)} | Calib={len(y_calib)} | Test={len(y_test)}') |
| 102 | + |
| 103 | + # Map all seen conditions directly to RPM |
| 104 | + unique_rpm = np.unique(cond) |
| 105 | + rpm_map = {float(r): float(r) for r in unique_rpm} |
| 106 | + |
| 107 | + # Paderborn 6203 Bearing Physics Config |
| 108 | + pu_physics = PhysicsConfig( |
| 109 | + bearing={'n_balls': 8, 'd_ball': 7.92, 'd_pitch': 28.55, 'contact_angle': 0.0}, |
| 110 | + cond_to_rpm=rpm_map, |
| 111 | + fs=64000, |
| 112 | + name='PU-6203', |
| 113 | + ) |
| 114 | + |
| 115 | + train_data = Dataset.from_arrays( |
| 116 | + X_train, y_train, cond_train, fs=64000, physics=pu_physics, name='PU_Train' |
| 117 | + ) |
| 118 | + calib_data = Dataset.from_arrays( |
| 119 | + X_calib, y_calib, cond_calib, fs=64000, physics=pu_physics, name='PU_Calib' |
| 120 | + ) |
| 121 | + test_data = Dataset.from_arrays( |
| 122 | + X_test, y_test, cond_test, fs=64000, physics=pu_physics, name='PU_Test' |
| 123 | + ) |
| 124 | + |
| 125 | + model = CNSD() |
| 126 | + |
| 127 | + print('\n[1] Training Neural Network on Authentic PU Data...') |
| 128 | + model.fit(train_data, epochs=20) # slightly fewer epochs to speed up for large dataset |
| 129 | + |
| 130 | + print('\n[2] Calibrating Tau threshold...') |
| 131 | + taus = np.arange(1.0, 4.1, 0.5) |
| 132 | + best_gap = -np.inf |
| 133 | + best_tau = 1.0 |
| 134 | + |
| 135 | + for tau in taus: |
| 136 | + model.symbolic.tau = float(tau) |
| 137 | + report = model.diagnose(calib_data) |
| 138 | + |
| 139 | + hb = headline_accuracy_by_verdict(report, y_calib) |
| 140 | + if 'CONFIRMED' in hb and 'CONFLICT' in hb: |
| 141 | + gap = hb['CONFIRMED']['cnn_accuracy'] - hb['CONFLICT']['cnn_accuracy'] |
| 142 | + else: |
| 143 | + gap = 0.0 |
| 144 | + |
| 145 | + conf_acc = hb.get('CONFIRMED', {}).get('cnn_accuracy', 0.0) |
| 146 | + print(f'Calib tau={tau:.1f} | Conf={conf_acc:.3f} | Gap={gap:+.3f}') |
| 147 | + if gap > best_gap: |
| 148 | + best_gap = gap |
| 149 | + best_tau = float(tau) |
| 150 | + |
| 151 | + print(f'\n=> Selected optimal tau: {best_tau}') |
| 152 | + model.symbolic.tau = best_tau |
| 153 | + |
| 154 | + print('\n[3] Evaluating on Test Set...') |
| 155 | + report = model.diagnose(test_data) |
| 156 | + |
| 157 | + hb = headline_accuracy_by_verdict(report, y_test) |
| 158 | + print('\n--- FINAL TEST RESULTS (PU DATASET) ---') |
| 159 | + if 'CONFIRMED' in hb: |
| 160 | + print( |
| 161 | + f'Physics-Confirmed Acc: {hb["CONFIRMED"]["cnn_accuracy"]:.3f} (n={hb["CONFIRMED"]["n"]})' |
| 162 | + ) |
| 163 | + if 'CONFLICT' in hb: |
| 164 | + print( |
| 165 | + f'Physics-Conflict Acc: {hb["CONFLICT"]["cnn_accuracy"]:.3f} (n={hb["CONFLICT"]["n"]})' |
| 166 | + ) |
| 167 | + if 'INCONCLUSIVE' in hb: |
| 168 | + print( |
| 169 | + f'Physics-Inconclusive Acc:{hb["INCONCLUSIVE"]["cnn_accuracy"]:.3f} (n={hb["INCONCLUSIVE"]["n"]})' |
| 170 | + ) |
| 171 | + |
| 172 | + if 'CONFIRMED' in hb and 'CONFLICT' in hb: |
| 173 | + gap = hb['CONFIRMED']['cnn_accuracy'] - hb['CONFLICT']['cnn_accuracy'] |
| 174 | + print(f'GAP: {gap:+.3f}') |
| 175 | + print('---------------------------------------') |
0 commit comments