cesar 6 hours ago
parent
commit
ef5e0f7270

BIN
model_v1_0._checkpoint.weights.h5 View File


BIN
model_v1_1._checkpoint.weights.h5 View File


BIN
model_v1_2._checkpoint.weights.h5 View File


BIN
model_v1_3._checkpoint.weights.h5 View File


BIN
model_v1_4._checkpoint.weights.h5 View File


+ 308
- 39
v1.py View File

@@ -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
+

+ 103
- 78
v1_multifailure.py View File

@@ -120,8 +120,11 @@ def plotData():
120 120
 
121 121
 #plotData()
122 122
 
123
-
124
-TIME_STEPS = 12
123
+NumFilters=64
124
+KernelSize=7
125
+DropOut=0.2
126
+ThresholdFactor=1.7
127
+TIME_STEPS = 48
125 128
 def create_sequences(values, time_steps=TIME_STEPS):
126 129
     output = []
127 130
     for i in range(len(values) - time_steps + 1):
@@ -137,42 +140,43 @@ model=[]
137 140
 modelckpt_callback =[]
138 141
 es_callback =[]
139 142
 path_checkpoint=[]
143
+
140 144
 for i in range(NumberOfFailures+1):
141 145
     model.append([])
142 146
     model[i] = keras.Sequential(
143 147
         [
144 148
             layers.Input(shape=(x_train[i].shape[1], x_train[i].shape[2])),
145 149
             layers.Conv1D(
146
-                filters=64,
147
-                kernel_size=7,
150
+                filters=NumFilters,
151
+                kernel_size=KernelSize,
148 152
                 padding="same",
149 153
                 strides=2,
150 154
                 activation="relu",
151 155
             ),
152
-            layers.Dropout(rate=0.2),
156
+            layers.Dropout(rate=DropOut),
153 157
             layers.Conv1D(
154
-                filters=32,
155
-                kernel_size=7,
158
+                filters=int(NumFilters/2),
159
+                kernel_size=KernelSize,
156 160
                 padding="same",
157 161
                 strides=2,
158 162
                 activation="relu",
159 163
             ),
160 164
             layers.Conv1DTranspose(
161
-                filters=32,
162
-                kernel_size=7,
165
+                filters=int(NumFilters/2),
166
+                kernel_size=KernelSize,
163 167
                 padding="same",
164 168
                 strides=2,
165 169
                 activation="relu",
166 170
             ),
167
-            layers.Dropout(rate=0.2),
171
+            layers.Dropout(rate=DropOut),
168 172
             layers.Conv1DTranspose(
169
-                filters=64,
170
-                kernel_size=7,
173
+                filters=NumFilters,
174
+                kernel_size=KernelSize,
171 175
                 padding="same",
172 176
                 strides=2,
173 177
                 activation="relu",
174 178
             ),
175
-            layers.Conv1DTranspose(filters=x_train[i].shape[2], kernel_size=7, padding="same"),
179
+            layers.Conv1DTranspose(filters=x_train[i].shape[2], kernel_size=KernelSize, padding="same"),
176 180
         ]
177 181
     )
178 182
     model[i].compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
@@ -213,7 +217,8 @@ for i in range(NumberOfFailures+1):
213 217
 
214 218
 print("Threshold : ",threshold)
215 219
 for i in range(NumberOfFailures+1):
216
-    threshold[i]=threshold[i]*1.3
220
+    threshold[i]=threshold[i]*ThresholdFactor
221
+
217 222
 # Threshold is enlarged because, otherwise, for subsamples at 5' have many false positives
218 223
 
219 224
 
@@ -234,7 +239,7 @@ for i in range(1,NumberOfFailures+1):
234 239
     rnext=r+dataTestNorm[i].shape[0]
235 240
     testRanges.append([r,rnext] )
236 241
     r=rnext
237
-testRanges.append([r, x_test.shape[0]  ])
242
+testRanges.append([r, x_test.shape[0]+TIME_STEPS  ])
238 243
 
239 244
 
240 245
 def AtLeastOneTrue(x):
@@ -253,26 +258,94 @@ for i in range(anomalies.shape[0]):
253 258
 #print(anomalous_data_indices)
254 259
 
255 260
 
256
-# Let's plot only a couple of features
257
-def plotData2():    
261
+# Let's plot some features
262
+
263
+colorline=['violet','lightcoral','cyan','lime','grey']
264
+colordot=['darkviolet','red','blue','green','black']
265
+
266
+featuresToPlot=['r1 s1','r1 s2','r1 s3','pa1 apiii']
267
+#featuresToPlot=features
268
+
269
+indexesToPlot=[]
270
+for i in featuresToPlot:
271
+    indexesToPlot.append(features.index(i))
272
+
273
+def plotData2():
274
+    NumFeaturesToPlot=len(indexesToPlot)
258 275
     fig, axes = plt.subplots(
259
-        nrows=2, ncols=1, figsize=(15, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
276
+        nrows=NumFeaturesToPlot, ncols=1, figsize=(25, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
260 277
     )
261
-    axes[0].plot(range(len(x_train[0])),x_train[0][:,0,0],label="normal")
262
-    axes[0].plot(range(len(x_train[0]),len(x_train[0])+len(x_test)),x_test[:,0,0],label="abnormal")
263
-    axes[0].plot(len(x_train[0])+np.array(anomalous_data_indices),x_test[anomalous_data_indices,0,0],color='red',marker='.',linewidth=0,label="abnormal detection")
264
-    axes[0].legend()
265
-    axes[1].plot(range(len(x_train[0])),x_train[0][:,0,1],label="normal")
266
-    axes[1].plot(range(len(x_train[0]),len(x_train[0])+len(x_test)),x_test[:,0,1],label="abnormal")
267
-    axes[1].plot(len(x_train[0])+np.array(anomalous_data_indices),x_test[anomalous_data_indices,0,1],color='red',marker='.',linewidth=0,label="abnormal detection")
268
-    axes[1].legend()
269
-    axes[0].set_ylabel(features[0])
270
-    axes[1].set_ylabel(features[1])
278
+    for i in range(NumFeaturesToPlot):
279
+        init=0
280
+        end=len(x_train[0])
281
+        axes[i].plot(range(init,end),x_train[0][:,0,indexesToPlot[i]],label="normal train")
282
+        init=end
283
+        end+=testRanges[0][1]
284
+        axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]],label="normal test")
285
+        init=end
286
+        end+=(testRanges[1][1]-testRanges[1][0])
287
+        for j in range(1,NumberOfFailures+1):
288
+            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])
289
+            init=end
290
+            end+=(testRanges[j+1][1]-testRanges[j+1][0])
291
+        # Shift TIME_STEPS because detection is performed at the end of time serie
292
+        trail=np.hstack((x_test[:,0,indexesToPlot[i]], x_test[-1:,1:TIME_STEPS,indexesToPlot[i]].reshape(TIME_STEPS-1)))
293
+        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" )
294
+        
295
+        init=end-(testRanges[NumberOfFailures+1][1]-testRanges[NumberOfFailures+1][0])
296
+        end=init+(testRanges[0][1]-testRanges[0][0])
297
+        axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]],color='orange')
298
+
299
+        if i==0:
300
+            axes[i].legend(bbox_to_anchor=(1, 0.5))
301
+        axes[i].set_ylabel(features[indexesToPlot[i]])
302
+        axes[i].grid()
271 303
     plt.show()
272 304
 
273
-#plotData2()
305
+
306
+def anomalyMetric(testList):  # first of list is non failure data
307
+    # FP, TP: false/true positive
308
+    # TN, FN: true/false negative
309
+    # Sensitivity: probab failure detection if data is fail: TP/(TP+FN)
310
+    # Specificity: true negative ratio given  data is OK: TN/(TN+FP)
311
+
312
+    x_test = create_sequences(testList[0])
313
+    x_test_pred = model[0].predict(x_test)
314
+    test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
315
+    anomalies = test_mae_loss > threshold[0]
316
+    count=0
317
+    for i in range(anomalies.shape[0]):
318
+        if AtLeastOneTrue(anomalies[i]):
319
+            count+=1
320
+    FP=count
321
+    TN=anomalies.shape[0]-count
322
+    count=0
323
+    TP=np.zeros((NumberOfFailures))
324
+    FN=np.zeros((NumberOfFailures))
325
+    for i in range(1,len(testList)):
326
+        x_test = create_sequences(testList[i])
327
+        x_test_pred = model[0].predict(x_test)
328
+        test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
329
+        anomalies = test_mae_loss > threshold[0]
330
+        count=0
331
+        for j in range(anomalies.shape[0]):
332
+            if AtLeastOneTrue(anomalies[j]):
333
+                count+=1
334
+        TP[i-1] = count
335
+        FN[i-1] = anomalies.shape[0]-count
336
+    Sensitivity=TP.sum()/(TP.sum()+FN.sum())
337
+    Specifity=TN/(TN+FP)
338
+    print("Sensitivity: ",Sensitivity)
339
+    print("Specifity: ",Specifity)
340
+    print("FP: ",FP)
341
+    return Sensitivity+Specifity
342
+
274 343
 
275 344
 
345
+anomalyMetric([dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]])
346
+plotData2()
347
+
348
+exit(0)
276 349
 #   2nd scenario. Go over anomalies and classify it by less error
277 350
 '''   
278 351
 #This code works, but too slow
@@ -309,16 +382,6 @@ for i in range(len(anomalous_data_indices)):
309 382
     anomalous_data_indices_by_failure[anomalous_data_type[i]].append(anomalous_data_indices[i])  
310 383
 
311 384
 
312
-colorline=['violet','lightcoral','cyan','lime','grey']
313
-colordot=['darkviolet','red','blue','green','black']
314
-
315
-featuresToPlot=['r1 s1','r1 s3','r1 s5','r2 s3','r2 s4','pa1 apiii','tc s1','tc s2','tc s3']
316
-featuresToPlot=features
317
-
318
-indexesToPlot=[]
319
-for i in featuresToPlot:
320
-    indexesToPlot.append(features.index(i))
321
-
322 385
 def plotData3():
323 386
     NumFeaturesToPlot=len(indexesToPlot)
324 387
     fig, axes = plt.subplots(
@@ -339,7 +402,7 @@ def plotData3():
339 402
             init=end
340 403
             end+=(testRanges[j+1][1]-testRanges[j+1][0])
341 404
 
342
-            axes[i].plot(len(x_train[0])+np.array(anomalous_data_indices_by_failure[j]),x_test[anomalous_data_indices_by_failure[j],0,indexesToPlot[i]],color=colordot[j-1],marker='.',linewidth=0,label="abnormal detection type "+str(j))
405
+            axes[i].plot(len(x_train[0])+np.array(anomalous_data_indices_by_failure[j])+TIME_STEPS,x_test[np.array(anomalous_data_indices_by_failure[j])+TIME_STEPS,0,indexesToPlot[i]],color=colordot[j-1],marker='.',linewidth=0,label="abnormal detection type "+str(j))
343 406
 
344 407
         init=end-(testRanges[NumberOfFailures+1][1]-testRanges[NumberOfFailures+1][0])
345 408
         end=init+(testRanges[0][1]-testRanges[0][0])
@@ -353,43 +416,5 @@ def plotData3():
353 416
 
354 417
 
355 418
 
356
-
357
-def anomalyMetric(testList):  # first of list is non failure data
358
-    # FP, TP: false/true positive
359
-    # TN, FN: true/false negative
360
-    # Sensitivity: probab of fail detection if data is fail
361
-    # Specificity: prob of no fail detection if data is well
362
-    x_test = create_sequences(testList[0])
363
-    x_test_pred = model[0].predict(x_test)
364
-    test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
365
-    anomalies = test_mae_loss > threshold[0]
366
-    count=0
367
-    for i in range(anomalies.shape[0]):
368
-        if AtLeastOneTrue(anomalies[i]):
369
-            count+=1
370
-    FP=count
371
-    TN=anomalies.shape[0]-1
372
-    count=0
373
-    TP=np.zeros((NumberOfFailures))
374
-    FN=np.zeros((NumberOfFailures))
375
-    for i in range(1,len(testList)):
376
-        x_test = create_sequences(testList[i])
377
-        x_test_pred = model[0].predict(x_test)
378
-        test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
379
-        anomalies = test_mae_loss > threshold[0]
380
-        count=0
381
-        for j in range(anomalies.shape[0]):
382
-            if AtLeastOneTrue(anomalies[j]):
383
-                count+=1
384
-        TP[i-1] = count
385
-        FN[i-1] = anomalies.shape[0]-count
386
-    Sensitivity=TP.sum()/(TP.sum()+FN.sum())
387
-    Specifity=TN/(TN+FP)
388
-    print("Sensitivity: ",Sensitivity)
389
-    print("Specifity: ",Specifity)
390
-    return Sensitivity+Specifity
391
-
392
-
393
-
394 419
 anomalyMetric([dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]])
395 420
 plotData3()

Powered by TurnKey Linux.