Repository navigation
Expand file tree
/
Copy pathdata_process.py
More file actions
255 lines (202 loc) · 8.56 KB
/
Copy pathdata_process.py
File metadata and controls
255 lines (202 loc) · 8.56 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from cProfile import label
import os.path as osp
import sys, time
from turtle import shape
import pandas as pd
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from sklearn.preprocessing import MinMaxScaler
import os
import numpy as np
import matplotlib.pyplot as plt
from ssqueezepy import ssq_cwt, ssq_stft, cwt
sys.path.append("/home/room/WKK/SFDA-RUL/data")
from dataset import DataSet_phm
from utils import *
import core
np.set_printoptions(suppress=True)
def ssq_cwt2(x):
new_x = np.zeros((len(x), 2560, 490))
for i in range(len(x)):
Twx0, *_ = ssq_cwt(x[i, :, 0])
Twx1, *_ = ssq_cwt(x[i, :, 1])
Twx0, Twx1 = abs(Twx0), abs(Twx1)
new_x[i, :, :] = np.concatenate((Twx0, Twx1), axis=0).transpose(1, 0)
print("\rSSQ_CWT:%.2f %%" % (i * 100 / len(x)), end="")
# x[i, :, :] = x[i, :, :].transpose(0, 2, 1)
return new_x
def ssq_cwt_res(x):
new_x = np.zeros((len(x), 2, 224, 224))
new_x = torch.tensor(new_x)
for i in range(len(x)):
Twx0, *_ = ssq_cwt(x[i, :, 0])
Twx1, *_ = ssq_cwt(x[i, :, 1])
Twx0, Twx1 = abs(Twx0), abs(Twx1)
Twx0_resized, Twx1_resized = core.imresize(torch.tensor(Twx0), sizes=(224, 224)), core.imresize(torch.tensor(Twx1), sizes=(224, 224))
x0, x1 = Twx0_resized.unsqueeze(0), Twx1_resized.unsqueeze(0)
new_x[i, :, :, :] = torch.cat((x0, x1), axis=0)
print("\rSSQ_CWT:%.2f %%" % (i * 100 / len(x)), end="")
# x[i, :, :] = x[i, :, :].transpose(0, 2, 1)
return new_x
def ssq_cwt_gpu(x):
new_x = np.zeros((len(x), 2, 224, 224))
new_x = torch.tensor(new_x).cuda()
x = torch.tensor(x).cuda()
for i in range(len(x)):
Twx0, *_ = ssq_cwt(x[i, :, 0])
Twx1, *_ = ssq_cwt(x[i, :, 1])
Twx0, Twx1 = abs(Twx0), abs(Twx1)
Twx0_resized, Twx1_resized = core.imresize(torch.tensor(Twx0), sizes=(224, 224)), core.imresize(torch.tensor(Twx1), sizes=(224, 224))
x0, x1 = Twx0_resized.unsqueeze(0), Twx1_resized.unsqueeze(0)
new_x[i, :, :, :] = torch.cat((x0, x1), axis=0)
print("\rSSQ_CWT:%.2f %%" % (i * 100 / len(x)), end="")
# x[i, :, :] = x[i, :, :].transpose(0, 2, 1)
return new_x
def minmaxscalar(data):
min = np.amin(data)
max = np.amax(data)
return (data - min)/(max-min)
def standardscalar(data):
mean = np.mean(data)
std = np.std(data)
return (data - mean) / std
def linearscalar(data):
max = np.max(data)
return data / max
class MyDataset(Dataset):
def __init__(self, pth, pth2):
self.pth = pth
self.pth2 = pth2
self.x_lists = os.listdir(self.pth)
self.x_lists.sort(key=lambda x:int(x[4:-4]))
self.y_data = np.loadtxt(self.pth2 + "labels.txt", dtype=int)
# self.y_data = minmaxscalar(self.y_data)
def __getitem__(self, index):
# print(self.pth + self.x_lists[index])
self.x1 = pd.read_csv(self.pth + self.x_lists[index],
header=None, usecols=[4]).values
self.x2 = pd.read_csv(self.pth + self.x_lists[index],
header=None, usecols=[5]).values
# self.x1 = minmaxscalar(self.x1)
# self.x2 = minmaxscalar(self.x2)
self.x = torch.tensor(np.concatenate((self.x1, self.x2), axis=1))
return self.x, self.y_data[index]
def __len__(self):
return len(self.x_lists)
class MyDataset_NL(Dataset):
def __init__(self, pth, pth2):
self.pth = pth
self.pth2 = pth2
self.x_lists = os.listdir(self.pth)
self.x_lists.sort(key=lambda x:int(x[4:-4]))
self.y_data = np.loadtxt(self.pth2 + "labels.csv")
def __getitem__(self, index):
# print(self.pth + self.x_lists[index])
self.x1 = pd.read_csv(self.pth + self.x_lists[index],
header=None, usecols=[0]).values
self.x2 = pd.read_csv(self.pth + self.x_lists[index],
header=None, usecols=[1]).values
# self.x1 = self.x1[::10]
# self.x2 = self.x2[::10]
self.x = torch.tensor(np.concatenate((self.x1, self.x2), axis=1))
return self.x, self.y_data[index]
def __len__(self):
return len(self.x_lists)
class MyDataset_new(Dataset):
def __init__(self, inputs, labels):
self.inputs = inputs
self.labels = labels
def __getitem__(self, index: int):
return self.inputs[index], self.labels[index]
def __len__(self):
return len(self.inputs)
def preprocess(select, train_bearings, test_bearings, norm_id, data_name):
dataset = DataSet_phm.load_dataset(name=data_name)
begin_1 = True
begin_2 = True
if select == 'train':
temp_data = dataset.get_value('data', condition={'bearing_name': train_bearings})
temp_label = dataset.get_value('RUL', condition={'bearing_name': train_bearings})
elif select == 'test':
temp_data = dataset.get_value('data', condition={'bearing_name': test_bearings})
temp_label = dataset.get_value('RUL', condition={'bearing_name': test_bearings})
else:
raise ValueError('wrong selection!')
for i, x in enumerate(temp_label):
temp_label[i] = np.arange(temp_data[i].shape[0]) + x
temp_label[i] = temp_label[i][:, np.newaxis, np.newaxis]
temp_label[i] = temp_label[i] / np.max(temp_label[i])
# temp_label[i] = temp_label[i][::8] # when chang 10
if begin_1 == True:
temp_y = temp_label[i]
begin_1 = False
else:
temp_y = np.concatenate((temp_y, temp_label[i]), axis=0)
for i, x in enumerate(temp_data):
temp_data[i] = x[::-1, ]
temp_data[i] = np.array(temp_data[i])
temp_data[i] = np.reshape(temp_data[i], (-1, 2))
if norm_id == "minmax":
minmax = MinMaxScaler()
temp_data[i] = minmax.fit_transform(temp_data[i])
# temp_data[i][:,0] = minmaxscalar(temp_data[i][:,0])
# temp_data[i][:,1] = minmaxscalar(temp_data[i][:,1])
elif norm_id == "standard":
temp_data[i][:,0] = standardscalar(temp_data[i][:,0])
temp_data[i][:,1] = standardscalar(temp_data[i][:,1])
elif norm_id == "linear":
temp_data[i][:,0] = linearscalar(temp_data[i][:,0])
temp_data[i][:,1] = linearscalar(temp_data[i][:,1])
else:
pass
if begin_2 == True:
temp_x = temp_data[i]
begin_2 = False
else:
temp_x = np.concatenate((temp_x, temp_data[i]), axis=0)
temp_y = np.array(temp_y).squeeze()
temp_x = np.reshape(temp_x, (-1, 2560, 2))
print(temp_x[:, :, 0][:, :, np.newaxis].shape)
return temp_x[:, :, 0][:, :, np.newaxis], temp_y
# return temp_x, temp_y
def preprocess_4_ResNet(select, train_bearings, test_bearings, data_name, gpu):
dataset = DataSet_phm.load_dataset(name=data_name)
begin_1 = True
begin_2 = True
if select == 'train':
temp_data = dataset.get_value('data', condition={'bearing_name': train_bearings})
temp_label = dataset.get_value('RUL', condition={'bearing_name': train_bearings})
elif select == 'test':
temp_data = dataset.get_value('data', condition={'bearing_name': test_bearings})
temp_label = dataset.get_value('RUL', condition={'bearing_name': test_bearings})
else:
raise ValueError('wrong selection!')
for i, x in enumerate(temp_label):
temp_label[i] = np.arange(temp_data[i].shape[0]) + x
temp_label[i] = temp_label[i][:, np.newaxis, np.newaxis]
temp_label[i] = temp_label[i] / np.max(temp_label[i])
# temp_label[i] = temp_label[i][::8] # when chang 10
if begin_1 == True:
temp_y = temp_label[i]
begin_1 = False
else:
temp_y = np.concatenate((temp_y, temp_label[i]), axis=0)
for i, x in enumerate(temp_data):
temp_data[i] = x[::-1, ]
temp_data[i] = np.array(temp_data[i])
temp_data[i] = np.reshape(temp_data[i], (-1, 2))
if begin_2 == True:
temp_x = temp_data[i]
begin_2 = False
else:
temp_x = np.concatenate((temp_x, temp_data[i]), axis=0)
temp_y = np.array(temp_y).squeeze()
temp_x = np.reshape(temp_x, (-1, 2560, 2))
if gpu == True:
temp_x = ssq_cwt_gpu(temp_x)
else:
temp_x = ssq_cwt_res(temp_x)
print('\n')
print(temp_x.shape)
return temp_x, temp_y