cesar 2 weeks ago
parent
commit
1cd637c0fc
1 changed files with 28 additions and 44 deletions
  1. 28
    44
      v5_class.py

+ 28
- 44
v5_class.py View File

25
 parser = OptionParser()
25
 parser = OptionParser()
26
 parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
26
 parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
27
 parser.add_option("-n", "--timesteps", dest="timesteps", help="TIME STEPS ", default=12)
27
 parser.add_option("-n", "--timesteps", dest="timesteps", help="TIME STEPS ", default=12)
28
+parser.add_option("-r", "--transition", dest="transition", help="Includes transition data (false)", default=False, action="store_true")
28
 #parser.add_option("-f", "--thresholdfactor", dest="TF", help="Threshold Factor ", default=1.4)
29
 #parser.add_option("-f", "--thresholdfactor", dest="TF", help="Threshold Factor ", default=1.4)
29
 # threshold makes no sense when classifying, becaues we apply many models and decide class for the less MSE
30
 # threshold makes no sense when classifying, becaues we apply many models and decide class for the less MSE
30
 
31
 
48
 
49
 
49
 # Next set of ddata corresponds to Freezer, SP=-26
50
 # Next set of ddata corresponds to Freezer, SP=-26
50
 datafiles[0][0]=['2024-08-07_5_','2024-08-08_5_','2025-01-25_5_','2025-01-26_5_'] 
51
 datafiles[0][0]=['2024-08-07_5_','2024-08-08_5_','2025-01-25_5_','2025-01-26_5_'] 
51
-datafiles[1][0]=['2025-01-27_5_','2025-01-28_5_'] 
52
-
53
 datafiles[0][1]=['2024-12-11_5_', '2024-12-12_5_','2024-12-13_5_'] 
52
 datafiles[0][1]=['2024-12-11_5_', '2024-12-12_5_','2024-12-13_5_'] 
54
-datafiles[1][1]=['2024-12-14_5_','2024-12-15_5_'] 
55
-
56
 datafiles[0][2]=['2024-12-18_5_','2024-12-21_5_','2024-12-22_5_','2024-12-23_5_','2024-12-24_5_'] 
53
 datafiles[0][2]=['2024-12-18_5_','2024-12-21_5_','2024-12-22_5_','2024-12-23_5_','2024-12-24_5_'] 
57
-datafiles[1][2]=['2024-12-19_5_','2024-12-25_5_','2024-12-26_5_'] 
54
+datafiles[0][3]=['2024-12-28_5_','2024-12-29_5_','2024-12-30_5_'] 
58
 
55
 
56
+if options.transition:
57
+    datafiles[1][0]=['2025-01-27_5_','2025-01-28_5_'] 
58
+    datafiles[1][1]=['2024-12-14_5_','2024-12-15_5_','2024-12-16_5_']  # with TRANSITION
59
+    datafiles[1][2]=['2024-12-17_5_','2024-12-19_5_','2024-12-25_5_','2024-12-26_5_'] # with TRANSITION
60
+    datafiles[1][3]=['2024-12-27_5_','2024-12-31_5_','2025-01-01_5_'] # with TRANSITION
61
+else:
62
+    datafiles[1][0]=['2025-01-27_5_','2025-01-28_5_'] 
63
+    datafiles[1][1]=['2024-12-14_5_','2024-12-15_5_'] 
64
+    datafiles[1][2]=['2024-12-19_5_','2024-12-25_5_','2024-12-26_5_'] 
65
+    datafiles[1][3]=['2024-12-31_5_','2025-01-01_5_'] 
66
+ 
67
+
68
+#datafiles[0][4]=['2025-02-05_5_'] 
69
+#datafiles[1][4]=['2025-02-05_5_'] 
59
 
70
 
60
-datafiles[0][3]=['2024-12-28_5_','2024-12-29_5_','2024-12-30_5_'] 
61
-datafiles[1][3]=['2024-12-31_5_','2025-01-01_5_'] 
62
 
71
 
63
 #r1s5 supply air flow temperature
72
 #r1s5 supply air flow temperature
64
 #r1s1 inlet evaporator temperature
73
 #r1s1 inlet evaporator temperature
72
 features=['r1 s5']
81
 features=['r1 s5']
73
 # Feature combination suggested by AKO
82
 # Feature combination suggested by AKO
74
 #features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
83
 #features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
75
-#features=['r1 s1','r1 s4','r1 s5']
84
+features=['r1 s1','r1 s4','r1 s5']
76
 #features=['r1 s1','r1 s5','pa1 apiii']
85
 #features=['r1 s1','r1 s5','pa1 apiii']
77
 #features=['r1 s5','pa1 apiii']
86
 #features=['r1 s5','pa1 apiii']
78
-features=['r1 s1','r1 s5']
87
+#features=['r1 s1','r1 s5']
79
 #features=['r1 s5']
88
 #features=['r1 s5']
80
 
89
 
81
 
90
 
92
 unitNames['r1 s5']='$(^{o}C)$'
101
 unitNames['r1 s5']='$(^{o}C)$'
93
 unitNames['pa1 apiii']='$(W)$'
102
 unitNames['pa1 apiii']='$(W)$'
94
 
103
 
95
-
96
-#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']
97
-
98
-#features=['r2 s2', 'tc s1','r1 s10','r1 s6','r2 s8']
99
-
100
 NumFeatures=len(features)
104
 NumFeatures=len(features)
101
 
105
 
102
 df_list=[[],[]]
106
 df_list=[[],[]]
121
         dftemp.append(df1)
125
         dftemp.append(df1)
122
     df_list[1][i]=pd.concat(dftemp)
126
     df_list[1][i]=pd.concat(dftemp)
123
 
127
 
124
-
125
-
126
 # subsampled to 5'  =  30 * 10"
128
 # subsampled to 5'  =  30 * 10"
127
 # We consider smaples every 5' because in production, we will only have data at this frequency
129
 # We consider smaples every 5' because in production, we will only have data at this frequency
128
 subsamplingrate=30
130
 subsamplingrate=30
137
     dataframe[0][i]=df_list[0][i].iloc[range(0,datalength,subsamplingrate)][features]
139
     dataframe[0][i]=df_list[0][i].iloc[range(0,datalength,subsamplingrate)][features]
138
     dataframe[0][i].reset_index(inplace=True,drop=True)
140
     dataframe[0][i].reset_index(inplace=True,drop=True)
139
     dataframe[0][i].dropna(inplace=True)
141
     dataframe[0][i].dropna(inplace=True)
142
+
140
 for i in range(NumberOfFailures+1):
143
 for i in range(NumberOfFailures+1):
141
     datalength=df_list[1][i].shape[0]
144
     datalength=df_list[1][i].shape[0]
142
     dataframe[1][i]=df_list[1][i].iloc[range(0,datalength,subsamplingrate)][features]
145
     dataframe[1][i]=df_list[1][i].iloc[range(0,datalength,subsamplingrate)][features]
144
     dataframe[1][i].dropna(inplace=True)
147
     dataframe[1][i].dropna(inplace=True)
145
 
148
 
146
 
149
 
147
-# Train data is first 2/3 of data
150
+# Train data is first 2/3 of data. Not exactly. L
148
 # Test data is: last 1/3 of data 
151
 # Test data is: last 1/3 of data 
149
 dataTrain=[]
152
 dataTrain=[]
150
 dataTest=[]
153
 dataTest=[]
151
 for i in range(NumberOfFailures+1):
154
 for i in range(NumberOfFailures+1):
152
-    dataTrain.append(dataframe[0][i].values)
153
-    dataTest.append(dataframe[0][i])
155
+    dataTrain.append(dataframe[0][i])
156
+    dataTest.append(dataframe[1][i])
154
 
157
 
155
 # Calculate means and stdev
158
 # Calculate means and stdev
156
 a=dataTrain[0]
159
 a=dataTrain[0]
171
 for i in range(NumberOfFailures+1):
174
 for i in range(NumberOfFailures+1):
172
     (dataTrainNorm[i],dataTestNorm[i])=normalize2(dataTrain[i],dataTest[i])
175
     (dataTrainNorm[i],dataTestNorm[i])=normalize2(dataTrain[i],dataTest[i])
173
 
176
 
174
-def plotData():    
175
-    fig, axes = plt.subplots(
176
-        nrows=NumberOfFailures+1, ncols=2, figsize=(15, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
177
-    )
178
-    for i in range(NumberOfFailures+1):
179
-        axes[i][0].plot(np.concatenate((dataTrainNorm[i][:,0],dataTestNorm[i][:,0])),label="Fail "+str(i)+",  feature 0")
180
-        axes[i][1].plot(np.concatenate((dataTrainNorm[i][:,1],dataTestNorm[i][:,1])),label="Fail "+str(i)+",  feature 1")
181
-    #axes[1].legend()
182
-    #axes[0].set_ylabel(features[0])
183
-    #axes[1].set_ylabel(features[1])
184
-    plt.show()
185
-
186
-#plotData()
187
-#exit(0)
188
-
189
 
177
 
190
 NumFilters=64
178
 NumFilters=64
191
 KernelSize=7
179
 KernelSize=7
192
 DropOut=0.2
180
 DropOut=0.2
193
-ThresholdFactor=1.4
194
 def create_sequences(values, time_steps):
181
 def create_sequences(values, time_steps):
195
     output = []
182
     output = []
196
     for i in range(len(values) - time_steps + 1):
183
     for i in range(len(values) - time_steps + 1):
197
         output.append(values[i : (i + time_steps)])
184
         output.append(values[i : (i + time_steps)])
198
     return np.stack(output)
185
     return np.stack(output)
199
 
186
 
200
-
201
-
202
 def listToString(l):
187
 def listToString(l):
203
     r=''
188
     r=''
204
     for i in l:
189
     for i in l:
295
 testRanges=[]
280
 testRanges=[]
296
 r=0
281
 r=0
297
 for i in range(len(datalist)):
282
 for i in range(len(datalist)):
298
-    testRanges.append([r,r+datalist[i].shape[0]-int(options.timesteps)])
299
-    r+=datalist[i].shape[0]-int(options.timesteps)
283
+    testRanges.append([r,r+datalist[i].shape[0]-int(options.timesteps)+1])
284
+    r+=datalist[i].shape[0]-int(options.timesteps)+1
300
 
285
 
301
 testClasses=[0,1,2,3]
286
 testClasses=[0,1,2,3]
302
 
287
 
348
                 end+=(testRanges[j+1][1]-testRanges[j+1][0])
333
                 end+=(testRanges[j+1][1]-testRanges[j+1][0])
349
 
334
 
350
             #if i==0:
335
             #if i==0:
351
-            #    axes[0].plot(x[j],y[j] ,color=colordot[j],marker='.',markersize=10,linewidth=0,label="Fail detect  type "+str(j) )
336
+            #    axes[0].plot(x[j],y[j] ,color=colordot[j],marker='.',markersize=10,linewidth=0,label="Fail detect  class "+str(j) )
352
 
337
 
353
 
338
 
354
 
339
 
364
 
349
 
365
     for j in range(NumberOfFailures+1):
350
     for j in range(NumberOfFailures+1):
366
         if NumFeaturesToPlot==1:
351
         if NumFeaturesToPlot==1:
367
-            axes.plot(x[j],y[j] ,color=colordot[j],marker='.',markersize=10,linewidth=0,label="Fail detect  type "+str(j) )
352
+            axes.plot(x[j],y[j] ,color=colordot[j],marker='.',markersize=10,linewidth=0,label="Fail detect  class "+str(j) )
368
         else:
353
         else:
369
-            axes[0].plot(x[j],y[j] ,color=colordot[j],marker='.',markersize=10,linewidth=0,label="Fail detect  type "+str(j) )
354
+            axes[0].plot(x[j],y[j] ,color=colordot[j],marker='.',markersize=10,linewidth=0,label="Fail detect  class "+str(j) )
370
             
355
             
371
     if NumFeaturesToPlot==1:
356
     if NumFeaturesToPlot==1:
372
         axes.legend(ncol=4,loc=(0.1,0.98))
357
         axes.legend(ncol=4,loc=(0.1,0.98))
384
     print("Error:  Class not exists")
369
     print("Error:  Class not exists")
385
     exit(0)        
370
     exit(0)        
386
 
371
 
387
-##   It remains to implemenent anomaly metrics for each failure type
372
+##   implemenent anomaly metrics for each failure class
388
 def anomalyMetric(classes,testranges,testclasses):  
373
 def anomalyMetric(classes,testranges,testclasses):  
389
     # FP, TP: false/true positive
374
     # FP, TP: false/true positive
390
     # TN, FN: true/false negative
375
     # TN, FN: true/false negative
423
 
408
 
424
 anomalyMetric(classes,testRanges,testClasses)
409
 anomalyMetric(classes,testRanges,testClasses)
425
 plotData4()
410
 plotData4()
426
-exit(0)
427
 
411
 
428
 
412
 

Powered by TurnKey Linux.