|
|
@@ -26,6 +26,7 @@ parser = OptionParser()
|
|
26
|
26
|
parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
|
|
27
|
27
|
parser.add_option("-n", "--timesteps", dest="timesteps", help="TIME STEPS ", default=12)
|
|
28
|
28
|
parser.add_option("-r", "--transition", dest="transition", help="Includes transition data (false)", default=False, action="store_true")
|
|
|
29
|
+parser.add_option("-p", "--plot", dest="plot", help="Only plot data (false)", default=False, action="store_true")
|
|
29
|
30
|
#parser.add_option("-f", "--thresholdfactor", dest="TF", help="Threshold Factor ", default=1.4)
|
|
30
|
31
|
# threshold makes no sense when classifying, becaues we apply many models and decide class for the less MSE
|
|
31
|
32
|
|
|
|
@@ -52,17 +53,21 @@ datafiles[0][0]=['2024-08-07_5_','2024-08-08_5_','2025-01-25_5_','2025-01-26_5_'
|
|
52
|
53
|
datafiles[0][1]=['2024-12-11_5_', '2024-12-12_5_','2024-12-13_5_']
|
|
53
|
54
|
datafiles[0][2]=['2024-12-18_5_','2024-12-21_5_','2024-12-22_5_','2024-12-23_5_','2024-12-24_5_']
|
|
54
|
55
|
datafiles[0][3]=['2024-12-28_5_','2024-12-29_5_','2024-12-30_5_']
|
|
|
56
|
+#datafiles[0][4]=['2025-02-05_5_','2025-02-10_5_']
|
|
55
|
57
|
|
|
56
|
58
|
if options.transition:
|
|
57
|
59
|
datafiles[1][0]=['2025-01-27_5_','2025-01-28_5_']
|
|
58
|
60
|
datafiles[1][1]=['2024-12-14_5_','2024-12-15_5_','2024-12-16_5_'] # with TRANSITION
|
|
59
|
61
|
datafiles[1][2]=['2024-12-17_5_','2024-12-19_5_','2024-12-25_5_','2024-12-26_5_'] # with TRANSITION
|
|
60
|
62
|
datafiles[1][3]=['2024-12-27_5_','2024-12-31_5_','2025-01-01_5_'] # with TRANSITION
|
|
|
63
|
+ #datafiles[1][4]=['2025-02-05_5_','2025-02-10_5_']
|
|
|
64
|
+
|
|
61
|
65
|
else:
|
|
62
|
66
|
datafiles[1][0]=['2025-01-27_5_','2025-01-28_5_']
|
|
63
|
67
|
datafiles[1][1]=['2024-12-14_5_','2024-12-15_5_']
|
|
64
|
68
|
datafiles[1][2]=['2024-12-19_5_','2024-12-25_5_','2024-12-26_5_']
|
|
65
|
69
|
datafiles[1][3]=['2024-12-31_5_','2025-01-01_5_']
|
|
|
70
|
+ #datafiles[1][4]=['2025-02-05_5_','2025-02-10_5_']
|
|
66
|
71
|
|
|
67
|
72
|
|
|
68
|
73
|
#datafiles[0][4]=['2025-02-05_5_']
|
|
|
@@ -175,6 +180,7 @@ for i in range(NumberOfFailures+1):
|
|
175
|
180
|
(dataTrainNorm[i],dataTestNorm[i])=normalize2(dataTrain[i],dataTest[i])
|
|
176
|
181
|
|
|
177
|
182
|
|
|
|
183
|
+
|
|
178
|
184
|
NumFilters=64
|
|
179
|
185
|
KernelSize=7
|
|
180
|
186
|
DropOut=0.2
|
|
|
@@ -269,9 +275,45 @@ for i in featuresToPlot:
|
|
269
|
275
|
indexesToPlot.append(features.index(i))
|
|
270
|
276
|
|
|
271
|
277
|
|
|
|
278
|
+def plotData():
|
|
|
279
|
+ NumFeaturesToPlot=len(indexesToPlot)
|
|
|
280
|
+ plt.rcParams.update({'font.size': 16})
|
|
|
281
|
+ fig, axes = plt.subplots(
|
|
|
282
|
+ nrows=NumFeaturesToPlot, ncols=1, figsize=(15, 10), dpi=80, facecolor="w", edgecolor="k",sharex=True
|
|
|
283
|
+ )
|
|
|
284
|
+ for i in range(NumFeaturesToPlot):
|
|
|
285
|
+ init=0
|
|
|
286
|
+ end=testRanges[0][1]
|
|
|
287
|
+ for j in range(NumberOfFailures+1):
|
|
|
288
|
+ if NumFeaturesToPlot==1:
|
|
|
289
|
+ axes.plot(range(init,end),x_test[testRanges[j][0]:testRanges[j][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="Class "+str(j), color=colorline[j],linewidth=1)
|
|
|
290
|
+ else:
|
|
|
291
|
+ axes[i].plot(range(init,end),x_test[testRanges[j][0]:testRanges[j][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="Class "+str(j), color=colorline[j],linewidth=1)
|
|
|
292
|
+ if j<NumberOfFailures:
|
|
|
293
|
+ init=end
|
|
|
294
|
+ end+=(testRanges[j+1][1]-testRanges[j+1][0])
|
|
|
295
|
+
|
|
|
296
|
+
|
|
|
297
|
+ s=''
|
|
|
298
|
+ s+=featureNames[features[indexesToPlot[i]]]
|
|
|
299
|
+ s+=' '+unitNames[features[indexesToPlot[i]]]
|
|
|
300
|
+ if NumFeaturesToPlot==1:
|
|
|
301
|
+ axes.set_ylabel(s)
|
|
|
302
|
+ axes.grid()
|
|
|
303
|
+ else:
|
|
|
304
|
+ axes[i].set_ylabel(s)
|
|
|
305
|
+ axes[i].grid()
|
|
|
306
|
+
|
|
|
307
|
+ if NumFeaturesToPlot==1:
|
|
|
308
|
+ axes.legend(ncol=4,loc=(0.1,0.98))
|
|
|
309
|
+ else:
|
|
|
310
|
+ axes[0].legend(ncol=4,loc=(0.1,0.98))
|
|
|
311
|
+ plt.show()
|
|
|
312
|
+
|
|
272
|
313
|
|
|
273
|
314
|
# 2nd scenario. Go over anomalies and classify it by less error
|
|
274
|
315
|
datalist=[dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3]]
|
|
|
316
|
+#datalist=[dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]]
|
|
275
|
317
|
x_test=create_sequences(datalist[0],int(options.timesteps))
|
|
276
|
318
|
for i in range(1,len(datalist)):
|
|
277
|
319
|
x_test=np.vstack((x_test,create_sequences(datalist[i],int(options.timesteps))))
|
|
|
@@ -283,6 +325,11 @@ for i in range(len(datalist)):
|
|
283
|
325
|
testRanges.append([r,r+datalist[i].shape[0]-int(options.timesteps)+1])
|
|
284
|
326
|
r+=datalist[i].shape[0]-int(options.timesteps)+1
|
|
285
|
327
|
|
|
|
328
|
+if options.plot:
|
|
|
329
|
+# Only plot data and exit
|
|
|
330
|
+ plotData()
|
|
|
331
|
+ exit(0)
|
|
|
332
|
+
|
|
286
|
333
|
testClasses=[0,1,2,3]
|
|
287
|
334
|
|
|
288
|
335
|
if not len(testClasses)==len(testRanges):
|
|
|
@@ -409,4 +456,27 @@ def anomalyMetric(classes,testranges,testclasses):
|
|
409
|
456
|
anomalyMetric(classes,testRanges,testClasses)
|
|
410
|
457
|
plotData4()
|
|
411
|
458
|
|
|
|
459
|
+# Compute delay until correct detection for a list of ranges (when transition data exists)
|
|
|
460
|
+def computeDelay(l,classes,testRanges,testClasses):
|
|
|
461
|
+ d=np.zeros(len(l))
|
|
|
462
|
+ NoFailsInARow=4
|
|
|
463
|
+ ind=0
|
|
|
464
|
+ for i in l:
|
|
|
465
|
+ start=testRanges[i][0]
|
|
|
466
|
+ count=0
|
|
|
467
|
+ while start<testRanges[i][1]:
|
|
|
468
|
+ start+=1
|
|
|
469
|
+ if classes[start]==testClasses[i]:
|
|
|
470
|
+ count+=1
|
|
|
471
|
+ if count==NoFailsInARow:
|
|
|
472
|
+ count=0
|
|
|
473
|
+ #print(start,start-testRanges[i][0]-NoFailsInARow+timesteps)
|
|
|
474
|
+ d[ind]=start-testRanges[i][0]-NoFailsInARow+timesteps
|
|
|
475
|
+ break
|
|
|
476
|
+ ind+=1
|
|
|
477
|
+ return(d.mean())
|
|
|
478
|
+
|
|
|
479
|
+d=computeDelay([2,3],classes,testRanges,testClasses)
|
|
|
480
|
+print("mean delay: ",d)
|
|
|
481
|
+
|
|
412
|
482
|
|