Csar Fdez 2 months ago
parent
commit
83246600e5
2 changed files with 17 additions and 101 deletions
  1. 1
    1
      v4.py
  2. 16
    100
      v4_class.py

+ 1
- 1
v4.py View File

71
 
71
 
72
 features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
72
 features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
73
 features=['r1 s1','r1 s4','r1 s5']
73
 features=['r1 s1','r1 s4','r1 s5']
74
-features=['r1 s1','r1 s5']
74
+#features=['r1 s1','r1 s5']
75
 featureNames={}
75
 featureNames={}
76
 featureNames['r1 s1']='$T_{evap}$'
76
 featureNames['r1 s1']='$T_{evap}$'
77
 featureNames['r1 s4']='$T_{cond}$'
77
 featureNames['r1 s4']='$T_{cond}$'

+ 16
- 100
v4_class.py View File

22
 parser = OptionParser()
22
 parser = OptionParser()
23
 parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
23
 parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
24
 parser.add_option("-n", "--timesteps", dest="timesteps", help="TIME STEPS ", default=12)
24
 parser.add_option("-n", "--timesteps", dest="timesteps", help="TIME STEPS ", default=12)
25
-parser.add_option("-f", "--thresholdfactor", dest="TF", help="Threshold Factor ", default=1.4)
25
+#parser.add_option("-f", "--thresholdfactor", dest="TF", help="Threshold Factor ", default=1.4)
26
+# threshold makes no sense when classifying, becaues we apply many models and decide class for the less MSE
26
 
27
 
27
 (options, args) = parser.parse_args()
28
 (options, args) = parser.parse_args()
28
 
29
 
236
         r+=str(i)
237
         r+=str(i)
237
     return(r.replace(' ',''))
238
     return(r.replace(' ',''))
238
 
239
 
239
-threshold={} 
240
-
241
-fname='threshold_class_v4_'+listToString(features)+'.pk'
242
-if os.path.isfile(fname):  # Checks if it's a file and exists
243
-    print("File ",fname," exists. Loading it!")
244
-    file = open(fname, 'rb')
245
-    threshold=pickle.load(file)
246
-    file.close()
247
-    if not int(options.timesteps) in threshold.keys():
248
-        threshold[int(options.timesteps)]=[]
249
-        for i in range(NumberOfFailures+1):
250
-            threshold[int(options.timesteps)].append(0) # Initzialize
251
-else:
252
-    threshold[int(options.timesteps)]=[]
253
-    for i in range(NumberOfFailures+1):
254
-        threshold[int(options.timesteps)].append(0) # Initzialize
255
-
256
 
240
 
257
 model=[]
241
 model=[]
258
 modelckpt_callback =[]
242
 modelckpt_callback =[]
313
         history.append(model[i].fit( x_train[i], x_train[i], epochs=400, batch_size=128, validation_split=0.3, callbacks=[  es_callback[i], modelckpt_callback[i]      ],))
297
         history.append(model[i].fit( x_train[i], x_train[i], epochs=400, batch_size=128, validation_split=0.3, callbacks=[  es_callback[i], modelckpt_callback[i]      ],))
314
 
298
 
315
         x_train_pred=model[i].predict(x_train[i])
299
         x_train_pred=model[i].predict(x_train[i])
316
-        train_mae_loss=np.mean(np.abs(x_train_pred - x_train[i]), axis=1)
317
-        threshold[timesteps][i]=np.max(train_mae_loss,axis=0)
318
-
319
-    file = open('threshold_class_v4_'+listToString(features)+'.pk', 'wb')
320
-    pickle.dump(threshold, file)
321
-    file.close()
322
 else:
300
 else:
323
     for i in range(NumberOfFailures+1):
301
     for i in range(NumberOfFailures+1):
324
         model[i].load_weights(path_checkpoint[i])
302
         model[i].load_weights(path_checkpoint[i])
325
 
303
 
326
-file = open('threshold_class_v4_'+listToString(features)+'.pk', 'rb')
327
-threshold=pickle.load(file)
328
-file.close()
329
-#print(threshold)   
330
 
304
 
331
-#  1st scenario. Detect only anomaly.  Later, we will classiffy it
332
-# Test data=  testnormal + testfail1 + testtail2 + testfail3 + testfail4 + testnormal
333
-datalist=[dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]]
334
-
335
-x_test=create_sequences(datalist[0],int(options.timesteps))
336
-for i in range(1,len(datalist)):
337
-    x_test=np.vstack((x_test,create_sequences(datalist[i],int(options.timesteps))))
338
-
339
-x_test_pred = model[0].predict(x_test)
340
-test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
341
-anomalies = test_mae_loss > threshold[int(options.timesteps)][0]*float(options.TF)
342
-anomalous_data_indices = []
343
-for i in range(anomalies.shape[0]):
344
-    if AtLeastOneTrue(anomalies[i]):
345
-    #if anomalies[i][0] or anomalies[i][1] or anomalies[i][2] or anomalies[i][3]:
346
-        anomalous_data_indices.append(i)
347
-
348
-# Define ranges for plotting in different colors
349
-testRanges=[]
350
-r=0
351
-for i in range(len(datalist)):
352
-    testRanges.append([r,r+datalist[i].shape[0]-int(options.timesteps)])
353
-    r+=datalist[i].shape[0]-int(options.timesteps)
354
 
305
 
355
 # Let's plot some features
306
 # Let's plot some features
356
 
307
 
366
 
317
 
367
 
318
 
368
 
319
 
369
-def plotData3():
370
-    NumFeaturesToPlot=len(indexesToPlot)
371
-    plt.rcParams.update({'font.size': 16})
372
-    fig, axes = plt.subplots(
373
-        nrows=NumFeaturesToPlot, ncols=1, figsize=(15, 10), dpi=80, facecolor="w", edgecolor="k",sharex=True
374
-    )
375
-    for i in range(NumFeaturesToPlot):
376
-        x=[]
377
-        y=[]
378
-        for k in anomalous_data_indices:
379
-            if (k)<x_test.shape[0]:
380
-                x.append(k)
381
-                y.append(x_test[k,0,indexesToPlot[i]]*stdevs[i]+means[i])
382
-        axes[i].plot(x,y ,color='black',marker='.',linewidth=0,label="Fail detection" )
383
-
384
-        init=0
385
-        end=testRanges[0][1]
386
-        axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="No fail")
387
-        init=end
388
-        end+=(testRanges[1][1]-testRanges[1][0])
389
-        for j in range(1,NumberOfFailures+1):
390
-            axes[i].plot(range(init,end),x_test[testRanges[j][0]:testRanges[j][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="Fail type "+str(j), color=colorline[j-1],linewidth=1)
391
-            if j<NumberOfFailures:
392
-                init=end
393
-                end+=(testRanges[j+1][1]-testRanges[j+1][0])
394
-
395
-        if i==(NumFeatures-1):
396
-            axes[i].legend(loc='right')
397
-        s=''
398
-        s+=featureNames[features[indexesToPlot[i]]]
399
-        s+=' '+unitNames[features[indexesToPlot[i]]]
400
-        axes[i].set_ylabel(s)
401
-        axes[i].grid()
402
-    axes[NumFeaturesToPlot-1].set_xlabel("Sample number")
403
-    plt.show()
320
+#   2nd scenario. Go over anomalies and classify it by less error
321
+datalist=[dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]]
322
+x_test=create_sequences(datalist[0],int(options.timesteps))
323
+for i in range(1,len(datalist)):
324
+    x_test=np.vstack((x_test,create_sequences(datalist[i],int(options.timesteps))))
404
 
325
 
405
-anomalyMetric(threshold[int(options.timesteps)][0]*float(options.TF), int(options.timesteps),datalist)
406
-plotData3()
407
-exit(0)
326
+# Define ranges for plotting in different colors
327
+testRanges=[]
328
+r=0
329
+for i in range(len(datalist)):
330
+    testRanges.append([r,r+datalist[i].shape[0]-int(options.timesteps)])
331
+    r+=datalist[i].shape[0]-int(options.timesteps)
408
 
332
 
409
-#   2nd scenario. Go over anomalies and classify it by less error
410
 
333
 
411
-anomalous_data_type=[]
412
 x_test_predict=[]
334
 x_test_predict=[]
413
 for m in range(1,NumberOfFailures+1):
335
 for m in range(1,NumberOfFailures+1):
414
     x_test_predict.append(model[m].predict(x_test))
336
     x_test_predict.append(model[m].predict(x_test))
415
 
337
 
416
-anomalous_data_type={}
417
-for i in range(1,NumberOfFailures+1):
418
-    anomalous_data_type[i-1]=[]
419
-
420
-for i in anomalous_data_indices:
421
-    error=[]
422
-    for m in range(1,NumberOfFailures+1):
423
-        error.append(np.mean(np.mean(np.abs(x_test_predict[m-1][i:i+1,:,:]-x_test[i:i+1,:,:]),axis=1)))
424
-    anomalous_data_type[np.argmin(error)].append(i)
338
+x_test_predict=np.array((x_test_predict))
425
 
339
 
340
+print(x_test_predict)
341
+exit(0)
426
 
342
 
427
 def plotData4():
343
 def plotData4():
428
     NumFeaturesToPlot=len(indexesToPlot)
344
     NumFeaturesToPlot=len(indexesToPlot)

Powered by TurnKey Linux.