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 15
/
phm_dataset.py
220 lines (195 loc) · 8.19 KB
/
phm_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
import pandas as pd
import numpy as np
import csv
class PHMToolWearDataset():
signal_cache_path = ".cache/all_sample_dat_num_5000.npy"
tool_wear_cache_path = ".cache/all_res_dat_num_5000.npy"
window_number = 10
@property
def get_signal_data(self):
return np.load(self.signal_cache_path)
@property
def get_tool_wear_data(self):
return np.load(self.tool_wear_cache_path)
@property
def get_extended_data(self):
extended_sample_array = []
extended_sample_label = []
all_data = self.get_signal_data
tool_wear_data = self.get_tool_wear_data
for tool_num in range(3):
signal_data = all_data[tool_num*315:(tool_num+1)*315]
label_data = tool_wear_data[tool_num*315:(tool_num+1)*315]
for i in range(signal_data.shape[0] - 1):
prev_array = signal_data[i]
post_array = signal_data[i + 1]
# only one dimension
prev_value = label_data[i]
post_value = label_data[i + 1]
print(prev_array.shape)
for part_num in range(1, self.window_number):
signal_length = prev_array.shape[0]
extended_array = np.concatenate((prev_array[part_num * signal_length // self.window_number:],
post_array[:part_num * signal_length // self.window_number]),
axis=0)
extended_value = (part_num * np.array(prev_value))/10 + ((10 - part_num) * np.array(post_value)) /10
# extended_value = (part_num * post_value) / 10 + ((10 - part_num) * prev_value) / 10
extended_sample_array.append(extended_array)
extended_sample_label.append(extended_value)
return np.array(extended_sample_array), np.array(extended_sample_label)
@property
def get_all_data(self):
all_data = self.get_signal_data
tool_wear_data = self.get_tool_wear_data
extend_signal,extend_tool_wear = self.get_extended_data
return np.concatenate((all_data,extend_signal),axis=0),np.concatenate((tool_wear_data,extend_tool_wear),axis=0)
def get_recoginition_data(self):
x,y = self.get_extended_data
y = y.max(axis=1)
tool_wear_peroid = np.zeros(y.shape,dtype=np.int)
i = 30
cnt = 0
STEP = 2
# 31 - 234
class_num = 105
# 0-102
while i < 240:
class_index = np.logical_and(y > i,y <= i+STEP)
tool_wear_peroid[class_index] = cnt
i += STEP
cnt += 1
print(y.shape,tool_wear_peroid.shape)
return x,tool_wear_peroid
def get_native_recoginition_data(self):
x = self.get_signal_data
y = self.get_tool_wear_data
# x,y = self.get_extended_data
y = y.max(axis=1)
tool_wear_peroid = np.zeros(y.shape,dtype=np.int)
i = 30
cnt = 0
STEP = 2
# 31 - 234
class_num = 105
# 0-102
while i < 240:
class_index = np.logical_and(y > i,y <= i+STEP)
tool_wear_peroid[class_index] = cnt
i += STEP
cnt += 1
print(y.shape,tool_wear_peroid.shape)
return x,tool_wear_peroid
def get_native_recoginition_data_in_class_num(self,class_num=10):
x = self.get_signal_data
y = self.get_tool_wear_data
# x,y = self.get_extended_data
y = y.max(axis=1)
tool_wear_peroid = np.zeros(y.shape,dtype=np.int)
i = 30
cnt = 0
STEP = (240-30)/ class_num
# 31 - 234
# 0-102
while i < 240:
class_index = np.logical_and(y > i,y <= i+STEP)
tool_wear_peroid[class_index] = cnt
i += STEP
cnt += 1
assert cnt == class_num
print(y.shape,tool_wear_peroid.shape)
return x,tool_wear_peroid
def get_recoginition_data_in_class_num(self,class_num=10):
x, y = self.get_extended_data
# x,y = self.get_extended_data
y = y.max(axis=1)
tool_wear_peroid = np.zeros(y.shape,dtype=np.int)
i = 30
cnt = 0
STEP = (240-30) / class_num
# 31 - 234
# 0-102
while i < 240:
class_index = np.logical_and(y > i,y <= i+STEP)
tool_wear_peroid[class_index] = cnt
i += STEP
cnt += 1
print("Set apart... ",cnt,class_num,STEP,tool_wear_peroid.max(),tool_wear_peroid.min())
# assert cnt - 1 == class_num
print(y.shape,tool_wear_peroid.shape)
return x,tool_wear_peroid
def get_reinforce_short_extend_data(self):
x, y = self.get_extended_data
print(x.shape,y.shape)
index = np.array([i for i in range(0,5000,10)])
# print(index)
reinforce_signal_data = []
reinforce_wear_data = []
MAX_SAMPLE_NUM = x.shape[0]
for data_idx in range(x.shape[0]):
tool_max_wear = y[data_idx].max()
cur_signal_data = x[data_idx]
cur_wear_data = y[data_idx]
if data_idx // (MAX_SAMPLE_NUM // 3) == 0:
# first wear filter
if tool_max_wear > 110 and tool_max_wear < 120:
reinforce_signal_data.append(cur_signal_data[::10,:])
reinforce_wear_data.append(cur_wear_data)
else:
# get reinforce
for stride_index in range(10):
reinforce_signal_data.append(cur_signal_data[index+stride_index])
reinforce_wear_data.append(cur_wear_data)
elif data_idx // (MAX_SAMPLE_NUM // 3) == 1:
# first wear filter
if tool_max_wear > 110 and tool_max_wear < 120:
reinforce_signal_data.append(cur_signal_data[::10,:])
reinforce_wear_data.append(cur_wear_data)
else:
# get reinforce
for stride_index in range(10):
reinforce_signal_data.append(cur_signal_data[index+stride_index])
reinforce_wear_data.append(cur_wear_data)
else:
# first wear filter
if tool_max_wear > 110 and tool_max_wear < 120:
reinforce_signal_data.append(cur_signal_data[::10,:])
reinforce_wear_data.append(cur_wear_data)
else:
# get reinforce
for stride_index in range(10):
reinforce_signal_data.append(cur_signal_data[index+stride_index])
reinforce_wear_data.append(cur_wear_data)
return np.array(reinforce_signal_data), np.array(reinforce_wear_data)
def get_reinforce_recoginition_data_in_class_num(self,class_num=10):
x, y = self.get_reinforce_short_extend_data()
# x,y = self.get_extended_data
y = y.max(axis=1)
tool_wear_peroid = np.zeros(y.shape,dtype=np.int)
i = 30
cnt = 0
STEP = (240-30) / class_num
# 31 - 234
# 0-102
while i < 240:
class_index = np.logical_and(y > i,y <= i+STEP)
tool_wear_peroid[class_index] = cnt
i += STEP
cnt += 1
print("Set apart... ",cnt,class_num,STEP,tool_wear_peroid.max(),tool_wear_peroid.min())
# assert cnt - 1 == class_num
print(y.shape,tool_wear_peroid.shape)
return x,tool_wear_peroid
if __name__ == "__main__":
tool_wear_dataset = PHMToolWearDataset()
x,y = tool_wear_dataset.get_reinforce_short_extend_data()
print(x.shape,y.shape)
# tool_wear_dataset.get_recoginition_data()
# x = tool_wear_dataset.get_signal_data
# print(x.shape)
# print(x.max(axis=1).max(axis=0),"\n",x.min(axis=1).min(axis=0))
# tool_wear_data = tool_wear_data.get_tool_wear_data
#print(tool_wear_dataset.get_extended_data[0].shape,tool_wear_dataset.get_extended_data[1].shape)
#print(tool_wear_dataset.get_all_data[0].shape, tool_wear_dataset.get_all_data[1].shape)
# a = RNNSeriesDataSet(2,5)
# dat_x,dat_y = a.get_rnn_data()
# print(dat_x.shape,dat_y.shape)