VoltStray.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from re import X
  2. import pandas as pd
  3. import numpy as np
  4. from pandas.core.frame import DataFrame
  5. import BatParam
  6. import pandas as pd
  7. # 计算充电过程
  8. def preprocess(df):
  9. # 滤除前后电压存在一增一减的情况(采样异常)
  10. pass
  11. # 计算SOC变化率
  12. def cal_volt_change(dfin, volt_column):
  13. df = dfin.copy()
  14. df_volt_rolling = df[volt_column]
  15. df_volt_rolling_sum=df_volt_rolling.sum(1)-df_volt_rolling.max(1)
  16. df_volt_rolling_sum=df_volt_rolling_sum-df_volt_rolling.min(1)
  17. mean1 = df_volt_rolling_sum/(len(volt_column)-2)
  18. df_volt_rolling_norm = df_volt_rolling.sub(mean1, axis=0)#.div(std,axis=0)
  19. df_volt_rolling_norm = df_volt_rolling_norm.reset_index(drop=True)#和均值的距离
  20. return df_volt_rolling_norm
  21. # 计算电压离群
  22. def cal_volt_sigma(dfin, volt_column):
  23. df = dfin.copy()
  24. df_volt_rolling = df[volt_column]
  25. mean1=df_volt_rolling.mean(axis=1)
  26. std = df_volt_rolling.std(axis=1)
  27. std = std.replace(0,0.000001)
  28. df_volt_rolling = df_volt_rolling.sub(mean1, axis=0).div(std,axis=0)
  29. df_volt_rolling = df_volt_rolling.reset_index(drop=True)#分布
  30. return df_volt_rolling
  31. # # 计算电压变化量的偏离度
  32. # def cal_voltdiff_uniform(dfin, volt_column, window=10, step=5, window2=10, step2=5,threshold=3):
  33. # df = dfin.copy()
  34. # time_list = dfin['time'].tolist()
  35. # # 电压滤波
  36. # df_volt = df[volt_column]
  37. # df_volt_rolling = df_volt.rolling(window).mean()[window-1::step] # 滑动平均值
  38. # time_list = time_list[window-1::step]
  39. # # 计算电压变化量的绝对值(# 计算前后的差值的绝对值, 时间列-1)
  40. # df_volt_diff = abs(df_volt_rolling.diff()[1:])
  41. # df_volt_diff = df_volt_diff.reset_index(drop=True)
  42. # time_list = time_list[1:]
  43. # # 压差归一化(偏离度)
  44. # # mean = df_volt_diff.mean(axis=1)
  45. # # std = df_volt_diff.std(axis=1)
  46. # # df_voltdiff_norm = df_volt_diff.sub(mean, axis=0).div(std,axis=0)
  47. # df_voltdiff_norm = df_volt_diff.copy()
  48. # # 压差偏离度滑动平均滤波
  49. # df_voltdiff_rolling = df_voltdiff_norm.rolling(window2).mean()[window2-1::step2] # 滑动平均值
  50. # time_list = time_list[window2-1::step2]
  51. # df_voltdiff_rolling_sum=df_voltdiff_rolling.sum(1)-df_voltdiff_rolling.max(1)
  52. # df_voltdiff_rolling_sum=df_voltdiff_rolling_sum-df_voltdiff_rolling.min(1)
  53. # mean = df_voltdiff_rolling_sum/(len(volt_column)-2)
  54. # std = df_voltdiff_rolling.std(axis=1)
  55. # # mean = [np.array(sorted(x)[1:-1]).mean() for x in df_voltdiff_rolling.values]
  56. # # std = [np.array(sorted(x)[1:-1]).std() for x in df_voltdiff_rolling.values]
  57. # df_voltdiff_rolling_norm = df_voltdiff_rolling.sub(mean, axis=0)#.div(std,axis=0)
  58. # df_voltdiff_rolling_norm = df_voltdiff_rolling_norm.reset_index(drop=True)
  59. # return df_voltdiff_rolling_norm, time_list
  60. def main(sn,df_bms,celltype):
  61. param=BatParam.BatParam(celltype)
  62. df_bms['PackCrnt']=df_bms['PackCrnt']*param.PackCrntDec
  63. df_bms['time']=pd.to_datetime(df_bms['time'], format='%Y-%m-%d %H:%M:%S')
  64. volt_column = ['CellVolt'+str(i) for i in range(1,param.CellVoltNums+1)]
  65. columns=['time']+volt_column
  66. df_bms=df_bms[(df_bms['PackSOC']>10)]
  67. # df_bms=df_bms[(df_bms['PackCrnt']<1)]
  68. # df_chrg=df_bms[(df_bms['PackCrnt']<-1)]
  69. #电压/SOC变化率计算
  70. if celltype<50:
  71. df_ori = df_bms[columns]
  72. df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据
  73. df= df.set_index('time')
  74. df=df[(df[volt_column]>2) & (df[volt_column]<4.5)]
  75. df[volt_column]=pd.DataFrame(df[volt_column],dtype=np.float)
  76. df=df.resample('H').mean() #取一个小时的平均值
  77. df=df.dropna(how='any')
  78. time_list1=df.index.tolist()
  79. fun=lambda x: np.interp(x, param.LookTab_OCV, param.LookTab_SOC)
  80. df_soc=df.applymap(fun)
  81. VolChng = cal_volt_change(df_soc,volt_column)
  82. else:
  83. # df_bms=df_bms[(df_bms['PackCrnt']>-0.1) & (df_bms['PackCrnt']<0.1)]
  84. df_ori = df_bms[columns]
  85. df = df_ori.drop_duplicates(subset=['time']) # 删除时间相同的数据
  86. df= df.set_index('time')
  87. df=df[(df[volt_column]>3.2) & (df[volt_column]<3.4)]
  88. df[volt_column]=pd.DataFrame(df[volt_column],dtype=np.float)
  89. df=df.resample('H').mean() #取一个小时的平均值
  90. df=df.dropna(how='any')
  91. time_list1=df.index.tolist()
  92. VolChng = cal_volt_change(df,volt_column)
  93. VolSigma = cal_volt_sigma(df,volt_column)
  94. OutLineVol=DataFrame(columns=['time','sn','VolOl_Uni','VolChng_Uni'])
  95. #静置电压变化率和离群度计算
  96. if len(VolChng)>5 and len(VolSigma)>5:
  97. VolChng['time'] = time_list1
  98. VolChng= VolChng.set_index('time')
  99. VolChng_Uni_result=VolChng.values.tolist()#改
  100. VolSigma['time'] = time_list1
  101. VolSigma= VolSigma.set_index('time')
  102. VolOl_Uni_result=VolSigma.values.tolist()#改
  103. for i in range(0,len(VolChng)):
  104. OutLineVol.loc[i,'VolOl_Uni']=str(list(np.around(VolOl_Uni_result[i],decimals=2)))
  105. OutLineVol.loc[i,'VolChng_Uni']=str(list(np.around(VolChng_Uni_result[i],decimals=2)))
  106. OutLineVol=OutLineVol[~OutLineVol['VolOl_Uni'].str.contains('nan')]
  107. OutLineVol=OutLineVol[~OutLineVol['VolChng_Uni'].str.contains('nan')]
  108. OutLineVol=OutLineVol.applymap((lambda x:''.join(x.split()) if type(x) is str else x))
  109. OutLineVol=OutLineVol.reset_index(drop=True)
  110. OutLineVol['time']= VolSigma.index
  111. OutLineVol['sn']=sn
  112. return(OutLineVol)