|
@@ -1,52 +1,321 @@
|
|
1
|
+# Csar Fdez, UdL, 2025
|
1
|
2
|
import pandas as pd
|
2
|
3
|
import matplotlib.pyplot as plt
|
3
|
4
|
import datetime
|
4
|
5
|
import numpy as np
|
|
6
|
+import keras
|
|
7
|
+import os.path
|
|
8
|
+import pickle
|
|
9
|
+from keras import layers
|
|
10
|
+from optparse import OptionParser
|
5
|
11
|
|
6
|
|
-#normal_datafiles_list=['2025-01-08_5_','2025-01-09_5_','2025-01-10_5_','2025-01-11_5_']
|
7
|
|
-normal_datafiles_list=['2025-01-09_5_','2025-01-10_5_','2025-01-11_5_']
|
8
|
|
-anormal_datafiles_list=['2025-01-04_5_','2025-01-05_5_','2025-01-06_5_','2025-01-07_5_']
|
9
|
12
|
|
10
|
|
-cols=['r1 s1','r1 s4','r1 s5','pa1 apiii']
|
|
13
|
+parser = OptionParser()
|
|
14
|
+parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
|
11
|
15
|
|
12
|
|
-df_list=[]
|
13
|
|
-for f in normal_datafiles_list:
|
14
|
|
- #df1 = pd.read_csv('./data/'+f+'.csv', parse_dates=['datetime'], dayfirst=True, index_col='datetime')
|
15
|
|
- df1 = pd.read_csv('./data/'+f+'.csv')
|
16
|
|
- df_list.append(df1)
|
|
16
|
+(options, args) = parser.parse_args()
|
17
|
17
|
|
18
|
|
-df=pd.concat(df_list)
|
19
|
|
-datalength=df.shape[0]
|
20
|
|
-# subsampled to 5' = 30 * 10"
|
21
|
|
-normaldataframe=df.iloc[range(0,datalength,30)][cols]
|
22
|
|
-normaldataframe.reset_index(inplace=True,drop=True)
|
23
|
|
-normaldata=normaldataframe.values
|
24
|
18
|
|
|
19
|
+# data files arrays. Index:
|
|
20
|
+# 0. No failure
|
|
21
|
+# 1. Blocked evaporator
|
|
22
|
+# 2. Full Blocked condenser
|
|
23
|
+# 3. Partial Blocked condenser
|
|
24
|
+# 4 Fan condenser not working
|
|
25
|
+# 5. Open door
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+NumberOfFailures=5
|
|
29
|
+NumberOfFailures=4 # So far, we have only data for the first 4 types of failures
|
|
30
|
+datafiles=[]
|
|
31
|
+for i in range(NumberOfFailures+1):
|
|
32
|
+ datafiles.append([])
|
|
33
|
+
|
|
34
|
+# Next set of ddata corresponds to Freezer, SP=-26
|
|
35
|
+datafiles[0]=['2024-08-07_5_','2024-08-08_5_']
|
|
36
|
+datafiles[1]=['2024-12-11_5_', '2024-12-12_5_','2024-12-13_5_','2024-12-14_5_','2024-12-15_5_']
|
|
37
|
+datafiles[2]=['2024-12-18_5_','2024-12-19_5_']
|
|
38
|
+datafiles[3]=['2024-12-21_5_','2024-12-22_5_','2024-12-23_5_','2024-12-24_5_','2024-12-25_5_','2024-12-26_5_']
|
|
39
|
+datafiles[4]=['2024-12-28_5_','2024-12-29_5_','2024-12-30_5_','2024-12-31_5_','2025-01-01_5_']
|
|
40
|
+#datafiles[4]=[]
|
|
41
|
+
|
|
42
|
+# Features suggested by Xavier
|
|
43
|
+# Care with 'tc s3' because on datafiles[0] is always nulll
|
|
44
|
+# Seems to be incoropored in new tests
|
|
45
|
+
|
|
46
|
+features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
|
|
47
|
+features=['r1 s1','r1 s2','r1 s3','r1 s4','r1 s5','r1 s6','r1 s7','r1 s8','r1 s9','r1 s10','r2 s1','r2 s2','r2 s3','r2 s4','r2 s5','r2 s6','r2 s7','r2 s8','r2 s9','pa1 apiii','tc s1','tc s2']
|
|
48
|
+
|
|
49
|
+#features=['r2 s2', 'tc s1','r1 s10','r1 s6','r2 s8']
|
|
50
|
+
|
|
51
|
+NumFeatures=len(features)
|
25
|
52
|
|
26
|
53
|
df_list=[]
|
27
|
|
-for f in anormal_datafiles_list:
|
28
|
|
- #df1 = pd.read_csv('./data/'+f+'.csv', parse_dates=['datetime'], dayfirst=True, index_col='datetime')
|
29
|
|
- df1 = pd.read_csv('./data/'+f+'.csv')
|
30
|
|
- df_list.append(df1)
|
|
54
|
+for i in range(NumberOfFailures+1):
|
|
55
|
+ df_list.append([])
|
|
56
|
+
|
|
57
|
+for i in range(NumberOfFailures+1):
|
|
58
|
+ dftemp=[]
|
|
59
|
+ for f in datafiles[i]:
|
|
60
|
+ print(" ", f)
|
|
61
|
+ #df1 = pd.read_csv('./data/'+f+'.csv', parse_dates=['datetime'], dayfirst=True, index_col='datetime')
|
|
62
|
+ df1 = pd.read_csv('./data/'+f+'.csv')
|
|
63
|
+ dftemp.append(df1)
|
|
64
|
+ df_list[i]=pd.concat(dftemp)
|
|
65
|
+
|
31
|
66
|
|
32
|
|
-df=pd.concat(df_list)
|
33
|
|
-datalength=df.shape[0]
|
34
|
67
|
# subsampled to 5' = 30 * 10"
|
35
|
|
-anormaldataframe=df.iloc[range(0,datalength,30)][cols]
|
36
|
|
-anormaldataframe.reset_index(inplace=True,drop=True)
|
37
|
|
-anormaldata=anormaldataframe.values
|
38
|
|
-
|
39
|
|
-
|
40
|
|
-nplots=len(cols)
|
41
|
|
-
|
42
|
|
-plt.rcParams.update({'font.size': 10})
|
43
|
|
-f,ax = plt.subplots(int(np.ceil(nplots/2)),2,figsize=(24,17), dpi=80, facecolor='white', edgecolor='k')
|
44
|
|
-for i in range(int(np.ceil(nplots/2))):
|
45
|
|
- for j in range(2):
|
46
|
|
- r=i*2+j
|
47
|
|
- if r<nplots:
|
48
|
|
- ax[i][j].plot(normaldata[:,r],label='normal')
|
49
|
|
- ax[i][j].plot(anormaldata[:,r],label='abnormal')
|
50
|
|
- ax[i][j].set_title(anormaldataframe.columns[r])
|
51
|
|
- ax[i][j].legend()
|
52
|
|
-plt.show()
|
|
68
|
+# We consider smaples every 5' because in production, we will only have data at this frequency
|
|
69
|
+subsamplingrate=30
|
|
70
|
+
|
|
71
|
+dataframe=[]
|
|
72
|
+for i in range(NumberOfFailures+1):
|
|
73
|
+ dataframe.append([])
|
|
74
|
+
|
|
75
|
+for i in range(NumberOfFailures+1):
|
|
76
|
+ datalength=df_list[i].shape[0]
|
|
77
|
+ dataframe[i]=df_list[i].iloc[range(0,datalength,subsamplingrate)][features]
|
|
78
|
+ dataframe[i].reset_index(inplace=True,drop=True)
|
|
79
|
+ dataframe[i].dropna(inplace=True)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+# Train data is first 2/3 of data
|
|
83
|
+# Test data is: last 1/3 of data
|
|
84
|
+dataTrain=[]
|
|
85
|
+dataTest=[]
|
|
86
|
+for i in range(NumberOfFailures+1):
|
|
87
|
+ dataTrain.append(dataframe[i].values[0:int(dataframe[i].shape[0]*2/3),:])
|
|
88
|
+ dataTest.append(dataframe[i].values[int(dataframe[i].shape[0]*2/3):,:])
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+def normalize2(train,test):
|
|
92
|
+ # merges train and test
|
|
93
|
+ means=[]
|
|
94
|
+ stdevs=[]
|
|
95
|
+ for i in range(NumFeatures):
|
|
96
|
+ means.append(train[:,i].mean())
|
|
97
|
+ stdevs.append(train[:,i].std())
|
|
98
|
+ return( (train-means)/stdevs, (test-means)/stdevs )
|
|
99
|
+
|
|
100
|
+dataTrainNorm=[]
|
|
101
|
+dataTestNorm=[]
|
|
102
|
+for i in range(NumberOfFailures+1):
|
|
103
|
+ dataTrainNorm.append([])
|
|
104
|
+ dataTestNorm.append([])
|
|
105
|
+
|
|
106
|
+for i in range(NumberOfFailures+1):
|
|
107
|
+ (dataTrainNorm[i],dataTestNorm[i])=normalize2(dataTrain[i],dataTest[i])
|
|
108
|
+
|
|
109
|
+def plotData():
|
|
110
|
+ fig, axes = plt.subplots(
|
|
111
|
+ nrows=NumberOfFailures+1, ncols=2, figsize=(15, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
|
|
112
|
+ )
|
|
113
|
+ for i in range(NumberOfFailures+1):
|
|
114
|
+ axes[i][0].plot(np.concatenate((dataTrainNorm[i][:,0],dataTestNorm[i][:,0])),label="Fail "+str(i)+", feature 0")
|
|
115
|
+ axes[i][1].plot(np.concatenate((dataTrainNorm[i][:,1],dataTestNorm[i][:,1])),label="Fail "+str(i)+", feature 1")
|
|
116
|
+ #axes[1].legend()
|
|
117
|
+ #axes[0].set_ylabel(features[0])
|
|
118
|
+ #axes[1].set_ylabel(features[1])
|
|
119
|
+ #plt.show()
|
|
120
|
+
|
|
121
|
+#plotData()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+NumFilters=64
|
|
125
|
+KernelSize=7
|
|
126
|
+DropOut=0.1
|
|
127
|
+ThresholdFactor=1.7
|
|
128
|
+TIME_STEPS = 48 # This is a trade off among better performance (high) and better response delay (low)
|
|
129
|
+def create_sequences(values, time_steps=TIME_STEPS):
|
|
130
|
+ output = []
|
|
131
|
+ for i in range(len(values) - time_steps + 1):
|
|
132
|
+ output.append(values[i : (i + time_steps)])
|
|
133
|
+ return np.stack(output)
|
|
134
|
+
|
|
135
|
+x_train=[]
|
|
136
|
+for i in range(NumberOfFailures+1):
|
|
137
|
+ x_train.append(create_sequences(dataTrainNorm[i]))
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+# Reused code from v1_multifailure for only one model. No classification
|
|
141
|
+#for i in range(NumberOfFailures+1):
|
|
142
|
+model = keras.Sequential(
|
|
143
|
+ [
|
|
144
|
+ layers.Input(shape=(x_train[0].shape[1], x_train[0].shape[2])),
|
|
145
|
+ layers.Conv1D(
|
|
146
|
+ filters=NumFilters,
|
|
147
|
+ kernel_size=7,
|
|
148
|
+ padding="same",
|
|
149
|
+ strides=2,
|
|
150
|
+ activation="relu",
|
|
151
|
+ ),
|
|
152
|
+ layers.Dropout(rate=DropOut),
|
|
153
|
+ layers.Conv1D(
|
|
154
|
+ filters=int(NumFilters/2),
|
|
155
|
+ kernel_size=KernelSize,
|
|
156
|
+ padding="same",
|
|
157
|
+ strides=2,
|
|
158
|
+ activation="relu",
|
|
159
|
+ ),
|
|
160
|
+ layers.Conv1DTranspose(
|
|
161
|
+ filters=int(NumFilters/2),
|
|
162
|
+ kernel_size=KernelSize,
|
|
163
|
+ padding="same",
|
|
164
|
+ strides=2,
|
|
165
|
+ activation="relu",
|
|
166
|
+ ),
|
|
167
|
+ layers.Dropout(rate=DropOut),
|
|
168
|
+ layers.Conv1DTranspose(
|
|
169
|
+ filters=NumFilters,
|
|
170
|
+ kernel_size=KernelSize,
|
|
171
|
+ padding="same",
|
|
172
|
+ strides=2,
|
|
173
|
+ activation="relu",
|
|
174
|
+ ),
|
|
175
|
+ layers.Conv1DTranspose(filters=x_train[i].shape[2], kernel_size=KernelSize, padding="same"),
|
|
176
|
+ ]
|
|
177
|
+)
|
|
178
|
+model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
|
|
179
|
+model.summary()
|
|
180
|
+path_checkpoint="model_noclass_v1_checkpoint.weights.h5"
|
|
181
|
+es_callback=keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
|
|
182
|
+modelckpt_callback=keras.callbacks.ModelCheckpoint( monitor="val_loss", filepath=path_checkpoint, verbose=1, save_weights_only=True, save_best_only=True,)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+if options.train:
|
|
186
|
+ history=model.fit( x_train[0], x_train[0], epochs=400, batch_size=128, validation_split=0.3, callbacks=[ es_callback, modelckpt_callback ],)
|
|
187
|
+else:
|
|
188
|
+ model.load_weights(path_checkpoint)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+x_train_pred=model.predict(x_train[0])
|
|
192
|
+train_mae_loss=np.mean(np.abs(x_train_pred - x_train[0]), axis=1)
|
|
193
|
+threshold=np.max(train_mae_loss,axis=0)
|
|
194
|
+
|
|
195
|
+print("Threshold : ",threshold)
|
|
196
|
+threshold=threshold*ThresholdFactor
|
|
197
|
+# Threshold is enlarged because, otherwise, for subsamples at 5' have many false positives
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+# 1st scenario. Detect only anomaly. Later, we will classiffy it
|
|
201
|
+# Test data= testnormal + testfail1 + testtail2 + testfail3 + testfail4 + testnormal
|
|
202
|
+d=np.vstack((dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4],dataTestNorm[0]))
|
|
203
|
+
|
|
204
|
+x_test = create_sequences(d)
|
|
205
|
+x_test_pred = model.predict(x_test)
|
|
206
|
+test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+# Define ranges for plotting in different colors
|
|
210
|
+testRanges=[]
|
|
211
|
+r=dataTestNorm[0].shape[0]
|
|
212
|
+testRanges.append([0,r])
|
|
213
|
+for i in range(1,NumberOfFailures+1):
|
|
214
|
+ rnext=r+dataTestNorm[i].shape[0]
|
|
215
|
+ testRanges.append([r,rnext] )
|
|
216
|
+ r=rnext
|
|
217
|
+
|
|
218
|
+testRanges.append([r, x_test.shape[0]+TIME_STEPS ])
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+def AtLeastOneTrue(x):
|
|
222
|
+ for i in range(NumFeatures):
|
|
223
|
+ if x[i]:
|
|
224
|
+ return True
|
|
225
|
+ return False
|
|
226
|
+
|
|
227
|
+anomalies = test_mae_loss > threshold
|
|
228
|
+anomalous_data_indices = []
|
|
229
|
+for i in range(anomalies.shape[0]):
|
|
230
|
+ if AtLeastOneTrue(anomalies[i]):
|
|
231
|
+ #if anomalies[i][0] or anomalies[i][1] or anomalies[i][2] or anomalies[i][3]:
|
|
232
|
+ anomalous_data_indices.append(i)
|
|
233
|
+
|
|
234
|
+#print(anomalous_data_indices)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+# Let's plot some features
|
|
238
|
+
|
|
239
|
+colorline=['violet','lightcoral','cyan','lime','grey']
|
|
240
|
+colordot=['darkviolet','red','blue','green','black']
|
|
241
|
+
|
|
242
|
+featuresToPlot=['r1 s1','r1 s2','r1 s3','pa1 apiii']
|
|
243
|
+#featuresToPlot=features
|
|
244
|
+
|
|
245
|
+indexesToPlot=[]
|
|
246
|
+for i in featuresToPlot:
|
|
247
|
+ indexesToPlot.append(features.index(i))
|
|
248
|
+
|
|
249
|
+def plotData2():
|
|
250
|
+ NumFeaturesToPlot=len(indexesToPlot)
|
|
251
|
+ fig, axes = plt.subplots(
|
|
252
|
+ nrows=NumFeaturesToPlot, ncols=1, figsize=(25, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
|
|
253
|
+ )
|
|
254
|
+ for i in range(NumFeaturesToPlot):
|
|
255
|
+ init=0
|
|
256
|
+ end=len(x_train[0])
|
|
257
|
+ axes[i].plot(range(init,end),x_train[0][:,0,indexesToPlot[i]],label="normal train")
|
|
258
|
+ init=end
|
|
259
|
+ end+=testRanges[0][1]
|
|
260
|
+ axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]],label="normal test")
|
|
261
|
+ init=end
|
|
262
|
+ end+=(testRanges[1][1]-testRanges[1][0])
|
|
263
|
+ for j in range(1,NumberOfFailures+1):
|
|
264
|
+ axes[i].plot(range(init,end),x_test[testRanges[j][0]:testRanges[j][1],0,indexesToPlot[i]],label="fail type "+str(j), color=colorline[j-1])
|
|
265
|
+ init=end
|
|
266
|
+ end+=(testRanges[j+1][1]-testRanges[j+1][0])
|
|
267
|
+ # Shift TIME_STEPS because detection is performed at the end of time serie
|
|
268
|
+ trail=np.hstack((x_test[:,0,indexesToPlot[i]], x_test[-1:,1:TIME_STEPS,indexesToPlot[i]].reshape(TIME_STEPS-1)))
|
|
269
|
+ axes[i].plot(len(x_train[0])+np.array(anomalous_data_indices)+TIME_STEPS,trail[np.array(anomalous_data_indices)+TIME_STEPS],color='grey',marker='.',linewidth=0,label="abnormal detection" )
|
|
270
|
+
|
|
271
|
+ init=end-(testRanges[NumberOfFailures+1][1]-testRanges[NumberOfFailures+1][0])
|
|
272
|
+ end=init+(testRanges[0][1]-testRanges[0][0])
|
|
273
|
+ axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]],color='orange')
|
|
274
|
+
|
|
275
|
+ if i==0:
|
|
276
|
+ axes[i].legend(bbox_to_anchor=(1, 0.5))
|
|
277
|
+ axes[i].set_ylabel(features[indexesToPlot[i]])
|
|
278
|
+ axes[i].grid()
|
|
279
|
+ plt.show()
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+def anomalyMetric(testList): # first of list is non failure data
|
|
283
|
+ # FP, TP: false/true positive
|
|
284
|
+ # TN, FN: true/false negative
|
|
285
|
+ # Sensitivity: probab failure detection if data is fail: TP/(TP+FN)
|
|
286
|
+ # Specificity: true negative ratio given data is OK: TN/(TN+FP)
|
|
287
|
+
|
|
288
|
+ x_test = create_sequences(testList[0])
|
|
289
|
+ x_test_pred = model.predict(x_test)
|
|
290
|
+ test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
|
|
291
|
+ anomalies = test_mae_loss > threshold
|
|
292
|
+ count=0
|
|
293
|
+ for i in range(anomalies.shape[0]):
|
|
294
|
+ if AtLeastOneTrue(anomalies[i]):
|
|
295
|
+ count+=1
|
|
296
|
+ FP=count
|
|
297
|
+ TN=anomalies.shape[0]-count
|
|
298
|
+ count=0
|
|
299
|
+ TP=np.zeros((NumberOfFailures))
|
|
300
|
+ FN=np.zeros((NumberOfFailures))
|
|
301
|
+ for i in range(1,len(testList)):
|
|
302
|
+ x_test = create_sequences(testList[i])
|
|
303
|
+ x_test_pred = model.predict(x_test)
|
|
304
|
+ test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
|
|
305
|
+ anomalies = test_mae_loss > threshold
|
|
306
|
+ count=0
|
|
307
|
+ for j in range(anomalies.shape[0]):
|
|
308
|
+ if AtLeastOneTrue(anomalies[j]):
|
|
309
|
+ count+=1
|
|
310
|
+ TP[i-1] = count
|
|
311
|
+ FN[i-1] = anomalies.shape[0]-count
|
|
312
|
+ Sensitivity=TP.sum()/(TP.sum()+FN.sum())
|
|
313
|
+ Specifity=TN/(TN+FP)
|
|
314
|
+ print("Sensitivity: ",Sensitivity)
|
|
315
|
+ print("Specifity: ",Specifity)
|
|
316
|
+ print("FP: ",FP)
|
|
317
|
+ return Sensitivity+Specifity
|
|
318
|
+
|
|
319
|
+anomalyMetric([dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]])
|
|
320
|
+plotData2()
|
|
321
|
+
|