cesar пре 2 недеља
родитељ
комит
7d78fc1832
2 измењених фајлова са 809 додато и 12 уклоњено
  1. 54
    12
      v1_unsupervised.py
  2. 755
    0
      v4.py

+ 54
- 12
v1_unsupervised.py Прегледај датотеку

@@ -16,6 +16,7 @@ from optparse import OptionParser
16 16
 import copy
17 17
 import pickle
18 18
 from tslearn.clustering import TimeSeriesKMeans
19
+from tslearn.neighbors import KNeighborsTimeSeries 
19 20
 from collections import Counter
20 21
 
21 22
 parser = OptionParser()
@@ -115,9 +116,14 @@ for i in range(NumberOfFailures+1):
115 116
 # Test data is: last 1/3 of data 
116 117
 dataTrain=[]
117 118
 dataTest=[]
119
+NumberOfSamplesForTest=300
120
+
118 121
 for i in range(NumberOfFailures+1):
119 122
     dataTrain.append(dataframe[i].values[0:int(dataframe[i].shape[0]*2/3),:])
120
-    dataTest.append(dataframe[i].values[int(dataframe[i].shape[0]*2/3):,:])
123
+    if NumberOfSamplesForTest==0:  # Take all
124
+        dataTest.append(dataframe[i].values[int(dataframe[i].shape[0]*2/3):,:])
125
+    else:
126
+        dataTest.append(dataframe[i].values[int(dataframe[i].shape[0]*2/3):int(dataframe[i].shape[0]*2/3)+NumberOfSamplesForTest,:])
121 127
 
122 128
 # Calculate means and stdev
123 129
 a=dataTrain[0]
@@ -172,7 +178,7 @@ for i in range(1,NumberOfFailures+1):
172 178
 
173 179
 xtrain=create_sequences(X,timesteps)
174 180
 
175
-km = TimeSeriesKMeans(n_clusters=NumberOfFailures+1, metric="dtw")
181
+km = TimeSeriesKMeans(n_clusters=NumberOfFailures+1, metric="dtw", random_state=0)
176 182
 modelpath="model_v1_unsupervised_"+str(timesteps)+listToString(features)+".pk"
177 183
 if options.train:
178 184
     km.fit(xtrain)
@@ -185,9 +191,6 @@ colorline=['violet','lightcoral','cyan','lime','grey']
185 191
 colordot=['darkviolet','red','blue','green','black']
186 192
 
187 193
 
188
-
189
-
190
-
191 194
 featuresToPlot=features
192 195
 indexesToPlot=[]
193 196
 for i in featuresToPlot:
@@ -195,7 +198,6 @@ for i in featuresToPlot:
195 198
 
196 199
 
197 200
 def plot(data,ranges):
198
-
199 201
     km.fit_predict(data)
200 202
 # Expand data to plot with the timesteps samples of last sample
201 203
     datatoplot=data[:,0,:]
@@ -299,11 +301,9 @@ xtest=create_sequences(X,timesteps)
299 301
 
300 302
 
301 303
 
302
-km.fit_predict(xtest)
303
-plot(xtest,Ranges)
304 304
 
305
-exit(0)
306 305
 def anomalyMetric(labels,ranges):
306
+    # Takes ONLY the firsts segments of Ranges
307 307
     # FP, TP: false/true positive
308 308
     # TN, FN: true/false negative
309 309
     # Sensitivity (recall): probab failure detection if data is fail: TP/(TP+FN)
@@ -312,18 +312,60 @@ def anomalyMetric(labels,ranges):
312 312
 
313 313
     lab=[]    #   Labels are assigned randomly by classifer
314 314
     TP=[]
315
+    FN=[]
316
+    TPFP=[]
315 317
     for i in range(NumberOfFailures+1):
316 318
         TP.append([])
319
+        FN.append([])
320
+        TPFP.append([])
317 321
         b=Counter(labels[ranges[i][0]:ranges[i][1]])
318 322
         lab.append(b.most_common(1)[0][0])
319 323
 
320
-    print(lab)
321
-    #for i in range(NumberOfFailures+1):
324
+    for i in range(NumberOfFailures+1):
325
+        counttp=0
326
+        countfn=0
327
+        for j in range(ranges[i][0],ranges[i][1]-timesteps):
328
+            if lab.index(labels[j])==i:
329
+                counttp+=1
330
+            else:
331
+                countfn+=1
332
+        TP[i]=counttp
333
+        FN[i]=countfn
322 334
 
335
+    for i in range(NumberOfFailures+1):
336
+        count=0
337
+        for ii in range(NumberOfFailures+1):
338
+            for j in range(ranges[ii][0],ranges[ii][1]-timesteps):
339
+                if lab.index(labels[j])==i:
340
+                    count+=1
341
+            TPFP[i]=count
342
+
343
+    segmentLength=[]
344
+    for i in range(NumberOfFailures+1):
345
+        segmentLength.append(ranges[i][1]-timesteps-ranges[i][0])
346
+    totalSegmentLength=0
347
+    for i in range(NumberOfFailures+1):
348
+        totalSegmentLength+=segmentLength[i]
323 349
 
324 350
 
351
+    Sensitivity=0
352
+    Precision=0
353
+    for i in range(NumberOfFailures+1):
354
+        Sensitivity+=TP[i]/(TP[i]+FN[i])*segmentLength[i]/totalSegmentLength
355
+        Precision+=TP[i]/(TPFP[i])*segmentLength[i]/totalSegmentLength
356
+
357
+
358
+    print(lab)
359
+    print("TP: ",TP)
360
+    print("FN: ",FN)
361
+    print("TPFP: ",TPFP)
362
+    print("Sensitivity: ",Sensitivity)
363
+    print("Precision: ",Precision)
364
+    print("F1-Score: ",2*Precision*Sensitivity/(Sensitivity+Precision))
325 365
 
326
-anomalyMetric(km.labels_,Ranges)
327 366
 
367
+km.fit_predict(xtest)
368
+anomalyMetric(km.labels_,Ranges)
369
+plot(xtest,Ranges)
328 370
 
329 371
 

+ 755
- 0
v4.py Прегледај датотеку

@@ -0,0 +1,755 @@
1
+# Csar Fdez, UdL, 2025
2
+# Changes from v1:   Normalization 
3
+# IN v1, each failure type has its own normalization pars (mean and stdevs)
4
+# In v2, mean and stdev is the same for all data
5
+# v3.py trains the models looping in TIME_STEPS (4,8,12,16,20,24,....) finding the optimal Threshold factor
6
+# Changes in v4:  see comments on v1_unsupervised.py
7
+# Be careful with v0_unsupervised  and all versions for supervised.
8
+# because dataTrain is not stacke before create_sequences,  so, 
9
+#  the sets are not aligned in time
10
+
11
+# Optimizxation of threshold factor changed, bacause is based on F1-Score  AKI
12
+
13
+import pandas as pd
14
+import matplotlib.pyplot as plt
15
+import datetime
16
+import numpy as np
17
+import keras
18
+import os.path
19
+from keras import layers
20
+from optparse import OptionParser
21
+import copy
22
+import pickle
23
+
24
+
25
+parser = OptionParser()
26
+parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
27
+parser.add_option("-o", "--optimizetf", dest="optimizetf", help="Optimzes Threshold Factor (false)", default=False, action="store_true")
28
+parser.add_option("-n", "--timesteps", dest="timesteps", help="TIME STEPS ", default=12)
29
+parser.add_option("-f", "--thresholdfactor", dest="TF", help="Threshold Factor ", default=1.4)
30
+
31
+(options, args) = parser.parse_args()
32
+
33
+
34
+# data files arrays. Index:
35
+# 0.  No failure
36
+# 1.  Blocked evaporator
37
+# 2.   Full Blocked condenser
38
+# 3.   Partial Blocked condenser
39
+# 4   Fan condenser not working
40
+# 5.  Open door
41
+
42
+
43
+NumberOfFailures=4  # So far, we have only data for the first 4 types of failures
44
+datafiles=[]
45
+for i in range(NumberOfFailures+1):
46
+    datafiles.append([])
47
+
48
+# Next set of ddata corresponds to Freezer, SP=-26
49
+datafiles[0]=['2024-08-07_5_','2024-08-08_5_','2025-01-25_5_','2025-01-26_5_','2025-01-27_5_','2025-01-28_5_'] 
50
+datafiles[1]=['2024-12-11_5_', '2024-12-12_5_','2024-12-13_5_','2024-12-14_5_','2024-12-15_5_'] 
51
+#datafiles[1]=['2024-12-17_5_','2024-12-16_5_','2024-12-11_5_', '2024-12-12_5_','2024-12-13_5_','2024-12-14_5_','2024-12-15_5_'] #   This have transitions
52
+datafiles[2]=['2024-12-18_5_','2024-12-19_5_'] 
53
+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_'] 
54
+datafiles[4]=['2024-12-28_5_','2024-12-29_5_','2024-12-30_5_','2024-12-31_5_','2025-01-01_5_'] 
55
+#datafiles[4]=['2024-12-27_5_','2024-12-28_5_','2024-12-29_5_','2024-12-30_5_','2024-12-31_5_','2025-01-01_5_']  #   This have transitions
56
+
57
+#datafiles[4]=[] 
58
+
59
+# Features suggested by Xavier
60
+# Care with 'tc s3' because on datafiles[0] is always nulll
61
+# Seems to be incoropored in new tests
62
+
63
+#r1s5 supply air flow temperature
64
+#r1s1 inlet evaporator temperature
65
+#r1s4 condenser outlet
66
+
67
+# VAriables r1s4 and pa1 apiii  may not exists in cloud controlers
68
+
69
+
70
+features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
71
+features=['r1 s1','r1 s4','r1 s5']
72
+featureNames={}
73
+featureNames['r1 s1']='$T_{evap}$'
74
+featureNames['r1 s4']='$T_{cond}$'
75
+featureNames['r1 s5']='$T_{air}$'
76
+featureNames['pa1 apiii']='$P_{elec}$'
77
+
78
+unitNames={}
79
+unitNames['r1 s1']='$(^{o}C)$'
80
+unitNames['r1 s4']='$(^{o}C)$'
81
+unitNames['r1 s5']='$(^{o}C)$'
82
+unitNames['pa1 apiii']='$(W)$'
83
+
84
+
85
+#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']
86
+
87
+#features=['r2 s2', 'tc s1','r1 s10','r1 s6','r2 s8']
88
+
89
+NumFeatures=len(features)
90
+
91
+df_list=[]
92
+for i in range(NumberOfFailures+1):
93
+    df_list.append([])
94
+
95
+for i in range(NumberOfFailures+1):
96
+    dftemp=[]
97
+    for f in datafiles[i]:
98
+        print("                 ", f)
99
+        #df1 = pd.read_csv('./data/'+f+'.csv', parse_dates=['datetime'], dayfirst=True, index_col='datetime')
100
+        df1 = pd.read_csv('./data/'+f+'.csv')
101
+        dftemp.append(df1)
102
+    df_list[i]=pd.concat(dftemp)
103
+
104
+
105
+# subsampled to 5'  =  30 * 10"
106
+# We consider smaples every 5' because in production, we will only have data at this frequency
107
+subsamplingrate=30
108
+
109
+dataframe=[]
110
+for i in range(NumberOfFailures+1):
111
+    dataframe.append([])
112
+
113
+for i in range(NumberOfFailures+1):
114
+    datalength=df_list[i].shape[0]
115
+    dataframe[i]=df_list[i].iloc[range(0,datalength,subsamplingrate)][features]
116
+    dataframe[i].reset_index(inplace=True,drop=True)
117
+    dataframe[i].dropna(inplace=True)
118
+
119
+
120
+# Train data is first 2/3 of data
121
+# Test data is: last 1/3 of data 
122
+dataTrain=[]
123
+dataTest=[]
124
+for i in range(NumberOfFailures+1):
125
+    dataTrain.append(dataframe[i].values[0:int(dataframe[i].shape[0]*2/3),:])
126
+    dataTest.append(dataframe[i].values[int(dataframe[i].shape[0]*2/3):,:])
127
+
128
+# Calculate means and stdev
129
+a=dataTrain[0]
130
+for i in range(1,NumberOfFailures+1):
131
+    a=np.vstack((a,dataTrain[i]))
132
+
133
+means=a.mean(axis=0) 
134
+stdevs=a.std(axis=0)
135
+def normalize2(train,test):
136
+    return( (train-means)/stdevs, (test-means)/stdevs )
137
+
138
+dataTrainNorm=[]
139
+dataTestNorm=[]
140
+for i in range(NumberOfFailures+1):
141
+    dataTrainNorm.append([])
142
+    dataTestNorm.append([])
143
+
144
+for i in range(NumberOfFailures+1):
145
+    (dataTrainNorm[i],dataTestNorm[i])=normalize2(dataTrain[i],dataTest[i])
146
+
147
+def plotData():    
148
+    fig, axes = plt.subplots(
149
+        nrows=NumberOfFailures+1, ncols=2, figsize=(15, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
150
+    )
151
+    for i in range(NumberOfFailures+1):
152
+        axes[i][0].plot(np.concatenate((dataTrainNorm[i][:,0],dataTestNorm[i][:,0])),label="Fail "+str(i)+",  feature 0")
153
+        axes[i][1].plot(np.concatenate((dataTrainNorm[i][:,1],dataTestNorm[i][:,1])),label="Fail "+str(i)+",  feature 1")
154
+    #axes[1].legend()
155
+    #axes[0].set_ylabel(features[0])
156
+    #axes[1].set_ylabel(features[1])
157
+    plt.show()
158
+
159
+#plotData()
160
+#exit(0)
161
+
162
+
163
+NumFilters=64
164
+KernelSize=7
165
+DropOut=0.2
166
+ThresholdFactor=1.4
167
+def create_sequences(values, time_steps):
168
+    output = []
169
+    for i in range(len(values) - time_steps + 1):
170
+        output.append(values[i : (i + time_steps)])
171
+    return np.stack(output)
172
+
173
+def AtLeastOneTrue(x):
174
+    for i in range(NumFeatures):
175
+        if x[i]:
176
+            return True
177
+    return False
178
+
179
+def anomalyMetric(th,ts,testList):  # first of list is non failure data
180
+    # FP, TP: false/true positive
181
+    # TN, FN: true/false negative
182
+    # Sensitivity (recall): probab failure detection if data is fail: TP/(TP+FN)
183
+    # Specificity: true negative ratio given  data is OK: TN/(TN+FP)
184
+    # Accuracy: Rate of correct predictions:  (TN+TP)/(TN+TP+FP+FN)
185
+    # Precision: Rate of positive results:  TP/(TP+FP)  
186
+    # F1-score: predictive performance measure: 2*Precision*Sensitity/(Precision+Sensitity)
187
+    # F2-score: predictive performance measure:  2*Specificity*Sensitity/(Specificity+Sensitity)
188
+
189
+    x_test = create_sequences(testList[0],ts)
190
+    x_test_pred = model.predict(x_test)
191
+    test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
192
+    anomalies = test_mae_loss > th
193
+    count=0
194
+    for i in range(anomalies.shape[0]):
195
+        if AtLeastOneTrue(anomalies[i]):
196
+            count+=1
197
+    FP=count
198
+    TN=anomalies.shape[0]-count
199
+    count=0
200
+    TP=np.zeros((NumberOfFailures))
201
+    FN=np.zeros((NumberOfFailures))
202
+    Sensitivity=np.zeros((NumberOfFailures))
203
+    Precision=np.zeros((NumberOfFailures))
204
+    for i in range(1,len(testList)):
205
+        x_test = create_sequences(testList[i],ts)
206
+        x_test_pred = model.predict(x_test)
207
+        test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
208
+        anomalies = test_mae_loss > th
209
+        count=0
210
+        for j in range(anomalies.shape[0]):
211
+            if AtLeastOneTrue(anomalies[j]):
212
+                count+=1
213
+        TP[i-1] = count
214
+        FN[i-1] = anomalies.shape[0]-count
215
+        Sensitivity[i-1]=TP[i-1]/(TP[i-1]+FN[i-1])
216
+        Precision[i-1]=TP[i-1]/(TP[i-1]+FP)
217
+
218
+    GlobalSensitivity=TP.sum()/(TP.sum()+FN.sum())
219
+    Specificity=TN/(TN+FP)
220
+    Accuracy=(TN+TP.sum())/(TN+TP.sum()+FP+FN.sum())
221
+    GlobalPrecision=TP.sum()/(TP.sum()+FP)
222
+    F1Score= 2*GlobalPrecision*GlobalSensitivity/(GlobalPrecision+GlobalSensitivity)
223
+    F2Score = 2*Specificity*GlobalSensitivity/(Specificity+GlobalSensitivity)
224
+
225
+    print("Sensitivity: ",Sensitivity)
226
+    print("Global Sensitivity: ",GlobalSensitivity)
227
+    #print("Precision: ",Precision)
228
+    #print("Global Precision: ",GlobalPrecision)
229
+    print("Specifity: ",Specificity)
230
+    #print("Accuracy: ",Accuracy)
231
+    #print("F1Score: ",F1Score)
232
+    print("F2Score: ",F2Score)
233
+    #print("FP: ",FP)
234
+    #return Sensitivity+Specifity
235
+    return F2Score
236
+
237
+FScoreHash={}
238
+threshold={}
239
+def getFScore(timestep,datalist):
240
+    FScoreHash[timestep]=[]
241
+    # plots FSCore as a function of Threshold  Factor
242
+    tf=0.3
243
+    while tf<8:
244
+        th=threshold[timestep]*tf
245
+        r=anomalyMetric(th,timestep,datalist)
246
+        FScoreHash[timestep].append([tf,r])
247
+        if tf<2:
248
+            tf+=0.1
249
+        else:
250
+            tf+=0.5
251
+
252
+
253
+def plotFScore(FS):
254
+    plt.rcParams.update({'font.size': 16})
255
+    fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(14, 10), dpi=80, facecolor="w", edgecolor="k")
256
+    for k in FS.keys():
257
+        ar=np.array((FS[k]))
258
+        axes.plot(ar[:,0],ar[:,1],label="$ns=$"+str(k),linewidth=3)
259
+    axes.set_xlabel("Threshold factor ($tf$)")
260
+    axes.set_ylabel("FScore")
261
+    axes.legend()
262
+    axes.grid()
263
+    s='['
264
+    for i in range(len(features)):
265
+        s+=featureNames[features[i]]
266
+        if i < len(features)-1:
267
+            s+=', '
268
+    s+=']'
269
+    plt.title(s)
270
+    plt.show()
271
+
272
+def listToString(l):
273
+    r=''
274
+    for i in l:
275
+        r+=str(i)
276
+    return(r.replace(' ',''))
277
+
278
+if options.train:   #  Train not needed to be changed
279
+    for timesteps in range(4,21,4):
280
+        x_train=[]
281
+        for i in range(NumberOfFailures+1):
282
+            x_train.append(create_sequences(dataTrainNorm[i],timesteps))
283
+
284
+        model = keras.Sequential(
285
+            [
286
+                layers.Input(shape=(x_train[0].shape[1], x_train[0].shape[2])),
287
+                layers.Conv1D(
288
+                    filters=NumFilters,
289
+                    kernel_size=KernelSize,
290
+                    padding="same",
291
+                    strides=2,
292
+                    activation="relu",
293
+                ),
294
+                layers.Dropout(rate=DropOut),
295
+                layers.Conv1D(
296
+                    filters=int(NumFilters/2),
297
+                    kernel_size=KernelSize,
298
+                    padding="same",
299
+                    strides=2,
300
+                    activation="relu",
301
+                ),
302
+                layers.Conv1DTranspose(
303
+                    filters=int(NumFilters/2),
304
+                    kernel_size=KernelSize,
305
+                    padding="same",
306
+                    strides=2,
307
+                    activation="relu",
308
+                ),
309
+                layers.Dropout(rate=DropOut),
310
+                layers.Conv1DTranspose(
311
+                    filters=NumFilters,
312
+                    kernel_size=KernelSize,
313
+                    padding="same",
314
+                    strides=2,
315
+                    activation="relu",
316
+                ),
317
+                layers.Conv1DTranspose(filters=x_train[i].shape[2], kernel_size=KernelSize, padding="same"),
318
+            ]
319
+        )
320
+        model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
321
+        model.summary()
322
+        path_checkpoint="model_noclass_v2_"+str(timesteps)+listToString(features)+"_checkpoint.weights.h5"
323
+        es_callback=keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
324
+        modelckpt_callback=keras.callbacks.ModelCheckpoint( monitor="val_loss", filepath=path_checkpoint, verbose=1, save_weights_only=True, save_best_only=True,)
325
+
326
+        history=model.fit( x_train[0], x_train[0], epochs=400, batch_size=128, validation_split=0.3, callbacks=[  es_callback, modelckpt_callback      ],)
327
+
328
+        x_train_pred=model.predict(x_train[0])
329
+        train_mae_loss=np.mean(np.abs(x_train_pred - x_train[0]), axis=1)
330
+        threshold[timesteps]=np.max(train_mae_loss,axis=0)
331
+    file = open('threshold'+listToString(features)+'.pk', 'wb')
332
+    pickle.dump(threshold, file)
333
+    file.close()
334
+    exit(0)
335
+else:
336
+    file = open('threshold'+listToString(features)+'.pk', 'rb')
337
+    threshold=pickle.load(file)
338
+    file.close()
339
+
340
+
341
+    x_train=[]
342
+    for i in range(NumberOfFailures+1):
343
+        x_train.append(create_sequences(dataTrainNorm[i],int(options.timesteps)))
344
+
345
+    model = keras.Sequential(
346
+        [
347
+            layers.Input(shape=(x_train[0].shape[1], x_train[0].shape[2])),
348
+            layers.Conv1D(
349
+                filters=NumFilters,
350
+                kernel_size=KernelSize,
351
+                padding="same",
352
+                strides=2,
353
+                activation="relu",
354
+            ),
355
+            layers.Dropout(rate=DropOut),
356
+            layers.Conv1D(
357
+                filters=int(NumFilters/2),
358
+                kernel_size=KernelSize,
359
+                padding="same",
360
+                strides=2,
361
+                activation="relu",
362
+            ),
363
+            layers.Conv1DTranspose(
364
+                filters=int(NumFilters/2),
365
+                kernel_size=KernelSize,
366
+                padding="same",
367
+                strides=2,
368
+                activation="relu",
369
+            ),
370
+            layers.Dropout(rate=DropOut),
371
+            layers.Conv1DTranspose(
372
+                filters=NumFilters,
373
+                kernel_size=KernelSize,
374
+                padding="same",
375
+                strides=2,
376
+                activation="relu",
377
+            ),
378
+            layers.Conv1DTranspose(filters=x_train[i].shape[2], kernel_size=KernelSize, padding="same"),
379
+        ]
380
+    )
381
+    model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
382
+    model.summary()
383
+
384
+    
385
+    if options.optimizetf:
386
+        for timesteps in range(4,21,4):
387
+            path_checkpoint="model_noclass_v2_"+str(timesteps)+listToString(features)+"_checkpoint.weights.h5"
388
+            es_callback=keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
389
+            modelckpt_callback=keras.callbacks.ModelCheckpoint( monitor="val_loss", filepath=path_checkpoint, verbose=1, save_weights_only=True, save_best_only=True,)
390
+            model.load_weights(path_checkpoint)
391
+            getFScore(timesteps,[dataTestNorm[0],dataTrainNorm[1],dataTrainNorm[2],dataTrainNorm[3],dataTrainNorm[4]])
392
+        file = open('FScore'+listToString(features)+'.pk', 'wb')
393
+        pickle.dump(FScoreHash, file)
394
+        file.close()
395
+
396
+
397
+    path_checkpoint="model_noclass_v2_"+str(options.timesteps)+listToString(features)+"_checkpoint.weights.h5"
398
+    es_callback=keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
399
+    modelckpt_callback=keras.callbacks.ModelCheckpoint( monitor="val_loss", filepath=path_checkpoint, verbose=1, save_weights_only=True, save_best_only=True,)
400
+    model.load_weights(path_checkpoint)
401
+
402
+
403
+    file = open('FScore'+listToString(features)+'.pk', 'rb')
404
+    FS=pickle.load(file)
405
+    file.close()
406
+
407
+
408
+    #plotFScore(FS)
409
+    #exit(0)
410
+
411
+TIME_STEPS=int(options.timesteps)
412
+#  1st scenario. Detect only anomaly.  Later, we will classiffy it
413
+# Test data=  testnormal + testfail1 + testtail2 + testfail3 + testfail4 + testnormal
414
+#d=np.vstack((dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4],dataTestNorm[0]))
415
+# For Failure data, we can use Train data becasue not used for training and includes the firsts samples
416
+#datalist=[dataTestNorm[0],dataTrainNorm[1],dataTrainNorm[2],dataTrainNorm[3],dataTrainNorm[4]]
417
+datalist=[dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4]]
418
+d=np.vstack((datalist))
419
+
420
+x_test = create_sequences(d,int(options.timesteps))
421
+x_test_pred = model.predict(x_test)
422
+test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
423
+
424
+
425
+# Define ranges for plotting in different colors
426
+testRanges=[]
427
+
428
+r=0
429
+for i in range(len(datalist)):
430
+    testRanges.append([r,r+datalist[i].shape[0]])
431
+    r+=datalist[i].shape[0]
432
+
433
+#r=dataTestNorm[0].shape[0]
434
+#testRanges.append([0,r])
435
+#for i in range(1,NumberOfFailures+1):
436
+#    rnext=r+dataTrainNorm[i].shape[0]
437
+#    testRanges.append([r,rnext] )
438
+#    r=rnext
439
+
440
+# Drop the last TIME_STEPS for plotting
441
+testRanges[NumberOfFailures][1]=testRanges[NumberOfFailures][1]-TIME_STEPS
442
+
443
+
444
+anomalies = test_mae_loss > threshold[int(options.timesteps)]*float(options.TF)
445
+anomalous_data_indices = []
446
+for i in range(anomalies.shape[0]):
447
+    if AtLeastOneTrue(anomalies[i]):
448
+    #if anomalies[i][0] or anomalies[i][1] or anomalies[i][2] or anomalies[i][3]:
449
+        anomalous_data_indices.append(i)
450
+
451
+# Let's plot some features
452
+
453
+colorline=['violet','lightcoral','cyan','lime','grey']
454
+colordot=['darkviolet','red','blue','green','black']
455
+
456
+#featuresToPlot=['r1 s1','r1 s2','r1 s3','pa1 apiii']
457
+featuresToPlot=features
458
+
459
+indexesToPlot=[]
460
+for i in featuresToPlot:
461
+    indexesToPlot.append(features.index(i))
462
+
463
+def plotData3():
464
+    NumFeaturesToPlot=len(indexesToPlot)
465
+    plt.rcParams.update({'font.size': 16})
466
+    fig, axes = plt.subplots(
467
+        nrows=NumFeaturesToPlot, ncols=1, figsize=(15, 10), dpi=80, facecolor="w", edgecolor="k",sharex=True
468
+    )
469
+    for i in range(NumFeaturesToPlot):
470
+        x=[]
471
+        y=[]
472
+        for k in anomalous_data_indices:
473
+            if (k+TIME_STEPS)<x_test.shape[0]:
474
+                x.append(k+TIME_STEPS)
475
+                y.append(x_test[k+TIME_STEPS,0,indexesToPlot[i]]*stdevs[i]+means[i])
476
+        axes[i].plot(x,y ,color='black',marker='.',linewidth=0,label="Fail detection" )
477
+
478
+
479
+        init=0
480
+        end=testRanges[0][1]
481
+        axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="No fail")
482
+        init=end
483
+        end+=(testRanges[1][1]-testRanges[1][0])
484
+        for j in range(1,NumberOfFailures+1):
485
+            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)
486
+            if j<NumberOfFailures:
487
+                init=end
488
+                end+=(testRanges[j+1][1]-testRanges[j+1][0])
489
+
490
+
491
+        if i==(NumFeatures-1):
492
+            axes[i].legend(loc='right')
493
+        s=''
494
+        s+=featureNames[features[indexesToPlot[i]]]
495
+        s+=' '+unitNames[features[indexesToPlot[i]]]
496
+        axes[i].set_ylabel(s)
497
+        axes[i].grid()
498
+    axes[NumFeaturesToPlot-1].set_xlabel("Sample number")
499
+    plt.show()
500
+
501
+
502
+anomalyMetric(threshold[int(options.timesteps)]*float(options.TF), int(options.timesteps),datalist)
503
+
504
+
505
+plotData3()
506
+
507
+
508
+def plotData5():
509
+    model1 = keras.Sequential(
510
+        [
511
+            layers.Input(shape=(4, 3)),
512
+            layers.Conv1D(
513
+                filters=NumFilters,
514
+                kernel_size=KernelSize,
515
+                padding="same",
516
+                strides=2,
517
+                activation="relu",
518
+            ),
519
+            layers.Dropout(rate=DropOut),
520
+            layers.Conv1D(
521
+                filters=int(NumFilters/2),
522
+                kernel_size=KernelSize,
523
+                padding="same",
524
+                strides=2,
525
+                activation="relu",
526
+            ),
527
+            layers.Conv1DTranspose(
528
+                filters=int(NumFilters/2),
529
+                kernel_size=KernelSize,
530
+                padding="same",
531
+                strides=2,
532
+                activation="relu",
533
+            ),
534
+            layers.Dropout(rate=DropOut),
535
+            layers.Conv1DTranspose(
536
+                filters=NumFilters,
537
+                kernel_size=KernelSize,
538
+                padding="same",
539
+                strides=2,
540
+                activation="relu",
541
+            ),
542
+            layers.Conv1DTranspose(filters=3, kernel_size=KernelSize, padding="same"),
543
+        ]
544
+    )
545
+    model1.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
546
+    model1.summary()
547
+    path_checkpoint="model_noclass_v2_"+str(4)+listToString(features)+"_checkpoint.weights.h5"
548
+    es_callback=keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
549
+    modelckpt_callback=keras.callbacks.ModelCheckpoint( monitor="val_loss", filepath=path_checkpoint, verbose=1, save_weights_only=True, save_best_only=True,)
550
+    model1.load_weights(path_checkpoint)
551
+
552
+    model2 = keras.Sequential(
553
+        [
554
+            layers.Input(shape=(20, 3)),
555
+            layers.Conv1D(
556
+                filters=NumFilters,
557
+                kernel_size=KernelSize,
558
+                padding="same",
559
+                strides=2,
560
+                activation="relu",
561
+            ),
562
+            layers.Dropout(rate=DropOut),
563
+            layers.Conv1D(
564
+                filters=int(NumFilters/2),
565
+                kernel_size=KernelSize,
566
+                padding="same",
567
+                strides=2,
568
+                activation="relu",
569
+            ),
570
+            layers.Conv1DTranspose(
571
+                filters=int(NumFilters/2),
572
+                kernel_size=KernelSize,
573
+                padding="same",
574
+                strides=2,
575
+                activation="relu",
576
+            ),
577
+            layers.Dropout(rate=DropOut),
578
+            layers.Conv1DTranspose(
579
+                filters=NumFilters,
580
+                kernel_size=KernelSize,
581
+                padding="same",
582
+                strides=2,
583
+                activation="relu",
584
+            ),
585
+            layers.Conv1DTranspose(filters=3, kernel_size=KernelSize, padding="same"),
586
+        ]
587
+    )
588
+    model2.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
589
+    model2.summary()
590
+    path_checkpoint="model_noclass_v2_"+str(20)+listToString(features)+"_checkpoint.weights.h5"
591
+    es_callback=keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
592
+    modelckpt_callback=keras.callbacks.ModelCheckpoint( monitor="val_loss", filepath=path_checkpoint, verbose=1, save_weights_only=True, save_best_only=True,)
593
+    model2.load_weights(path_checkpoint)
594
+
595
+    datalist=[dataTestNorm[0],dataTestNorm[3],dataTestNorm[2],dataTestNorm[1],dataTestNorm[4]]
596
+    d=np.vstack((datalist))
597
+    x_test = create_sequences(d,4)
598
+    x_test_pred = model1.predict(x_test)
599
+    test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
600
+    testRanges=[]
601
+    TIME_STEPS=4
602
+    r=0
603
+    for i in range(len(datalist)):
604
+        testRanges.append([r,r+datalist[i].shape[0]])
605
+        r+=datalist[i].shape[0]
606
+    testRanges[NumberOfFailures][1]=testRanges[NumberOfFailures][1]-TIME_STEPS
607
+    anomalies = test_mae_loss > threshold[4]*float(options.TF)
608
+    anomalous_data_indices = []
609
+    for i in range(anomalies.shape[0]):
610
+        if AtLeastOneTrue(anomalies[i]):
611
+            anomalous_data_indices.append(i)
612
+
613
+    plt.rcParams.update({'font.size': 16})
614
+    fig, axes = plt.subplots(
615
+        nrows=2, ncols=1, figsize=(15, 7), dpi=80, facecolor="w", edgecolor="k" , sharex=True
616
+ )
617
+    for i in range(1):
618
+        init=0
619
+        end=testRanges[0][1]
620
+        axes[i].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="No fail")
621
+        init=end
622
+        end+=(testRanges[1][1]-testRanges[1][0])
623
+        for j in range(1,NumberOfFailures+1):
624
+            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])
625
+            if j<NumberOfFailures:
626
+                init=end
627
+                end+=(testRanges[j+1][1]-testRanges[j+1][0])
628
+        x=[]
629
+        y=[]
630
+        for k in anomalous_data_indices:
631
+            if (k+TIME_STEPS)<x_test.shape[0]:
632
+                x.append(k+TIME_STEPS)
633
+                y.append(x_test[k+TIME_STEPS,0,indexesToPlot[i]]*stdevs[i]+means[i])
634
+        axes[i].plot(x,y ,color='black',marker='.',linewidth=0,label="Fail detection" )
635
+
636
+        if i==(NumFeatures-1):
637
+            axes[i].legend(loc='right')
638
+        s=''
639
+        s+=featureNames[features[indexesToPlot[i]]]
640
+        s+=' '+unitNames[features[indexesToPlot[i]]]
641
+        axes[i].set_ylabel(s)
642
+        axes[i].grid()
643
+
644
+
645
+    x_test = create_sequences(d,20)
646
+    x_test_pred = model2.predict(x_test)
647
+    test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
648
+    testRanges=[]
649
+    r=0
650
+    TIME_STEPS=20
651
+    for i in range(len(datalist)):
652
+        testRanges.append([r,r+datalist[i].shape[0]])
653
+        r+=datalist[i].shape[0]
654
+    testRanges[NumberOfFailures][1]=testRanges[NumberOfFailures][1]-TIME_STEPS
655
+    anomalies = test_mae_loss > threshold[20]*float(options.TF)
656
+    anomalous_data_indices = []
657
+    for i in range(anomalies.shape[0]):
658
+        if AtLeastOneTrue(anomalies[i]):
659
+            anomalous_data_indices.append(i)
660
+    print(testRanges)
661
+    for i in range(1):
662
+        init=0
663
+        end=testRanges[0][1]
664
+        axes[i+1].plot(range(init,end),x_test[testRanges[0][0]:testRanges[0][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="No fail")
665
+        init=end
666
+        end+=(testRanges[1][1]-testRanges[1][0])
667
+        for j in range(1,NumberOfFailures+1):
668
+            if j==1:
669
+                axes[i+1].plot(range(init,end),x_test[testRanges[j][0]:testRanges[j][1],0,indexesToPlot[i]]*stdevs[i]+means[i],label="Fail type 3", color=colorline[j-1])
670
+            else:
671
+                axes[i+1].plot(range(init,end),x_test[testRanges[j][0]:testRanges[j][1],0,indexesToPlot[i]]*stdevs[i]+means[i], color=colorline[j-1])
672
+            if j<NumberOfFailures:
673
+                init=end
674
+                end+=(testRanges[j+1][1]-testRanges[j+1][0])
675
+        x=[]
676
+        y=[]
677
+        for k in anomalous_data_indices:
678
+            if (k+TIME_STEPS)<x_test.shape[0]:
679
+                x.append(k+TIME_STEPS)
680
+                y.append(x_test[k+TIME_STEPS,0,indexesToPlot[i]]*stdevs[i]+means[i])
681
+        axes[i+1].plot(x,y ,color='black',marker='.',linewidth=0,label="Fail detection" )
682
+        if i==0:
683
+            axes[i+1].legend(loc='right')
684
+        s=''
685
+        s+=featureNames[features[indexesToPlot[i]]]
686
+        s+=' '+unitNames[features[indexesToPlot[i]]]
687
+        axes[i+1].set_ylabel(s)
688
+        axes[i+1].grid()
689
+    
690
+    axes[0].set_xlim(460,480)
691
+    axes[1].set_xlim(460,480)
692
+
693
+    axes[0].set_title('$ns=4$')
694
+    axes[1].set_title('$ns=20$')
695
+    axes[1].set_xlabel("Sample number")
696
+    plt.show()
697
+
698
+
699
+
700
+plotData5()
701
+exit(0)
702
+
703
+
704
+
705
+#  2nd scenario. Detect only anomaly.  Later, we will classiffy it
706
+# Test data=  testnormal + testfail1 + testtail2 + testfail3 + testfail4 + testnormal
707
+#d=np.vstack((dataTestNorm[0],dataTestNorm[1],dataTestNorm[2],dataTestNorm[3],dataTestNorm[4],dataTestNorm[0]))
708
+num=100
709
+d=np.vstack((dataTestNorm[0][0:num,:],dataTestNorm[1][0:num,:],dataTestNorm[0][num:2*num,:],dataTestNorm[2][70:70+num,:],dataTestNorm[0][2*num-90:3*num-90,:],dataTestNorm[3][50:num+50,:],dataTestNorm[0][150:150+num,:],dataTestNorm[4][0:num+TIME_STEPS,:]))
710
+
711
+x_test = create_sequences(d,int(options.timesteps))
712
+x_test_pred = model.predict(x_test)
713
+test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
714
+
715
+
716
+anomalies = test_mae_loss > threshold[int(options.timesteps)]*float(options.TF)
717
+anomalous_data_indices = []
718
+for i in range(anomalies.shape[0]):
719
+    if AtLeastOneTrue(anomalies[i]):
720
+    #if anomalies[i][0] or anomalies[i][1] or anomalies[i][2] or anomalies[i][3]:
721
+        anomalous_data_indices.append(i)
722
+
723
+def plotData4():
724
+    NumFeaturesToPlot=len(indexesToPlot)
725
+    plt.rcParams.update({'font.size': 16})
726
+    fig, axes = plt.subplots(
727
+        nrows=NumFeaturesToPlot, ncols=1, figsize=(15, 10), dpi=80, facecolor="w", edgecolor="k",sharex=True
728
+    )
729
+    for i in range(NumFeaturesToPlot):
730
+        for j in range(1,NumberOfFailures+1):
731
+            if j==1:
732
+                axes[i].plot(range((j-1)*2*num,(j-1)*2*num+num),x_test[(j-1)*2*num:(j-1)*2*num+num,0,indexesToPlot[i]],label="No fail", color='C0')
733
+            else:
734
+                axes[i].plot(range((j-1)*2*num,(j-1)*2*num+num),x_test[(j-1)*2*num:(j-1)*2*num+num,0,indexesToPlot[i]], color='C0')
735
+            axes[i].plot(range(j*2*num-num,j*2*num),x_test[j*2*num-num:j*2*num,0,indexesToPlot[i]],label="File type "+str(j),color=colorline[j-1])
736
+        x=[]
737
+        y=[]
738
+        for k in anomalous_data_indices:
739
+            if (k+TIME_STEPS)<x_test.shape[0]:
740
+                x.append(k+TIME_STEPS)
741
+                y.append(x_test[k+TIME_STEPS,0,indexesToPlot[i]])
742
+        axes[i].plot(x,y ,color='black',marker='.',linewidth=0,label="Fail detection" )
743
+
744
+        if i==0:
745
+            axes[i].legend(bbox_to_anchor=(0.9, 0.4))
746
+
747
+        s=''
748
+        s+=featureNames[features[indexesToPlot[i]]]
749
+        axes[i].set_ylabel(s)
750
+        axes[i].grid()
751
+    axes[NumFeaturesToPlot-1].set_xlabel("Sample number")
752
+    plt.show()
753
+
754
+
755
+plotData4()

Powered by TurnKey Linux.