MakeDataSet_History.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #历史剩余充电时间统计 2021-10
  2. from LIB.BACKEND.Tools import Tools
  3. from LIB.BACKEND.DataPreProcess import DataPreProcess
  4. import pandas as pd
  5. import numpy as np
  6. import os
  7. import math
  8. import datetime
  9. from time import strftime,gmtime
  10. from LIB.BACKEND import DBManager
  11. dbManager = DBManager.DBManager()
  12. #将时间戳由 "%Y-%m-%d %H:%M:%S" 切换为 sec
  13. def timeconvert(df):
  14. df.index=range(len(df))
  15. time=df.时间戳
  16. timeInSeries=[]
  17. time2=datetime.datetime.strptime(time[0],"%Y-%m-%d %H:%M:%S")
  18. for k in range(len(time)):
  19. time1=datetime.datetime.strptime(time[k],"%Y-%m-%d %H:%M:%S")
  20. t=(time1-time2)
  21. timeInSeries.append(t.days*86400+t.seconds)
  22. df.loc[:,'相对时间']=pd.DataFrame(timeInSeries,columns=['相对时间'])
  23. return df
  24. #构建新数据表统计剩余充电时间
  25. def data_handle(df_in):
  26. #数据预处理
  27. df_in=timeconvert(df_in)
  28. dfOut_temp=DataPreProcess.data_split_by_status(DataPreProcess, df_in, drive_interval_threshold=120, charge_interval_threshold=300,drive_stand_threshold=120, charge_stand_threshold=300)
  29. dfOut=dfOut_temp.copy()
  30. df_charge=dfOut[dfOut['data_status']=='charge']
  31. unique_status_idx=np.unique(df_charge.data_split_by_status.values)
  32. #计算剩余充电时间
  33. df_statisticsInfo=pd.DataFrame()
  34. for n in unique_status_idx:
  35. df_charge_buff=df_charge[df_charge.data_split_by_status==n]
  36. if(np.max(df_charge_buff['SOC[%]'].values[-1])==100): #满充状态数据提取
  37. times=df_charge_buff['相对时间'].values
  38. times1=times[:-1]
  39. times2=times[1:]
  40. delta=times2-times1
  41. if(np.max(delta)<50):
  42. times_inv=[times[-1]-t for t in times ]
  43. df_charge_buff.loc[:,'剩余充电时间[sec]']=times_inv
  44. times_hms=[]
  45. for k in list(range(len(times_inv))):
  46. time_hms=strftime("%H:%M:%S", gmtime(df_charge_buff.loc[:,'剩余充电时间[sec]'].values[k]))
  47. times_hms.append(time_hms)
  48. df_charge_buff.loc[:,'剩余充电时间[时分秒]']=times_hms
  49. df_dataset_temp=df_charge_buff.drop(['GSM信号','故障等级','故障代码','外电压','绝缘电阻','总输出状态','上锁状态','充电状态','加热状态','单体压差','单体均衡状态','相对时间','data_split_by_crnt','data_split_by_status','data_status'],axis=1)
  50. df_statisticsInfo=df_statisticsInfo.append(df_dataset_temp)
  51. #简化特征
  52. list_col=list(df_statisticsInfo)
  53. list1=[s for s in list_col if '单体电压' in s]
  54. list2=[s for s in list_col if '单体温度' in s]
  55. list3=[s for s in list_col if '其他温度' in s]
  56. list_v=['单体电压'+str(i) for i in range(1,len(list1)+1)]
  57. list_T=['单体温度'+str(i) for i in range(1,len(list2)+1)]
  58. A_cellVolt=df_statisticsInfo[list_v].values
  59. celVolt_max=np.max(A_cellVolt,axis=1)
  60. celVolt_min=np.min(A_cellVolt,axis=1)
  61. A_cellTemp=df_statisticsInfo[list_T].values
  62. celTemp_max=np.max(A_cellTemp,axis=1)
  63. celTemp_min=np.min(A_cellTemp,axis=1)
  64. df_statisticsInfo.loc[:,'最高单体电压[V]']=celVolt_max
  65. df_statisticsInfo.loc[:,'最高单体温度[℃]']=celTemp_max
  66. df_statisticsInfo.loc[:,'最低单体温度[℃]']=celTemp_min
  67. df_statisticsInfo=df_statisticsInfo.drop(list_v,axis=1)
  68. df_statisticsInfo=df_statisticsInfo.drop(list_T,axis=1)
  69. df_statisticsInfo=df_statisticsInfo.drop(['其他温度'+str(i) for i in range(1,len(list3)+1)],axis=1)
  70. return(df_statisticsInfo)
  71. #Dataset
  72. data_sn=pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
  73. fileNames=data_sn['sn']
  74. df_dataset=pd.DataFrame()
  75. for k in range(2):
  76. df_data = dbManager.get_data(sn=fileNames[k], start_time='2021-10-01 00:00:00', end_time='2021-11-01 00:00:00', data_groups=['bms'])
  77. if len(df_data)==0:
  78. continue
  79. dataIn = df_data['bms']
  80. dataIn=data_handle(dataIn)
  81. dataIn['sn']=fileNames[k]
  82. df_dataset=df_dataset.append(dataIn)
  83. df_dataset.to_csv('df_datatest.csv')