Browse Source

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

Eric412V 2 years ago
parent
commit
419142fc73

+ 3 - 3
LIB/MIDDLE/CellStateEstimation/BatSafetyWarning/V1_0_1/CBMSBatInterShort.py

@@ -120,11 +120,11 @@ class BatInterShort():
     def _celldeltsoc_get(self,cellvolt_list,dict_baltime,capacity): 
         cellsoc=[]
         celldeltsoc=[]
-        for j in range(1, self.param.CellVoltNums+1):   #获取每个电芯电压对应的SOC值
-            cellvolt=cellvolt_list[j+1]
+        for j in range(self.param.CellVoltNums):   #获取每个电芯电压对应的SOC值
+            cellvolt=cellvolt_list[j]
             ocv_soc=np.interp(cellvolt,self.param.LookTab_OCV,self.param.LookTab_SOC)
             if j in dict_baltime.keys():
-                ocv_soc=ocv_soc+dict_baltime[j]*self.param.BalCurrent/(capacity*3600)   #补偿均衡电流
+                ocv_soc=ocv_soc+dict_baltime[j+1]*self.param.BalCurrent/(capacity*3600)   #补偿均衡电流
             else:
                 pass
             cellsoc.append(ocv_soc)

+ 49 - 0
LIB/MIDDLE/OutlierDetection/VoltOutlier/V_1_0_1/anomalySparsePCA.py

@@ -0,0 +1,49 @@
+from sklearn.decomposition import PCA
+import pandas as pd
+from sklearn.preprocessing import StandardScaler
+import numpy as np
+import matplotlib.pyplot as plt
+import seaborn as sns 
+from sklearn.decomposition import SparsePCA
+import datetime
+from scipy.signal import savgol_filter
+
+#异常指数
+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 anomaly(df_data):
+    #数据预处理
+    df_data=df_data.drop(['Unnamed: 0','GSM信号','外电压','单体压差','SOH[%]','开关状态','充电状态','故障等级','故障代码','绝缘电阻','上锁状态','加热状态','单体均衡状态','总输出状态'],axis=1,errors='ignore')
+    df_data['时间']=[df_data.loc[i,'时间戳'][0:13] for i in range(len(df_data))] 
+    df_data=df_data.groupby('时间').mean()
+    data_set=df_data.drop(['时间戳','时间'],axis=1,errors='ignore')
+    features=data_set.columns
+    sX=StandardScaler(copy=True)
+    data_set.loc[:,features]=sX.fit_transform(data_set[features])
+    #稀疏PCA异常检测
+    n_components=20
+    alpha=0.0001
+    random_state=10001
+    n_jobs=-1
+    #降维
+    sparsePCA=SparsePCA(n_components=n_components,alpha=alpha,random_state=random_state,n_jobs=n_jobs)
+    sparsePCA.fit(data_set.loc[:,:])
+    X_train_sparsePCA=sparsePCA.transform(data_set)
+    X_train_sparsePCA=pd.DataFrame(data=X_train_sparsePCA,index=data_set.index)
+    #还原
+    X_train_sparsePCA_inverse=np.array(X_train_sparsePCA).dot(sparsePCA.components_)+np.array(data_set.mean(axis=0))
+    X_train_sparsePCA_inverse=pd.DataFrame(data=X_train_sparsePCA_inverse,index=data_set.index)
+    #异常指数
+    anomalyScoressparsePCA=anomalyScores(data_set,X_train_sparsePCA_inverse)
+    anomalyScoressparsePCA=savgol_filter(anomalyScoressparsePCA,101,3)
+    df_data['anomalyScoressparsePCA']=anomalyScoressparsePCA
+
+    return df_data
+
+                                                                                                                                                                                                                                                                                                                                                                

+ 33 - 0
LIB/MIDDLE/OutlierDetection/VoltOutlier/V_1_0_1/main_anomaly.py

@@ -0,0 +1,33 @@
+from LIB.MIDDLE.CellStateEstimation.Common import log
+from LIB.BACKEND import DBManager
+dbManager = DBManager.DBManager()
+import pandas as pd
+import anomalySparsePCA
+
+dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='格林美-CATL7255')
+fileNames = dataSOH['sn']
+fileNames = list(fileNames)
+l = len(fileNames)
+
+#log信息配置
+mylog=log.Mylog('log.txt','error')
+mylog.logcfg()
+
+data_res=pd.DataFrame()
+for k in range(3):
+    try:
+        sn = fileNames[k]
+        df_data = dbManager.get_data(sn=sn, start_time='2021-01-01 00:00:00', end_time='2021-11-01 00:00:00', data_groups=['bms'])
+        data_bms = df_data['bms']
+
+        #...............训练模型............................................................................
+        if len(data_bms['时间戳'])>0:
+            data_stat=anomalySparsePCA.anomaly(data_bms)
+            data_stat.to_csv('result_'+sn+'.csv',encoding='gbk')
+            data_stat['sn']=sn
+            data_res=data_res.append(data_stat)
+              
+    except Exception as e:
+        print(repr(e))
+        mylog.logopt(sn,e)
+        pass 

BIN
LIB/MIDDLE/OutlierDetection/VoltOutlier/V_1_0_1/输出表单.xlsx