|
1 | | -import os |
2 | 1 | import glob |
| 2 | +import os |
| 3 | + |
3 | 4 | import numpy as np |
4 | 5 | import pandas as pd |
| 6 | + |
5 | 7 | from cnsd.datasets.contract import Dataset |
6 | 8 | from cnsd.physics.configs import XJTUSY_PHYSICS |
7 | 9 |
|
8 | 10 | # Known fault mappings per XJTU-SY paper |
9 | 11 | # We map Bearing ID to CNSD fault class: 1=Outer, 2=Inner, 3=Cage |
10 | 12 | XJTUSY_FAULTS = { |
11 | 13 | '35Hz12kN': { |
12 | | - 'Bearing1_1': 1, # Outer |
13 | | - 'Bearing1_2': 1, # Outer |
14 | | - 'Bearing1_3': 1, # Outer |
| 14 | + 'Bearing1_1': 1, # Outer |
| 15 | + 'Bearing1_2': 1, # Outer |
| 16 | + 'Bearing1_3': 1, # Outer |
15 | 17 | }, |
16 | 18 | '37.5Hz11kN': { |
17 | | - 'Bearing2_1': 2, # Inner |
18 | | - 'Bearing2_2': 1, # Outer |
19 | | - 'Bearing2_4': 1, # Outer |
20 | | - 'Bearing2_5': 1, # Outer |
| 19 | + 'Bearing2_1': 2, # Inner |
| 20 | + 'Bearing2_2': 1, # Outer |
| 21 | + 'Bearing2_4': 1, # Outer |
| 22 | + 'Bearing2_5': 1, # Outer |
21 | 23 | }, |
22 | 24 | '40Hz10kN': { |
23 | | - 'Bearing3_1': 1, # Outer |
24 | | - 'Bearing3_2': 2, # Inner |
25 | | - 'Bearing3_3': 2, # Inner |
26 | | - 'Bearing3_4': 2, # Inner |
27 | | - 'Bearing3_5': 1, # Outer |
28 | | - } |
| 25 | + 'Bearing3_1': 1, # Outer |
| 26 | + 'Bearing3_2': 2, # Inner |
| 27 | + 'Bearing3_3': 2, # Inner |
| 28 | + 'Bearing3_4': 2, # Inner |
| 29 | + 'Bearing3_5': 1, # Outer |
| 30 | + }, |
29 | 31 | } |
30 | 32 |
|
31 | | -def load_xjtusy_domain_split(data_dir=r'E:\301\CNSD\data\XJTU-SY\XJTU-SY_Bearing_Datasets', window_size=32768, train_cond='35Hz12kN', test_cond='37.5Hz11kN'): |
| 33 | + |
| 34 | +def load_xjtusy_domain_split( |
| 35 | + data_dir=r'E:\301\CNSD\data\XJTU-SY\XJTU-SY_Bearing_Datasets', |
| 36 | + window_size=32768, |
| 37 | + train_cond='35Hz12kN', |
| 38 | + test_cond='37.5Hz11kN', |
| 39 | +): |
32 | 40 | """ |
33 | 41 | Loads authentic XJTU-SY dataset and strictly splits by condition (Domain Shift). |
34 | | - Uses the run-to-failure nature to grab the first 15% as Healthy (0) and the |
| 42 | + Uses the run-to-failure nature to grab the first 15% as Healthy (0) and the |
35 | 43 | last 15% as the Fault label. |
36 | 44 | """ |
| 45 | + |
37 | 46 | def _load_condition(cond_folder, rpm_val): |
38 | 47 | X, y, cond = [], [], [] |
39 | 48 | cond_path = os.path.join(data_dir, cond_folder) |
40 | | - |
| 49 | + |
41 | 50 | if not os.path.exists(cond_path): |
42 | 51 | return [], [], [] |
43 | 52 |
|
44 | 53 | for bearing_folder in os.listdir(cond_path): |
45 | 54 | bearing_path = os.path.join(cond_path, bearing_folder) |
46 | 55 | if not os.path.isdir(bearing_path): |
47 | 56 | continue |
48 | | - |
| 57 | + |
49 | 58 | fault_label = XJTUSY_FAULTS.get(cond_folder, {}).get(bearing_folder, None) |
50 | | - if fault_label is None or bearing_folder == 'Bearing1_5': # skip 1_5 to avoid mixed labels |
| 59 | + if ( |
| 60 | + fault_label is None or bearing_folder == 'Bearing1_5' |
| 61 | + ): # skip 1_5 to avoid mixed labels |
51 | 62 | continue |
52 | | - |
53 | | - csv_files = sorted(glob.glob(os.path.join(bearing_path, '*.csv')), |
54 | | - key=lambda x: int(os.path.splitext(os.path.basename(x))[0])) |
55 | | - |
| 63 | + |
| 64 | + csv_files = sorted( |
| 65 | + glob.glob(os.path.join(bearing_path, '*.csv')), |
| 66 | + key=lambda x: int(os.path.splitext(os.path.basename(x))[0]), |
| 67 | + ) |
| 68 | + |
56 | 69 | total_files = len(csv_files) |
57 | 70 | if total_files < 10: |
58 | | - continue # ignore wildly corrupted directories |
59 | | - |
| 71 | + continue # ignore wildly corrupted directories |
| 72 | + |
60 | 73 | healthy_count = max(1, int(total_files * 0.20)) |
61 | 74 | fault_count = max(1, int(total_files * 0.20)) |
62 | | - |
| 75 | + |
63 | 76 | # Sliding window parameters |
64 | 77 | step_size = 1024 |
65 | | - |
| 78 | + |
66 | 79 | # Extract Healthy |
67 | 80 | for fpath in csv_files[:healthy_count]: |
68 | 81 | df = pd.read_csv(fpath) |
69 | | - sig = df.iloc[:, 0].values # Horizontal acceleration |
| 82 | + sig = df.iloc[:, 0].values # Horizontal acceleration |
70 | 83 | sig = (sig - np.mean(sig)) / (np.std(sig) + 1e-8) |
71 | | - |
| 84 | + |
72 | 85 | # Slicing the 32768 array into overlapping 4096 windows |
73 | 86 | for start_idx in range(0, len(sig) - window_size + 1, step_size): |
74 | 87 | X.append(sig[start_idx : start_idx + window_size]) |
75 | 88 | y.append(0) |
76 | 89 | cond.append(rpm_val) |
77 | | - |
| 90 | + |
78 | 91 | # Extract Fault |
79 | 92 | for fpath in csv_files[-fault_count:]: |
80 | 93 | df = pd.read_csv(fpath) |
81 | 94 | sig = df.iloc[:, 0].values |
82 | 95 | sig = (sig - np.mean(sig)) / (np.std(sig) + 1e-8) |
83 | | - |
| 96 | + |
84 | 97 | for start_idx in range(0, len(sig) - window_size + 1, step_size): |
85 | 98 | X.append(sig[start_idx : start_idx + window_size]) |
86 | 99 | y.append(fault_label) |
87 | 100 | cond.append(rpm_val) |
88 | | - |
| 101 | + |
89 | 102 | return X, y, cond |
90 | 103 |
|
91 | 104 | X_train, y_train, c_train = _load_condition(train_cond, 2100.0) |
92 | 105 | X_test, y_test, c_test = _load_condition(test_cond, 2250.0) |
93 | | - |
| 106 | + |
94 | 107 | if not X_train or not X_test: |
95 | | - raise FileNotFoundError(f"Could not load data. Ensure {data_dir} contains extracted {train_cond} and {test_cond} folders.") |
| 108 | + raise FileNotFoundError( |
| 109 | + f'Could not load data. Ensure {data_dir} contains extracted {train_cond} and {test_cond} folders.' |
| 110 | + ) |
96 | 111 |
|
97 | 112 | ds_train = Dataset.from_arrays( |
98 | | - X=np.array(X_train, dtype=np.float32), |
99 | | - y=np.array(y_train, dtype=np.int32), |
100 | | - cond=np.array(c_train, dtype=np.float32), |
101 | | - fs=25600, |
102 | | - physics=XJTUSY_PHYSICS, |
| 113 | + X=np.array(X_train, dtype=np.float32), |
| 114 | + y=np.array(y_train, dtype=np.int32), |
| 115 | + cond=np.array(c_train, dtype=np.float32), |
| 116 | + fs=25600, |
| 117 | + physics=XJTUSY_PHYSICS, |
103 | 118 | taxonomy={0: ('Normal', 'None'), 1: ('Outer Race', 'Medium'), 2: ('Inner Race', 'High')}, |
104 | | - name='XJTUSY_Train' |
| 119 | + name='XJTUSY_Train', |
105 | 120 | ) |
106 | | - |
| 121 | + |
107 | 122 | ds_test = Dataset.from_arrays( |
108 | | - X=np.array(X_test, dtype=np.float32), |
109 | | - y=np.array(y_test, dtype=np.int32), |
110 | | - cond=np.array(c_test, dtype=np.float32), |
111 | | - fs=25600, |
112 | | - physics=XJTUSY_PHYSICS, |
| 123 | + X=np.array(X_test, dtype=np.float32), |
| 124 | + y=np.array(y_test, dtype=np.int32), |
| 125 | + cond=np.array(c_test, dtype=np.float32), |
| 126 | + fs=25600, |
| 127 | + physics=XJTUSY_PHYSICS, |
113 | 128 | taxonomy={0: ('Normal', 'None'), 1: ('Outer Race', 'Medium'), 2: ('Inner Race', 'High')}, |
114 | | - name='XJTUSY_Test' |
| 129 | + name='XJTUSY_Test', |
115 | 130 | ) |
116 | | - |
| 131 | + |
117 | 132 | return ds_train, ds_test |
0 commit comments