This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
dataset.py
229 lines (196 loc) · 10.8 KB
/
dataset.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import pandas as pd
import numpy as np
from keras.utils import to_categorical
# from keras.utils import to_categorical
XJTU_DATA_PATH = r"E:\IDM_download\XJTU-SY_Bearing_Datasets"
CONDITION_DATA_FOLDER_LIST = ["35Hz12kN", "37.5Hz11kN", "40Hz10kN"]
CONDITION_LABEL = [[35, 12], [37.5, 11], [40, 10]]
CONDITION_MINUTES_NUMBER = [[123, 161, 158, 122, 52], [491, 161, 533, 42, 339], [2538, 2496, 371, 1515, 114]]
# INNER_RACE -> 1, OUTER_RACE -> 2, CAGE -> 3, BALL -> 4
FAULT_LABEL = [
[2, 2, 2, 3, [1, 2]],
[1, 2, 3, 2, 2],
[2, [1, 2, 3, 4], 1, 1, 2]
]
MACHINING_PARAMETER_LABEL = [[35, 12], [37.5, 11], [40, 10]]
# 2^14 -> 32768
TIMESTEP = 32768
class dataSet(object):
secondSamplingRatio = 16
# TRAIN - > 1, TEST -> 0
trainTestDist = [
[1, 0, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 0, 0]
]
# indicates alert 30 minutes ago
alertTime = 30
maxSampleTime = 300
# cache configuration
CACHE_DIR = "./cache"
TRAIN_DATA_CACHE_NAME = "TRAIN_DATA"
TEST_DATA_CACHE_NAME = "TEST_DATA"
TRAIN_LABELS_CACHE_NAME = "TRAIN_LABELS"
TEST_LABELS_CACHE_NAME = "TEST_LABELS"
TRAIN_RUL_CACHE_NAME = "TRAIN_RUL"
TEST_RUL_CACHE_NAME = "TEST_RUL"
TRAIN_CONDITION_NAME = "TRAIN_CONDITION"
TEST_CONDITION_NAME = "TEST_CONDITION"
def __init__(self, secondSamplingRatio: int = 16, alertTime: int = 30):
self.secondSamplingRatio = secondSamplingRatio
self.alertTime = alertTime
def get_condition_folder_path(self, conditionLabel: str):
return os.path.join(XJTU_DATA_PATH, conditionLabel)
def read_signal_file_to_array_by_path(self, path):
pd_data = pd.read_csv(path)
# print(pd_data)
horizontal_signal = pd_data["Horizontal_vibration_signals"][::self.secondSamplingRatio].reshape(-1, 1)
vertical_signal = pd_data["Horizontal_vibration_signals"][::self.secondSamplingRatio].reshape(-1, 1)
np_data = np.concatenate((horizontal_signal, vertical_signal), axis=1)
return np_data
def read_reinforce_signal_file_to_array_by_path(self, path, ratio=8):
pd_data = pd.read_csv(path)
# print(pd_data)
sample_base_index = np.arange(0,TIMESTEP,self.secondSamplingRatio)
horizontal_signal = pd_data["Horizontal_vibration_signals"].reshape(-1, 1)
vertical_signal = pd_data["Horizontal_vibration_signals"].reshape(-1, 1)
np_data = np.concatenate((horizontal_signal, vertical_signal), axis=1)
# reinforce data
reinforce_step_range = self.secondSamplingRatio // ratio
reinforce_dataset = []
for step in range(0,reinforce_step_range):
sample_index = sample_base_index + step
reinforce_sample = np_data[sample_index,:]
reinforce_dataset.append(reinforce_sample)
return reinforce_dataset
def get_all_data(self):
train_data = []
test_data = []
train_alert_labels = []
test_alert_labels = []
train_rul_minutes = []
test_rul_minutes = []
train_condition_labels = []
test_condition_labels = []
for conditionIdx, conditionVal in enumerate([1, 2, 3]):
print("# TOUCH condition folder :", CONDITION_DATA_FOLDER_LIST[conditionIdx])
conditionFolderPath = os.path.join(XJTU_DATA_PATH, CONDITION_DATA_FOLDER_LIST[conditionIdx])
for subSampleVal in range(1, 6):
if (conditionVal == 1 and subSampleVal == 5) or (conditionVal == 3 and subSampleVal == 2):
print("Skip example condition",conditionVal,subSampleVal)
continue
print("! TOUCH sample folder ", "Bearing%d_%d" % (conditionVal, subSampleVal))
subSampleIdx = subSampleVal - 1
bearingSampleFolderPath = os.path.join(conditionFolderPath,
"Bearing%d_%d" % (conditionVal, subSampleVal))
isTrain = self.trainTestDist[conditionIdx][subSampleIdx]
maxSampleIdx = CONDITION_MINUTES_NUMBER[conditionIdx][subSampleIdx]
fault_label = FAULT_LABEL[conditionIdx][subSampleIdx]
condition_label = MACHINING_PARAMETER_LABEL[conditionIdx]
if isinstance(fault_label, int):
# one_hot_fault_label = [0 for _ in range(5)]
# one_hot_fault_label[fault_label] = 1
one_hot_fault_label = to_categorical(fault_label, num_classes=4)
elif isinstance(fault_label, list):
raise TypeError("We skip the multi-label sample, it should not be list type")
one_hot_fault_label = [0 for _ in range(4)]
for i in fault_label:
one_hot_fault_label[i] = 1
else:
raise ValueError("Inputed fault label does not fit type, given", type(fault_label),
" required str or list")
print("# MAX SAMPLE IDX :", maxSampleIdx)
startMinute = 1
endMinutes = maxSampleIdx
if maxSampleIdx > self.maxSampleTime:
startMinute = endMinutes - self.maxSampleTime
# add sample to data set
for i in range(startMinute, endMinutes + 1):
print("@ SCANNING CSV ", conditionVal, subSampleVal, "%d.csv" % (i))
sampleCSVPath = os.path.join(bearingSampleFolderPath, "%d.csv" % (i))
np_data = self.read_signal_file_to_array_by_path(sampleCSVPath)
if isTrain:
# we need to reinforce train dataset
reinforce_data = self.read_reinforce_signal_file_to_array_by_path(sampleCSVPath,ratio=4)
if endMinutes - i <= self.alertTime:
# it's not normal
for reinforce_sample in reinforce_data:
train_data.append(reinforce_sample)
train_alert_labels.append(one_hot_fault_label)
train_rul_minutes.append(endMinutes - i)
train_condition_labels.append(condition_label)
else:
train_data.append(np_data)
train_alert_labels.append([1, 0, 0, 0])
train_rul_minutes.append(endMinutes - i)
train_condition_labels.append(condition_label)
# for reinforce_sample in reinforce_data:
# train_data.append(reinforce_sample)
# train_alert_labels.append([1, 0, 0, 0, 0])
# train_rul_minutes.append(endMinutes - i)
# train_condition_labels.append(condition_label)
else:
if endMinutes - i <= self.alertTime:
# it's not normal
test_data.append(np_data)
test_alert_labels.append(one_hot_fault_label)
else:
test_data.append(np_data)
test_alert_labels.append([1, 0, 0, 0])
test_condition_labels.append(condition_label)
test_rul_minutes.append(endMinutes - i)
pass
return np.array(train_data), np.array(train_alert_labels), np.array(train_rul_minutes), np.array(train_condition_labels), \
np.array(test_data), np.array(test_alert_labels), np.array(test_rul_minutes), np.array(test_condition_labels)
def get_all_cache_data(self):
train_data_cache_path = os.path.join(self.CACHE_DIR, self.TRAIN_DATA_CACHE_NAME)
train_alert_cache_path = os.path.join(self.CACHE_DIR, self.TRAIN_LABELS_CACHE_NAME)
train_rul_cache_path = os.path.join(self.CACHE_DIR, self.TRAIN_RUL_CACHE_NAME)
test_data_cache_path = os.path.join(self.CACHE_DIR, self.TEST_DATA_CACHE_NAME)
test_alert_cache_path = os.path.join(self.CACHE_DIR, self.TEST_LABELS_CACHE_NAME)
test_rul_cache_path = os.path.join(self.CACHE_DIR, self.TEST_RUL_CACHE_NAME)
train_condition_cache_path = os.path.join(self.CACHE_DIR,self.TRAIN_CONDITION_NAME)
test_condition_cache_path = os.path.join(self.CACHE_DIR, self.TEST_CONDITION_NAME)
if os.path.exists(train_data_cache_path + ".npy"):
print("! Using cached data")
return np.load(train_data_cache_path + ".npy"), np.load(train_alert_cache_path + ".npy"), np.load(
train_rul_cache_path + ".npy"), np.load(train_condition_cache_path+".npy"), \
np.load(test_data_cache_path + ".npy"), np.load(test_alert_cache_path + ".npy"), np.load(
test_rul_cache_path + ".npy"), np.load(test_condition_cache_path+".npy"),
else:
print("! Scanning directory data")
train_data, train_alert_labels, train_rul_minutes, train_condition, test_data, test_alert_labels, test_rul_minutes, test_condition = self.get_all_data()
np.save(train_data_cache_path, train_data)
np.save(train_alert_cache_path, train_alert_labels)
np.save(train_rul_cache_path, train_rul_minutes)
np.save(test_data_cache_path, test_data)
np.save(test_alert_cache_path, test_alert_labels)
np.save(test_rul_cache_path, test_rul_minutes)
np.save(train_condition_cache_path, train_condition)
np.save(test_condition_cache_path, test_condition)
return train_data, train_alert_labels, train_rul_minutes, train_condition, test_data, test_alert_labels, test_rul_minutes, test_condition
if __name__ == "__main__":
data = dataSet()
train_data, train_alert_labels, train_rul_minutes, train_condition, test_data, test_alert_labels, test_rul_minutes, test_condition = data.get_all_cache_data()
print("Train and test shape", train_data.shape, test_data.shape, train_rul_minutes.shape, train_alert_labels.shape, train_condition.shape)
# data.get_all_data()
# np_data = data.read_signal_file_to_array_by_path(
# r"E:\IDM_download\XJTU-SY_Bearing_Datasets\35Hz12kN\Bearing1_1\1.csv")
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("Qt5Agg")
idList = [0,50,75,100,150]
for idx,sampleIdx in enumerate(idList):
plt.subplot("%d%d%d"%(len(idList),1,idx+1))
plt.plot(train_data[sampleIdx,:,0],label="Horizon_%d"%(sampleIdx))
plt.xlabel("Time")
plt.ylabel("Signal")
plt.legend()
plt.show()
plt.plot(np.argmax(train_alert_labels,axis=1), label="TRAIN_ALERT_LABEL")
plt.plot(train_rul_minutes,label="Train RUL")
plt.xlabel("Time")
plt.ylabel("Signal")
plt.legend()
plt.show()