Переглянути джерело

Merge branch 'dev' of http://git.fast-fun.cn:92/lmstack/data_analyze_platform into dev

shangguanlie23 2 роки тому
батько
коміт
64be6cdebb

+ 55 - 5
LIB/MIDDLE/CellStateEstimation/Common/V1_0_1/BatParam.py

@@ -41,7 +41,18 @@ class BatParam:
                                     [0,0,  0.1,0.3,  0.7, 1,  1,   1],
                                     [0,0,  0.2,0.3,  0.8, 1,  1,   1]]
 
-
+        #采样诊断参数
+        self.CellMaxUSBTemp=55
+        self.AllowChgMinTemp=0
+        self.AllowDsChgTemp=-5
+        self.AvgVolGap=1
+        self.AvgCellTempGap=10
+        self.PackOTlmt=65
+        self.PackUTlmt=-20
+        self.OtherOTlmt=91
+        self.OtherUTlmt=-20        
+        self.FaultCount=100
+        
         if celltype==1: #6040
             self.Capacity = 41
             self.PackFullChrgVolt=69.99
@@ -81,6 +92,12 @@ class BatParam:
             self.CellTempDiffLv2=15
 
             self.TrwVoltRate=-1
+            
+            self.DifVolGap = 3
+            self.CellOVlmt=5
+            self.CellUVlmt=2
+            self.CantChrgVol=3
+            self.AvgOtherTempGap=99
         elif celltype==2: #4840
             self.Capacity = 41
             self.PackFullChrgVolt=69.99
@@ -120,6 +137,12 @@ class BatParam:
             self.CellTempDiffLv2=15
 
             self.TrwVoltRate=-1
+            
+            self.DifVolGap = 3
+            self.CellOVlmt=5
+            self.CellUVlmt=2
+            self.CantChrgVol=3
+            self.AvgOtherTempGap=99   
         elif celltype==3:   #力信50ah三元电芯
             self.Capacity = 51
             self.PackFullChrgVolt=80
@@ -158,7 +181,13 @@ class BatParam:
             self.CellTempDiffLv1=10
             self.CellTempDiffLv2=15  
 
-            self.TrwVoltRate=-1         
+            self.TrwVoltRate=-1
+            
+            self.DifVolGap = 3
+            self.CellOVlmt=5
+            self.CellUVlmt=2
+            self.CantChrgVol=3
+            self.AvgOtherTempGap=40         
         elif celltype==4:   #CATL 50ah三元电芯
             self.Capacity = 50
             self.PackFullChrgVolt=80
@@ -200,7 +229,13 @@ class BatParam:
             self.CellTempDiffLv1=10
             self.CellTempDiffLv2=15   
 
-            self.TrwVoltRate=-1        
+            self.TrwVoltRate=-1    
+            
+            self.DifVolGap = 3
+            self.CellOVlmt=5
+            self.CellUVlmt=2
+            self.CantChrgVol=3
+            self.AvgOtherTempGap=40    
         elif celltype==99:   #60ah磷酸铁锂电芯
             self.Capacity = 54
             self.PackFullChrgVolt=69.99
@@ -250,7 +285,14 @@ class BatParam:
             self.CellTempDiffLv1=10
             self.CellTempDiffLv2=15   
 
-            self.TrwVoltRate=-8                    
+            self.TrwVoltRate=-8
+            
+            
+            self.DifVolGap = 3
+            self.CellOVlmt=4
+            self.CellUVlmt=2
+            self.CantChrgVol=2.6
+            self.AvgOtherTempGap=99                    
         elif celltype==100:
             self.Capacity = 228*2
             self.PackFullChrgVolt=3.65*192
@@ -261,6 +303,7 @@ class BatParam:
             self.OcvInflexionAbove=3.4
             self.CellVoltNums=384
             self.CellTempNums=64
+            self.OtherTempNums=0
             self.FullChrgSoc=98
             self.PeakSoc=59
             self.PeakVoltLowLmt=3.35
@@ -296,8 +339,15 @@ class BatParam:
             self.CellTempDiffLv1=28
             self.CellTempDiffLv2=32   
 
-            self.TrwVoltRate=-8                         
+            self.TrwVoltRate=-8  
             
+                                   
+            self.DifVolGap = 3
+            self.CellOVlmt=4
+            self.CellUVlmt=2
+            self.CantChrgVol=2.6
+            self.AvgOtherTempGap=40
+ 
         else:
             print('未找到对应电池编号!!!')
             # sys.exit()

+ 75 - 0
LIB/MIDDLE/OutlierDetection/V_1_0_2/anomalyPCA.py

@@ -0,0 +1,75 @@
+import pandas as pd
+import numpy as np
+from scipy.signal import savgol_filter
+from sklearn.preprocessing import RobustScaler
+from sklearn.decomposition import PCA
+import matplotlib.pyplot as plt
+
+def makedataset(df_data):
+    df_data=df_data.drop(['Unnamed: 0','总电流[A]','GSM信号','外电压','单体压差','SOH[%]','开关状态','充电状态','故障等级','故障代码','绝缘电阻','上锁状态','加热状态','单体均衡状态','总输出状态'],axis=1,errors='ignore')
+    for i in range(1,21):
+        df_data=df_data[(df_data['单体电压'+str(i)]>2200) & (df_data['单体电压'+str(i)]<4800)]
+    df_data=df_data[df_data['SOC[%]']>12]
+    df_data['时间']=[df_data.loc[i,'时间戳'][0:15] for i in df_data.index]
+    df_data=df_data.groupby('时间').mean()
+    for k in df_data.columns:
+        df_data[k]=savgol_filter(df_data[k],3,2)
+    return df_data
+
+def process(data_set):
+    features=data_set.columns
+    sX=RobustScaler(copy=True)
+    data_set2=data_set.copy()
+    data_set2.loc[:,features]=sX.fit_transform(data_set2[features])
+    return data_set2
+
+def anomalyScores(originalDF,reducedDF):
+    loss=np.sum((np.array(originalDF)-np.array(reducedDF))**2,axis=1)
+    loss=pd.Series(data=loss,index=originalDF.index)
+    loss=(loss-np.min(loss))/(np.max(loss)-np.min(loss))
+    return loss
+
+def anomalyPCA(x_train_pro):
+    n_components=4
+    whiten=True
+    random_state=2
+    pca=PCA(n_components=n_components,whiten=whiten,random_state=random_state)
+    pca.fit(x_train_pro)
+    return pca
+
+def transform(df_data_pro,model,df_data):
+    #降维
+    X_train=model.transform(df_data_pro)
+    X_train=pd.DataFrame(data=X_train,index=df_data_pro.index)
+    #还原
+    X_train_inverse=model.inverse_transform(X_train)
+    X_train_inverse=pd.DataFrame(data=X_train_inverse,index=df_data_pro.index)
+    #异常指数
+    anomalyScoresModel=anomalyScores(df_data_pro,X_train_inverse)
+    anomalyScoresModel=savgol_filter(anomalyScoresModel,15,3)
+    df_data2=df_data.copy()
+    df_data2['anomalyScores_'+str(model)]=anomalyScoresModel
+    return df_data2
+
+def detect_outliers(data,threshold=3):
+    anomaly=data['anomalyScores_PCA(n_components=4, random_state=2, whiten=True)']
+    mean_d=np.mean(anomaly.values)
+    std_d=np.std(anomaly.values)
+    outliers=pd.DataFrame()
+    for k in anomaly.index:
+        z_score= (anomaly[k]-mean_d)/std_d
+        if np.abs(z_score) >threshold:
+            outliers=outliers.append(data[anomaly.values==anomaly[k]])
+    return outliers
+
+def detect_outliers2(data,pred,threshold=3):
+    anomaly=data['anomalyScores_PCA(n_components=4, random_state=2, whiten=True)']
+    anomalypred=pred['anomalyScores_PCA(n_components=4, random_state=2, whiten=True)']
+    mean_d=np.mean(anomaly.values)
+    std_d=np.std(anomaly.values)
+    outliers2=pd.DataFrame()
+    for k in anomalypred.index:
+        z_score= (anomalypred[k]-mean_d)/std_d
+        if np.abs(z_score) >threshold:
+            outliers2=outliers2.append(pred[anomalypred.values==anomalypred[k]])
+    return outliers2

+ 40 - 0
LIB/MIDDLE/OutlierDetection/V_1_0_2/main_anomalyPCA.py

@@ -0,0 +1,40 @@
+from LIB.BACKEND import DBManager
+dbManager = DBManager.DBManager()
+from LIB.MIDDLE.CellStateEstimation.Common import log
+import pandas as pd
+import anomalyPCA
+
+dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='格林美-CATL7255')
+fileNames = dataSOH['sn']
+fileNames = list(fileNames)
+l = len(fileNames)
+
+mylog=log.Mylog('log.txt','error')
+mylog.logcfg()
+
+for k in range(l): 
+    try: 
+        sn = fileNames[k]
+        df_data = dbManager.get_data(sn=sn, start_time='2021-06-01 00:00:00', end_time='2021-10-01 00:00:00', data_groups=['bms'])
+        data_train = df_data['bms']
+        df_data = dbManager.get_data(sn=sn, start_time='2021-10-01 00:00:00', end_time='2021-11-01 00:00:00', data_groups=['bms'])
+        data_test = df_data['bms']
+        if len(data_train)>0:
+            x_train=anomalyPCA.makedataset(data_train)  
+            x_train_pro=anomalyPCA.process(x_train) 
+            pca=anomalyPCA.anomalyPCA(x_train_pro) 
+            res=anomalyPCA.transform(x_train_pro,pca,x_train)
+            res.to_csv('res'+sn+'.csv',encoding='gbk')
+        if len(data_test)>0: 
+            x_test=anomalyPCA.makedataset(data_test)  
+            x_test_pro=anomalyPCA.process(x_test) 
+            pred=anomalyPCA.transform(x_test_pro,pca,x_test) 
+            pred.to_csv('pred'+sn+'.csv',encoding='gbk')
+            outliers=anomalyPCA.detect_outliers(pred,threshold=0.95)
+            outliers2=anomalyPCA.detect_outliers2(res,pred,threshold=36)
+            outliers.to_csv('outliers'+sn+'.csv',encoding='gbk')
+            outliers2.to_csv('outliers2'+sn+'.csv',encoding='gbk')
+    except Exception as e:
+        print(repr(e))
+        mylog.logopt(sn,e)
+        pass 

+ 8 - 2
LIB/MIDDLE/SaftyCenter/Common/QX_BatteryParam.py

@@ -5,7 +5,7 @@ class BatteryInfo():
         self.AllowDsChgTemp=-5
         self.AvgVolGap=1
         self.AvgCellTempGap=10
-        self.AvgOtherTempGap=40
+        
         self.PackOTlmt=65
         self.PackUTlmt=-20
         self.OtherOTlmt=91
@@ -31,6 +31,7 @@ class BatteryInfo():
             self.CellOVlmt=5
             self.CellUVlmt=2
             self.CantChrgVol=3
+            self.AvgOtherTempGap=99
           
         elif celltype==2: #4840
             self.Capacity = 41
@@ -48,7 +49,8 @@ class BatteryInfo():
             self.DifVolGap = 3
             self.CellOVlmt=5
             self.CellUVlmt=2
-            self.CantChrgVol=3        
+            self.CantChrgVol=3
+            self.AvgOtherTempGap=99        
 
         elif  celltype==3:   #力信50ah三元电芯
             self.Capacity = 51
@@ -67,6 +69,7 @@ class BatteryInfo():
             self.CellOVlmt=5
             self.CellUVlmt=2
             self.CantChrgVol=3
+            self.AvgOtherTempGap=40
 
         elif celltype==4:   #CATL 50ah三元电芯
             self.Capacity = 50
@@ -85,6 +88,7 @@ class BatteryInfo():
             self.CellOVlmt=5
             self.CellUVlmt=2
             self.CantChrgVol=3
+            self.AvgOtherTempGap=40
           
 
         elif celltype==99:   #60ah磷酸铁锂电芯
@@ -111,6 +115,7 @@ class BatteryInfo():
             self.CellOVlmt=4
             self.CellUVlmt=2
             self.CantChrgVol=2.6
+            self.AvgOtherTempGap=99
  
 
         elif celltype==100:
@@ -137,6 +142,7 @@ class BatteryInfo():
             self.CellOVlmt=4
             self.CellUVlmt=2
             self.CantChrgVol=2.6
+            self.AvgOtherTempGap=40
  
 
         else:

+ 20 - 15
LIB/MIDDLE/SaftyCenter/DataDiag_Static/main.py

@@ -13,7 +13,7 @@ from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload as DBDownloa
 from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
 from pandas.core.frame import DataFrame
 import datacompy
-from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
+from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import BatParam as QX_BatteryParam
 from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
 
 #...................................电池包电芯安全诊断函数......................................................................................................................
@@ -61,7 +61,7 @@ def diag_cal():
             print('SN:{},未找到对应电池类型!!!'.format(sn))
             continue
             # sys.exit()
-        param=QX_BatteryParam.BatteryInfo(celltype)   
+        param=QX_BatteryParam.BatParam(celltype)   
         #读取原始数据库数据........................................................................................................................................................
         dbManager = DBManager.DBManager()
         df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
@@ -123,20 +123,25 @@ def DaTa_Sta_Week_Task():
     task_on=1
     factory_info=['骑享','金茂换电']
     all_period_fault_info=DataFrame(columns=['factory','week','level1_count','level2_count','level3_count','level4_count','level5_count','solve_rate'])
-    toweek='Week'+time.strftime('%W')
-    host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
-    port=3306
-    db='safety_platform'
-    user='qx_algo_readonly'
-    password='qx@123456'
-    mode=2
-    tablename1='all_fault_info'
-    DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
-    with DBRead as DBRead:
-        df_fltinfo=DBRead.getdata('product_id','level','code','start_time','end_time','batpos','factory',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
+    df_fltinfopt=DataFrame(columns=['product_id','level','code','start_time','end_time','batpos','factory'])
+    for i in range (0,len(factory_info)):
+        host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
+        port=3306
+        db='safety_platform'
+        user='qx_algo_readonly'
+        password='qx@123456'
+        mode=2
+        tablename1='all_fault_info'
+        DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
+        with DBRead as DBRead:
+            df_fltinfo=DBRead.getdata('product_id','level','code','start_time','end_time','batpos','factory',tablename=tablename1,factory=factory_info[i],sn='',timename='',st='',sp='')#dbdownload经过了改编
+        df_fltinfopt=df_fltinfopt.append(df_fltinfo)
+        df_fltinfopt=df_fltinfopt.reset_index(drop=True)
+        print(df_fltinfopt)
 #............................获取数据................................
     for j in range(0,len(factory_info)):
-        df_fltinfo=df_fltinfo[df_fltinfo['factory']==factory_info[j]]
+        toweek='Week'+time.strftime('%W')
+        df_fltinfo=df_fltinfopt[df_fltinfopt['factory']==factory_info[j]]
         #............................获取时间................................      
         end_time=datetime.datetime.now()
         # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
@@ -154,7 +159,7 @@ def DaTa_Sta_Week_Task():
         all_period_fault_info.loc[j,'week']=toweek
         all_period_fault_info.loc[j,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate']
         all_period_fault_info.fillna(0,inplace=False)
-
+        print(all_period_fault_info)
         task_on=0
 def DaTa_Sta_Minutes_Task():
     task_on=1

BIN
all_statistic_info.xlsx