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