/
Generate_C-MAPSS.py
executable file
·194 lines (155 loc) · 6.99 KB
/
Generate_C-MAPSS.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
import numpy as np
import pandas as pd
import os
data_dir = '../CMAPSSData/'
save_dir = './data/'
# DROPLIST ['S18', 'S1', 'S19', 'S5', 'S16', 'S10', 'C2', 'S6', 'C1', 'C3']
# ['S2', 'S3', 'S4', 'S7', 'S8', 'S9', 'S11', 'S12', 'S13', 'S14', 'S15', 'S17', 'S20', 'S21']
data_name = 'FD003'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
def add_rul(df, total_rul, end_point):
engine_number = df['ID'].max()
for i in range(engine_number):
index = df[df['ID'] == i + 1].index
# whether the true RUL larger than assumed total RUL
if end_point[i] > total_rul:
df.loc[index, 'RUL'] = total_rul #end_point[i]
else:
rul = df.loc[index, 'Cycle'].sort_values(ascending=False) - 1 + end_point[i]
rul[0: len(index) - total_rul + int(end_point[i])] = total_rul
df.loc[index, 'RUL'] = list(rul)
return df
def pre_processing(df, cols, type='z-score'):
for j in range(len(cols)):
if type == '-1-1':
min_value = df.loc[:, cols[j]].min()
max_value = df.loc[:, cols[j]].max()
df.loc[:, cols[j]] = 2 * (df.loc[:, cols[j]] - min_value) / (max_value - min_value) - 1
elif type == 'z-score':
mean_value = df.loc[:, cols[j]].mean()
std_value = df.loc[:, cols[j]].std()
df.loc[:, cols[j]] = (df.loc[:, cols[j]] - mean_value) / std_value
else:
raise NameError('This normalization is not included!')
return df
def pre_processing_combined(df_train, df_test, cols, type='z-score'):
for j in range(len(cols)):
if type == '-1-1':
min_value = df_train.loc[:, cols[j]].min()
max_value = df_train.loc[:, cols[j]].max()
df_train.loc[:, cols[j]] = 2 * (df_train.loc[:, cols[j]] - min_value) / (max_value - min_value) - 1
df_test.loc[:, cols[j]] = 2 * (df_test.loc[:, cols[j]] - min_value) / (max_value - min_value) - 1
elif type == 'z-score':
mean_value = df_train.loc[:, cols[j]].mean()
std_value = df_train.loc[:, cols[j]].std()
df_train.loc[:, cols[j]] = (df_train.loc[:, cols[j]] - mean_value) / std_value
df_test.loc[:, cols[j]] = (df_test.loc[:, cols[j]] - mean_value) / std_value
else:
raise NameError('This normalization is not included!')
return df_train, df_test
def prepare_data(df, win_len, cols):
labels = []
engine_id = []
cycles = []
data = []
engine_number = df['ID'].max()
for i in range(engine_number):
index = df[df['ID'] == i + 1].index
# print(index)
# whether the length of cycle smaller than win_len, if smaller, we pad the closet point in the front
k = len(index)
if k < win_len:
labels.append(df.loc[index[-1], 'RUL'])
engine_id.append(i + 1)
cycles.append(index[-1])
data.append(np.hstack(np.ones((win_len-k, len(cols)))*df.loc[0, cols].values.reshape(1, -1),
df.loc[:, cols].values.reshape(k, -1)))
else:
for j in range(k-win_len+1):
labels.append(df.loc[index[win_len+j-1], 'RUL'])
engine_id.append(i + 1)
cycles.append(win_len+j)
#print(df.loc[index[j:j+win_len], cols].values.shape)
data.append(df.loc[index[j:j+win_len], cols].values)
prepared_df = pd.DataFrame()
prepared_df['engine_id'] = engine_id
prepared_df['cycle'] = cycles
prepared_df['data'] = data
prepared_df['label'] = labels
return prepared_df
def prepare_data_test(df, win_len, cols):
labels = []
engine_id = []
cycles = []
data = []
engine_number = df['ID'].max()
for i in range(engine_number):
index = df[df['ID'] == i + 1].index
# whether the length of cycle smaller than win_len, if smaller, we pad the closet point in the front
k = len(index)
if k < win_len:
labels.append(df.loc[index[-1], 'RUL'])
engine_id.append(i + 1)
cycles.append(index[-1])
data.append(np.hstack(np.ones((win_len-k, len(cols)))*df.loc[0, cols].values.reshape(1, -1),
df.loc[:, cols].values.reshape(k, -1)))
else:
labels.append(df.loc[index[-1], 'RUL'])
engine_id.append(i + 1)
cycles.append(k)
#print(df.loc[index[k-win_len:], cols].values.shape)
data.append(df.loc[index[k-win_len:], cols].values)
prepared_df = pd.DataFrame()
prepared_df['engine_id'] = engine_id
prepared_df['cycle'] = cycles
prepared_df['data'] = data
prepared_df['label'] = labels
return prepared_df
# Read the data
data_train_path = data_dir + 'train_' + data_name + '.txt'
data_test_path = data_dir + 'test_' + data_name + '.txt'
data_rul_path = data_dir + 'RUL_' + data_name + '.txt'
test_rul = np.loadtxt(data_rul_path)
print(test_rul.shape)
data_train = pd.read_csv(data_train_path, delim_whitespace=True, header=None)
data_test = pd.read_csv(data_test_path, delim_whitespace=True, header=None)
new_cols = ['ID', 'Cycle', 'C1', 'C2', 'C3'] + ['S' + str(x) for x in range(1, 26 - 4)]
data_train.columns = new_cols
data_test.columns = new_cols
# ['S2', 'S3', 'S4', 'S7', 'S8', 'S9', 'S11', 'S12', 'S13', 'S14', 'S15', 'S17', 'S20', 'S21']
# Drop the useless columns
droplist = []
engine_number = data_train['ID'].max()
for i in range(engine_number):
df = data_train[data_train['ID'] == i + 1]
droplist = list(set(droplist + list(df.std()[(df.std() < 1e-3)].index)))
engine_number = data_test['ID'].max()
for i in range(engine_number):
df = data_test[data_test['ID'] == i + 1]
droplist = list(set(droplist + list(df.std()[(df.std() < 1e-3)].index)))
drop_list = list(set(droplist + ['C1', 'C2', 'C3']))
drop_list.remove('ID')
print(drop_list)
data_train = data_train.drop(drop_list, axis=1)
data_test = data_test.drop(drop_list, axis=1)
# Normalization
cols = [x for x in data_train.columns if 'S' in x]
print(cols)
print(len(cols))
#data_train = pre_processing(data_train, cols, type='-1-1')
#data_test = pre_processing(data_test, cols, type='-1-1')
data_train, data_test = pre_processing_combined(data_train, data_test, cols, type='-1-1')
# Add RUL
data_train = add_rul(data_train, total_rul=125, end_point=np.zeros(data_train['ID'].max()))
data_test = add_rul(data_test, total_rul=125, end_point=test_rul)
# Generate Training and testing examples
prepared_train = prepare_data(data_train, win_len=30, cols=cols)
prepared_test = prepare_data_test(data_test, win_len=30, cols=cols)
prepared_test_all = prepare_data(data_test, win_len=30, cols=cols)
#prepared_test.to_csv(save_dir + 'test_' + data_name + '.csv', index=0)
#prepared_train.to_csv(save_dir + 'train_' + data_name + '.csv', index=0)
# Read pd.read_pickle('test.pkl')
prepared_test_all.to_pickle(save_dir + 'test_all_' + data_name + '.pkl')
prepared_test.to_pickle(save_dir + 'test_' + data_name + '.pkl')
prepared_train.to_pickle(save_dir + 'train_' + data_name + '.pkl')