-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_case_study_1.py
266 lines (202 loc) · 11.6 KB
/
Final_case_study_1.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
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
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
# coding: utf-8
# # def final_fun_1(X):
# In[1]:
import seaborn as sns
import os
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# In[2]:
def final_fun_1(PdM_telemetry, PdM_errors, PdM_maint, PdM_machines, finalized_model):
'''
This function returns status of failure of machines' components with the following inputs:
PdM_telemetry = Hourly average data of voltage, rotation, pressure, vibration
collected from machines.
PdM_errors = Errors encountered by the machines in operating condition.
PdM_maint = Replacement of component history.
PdM_machines = Model type & age of the Machines (Metadata of machine).
finalized_model = A pre-trained Machine Learning model.
'''
#Loading all the datasets using Pandas library
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
telemetry = pd.read_csv(PdM_telemetry, nrows= 24) #66666
errors = pd.read_csv(PdM_errors)
maint = pd.read_csv(PdM_maint)
machines = pd.read_csv(PdM_machines)
# Formating datetime field.
telemetry['datetime'] = pd.to_datetime(telemetry['datetime'], format="%Y-%m-%d %H:%M:%S")
errors['datetime'] = pd.to_datetime(errors['datetime'], format="%Y-%m-%d %H:%M:%S")
errors['errorID'] = errors['errorID'].astype('category')
maint['datetime'] = pd.to_datetime(maint['datetime'], format="%Y-%m-%d %H:%M:%S")
maint['comp'] = maint['comp'].astype('category')
machines['model'] = machines['model'].astype('category')
#Lag Features from Telemetry data
# Calculate "resample min values" over the last 3 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry,
index='datetime',
columns='machineID',
values=col).resample('3H', closed='left', label='right').min().unstack())
telemetry_min_3h = pd.concat(temp, axis=1)
telemetry_min_3h.columns = [i + '_min_3h' for i in fields]
telemetry_min_3h.reset_index(inplace=True)
# Calculate "resample max values" over the last 3 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry,
index='datetime',
columns='machineID',
values=col).resample('3H', closed='left', label='right').max().unstack())
telemetry_max_3h = pd.concat(temp, axis=1)
telemetry_max_3h.columns = [i + '_max_3h' for i in fields]
telemetry_max_3h.reset_index(inplace=True)
# Calculate "resample mean values" over the last 3 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry,
index='datetime',
columns='machineID',
values=col).resample('3H', closed='left', label='right').mean().unstack())
telemetry_mean_3h = pd.concat(temp, axis=1)
telemetry_mean_3h.columns = [i + '_mean_3h' for i in fields]
telemetry_mean_3h.reset_index(inplace=True)
# Calculate "resample standard deviation" over the last 3 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry,
index='datetime',
columns='machineID',
values=col).resample('3H', closed='left', label='right').std().unstack())
telemetry_sd_3h = pd.concat(temp, axis=1)
telemetry_sd_3h.columns = [i + '_sd_3h' for i in fields]
telemetry_sd_3h.reset_index(inplace=True)
#Capturing a longer term effect, 24 hour lag features
#Calculate "rolling min" over the last 24 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry, index='datetime',
columns='machineID',
values=col).rolling(window=24,center=False).min().resample('3H',
closed='left',
label='right').first().unstack())
telemetry_min_24h = pd.concat(temp, axis=1)
telemetry_min_24h.columns = [i + '_min_24h' for i in fields]
telemetry_min_24h.reset_index(inplace=True)
telemetry_min_24h = telemetry_min_24h.loc[-telemetry_min_24h['volt_min_24h'].isnull()]
#Calculate "rolling max" over the last 24 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry, index='datetime',
columns='machineID',
values=col).rolling(window=24,center=False).max().resample('3H',
closed='left',
label='right').first().unstack())
telemetry_max_24h = pd.concat(temp, axis=1)
telemetry_max_24h.columns = [i + '_max_24h' for i in fields]
telemetry_max_24h.reset_index(inplace=True)
telemetry_max_24h = telemetry_max_24h.loc[-telemetry_max_24h['volt_max_24h'].isnull()]
#Calculate "rolling mean" over the last 24 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry, index='datetime',
columns='machineID',
values=col).rolling(window=24,center=False).mean().resample('3H',
closed='left',
label='right').first().unstack())
telemetry_mean_24h = pd.concat(temp, axis=1)
telemetry_mean_24h.columns = [i + '_mean_24h' for i in fields]
telemetry_mean_24h.reset_index(inplace=True)
telemetry_mean_24h = telemetry_mean_24h.loc[-telemetry_mean_24h['volt_mean_24h'].isnull()]
#Calculate "rolling standard deviation" over the last 24 hour lag window for telemetry features.
temp = []
fields = ['volt', 'rotate', 'pressure', 'vibration']
for col in fields:
temp.append(pd.pivot_table(telemetry, index='datetime',
columns='machineID',
values=col).rolling(window=24,center=False).std().resample('3H',
closed='left', label='right').first().unstack())
telemetry_sd_24h = pd.concat(temp, axis=1)
telemetry_sd_24h.columns = [i + '_sd_24h' for i in fields]
telemetry_sd_24h.reset_index(inplace=True)
telemetry_sd_24h = telemetry_sd_24h.loc[-telemetry_sd_24h['volt_sd_24h'].isnull()]
# Merge columns of feature sets created earlier
telemetry_feat = pd.concat([telemetry_min_3h,
telemetry_max_3h.iloc[:, 2:6],
telemetry_mean_3h.iloc[:, 2:6],
telemetry_sd_3h.iloc[:, 2:6],
telemetry_min_24h.iloc[:, 2:6],
telemetry_max_24h.iloc[:, 2:6],
telemetry_mean_24h.iloc[:, 2:6],
telemetry_sd_24h.iloc[:, 2:6]], axis=1).dropna()
#Lag Features from Errors dataset
# Create a column for each error type
error_count = pd.get_dummies(errors.set_index('datetime')).reset_index()
error_count.columns = ['datetime', 'machineID', 'error1', 'error2', 'error3', 'error4', 'error5']
# Combine errors for a given machine in a given hour
error_count = error_count.groupby(['machineID', 'datetime']).sum().reset_index()
error_count = telemetry[['datetime', 'machineID']].merge(error_count, on=['machineID', 'datetime'],
how='left').fillna(0.0)
#Total number of errors of each type over the last 24 hours
temp = []
fields = ['error%d' % i for i in range(1,6)]
for col in fields:
temp.append(pd.pivot_table(error_count,
index='datetime',
columns='machineID',
values=col).rolling(window=24).sum().resample('3H',
closed='left', label='right').first().unstack())
error_count = pd.concat(temp, axis=1)
error_count.columns = [i + 'count' for i in fields]
# error_count.reset_index(inplace=True)#To be activate
error_count = error_count.dropna()
# Days Since Last Replacement from Maintenance
import numpy as np
# Create a column for each error type
comp_rep = pd.get_dummies(maint.set_index('datetime')).reset_index()
comp_rep.columns = ['datetime', 'machineID', 'comp1', 'comp2', 'comp3', 'comp4']
# Combine repairs for a given machine in a given hour
comp_rep = comp_rep.groupby(['machineID', 'datetime']).sum().reset_index()
# Add timepoints where no components were replaced
comp_rep = telemetry[['datetime', 'machineID']].merge(comp_rep,
on=['datetime', 'machineID'],
how='outer').fillna(0).sort_values(by=['machineID', 'datetime'])
components = ['comp1', 'comp2', 'comp3', 'comp4']
for comp in components:
# Convert indicator to most recent date of component change
comp_rep.loc[comp_rep[comp] < 1, comp] = None
comp_rep.loc[-comp_rep[comp].isnull(), comp] = comp_rep.loc[-comp_rep[comp].isnull(),'datetime']
# Forward-fill the most-recent date of component change
comp_rep[comp] = comp_rep[comp].fillna(method='ffill')
# Remove dates in 2014 (may have NaN or future component change dates)
comp_rep = comp_rep.loc[comp_rep['datetime'] > pd.to_datetime('2015-01-01')]
# Replace dates of most recent component change with days since most recent component change
for comp in components:
comp_rep[comp] = (comp_rep['datetime'] - comp_rep[comp]) / np.timedelta64(1, 'D')
#Machine Features
final_feat = telemetry_feat.merge(error_count, on=['datetime', 'machineID'], how='left')
final_feat = final_feat.merge(comp_rep, on=['datetime', 'machineID'], how='left')
final_feat = final_feat.merge(machines, on=['machineID'], how='left')
#Preparation for prediction
X = final_feat.drop(['datetime', 'machineID'], 1)
X_final = pd.get_dummies(X)
X_final_train = X_final.values
import pickle
# load the model from disk
model = pickle.load(open(finalized_model, 'rb'))
prediction = model.predict(X_final_train)
return prediction
# In[8]:
import pickle
print(pickle.format_version)