Skip to content

Commit 87fe6a5

Browse files
committed
Address final CWRU fixes for Issue #8
1 parent 0b95682 commit 87fe6a5

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def add_awgn(signals, snr_db):
2323

2424
def main():
2525
print('=' * 80)
26-
print('CNSD CROSS-DOMAIN VALIDATION (AWGN NOISE INJECTION)')
26+
print('CNSD CROSS-CONDITION ROBUSTNESS (AWGN NOISE INJECTION)')
2727
print('=' * 80)
2828

2929
# 1. Load baseline data
@@ -47,7 +47,7 @@ def main():
4747
model.fit(train_data, epochs=30)
4848
print('Training complete.\n')
4949

50-
# 3. Evaluate on noisy cross-domains
50+
# 3. Evaluate on noisy cross-conditions
5151
snr_levels = [None, 0, -5, -10]
5252

5353
for snr in snr_levels:

threshold_sweep.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
threshold_sweep.py - sweep the envelope-prominence threshold on the CWRU dataset.
3+
"""
4+
import numpy as np
5+
from cnsd import Dataset
6+
from cnsd.diagnosis.system import CNSD
7+
8+
# Reuse CWRU loader and configs from validate_run
9+
from validate_run import load_cwru, CWRU, TAXONOMY
10+
11+
def main():
12+
print('=' * 50)
13+
print('CWRU THRESHOLD SWEEP')
14+
print('=' * 50)
15+
16+
X, y, cond = load_cwru()
17+
X = np.asarray(X, np.float32); y = np.asarray(y); cond = np.asarray(cond)
18+
19+
test_mask = cond == 3
20+
train_mask = cond < 3
21+
22+
train_data = Dataset.from_arrays(X[train_mask], y[train_mask], cond[train_mask], fs=12000, physics=CWRU, taxonomy=TAXONOMY, name='CWRU_Train')
23+
test_data = Dataset.from_arrays(X[test_mask], y[test_mask], cond[test_mask], fs=12000, physics=CWRU, taxonomy=TAXONOMY, name='CWRU_Test')
24+
25+
print('Training baseline CNSD model (this happens once)...')
26+
model = CNSD()
27+
model.fit(train_data, epochs=30)
28+
29+
print('\nStarting Threshold Sweep on Test Data:')
30+
thresholds = [3.0, 2.5, 2.0, 1.5]
31+
32+
for t in thresholds:
33+
print('-' * 40)
34+
print(f'Testing Threshold: {t}')
35+
model.symbolic.tau = t # Hot-swap the threshold!
36+
report = model.diagnose(test_data)
37+
vr = report.verification_rate()
38+
print(f" CONFIRMED : {vr.get('CONFIRMED', 0.0):.1%}")
39+
print(f" CONFLICT : {vr.get('CONFLICT', 0.0):.1%}")
40+
print(f" INCONCLUSIVE: {vr.get('INCONCLUSIVE', 0.0):.1%}")
41+
42+
if __name__ == '__main__':
43+
main()

validate_run.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ def read_mat(path, label, load):
7474
for size, label in size_map.items():
7575
dir_path = os.path.join(fault_dir, ftype, size)
7676
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)
77+
for root, dirs, files in os.walk(dir_path):
78+
for f in files:
79+
if f.endswith('.mat'):
80+
load = int(f.split('_')[-1].split('.')[0])
81+
read_mat(os.path.join(root, f), label, load)
8182

8283
return np.array(X, dtype=np.float32), np.array(y), np.array(cond)
8384

@@ -174,9 +175,19 @@ def main():
174175
'Run: pip install dowhy')
175176

176177
# 7. example auditable diagnoses
177-
print('\n[examples] auditable root-cause diagnoses:')
178-
for r in report.records[:5]:
179-
print(f' [{r["status"]}] {r["root_cause"]["statement"]}')
178+
print('\n[examples] auditable root-cause diagnoses (CONFIRMED faults only):')
179+
seen_classes = set()
180+
examples = []
181+
for idx, r in enumerate(report.records):
182+
y_true = test_data.y[idx]
183+
if r["physics_verdict"] == "CONFIRMED" and y_true > 0:
184+
if y_true not in seen_classes:
185+
examples.append(f' [Class {y_true}] [{r["status"]}] {r["root_cause"]["statement"]}')
186+
seen_classes.add(y_true)
187+
if len(examples) >= 5:
188+
break
189+
for ex in examples:
190+
print(ex)
180191

181192
print('\n' + '=' * 68)
182193
print('VALIDATION COMPLETE - if all sections printed, the pipeline runs '

0 commit comments

Comments
 (0)