1919import sys
2020import numpy as np
2121
22- from cnsd import CNSD , Dataset
22+ from cnsd import Dataset
23+ from cnsd .diagnosis .system import CNSD
2324from cnsd .physics import PhysicsConfig
2425from cnsd .causal import signal_kurtosis
2526from cnsd .counterfactual import dowhy_gcm_available , build_scm , counterfactual_for_unit
2627
2728
2829# ── the ONLY dataset-specific code: return raw arrays from your CWRU files ────
2930def load_cwru ():
30- """Return (X, y, cond) for CWRU. Replace the body with your loader.
31-
32- X : (n, 1024) float non-overlapping test windows, per-window normalized
33- y : (n,) int fault class 0..9 (0 = Normal)
34- cond : (n,) int motor load 0..3
35- """
36- raise NotImplementedError (
37- "Wire your CWRU loader here: return X (n,1024), y (n,), cond (n,). "
38- "Use Protocol B (train loads 0-2, test load 3) for the real result." )
31+ import os
32+ from scipy .io import loadmat
33+ base_dir = r"E:\301\CWRU-dataset"
34+ if not os .path .exists (base_dir ):
35+ raise FileNotFoundError (f"CWRU dataset not found at { base_dir } " )
36+
37+ X , y , cond = [], [], []
38+
39+ def read_mat (path , label , load ):
40+ if not os .path .exists (path ): return
41+ try :
42+ mat = loadmat (path )
43+ key = [k for k in mat .keys () if 'DE_time' in k ]
44+ if not key : return
45+ time_series = mat [key [0 ]][:, 0 ]
46+
47+ length = 1024
48+ idx_last = - (time_series .shape [0 ] % length )
49+ if idx_last == 0 :
50+ clips = time_series .reshape (- 1 , length )
51+ else :
52+ clips = time_series [:idx_last ].reshape (- 1 , length )
53+
54+ for clip in clips :
55+ X .append (clip )
56+ y .append (label )
57+ cond .append (load )
58+ except Exception as e :
59+ print (f"Error reading { path } : { e } " )
60+
61+ normal_dir = os .path .join (base_dir , "Normal" )
62+ for f in os .listdir (normal_dir ):
63+ if f .endswith ('.mat' ):
64+ load = int (f .split ('_' )[- 1 ].split ('.' )[0 ])
65+ read_mat (os .path .join (normal_dir , f ), 0 , load )
66+
67+ fault_dir = os .path .join (base_dir , "12k_Drive_End_Bearing_Fault_Data" )
68+ fault_map = {
69+ 'B' : { '007' : 1 , '014' : 2 , '021' : 3 },
70+ 'IR' : { '007' : 4 , '014' : 5 , '021' : 6 },
71+ 'OR' : { '007' : 7 , '014' : 8 , '021' : 9 }
72+ }
73+ for ftype , size_map in fault_map .items ():
74+ for size , label in size_map .items ():
75+ dir_path = os .path .join (fault_dir , ftype , size )
76+ if not os .path .exists (dir_path ): continue
77+ for f in os .listdir (dir_path ):
78+ if f .endswith ('.mat' ):
79+ load = int (f .split ('_' )[- 1 ].split ('.' )[0 ])
80+ read_mat (os .path .join (dir_path , f ), label , load )
81+
82+ return np .array (X , dtype = np .float32 ), np .array (y ), np .array (cond )
3983
4084
4185# CWRU 6205 physics + taxonomy (this is config, not hardcoded engine logic)
@@ -73,14 +117,21 @@ def main():
73117 # 1. data
74118 X , y , cond = load_cwru ()
75119 X = np .asarray (X , np .float32 ); y = np .asarray (y ); cond = np .asarray (cond )
76- data = Dataset .from_arrays (X , y , cond , fs = 12000 , physics = CWRU ,
77- taxonomy = TAXONOMY , name = 'CWRU' )
78- print (f'[data] { data .summary ()} ' )
120+
121+ train_mask = cond < 3
122+ test_mask = cond == 3
123+
124+ train_data = Dataset .from_arrays (X [train_mask ], y [train_mask ], cond [train_mask ], fs = 12000 , physics = CWRU , taxonomy = TAXONOMY , name = 'CWRU_Train' )
125+ test_data = Dataset .from_arrays (X [test_mask ], y [test_mask ], cond [test_mask ], fs = 12000 , physics = CWRU , taxonomy = TAXONOMY , name = 'CWRU_Test' )
126+ full_data = Dataset .from_arrays (X , y , cond , fs = 12000 , physics = CWRU , taxonomy = TAXONOMY , name = 'CWRU_Full' )
127+
128+ print (f'[train_data] { train_data .summary ()} ' )
129+ print (f'[test_data] { test_data .summary ()} ' )
79130
80131 # 2. fit + 3. diagnose (Layers 1,2,4 live)
81132 model = CNSD ()
82- model .fit (data , epochs = 30 )
83- report = model .diagnose (data )
133+ model .fit (train_data , epochs = 30 )
134+ report = model .diagnose (test_data )
84135 print (f'\n [pipeline] { report .summary ()} ' )
85136
86137 # 3. Layer-2 verification rate
@@ -90,7 +141,7 @@ def main():
90141 print (f' { k :13} : { v :.1%} ' )
91142
92143 # 4. HEADLINE: CNN accuracy split by verdict
93- hb = headline_accuracy_by_verdict (report , y )
144+ hb = headline_accuracy_by_verdict (report , test_data . y )
94145 print ('\n [HEADLINE] CNN accuracy by physics verdict:' )
95146 for v , d in hb .items ():
96147 print (f' { v :13} : acc={ d ["cnn_accuracy" ]:.3f} (n={ d ["n" ]} )' )
@@ -100,18 +151,18 @@ def main():
100151 f'({ "physics is a real reliability signal" if gap > 0 else "investigate" } )' )
101152
102153 # 5. Layer-3 causal do(Z)
103- eff = model .condition_effect (data )
154+ eff = model .condition_effect (full_data )
104155 print (f'\n [Layer 3] do(Z) operating-condition effect: rung={ eff ["rung" ]} '
105156 f'max_contrast={ eff ["max_contrast" ]:.4f} p={ eff ["p_value" ]:.4f} ' )
106157
107158 # 6. Layer-3B: REAL Rung-3 counterfactual (must actually execute, not fallback)
108159 print (f'\n [Layer 3B] DoWhy available: { dowhy_gcm_available ()} ' )
109160 if dowhy_gcm_available ():
110- feat = signal_kurtosis (data .X )
111- scm = build_scm (data .cond , feat , data .y )
161+ feat = signal_kurtosis (full_data .X )
162+ scm = build_scm (full_data .cond , feat , full_data .y )
112163 if scm is not None :
113- row = {'Z' : float (data .cond [0 ]), 'X' : float (feat [0 ]), 'Y' : float (data .y [0 ] > 0 )}
114- cf_cond = int (min (np .unique (data .cond )))
164+ row = {'Z' : float (full_data .cond [0 ]), 'X' : float (feat [0 ]), 'Y' : float (full_data .y [0 ] > 0 )}
165+ cf_cond = int (min (np .unique (full_data .cond )))
115166 cf = counterfactual_for_unit (scm , row , cf_cond )
116167 print (f' counterfactual executed: { cf ["method" ]} ' )
117168 print (f' factual Y={ cf ["factual" ]["Y" ]:.2f} -> '
0 commit comments