1+ import argparse
12import os
23import sys
3- import argparse
44import traceback
55
66import numpy as np
1313 from cnsd import Dataset
1414 from cnsd .diagnosis .system import CNSD
1515 from cnsd .perception .cnn import _train_cnn
16-
16+
1717 # Argparse
1818 parser = argparse .ArgumentParser ()
1919 parser .add_argument ('--dataset' , type = str , required = True , choices = ['cwru' , 'pu' , 'xjtusy' ])
2424 if args .dataset == 'pu' :
2525 from cnsd .physics import PhysicsConfig
2626 from validate_pu import load_pu_domain_split
27- (X_train_full , y_train_full , cond_train_full ), (X_target , y_target , cond_target ) = load_pu_domain_split ()
27+
28+ (X_train_full , y_train_full , cond_train_full ), (X_target , y_target , cond_target ) = (
29+ load_pu_domain_split ()
30+ )
2831 unique_rpm = set (cond_train_full ).union (set (cond_target ))
2932 rpm_map = {float (r ): float (r ) for r in unique_rpm }
3033 physics = PhysicsConfig (
3538 )
3639 taxonomy = {0 : ('Normal' , 'None' ), 1 : ('Outer Race' , 'Medium' ), 2 : ('Inner Race' , 'High' )}
3740 fs = 64000
38-
41+
3942 elif args .dataset == 'cwru' :
40- from validate_run import load_cwru , CWRU , TAXONOMY
43+ from validate_run import CWRU , TAXONOMY , load_cwru
44+
4145 X , y , cond = load_cwru ()
4246 X = np .asarray (X , np .float32 )
4347 y = np .asarray (y )
4448 cond = np .asarray (cond )
45-
49+
4650 train_mask = cond < 3
4751 target_mask = cond == 3
48-
52+
4953 X_train_full = X [train_mask ]
5054 y_train_full = y [train_mask ]
5155 cond_train_full = cond [train_mask ]
52-
56+
5357 X_target = X [target_mask ]
5458 y_target = y [target_mask ]
5559 cond_target = cond [target_mask ]
56-
60+
5761 physics = CWRU
5862 taxonomy = TAXONOMY
5963 fs = 12000
60-
64+
6165 elif args .dataset == 'xjtusy' :
6266 from cnsd .datasets .xjtusy import load_xjtusy_domain_split
63- from cnsd . physics . configs import XJTUSY_PHYSICS
67+
6468 train_ds , target_ds = load_xjtusy_domain_split (window_size = 4096 )
65-
69+
6670 X_train_full = train_ds .X
6771 y_train_full = train_ds .y
6872 cond_train_full = train_ds .cond
69-
73+
7074 X_target = target_ds .X
7175 y_target = target_ds .y
7276 cond_target = target_ds .cond
73-
77+
7478 physics = target_ds .physics
7579 taxonomy = target_ds .taxonomy
7680 fs = target_ds .fs
@@ -112,8 +116,13 @@ def clone_for_mc(layer):
112116
113117 # Test data processing
114118 test_ds = Dataset .from_arrays (
115- X_target , y_target , cond_target ,
116- fs = fs , physics = physics , taxonomy = taxonomy , name = f'{ args .dataset .upper ()} _Test'
119+ X_target ,
120+ y_target ,
121+ cond_target ,
122+ fs = fs ,
123+ physics = physics ,
124+ taxonomy = taxonomy ,
125+ name = f'{ args .dataset .upper ()} _Test' ,
117126 )
118127 sig_te = np .stack ([test_ds .X [i ].reshape (- 1 ) for i in range (len (test_ds .X ))]).astype (np .float32 )
119128 yte = test_ds .y
@@ -134,14 +143,34 @@ def clone_for_mc(layer):
134143 train_idx = indices [:split_idx ]
135144 calib_idx = indices [split_idx :]
136145
137- X_tr , y_tr , cond_tr = X_train_full [train_idx ], y_train_full [train_idx ], cond_train_full [train_idx ]
138- X_ca , y_ca , cond_ca = X_train_full [calib_idx ], y_train_full [calib_idx ], cond_train_full [calib_idx ]
146+ X_tr , y_tr , cond_tr = (
147+ X_train_full [train_idx ],
148+ y_train_full [train_idx ],
149+ cond_train_full [train_idx ],
150+ )
151+ X_ca , y_ca , cond_ca = (
152+ X_train_full [calib_idx ],
153+ y_train_full [calib_idx ],
154+ cond_train_full [calib_idx ],
155+ )
139156
140157 train_ds = Dataset .from_arrays (
141- X_tr , y_tr , cond_tr , fs = fs , physics = physics , taxonomy = taxonomy , name = f'{ args .dataset .upper ()} _Train'
158+ X_tr ,
159+ y_tr ,
160+ cond_tr ,
161+ fs = fs ,
162+ physics = physics ,
163+ taxonomy = taxonomy ,
164+ name = f'{ args .dataset .upper ()} _Train' ,
142165 )
143166 calib_ds = Dataset .from_arrays (
144- X_ca , y_ca , cond_ca , fs = fs , physics = physics , taxonomy = taxonomy , name = f'{ args .dataset .upper ()} _Calib'
167+ X_ca ,
168+ y_ca ,
169+ cond_ca ,
170+ fs = fs ,
171+ physics = physics ,
172+ taxonomy = taxonomy ,
173+ name = f'{ args .dataset .upper ()} _Calib' ,
145174 )
146175
147176 # 2. Train Primary Model (Bypass SCM to prevent multiprocess deadlocks)
@@ -161,7 +190,9 @@ def clone_for_mc(layer):
161190 ens .append (m_cnn )
162191
163192 # 4. Calibrate Tau
164- sig_ca = np .stack ([calib_ds .X [i ].reshape (- 1 ) for i in range (len (calib_ds .X ))]).astype (np .float32 )
193+ sig_ca = np .stack ([calib_ds .X [i ].reshape (- 1 ) for i in range (len (calib_ds .X ))]).astype (
194+ np .float32
195+ )
165196 Xin_ca = sig_ca [..., None ]
166197 probs_ca = model .cnn .predict (Xin_ca , batch_size = 128 , verbose = 0 )
167198 pred_ca = probs_ca .argmax (1 )
@@ -172,7 +203,10 @@ def clone_for_mc(layer):
172203 for tau in [1.0 , 1.5 , 2.0 , 2.5 , 3.0 ]:
173204 model .symbolic .tau = float (tau )
174205 verds = np .array (
175- [model .symbolic .diagnose (sig_ca [i ], pred_ca [i ], calib_ds .cond [i ])['verdict' ] for i in range (len (sig_ca ))]
206+ [
207+ model .symbolic .diagnose (sig_ca [i ], pred_ca [i ], calib_ds .cond [i ])['verdict' ]
208+ for i in range (len (sig_ca ))
209+ ]
176210 )
177211 conf = verds == 'CONFIRMED'
178212 cnfl = verds == 'CONFLICT'
@@ -193,7 +227,10 @@ def clone_for_mc(layer):
193227
194228 # Physics evaluation
195229 verds = np .array (
196- [model .symbolic .diagnose (sig_te [i ], pred_te [i ], cond_te [i ])['verdict' ] for i in range (len (sig_te ))]
230+ [
231+ model .symbolic .diagnose (sig_te [i ], pred_te [i ], cond_te [i ])['verdict' ]
232+ for i in range (len (sig_te ))
233+ ]
197234 )
198235 conf = verds == 'CONFIRMED'
199236 cnfl = verds == 'CONFLICT'
@@ -235,7 +272,9 @@ def clone_for_mc(layer):
235272 ens_gap = get_matched_coverage_gap (ens_score , ens_correct , target_n )
236273 results ['ens_gap' ].append (ens_gap )
237274
238- print (f'Matched Coverage GAPs -> Softmax:{ soft_gap :+.3f} | MC-Drop:{ mc_gap :+.3f} | Ens:{ ens_gap :+.3f} ' )
275+ print (
276+ f'Matched Coverage GAPs -> Softmax:{ soft_gap :+.3f} | MC-Drop:{ mc_gap :+.3f} | Ens:{ ens_gap :+.3f} '
277+ )
239278
240279 # 6. Noise Test
241280 rng = np .random .RandomState (seed )
@@ -248,13 +287,18 @@ def clone_for_mc(layer):
248287 sig_n = sig_te + rng .randn (* sig_te .shape ).astype (np .float32 ) * np .sqrt (npow )
249288
250289 Xin_n = sig_n [..., None ]
251- ep_class = np .stack ([m .predict (Xin_n , batch_size = 128 , verbose = 0 ).argmax (1 ) for m in ens ])
290+ ep_class = np .stack (
291+ [m .predict (Xin_n , batch_size = 128 , verbose = 0 ).argmax (1 ) for m in ens ]
292+ )
252293 v = stats .mode (ep_class , axis = 0 , keepdims = False ).mode
253294 unan = (ep_class == v ).all (0 )
254295 ok = v == yte
255296
256297 pv = np .array (
257- [model .symbolic .diagnose (sig_n [i ], v [i ], cond_te [i ])['verdict' ] for i in range (len (sig_n ))]
298+ [
299+ model .symbolic .diagnose (sig_n [i ], v [i ], cond_te [i ])['verdict' ]
300+ for i in range (len (sig_n ))
301+ ]
258302 )
259303 pc = pv == 'CONFLICT'
260304 uw = unan & (~ ok )
@@ -264,10 +308,16 @@ def clone_for_mc(layer):
264308 print (f' Noise={ s :>5} | catch_rate={ catch :.3f} ' )
265309
266310 print ('\n ' + '=' * 60 + f'\n FINAL AGGREGATED RESULTS ({ len (seeds )} Seeds)\n ' + '=' * 60 )
267- print (f'Physics GAP: { np .nanmean (results ["phys_gap" ]):+.3f} ± { np .nanstd (results ["phys_gap" ]):.3f} ' )
268- print (f'Softmax GAP: { np .nanmean (results ["soft_gap" ]):+.3f} ± { np .nanstd (results ["soft_gap" ]):.3f} ' )
311+ print (
312+ f'Physics GAP: { np .nanmean (results ["phys_gap" ]):+.3f} ± { np .nanstd (results ["phys_gap" ]):.3f} '
313+ )
314+ print (
315+ f'Softmax GAP: { np .nanmean (results ["soft_gap" ]):+.3f} ± { np .nanstd (results ["soft_gap" ]):.3f} '
316+ )
269317 print (f'MC-Drop GAP: { np .nanmean (results ["mc_gap" ]):+.3f} ± { np .nanstd (results ["mc_gap" ]):.3f} ' )
270- print (f'Ensemble GAP: { np .nanmean (results ["ens_gap" ]):+.3f} ± { np .nanstd (results ["ens_gap" ]):.3f} ' )
318+ print (
319+ f'Ensemble GAP: { np .nanmean (results ["ens_gap" ]):+.3f} ± { np .nanstd (results ["ens_gap" ]):.3f} '
320+ )
271321
272322 t_stat , p_val = stats .ttest_rel (results ['phys_gap' ], results ['ens_gap' ])
273323 avg_ncov = np .mean (results ['ncov' ])
0 commit comments