from re import X import pandas as pd import numpy as np from pandas.core.frame import DataFrame import BatParam import pandas as pd # 计算充电过程 def preprocess(df): # 滤除前后电压存在一增一减的情况(采样异常) pass # 计算SOC变化率 def cal_volt_change(dfin, volt_column): df = dfin.copy() df_volt_rolling = df[volt_column] df_volt_rolling_sum=df_volt_rolling.sum(1)-df_volt_rolling.max(1) df_volt_rolling_sum=df_volt_rolling_sum-df_volt_rolling.min(1) mean1 = df_volt_rolling_sum/(len(volt_column)-2) df_volt_rolling_norm = df_volt_rolling.sub(mean1, axis=0)#.div(std,axis=0) df_volt_rolling_norm = df_volt_rolling_norm.reset_index(drop=True)#和均值的距离 return df_volt_rolling_norm # 计算电压离群 def cal_volt_sigma(dfin, volt_column): df = dfin.copy() df_volt_rolling = df[volt_column] mean1=df_volt_rolling.mean(axis=1) std = df_volt_rolling.std(axis=1) std = std.replace(0,0.000001) df_volt_rolling = df_volt_rolling.sub(mean1, axis=0).div(std,axis=0) df_volt_rolling = df_volt_rolling.reset_index(drop=True)#分布 return df_volt_rolling # # 计算电压变化量的偏离度 # def cal_voltdiff_uniform(dfin, volt_column, window=10, step=5, window2=10, step2=5,threshold=3): # df = dfin.copy() # time_list = dfin['time'].tolist() # # 电压滤波 # df_volt = df[volt_column] # df_volt_rolling = df_volt.rolling(window).mean()[window-1::step] # 滑动平均值 # time_list = time_list[window-1::step] # # 计算电压变化量的绝对值(# 计算前后的差值的绝对值, 时间列-1) # df_volt_diff = abs(df_volt_rolling.diff()[1:]) # df_volt_diff = df_volt_diff.reset_index(drop=True) # time_list = time_list[1:] # # 压差归一化(偏离度) # # mean = df_volt_diff.mean(axis=1) # # std = df_volt_diff.std(axis=1) # # df_voltdiff_norm = df_volt_diff.sub(mean, axis=0).div(std,axis=0) # df_voltdiff_norm = df_volt_diff.copy() # # 压差偏离度滑动平均滤波 # df_voltdiff_rolling = df_voltdiff_norm.rolling(window2).mean()[window2-1::step2] # 滑动平均值 # time_list = time_list[window2-1::step2] # df_voltdiff_rolling_sum=df_voltdiff_rolling.sum(1)-df_voltdiff_rolling.max(1) # df_voltdiff_rolling_sum=df_voltdiff_rolling_sum-df_voltdiff_rolling.min(1) # mean = df_voltdiff_rolling_sum/(len(volt_column)-2) # std = df_voltdiff_rolling.std(axis=1) # # mean = [np.array(sorted(x)[1:-1]).mean() for x in df_voltdiff_rolling.values] # # std = [np.array(sorted(x)[1:-1]).std() for x in df_voltdiff_rolling.values] # df_voltdiff_rolling_norm = df_voltdiff_rolling.sub(mean, axis=0)#.div(std,axis=0) # df_voltdiff_rolling_norm = df_voltdiff_rolling_norm.reset_index(drop=True) # return df_voltdiff_rolling_norm, time_list def main(sn,df_bms,celltype): param=BatParam.BatParam(celltype) df_bms['PackCrnt']=df_bms['PackCrnt']*param.PackCrntDec df_bms['time']=pd.to_datetime(df_bms['time'], format='%Y-%m-%d %H:%M:%S') volt_column = ['CellVolt'+str(i) for i in range(1,param.CellVoltNums+1)] columns=['time']+volt_column df_bms=df_bms[(df_bms['PackSOC']>10)] # df_bms=df_bms[(df_bms['PackCrnt']<1)] # df_chrg=df_bms[(df_bms['PackCrnt']<-1)] #电压/SOC变化率计算 if celltype<50: df_ori = df_bms[columns] df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据 df= df.set_index('time') df=df[(df[volt_column]>2) & (df[volt_column]<4.5)] df[volt_column]=pd.DataFrame(df[volt_column],dtype=np.float) df=df.resample('H').mean() #取一个小时的平均值 df=df.dropna(how='any') time_list1=df.index.tolist() fun=lambda x: np.interp(x, param.LookTab_OCV, param.LookTab_SOC) df_soc=df.applymap(fun) VolChng = cal_volt_change(df_soc,volt_column) else: # df_bms=df_bms[(df_bms['PackCrnt']>-0.1) & (df_bms['PackCrnt']<0.1)] df_ori = df_bms[columns] df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据 df= df.set_index('time') df=df[(df[volt_column]>3.2) & (df[volt_column]<3.4)] df[volt_column]=pd.DataFrame(df[volt_column],dtype=np.float) df=df.resample('H').mean() #取一个小时的平均值 df=df.dropna(how='any') time_list1=df.index.tolist() VolChng = cal_volt_change(df,volt_column) VolSigma = cal_volt_sigma(df,volt_column) OutLineVol=DataFrame(columns=['time','sn','VolOl_Uni','VolChng_Uni']) #静置电压变化率和离群度计算 if len(VolChng)>5 and len(VolSigma)>5: VolChng['time'] = time_list1 VolChng= VolChng.set_index('time') VolChng_Uni_result=VolChng.values.tolist()#改 VolSigma['time'] = time_list1 VolSigma= VolSigma.set_index('time') VolOl_Uni_result=VolSigma.values.tolist()#改 for i in range(0,len(VolChng)): OutLineVol.loc[i,'VolOl_Uni']=str(list(np.around(VolOl_Uni_result[i],decimals=2))) OutLineVol.loc[i,'VolChng_Uni']=str(list(np.around(VolChng_Uni_result[i],decimals=2))) OutLineVol=OutLineVol[~OutLineVol['VolOl_Uni'].str.contains('nan')] OutLineVol=OutLineVol[~OutLineVol['VolChng_Uni'].str.contains('nan')] OutLineVol=OutLineVol.applymap((lambda x:''.join(x.split()) if type(x) is str else x)) OutLineVol=OutLineVol.reset_index(drop=True) OutLineVol['time']= VolSigma.index OutLineVol['sn']=sn return(OutLineVol)