Browse Source

change detection

zhuxi 2 years ago
parent
commit
902156e030
1 changed files with 15 additions and 7 deletions
  1. 15 7
      LIB/MIDDLE/Anomaly_Detection/V1_0_0/main_detection.py

+ 15 - 7
LIB/MIDDLE/Anomaly_Detection/V1_0_0/main_detection.py

@@ -52,7 +52,7 @@ for k in range(l):
     try: 
         sn = fileNames[k]
         df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
-        df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
+        df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time ,data_groups=['bms'])
         data_test = df_data['bms']
         data_test=data_test[data_test['SOC[%]']>20]
         if len(data_test)>15:
@@ -61,6 +61,8 @@ for k in range(l):
             res1 = pd.read_csv('res1_'+sn+'.csv',encoding='gbk')
             res2 = pd.read_csv('res2_'+sn+'.csv',encoding='gbk')
             pred1,pred2=prediction(data_test,pca1,pca2)
+            pred1=pred1.reset_index()
+            pred2=pred2.reset_index()
             outliers1=detect_outliers(res1,pred1,threshold=30)
             outliers2=detect_outliers(res2,pred2,threshold=16)
             if (len(outliers1)>0) & (len(outliers2)>0):
@@ -69,14 +71,18 @@ for k in range(l):
                     outliers['sn']=sn
                     outliers=outliers.reset_index()
                     anomalies=anomalies.append(outliers)
+                    u_th=boxplot_fill(res2)
+                    outliers3=pred2[pred2['低压差']>u_th]
                     if df_diag_ram_sn.empty: 
                         product_id=sn
                         start_time=outliers.loc[0,'时间']
-                        start_time=start_time[:14]+start_time[16]+'0:00'
-                        if outliers.loc[-1,'时间'] == pred1.loc[-1,'时间']:
+                        start_time=start_time+'0:00'
+                        if outliers.loc[outliers.index[-1],'时间'] == pred1.loc[pred1.index[-1],'时间']:
+                            end_time='0000-00-00 00:00:00'
+                        elif outliers1.loc[outliers1.index[-1],'时间'] == outliers3.loc[outliers3.index[-1],'时间']:
                             end_time='0000-00-00 00:00:00'
                         else:
-                            end_time=outliers.loc[-1,'时间']
+                            end_time=outliers.loc[outliers.index[-1],'时间']
                         code='C493'
                         level=4
                         info='热失控预警'
@@ -85,10 +91,12 @@ for k in range(l):
                         with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
                             file.write(str(tuple(df_res.iloc[-1]))+'\n')    
                     else:
-                        if outliers.loc[-1,'时间'] == pred1.loc[-1,'时间']:
+                        if outliers.loc[outliers.index[-1],'时间'] == pred1.loc[pred1.index[-1],'时间']:
+                            end_time='0000-00-00 00:00:00'
+                        elif outliers1.loc[outliers1.index[-1],'时间'] == outliers3.loc[outliers3.index[-1],'时间']:
                             end_time='0000-00-00 00:00:00'
                         else:
-                            end_time=outliers.loc[-1,'时间']
+                            end_time=outliers.loc[outliers.index[-1],'时间']
                             df_diag_ram_sn['end_time']=end_time
                             with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
                                 file.write(str(tuple(df_diag_ram_sn.iloc[-1]))+'\n')
@@ -97,4 +105,4 @@ for k in range(l):
     except Exception as e:
         print(repr(e))
         mylog.logopt(sn,e)
-        pass 
+        pass