Browse Source

Anomaly Detection

zhuxi 3 years ago
parent
commit
b73347fb33

+ 4 - 5
LIB/MIDDLE/Anomaly Detection/V1_0_0/anomalyPCA.py → LIB/MIDDLE/Anomaly_Detection/V1_0_0/anomalyPCA.py

@@ -110,10 +110,9 @@ def prediction(data_test,pca1,pca2):
 
 #判定异常
 def check_anomaly(outliers1,outliers2):
-    if (len(outliers1)>0) & (len(outliers2)>0):
-        outliers=pd.merge(outliers1,outliers2,on='时间')
-        outliers=outliers[outliers['SOC[%]_x']>45]
-        outliers=outliers.drop(['总电压[V]_y','单体压差_y','SOC[%]_y'],axis=1)
-        return outliers
+    outliers=pd.merge(outliers1,outliers2,on='时间')
+    outliers=outliers[outliers['SOC[%]_x']>45]
+    outliers=outliers.drop(['总电压[V]_y','单体压差_y','SOC[%]_y'],axis=1)
+    return outliers
     
 

+ 0 - 0
LIB/MIDDLE/Anomaly Detection/V1_0_0/main_anomalyPCA.py → LIB/MIDDLE/Anomaly_Detection/V1_0_0/main_anomalyPCA.py


+ 52 - 0
LIB/MIDDLE/Anomaly_Detection/V1_0_0/main_detection.py

@@ -0,0 +1,52 @@
+#热失控预警:PCA异常指数
+#预测及异常预警
+
+from LIB.BACKEND import DBManager
+dbManager = DBManager.DBManager()
+from LIB.MIDDLE.CellStateEstimation.Common import log
+import pandas as pd
+from anomalyPCA import *
+import joblib
+import datetime
+
+dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
+fileNames = dataSOH['sn']
+fileNames = list(fileNames)
+l = len(fileNames)
+
+now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')   #type: str
+now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S')     #type: datetime
+start_time=now_time-datetime.timedelta(hours=3)
+end_time=str(now_time)
+start_time=str(start_time)
+
+mylog=log.Mylog('log.txt','error')
+mylog.logcfg()
+
+anomalies=pd.DataFrame()
+for k in range(l): 
+    try: 
+        sn = fileNames[k]
+        df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
+        data_test = df_data['bms']
+
+        if len(data_test)>0:
+            pca1 = joblib.load('pca1_'+sn+'.m')  
+            pca2 = joblib.load('pca2_'+sn+'.m') 
+            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)
+            outliers1=detect_outliers(res1,pred1,threshold=30)
+            outliers2=detect_outliers(res2,pred2,threshold=16)
+            if (len(outliers1)>0) & (len(outliers2)>0):
+                outliers=check_anomaly(outliers1,outliers2)
+                if len(outliers)>5:
+                    outliers.to_csv('outliers'+sn+'.csv',encoding='gbk')
+                    outliers['sn']=sn
+                    anomalies=anomalies.append(outliers)
+                    anomalies.to_csv('anomalies.csv',encoding='gbk')
+           
+    except Exception as e:
+        print(repr(e))
+        mylog.logopt(sn,e)
+        pass 

+ 0 - 0
LIB/MIDDLE/Anomaly Detection/V1_0_0/sn-20210903.xlsx → LIB/MIDDLE/Anomaly_Detection/V1_0_0/sn-20210903.xlsx