DataPreProcess.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. '''
  2. 数据预处理类
  3. '''
  4. __author__ = 'Wang Liming'
  5. import CONFIGURE.PathSetting as PathSetting
  6. import sys
  7. sys.path.append(PathSetting.backend_path)
  8. from os import defpath
  9. import pandas as pd
  10. import numpy as np
  11. import pdb
  12. from numba import jit
  13. import Tools
  14. class DataPreProcess:
  15. def __init__(self):
  16. self.tools = Tools.Tools()
  17. pass
  18. # def data_split(self, dfin, drive_interval_threshold=120, charge_interval_threshold=300,
  19. # drive_stand_threshold=120, charge_stand_threshold=300,
  20. # default_time_threshold = 300, drive_time_threshold=300, charge_time_threshold=300,
  21. # stand_time_threshold = 1800):
  22. # '''
  23. # 数据分段函数,会调用_data_split_by_status和_data_split_by_time函数。
  24. # 其中_data_split_by_status 将数据分为charge、drive、stand、和none段;
  25. # _data_split_by_time 将每个段内的数据,根据时间跳变继续分段。
  26. # '''
  27. def time_filter(self, df_bms, df_gps):
  28. df_bms.drop_duplicates(subset=['时间戳'], keep='first', inplace=True)
  29. df_gps.drop_duplicates(subset=['时间戳'], keep='first', inplace=True)
  30. df_bms = df_bms.reset_index(drop=True)
  31. df_gps = df_gps.reset_index(drop=True)
  32. return df_bms, df_gps
  33. def data_split_by_status(self, dfin, drive_interval_threshold=120, charge_interval_threshold=300,
  34. drive_stand_threshold=120, charge_stand_threshold=300):
  35. '''
  36. # 数据预处理分段, 将原始数据段分为 charge、drive、stand、none段
  37. # 状态判断
  38. # 1、drive:(状态为2或3 且 存在电流>0 ) 或 (电流持续为0 且 持续时间<阈值 且 上一段数据为行车)
  39. # 2、charge:(状态为2或3 且 不存在电流>0 ) 或 (电流持续为0 且 持续时间<阈值 且 上一段数据为充电)
  40. # 3、stand:(电流持续为0 且 是数据段的第一段) 或 (电流持续为0 且 持续时间>阈值)
  41. # 4、none: 其他
  42. --------------输入参数-------------:
  43. drive_interval_threshold: 行车段拼接阈值,如果两段行车的间隔时间小于该值,则两段行车合并。
  44. charge_interval_threshold: 充电段拼接阈值,如果两段充电的间隔时间小于该值,则两段充电合并。
  45. drive_stand_threshold: 静置段合并至行车段阈值,如果静置时间小于该值,则合并到上一段的行车中。
  46. charge_stand_threshold: 静置段合并至充电段阈值,如果静置时间小于该值,则合并到上一段的充电中。
  47. --------------输出-----------------:
  48. 在原始数据后面,增加data_split_by_crnt, data_split_by_status, data_status 三列
  49. data_split_by_crnt: 按电流分段的序号
  50. data_split_by_status:按电流和状态分段的序号
  51. data_status: 状态标识
  52. '''
  53. # 首先根据电流是否为0 ,将数据分段
  54. df = dfin.copy()
  55. df['时间戳'] = pd.to_datetime(df['时间戳'])
  56. crnt_zero_or_not = df['总电流[A]']==0
  57. last_crnt_flag = crnt_zero_or_not[0]
  58. temp = 1
  59. group_id = [temp]
  60. for cur_crnt_flag in crnt_zero_or_not[1:]:
  61. if last_crnt_flag ^ cur_crnt_flag:
  62. temp = temp + 1
  63. last_crnt_flag = cur_crnt_flag
  64. group_id.append(temp)
  65. df['data_split_by_crnt'] = group_id
  66. # 然后判断每个段内的 充电状态及电流=0持续时长,决定当前状态
  67. temp = 1
  68. last_status = ""
  69. status_id = []
  70. status_list = []
  71. data_number_list = sorted(list(set(df['data_split_by_crnt'])))
  72. for data_number in data_number_list:
  73. df_sel = df[df['data_split_by_crnt'] == data_number]
  74. origin_index = list(df_sel.index)
  75. df_sel = df_sel.reset_index(drop=True)
  76. temp_2 = 0
  77. # 如果当前数据段的电流非0,则可能分为charge、drive或none段
  78. if df_sel.loc[0,'总电流[A]'] != 0:
  79. # 电流 分段中可能存在状态变化的时刻, 内部根据状态进行分段.
  80. # 该数据段内部,根据bms状态信号进行二次分段
  81. status_drive_or_not = df_sel['充电状态']==3
  82. last_status_flag = status_drive_or_not[0]
  83. temp_2 = 0
  84. group_id_2 = [temp_2]
  85. for cur_status_flag in status_drive_or_not[1:]:
  86. if last_status_flag ^ cur_status_flag:
  87. temp_2 = temp_2 + 1
  88. last_status_flag = cur_status_flag
  89. group_id_2.append(temp_2)
  90. # 遍历二次状态分段
  91. temp_2 = 0
  92. last_status_2 = last_status
  93. df_sel['index'] = group_id_2
  94. data_number_list_2 = sorted(list(set(group_id_2)))
  95. for data_number_2 in data_number_list_2:
  96. df_sel_2 = df_sel[df_sel['index'] == data_number_2]
  97. df_sel_2 = df_sel_2.reset_index(drop=True)
  98. # 根据bms状态 及 电流符号决定是charge还是drive
  99. # 如果状态为2或3, 且电流均<=0 则记为充电
  100. if df_sel_2.loc[0, '充电状态'] in [2, 3] and len(df_sel_2[df_sel_2['总电流[A]'] > 0]) == 0:
  101. cur_status = 'charge'
  102. # 如果状态为2或3,且存在电流>0 则记为行车
  103. elif df_sel_2.loc[0, '充电状态'] in [2, 3] and len(df_sel_2[df_sel_2['总电流[A]'] > 0]) > 0:
  104. cur_status = 'drive'
  105. # 否则 记为none
  106. else:
  107. cur_status = 'none'
  108. status_list.extend([cur_status] * len(df_sel_2))
  109. # 状态id号与前面电流为0的相同状态进行合并, 均判断应不应该与上一段合并
  110. if origin_index[0] == 0: # 如果是所有数据的起始段数据,则直接赋值id号
  111. status_id.extend([temp + temp_2]*len(df_sel_2))
  112. else: # 判断是否与上一段数据合并
  113. deltaT = (df.loc[origin_index[0], '时间戳'] - df.loc[origin_index[0]-1, '时间戳']).total_seconds()
  114. # 如果 状态一致, 且 间隔时间小于阈值,则合并
  115. if last_status_2 == 'drive' and cur_status == last_status_2 and deltaT < drive_interval_threshold:
  116. temp_2 = temp_2 - 1
  117. status_id.extend([temp + temp_2]*len(df_sel_2))
  118. # 如果状态一致, 且 间隔时间小于阈值,则合并
  119. elif last_status_2 == 'charge' and cur_status == last_status_2 and deltaT < charge_interval_threshold:
  120. temp_2 = temp_2 - 1
  121. status_id.extend([temp + temp_2]*len(df_sel_2))
  122. else:
  123. status_id.extend([temp + temp_2]*len(df_sel_2))
  124. temp_2 = temp_2 + 1
  125. last_status_2 = status_list[-1]
  126. temp_2 = temp_2 - 1
  127. else:
  128. # 如果当前数据段的电流为0,则可能分为stand,charge、drive或none段
  129. if origin_index[0] == 0: # 如果是数据的起始,则无论长短,都认为是stand
  130. status_id.extend([temp]*len(df_sel))
  131. status_list.extend(['stand'] * len(df_sel))
  132. else: # 不是数据的起始
  133. cur_deltaT = (df.loc[origin_index[-1], '时间戳'] - df.loc[origin_index[0], '时间戳']).total_seconds()
  134. if last_status == 'charge': # 如果上一个状态为充电
  135. if cur_deltaT < charge_stand_threshold: # 如果本次电流为0的持续时间小于 阈值,则合并
  136. status_list.extend(['charge'] * len(df_sel))
  137. temp = temp - 1
  138. status_id.extend([temp]*len(df_sel))
  139. else: # 否则超过了阈值,记为stand
  140. status_id.extend([temp]*len(df_sel))
  141. status_list.extend(['stand'] * len(df_sel))
  142. elif last_status == 'drive': # 如果上一个状态为行车
  143. if cur_deltaT < drive_stand_threshold: # 如果本次电流为0的持续时间小于 阈值,则合并
  144. status_list.extend(['drive'] * len(df_sel))
  145. temp = temp - 1
  146. status_id.extend([temp]*len(df_sel))
  147. else: # 否则超过了阈值,记为stand
  148. status_id.extend([temp]*len(df_sel))
  149. status_list.extend(['stand'] * len(df_sel))
  150. elif last_status == 'none': # 如果上一个状态未知
  151. status_id.extend([temp] * len(df_sel))
  152. status_list.extend(['stand'] * len(df_sel))
  153. temp = temp + temp_2 + 1
  154. last_status = status_list[-1] # 上一组状态
  155. df['data_split_by_status'] = status_id
  156. df['data_status'] = status_list
  157. return df
  158. def data_split_by_time(self, dfin, default_time_threshold = 300, drive_time_threshold=300, charge_time_threshold=300,
  159. stand_time_threshold = 1800):
  160. '''
  161. # 该函数用来解决数据丢失问题导致的分段序号异常,
  162. # 将经过data_split_by_status分段后的数据,每个段内两行数据的时间跳变如果超过阈值,则继续分为两段
  163. --------------输入参数-------------:
  164. dfin: 调用data_split_by_status之后的函数
  165. default_time_threshold: 默认时间阈值,如果状态内部时间跳变大于该值,则划分为两段
  166. drive_time_threshold: 行车时间阈值,如果行车状态内部时间跳变大于该值,则划分为两段
  167. charge_time_threshold: 充电时间阈值,如果充电状态内部时间跳变大于该值,则划分为两段
  168. stand_time_threshold:静置时间阈值,如果静置状态内部时间跳变大于该值,则划分为两段
  169. --------------输出-----------------:
  170. 在输入数据后面,增加data_split_by_status_time 一列
  171. data_split_by_status_time: 按照状态和时间分段后的序号
  172. '''
  173. data_id = []
  174. temp = 1
  175. data_number_list = sorted(list(set(dfin['data_split_by_status'])))
  176. for data_number in data_number_list:
  177. # if data_number == 1203:
  178. # pdb.set_trace()
  179. status = list(dfin[dfin['data_split_by_status']==data_number]['data_status'])[0]
  180. cur_indexes = dfin[dfin['data_split_by_status']==data_number].index
  181. time_array = np.array(dfin[dfin['data_split_by_status']==data_number]['时间戳'])
  182. time_diff = np.diff(time_array)
  183. time_diff = time_diff.astype(np.int64)
  184. time_interval = default_time_threshold
  185. if status == 'drive':
  186. time_interval = drive_time_threshold
  187. elif status == 'charge':
  188. time_interval = charge_time_threshold
  189. elif status == 'stand':
  190. time_interval = stand_time_threshold
  191. time_diff_index = (np.argwhere(((time_diff/1e9) > time_interval)==True))[:,0]
  192. time_diff_origin_index = cur_indexes[time_diff_index]+1
  193. if len(time_diff_index) == 0:
  194. data_id.extend([temp] * len(cur_indexes))
  195. temp += 1
  196. else:
  197. last_index = cur_indexes[0]
  198. for index, cur_index in enumerate(time_diff_origin_index):
  199. if index == len(time_diff_origin_index)-1: # 如果是最后一个index,则
  200. data_id.extend([temp]* (cur_index-last_index))
  201. last_index = cur_index
  202. temp += 1
  203. data_id.extend([temp]* (cur_indexes[-1]-last_index+1))
  204. else:
  205. data_id.extend([temp]* (cur_index-last_index))
  206. last_index = cur_index
  207. temp += 1
  208. dfin['data_split_by_status_time'] = data_id
  209. return dfin
  210. def combine_drive_stand(self, dfin):
  211. '''
  212. 合并放电和静置段:将两次充电之间的所有数据段合并为一段, 状态分为 charge 和not charge
  213. ---------------输入----------
  214. dfin: 调用data_split_by_status()后输出的bms数据
  215. ---------------输出----------
  216. 在输入数据后面,增加data_split_by_status_after_combine, data_status_after_combine 两列
  217. data_split_by_status_after_combine: 将两次充电间的数据合并后的段序号
  218. data_status_after_combine: 每段数据的状态标识
  219. '''
  220. df = dfin.copy()
  221. data_split_by_status_1 = []
  222. data_status_1 = []
  223. number = 1
  224. first_flag = True
  225. data_number_list = sorted(list(set(df['data_split_by_status_time'])))
  226. for data_number in data_number_list:
  227. status = list(df[df['data_split_by_status_time']==data_number]['data_status'])
  228. cur_status = status[0]
  229. if first_flag:
  230. first_flag = False
  231. elif (last_status not in ['charge'] and cur_status in ['charge']) or (last_status in ['charge'] and cur_status not in ['charge']):
  232. number += 1
  233. data_split_by_status_1.extend([number]*len(status))
  234. if cur_status in ['charge']:
  235. data_status_1.extend(['charge']*len(status))
  236. else:
  237. data_status_1.extend(['not charge']*len(status))
  238. last_status = cur_status
  239. df['data_split_by_status_after_combine'] = data_split_by_status_1
  240. df['data_status_after_combine'] = data_status_1
  241. return df
  242. def cal_stand_time(self, dfin):
  243. '''
  244. # 计算静置时间
  245. # 将每次行车或充电的前后静置时间,赋值给stand_time 列, 单位为分钟
  246. ----------------输入参数---------
  247. dfin: 调用data_split_by_status()后输出的bms数据
  248. ----------------输出参数----------
  249. 在输入数据后面,增加stand_time列
  250. stand_time : 在行车段或充电段的起止两个位置处,表明开始前和结束后的静置时长,单位为分钟
  251. '''
  252. df = dfin.copy()
  253. stand_time = []
  254. first_flag = True
  255. data_number_list = sorted(list(set(df['data_split_by_status_time'])))
  256. for index, data_number in enumerate(data_number_list):
  257. status = list(df[df['data_split_by_status_time']==data_number]['data_status'])
  258. time = list(df[df['data_split_by_status_time']==data_number]['时间戳'])
  259. cur_status = status[0]
  260. cur_delta_time = (time[-1]-time[0]).total_seconds() / 60.0 # 分钟
  261. if len(status) >= 2:
  262. if first_flag:
  263. first_flag = False
  264. if index < len(data_number_list)-1:
  265. if cur_status in ['charge', 'drive']:
  266. next_status = list(df[df['data_split_by_status_time']==data_number_list[index+1]]['data_status'])[0]
  267. stand_time.extend([None]*(len(status)-1))
  268. if next_status == 'stand':
  269. next_time = list(df[df['data_split_by_status_time']==data_number_list[index+1]]['时间戳'])
  270. stand_time.extend([(next_time[-1]-next_time[0]).total_seconds() / 60.0])
  271. else:
  272. stand_time.extend([0])
  273. else:
  274. stand_time.extend([None]*len(status))
  275. else:
  276. stand_time.extend([None]*len(status))
  277. else:
  278. if cur_status in ['charge', 'drive']:
  279. if last_status == 'stand':
  280. stand_time.extend([last_delta_time])
  281. else:
  282. stand_time.extend([0])
  283. stand_time.extend([None]*(len(status)-2))
  284. if index < len(data_number_list)-1:
  285. next_status = list(df[df['data_split_by_status_time']==data_number_list[index+1]]['data_status'])[0]
  286. if next_status == 'stand':
  287. next_time = list(df[df['data_split_by_status_time']==data_number_list[index+1]]['时间戳'])
  288. stand_time.extend([(next_time[-1]-next_time[0]).total_seconds() / 60.0])
  289. else:
  290. stand_time.extend([0])
  291. else:
  292. stand_time.extend([None])
  293. else:
  294. stand_time.extend([None]*len(status))
  295. else:
  296. stand_time.extend([None])
  297. last_status = cur_status
  298. last_delta_time = cur_delta_time
  299. df['stand_time'] = stand_time
  300. return df
  301. # 输入GPS数据,返回本段数据的累积里程,及平均时速(如果两点之间)
  302. @jit
  303. def _cal_odo_speed(self, lat_list, long_list, time_list):
  304. '''
  305. 输入:经度列表, 纬度列表, 时间列表;
  306. 输出:每两个经纬度坐标之间的距离,以及速度 的数组
  307. '''
  308. dis_array = []
  309. speed_array = []
  310. for i in range(len(lat_list)-1):
  311. dis = self.tools.cal_distance(lat_list[i],long_list[i], lat_list[i+1],long_list[i+1])
  312. dis_array.append(dis)
  313. deltaT = abs(time_list[i] - time_list[i+1]).total_seconds()
  314. speed_array.append(dis * 3600.0/deltaT)
  315. return np.array(dis_array), np.array(speed_array)
  316. def gps_data_judge(self, df_bms, df_gps, time_diff_thre=300, odo_sum_thre=200, drive_spd_thre=80, parking_spd_thre=2):
  317. '''
  318. GPS数据可靠性判断函数(基于combine前的分段)
  319. GPS数据出现以下情况时,判定为不可靠:
  320. 1)如果该段对应的地理位置数据 少于2 个,则认为不可靠
  321. 2)如果截取的GPS数据的起止时间,与BMS数据段的起止时间相差超过阈值,则认为不可靠
  322. 3)如果行车段 累积里程超过阈值,车速超过阈值
  323. 4) 如果非行车段 车速超过阈值
  324. --------------输入参数--------------:
  325. time_diff_thre: 时间差阈值
  326. odo_sum_thre: 累积里程阈值
  327. drive_spd_thre: 行车车速阈值
  328. parking_spd_thre: 非行车状态车速阈值
  329. --------------输出参数--------------:
  330. df_bms 增加一列gps_rely, 表明对应的GPS数据是否可靠。
  331. 1:可靠
  332. <0: 表示不可靠的原因
  333. df_gps 增加两列odo, speed, 分别表示前后两点间的距离和速度
  334. '''
  335. df_gps['时间戳'] = pd.to_datetime(df_gps['时间戳'])
  336. res_record = {'drive':0, 'charge':0, 'stand':0, 'none':0, 'total':0}
  337. rely_list = []
  338. df_gps['odo'] = [None] * len(df_gps)
  339. df_gps['speed'] = [None] * len(df_gps)
  340. data_number_list = sorted(list(set(df_bms['data_split_by_status_time'])))
  341. for data_number in data_number_list[:]:
  342. df_sel = df_bms[df_bms['data_split_by_status_time'] == data_number]
  343. df_sel = df_sel.reset_index(drop=True)
  344. df_sel_gps = df_gps[(df_gps['时间戳']>=df_sel.loc[0,'时间戳']) & (df_gps['时间戳']<=df_sel.loc[len(df_sel)-1,'时间戳'])]
  345. origin_index = list(df_sel_gps.index)
  346. df_sel_gps = df_sel_gps.reset_index(drop=True)
  347. # 如果当前段数据对应的地理位置数据少于2个
  348. if len(df_sel_gps) <= 1:
  349. rely_list.extend([-1]*len(df_sel))
  350. res_record[str(df_sel.loc[0, 'data_status'])] = res_record[str(df_sel.loc[0, 'data_status'])] + 1
  351. continue
  352. # 如果GPS 起止时间段和BMS数据相差超过阈值
  353. if abs(df_sel_gps.loc[0, '时间戳'] - df_sel.loc[0,'时间戳']).total_seconds() > time_diff_thre or \
  354. abs(df_sel_gps.loc[len(df_sel_gps)-1, '时间戳'] - df_sel.loc[len(df_sel)-1,'时间戳']).total_seconds() > time_diff_thre:
  355. rely_list.extend([-2]*len(df_sel))
  356. res_record[str(df_sel.loc[0, 'data_status'])] = res_record[str(df_sel.loc[0, 'data_status'])] + 1
  357. continue
  358. # 计算该段数据每两点之间的里程以及速度
  359. dis_array, speed_array = self._cal_odo_speed(df_sel_gps['纬度'], df_sel_gps['经度'], df_sel_gps['时间戳'])
  360. # 如果 累积里程异常 或 平均车速异常 或两点间车速异常
  361. avg_speed = np.sum(dis_array) *3600.0 / abs(df_sel_gps.loc[0, '时间戳'] - df_sel_gps.loc[len(df_sel_gps)-1, '时间戳']).total_seconds()
  362. if np.sum(dis_array) > odo_sum_thre or avg_speed > drive_spd_thre or (speed_array > drive_spd_thre).any():
  363. rely_list.extend([-3]*len(df_sel))
  364. res_record[str(df_sel.loc[0, 'data_status'])] = res_record[str(df_sel.loc[0, 'data_status'])] + 1
  365. continue
  366. # 如果停车,且 平均时速超过阈值,则不可靠
  367. if (str(df_sel.loc[0, 'data_status']) == 'charge' or str(df_sel.loc[0, 'data_status']) == 'stand') and avg_speed > parking_spd_thre :
  368. rely_list.extend([-4]*len(df_sel))
  369. res_record[str(df_sel.loc[0, 'data_status'])] = res_record[str(df_sel.loc[0, 'data_status'])] + 1
  370. continue
  371. # 剩下的记录为可靠
  372. rely_list.extend([1]*len(df_sel))
  373. df_gps.loc[origin_index[1:], 'odo'] = dis_array
  374. df_gps.loc[origin_index[1:], 'speed'] = speed_array
  375. df_bms['gps_rely'] = rely_list
  376. res_record['total'] = (res_record['drive'] + res_record['charge'] + res_record['stand'] + res_record['none'] )/df_bms['data_split_by_status_time'].max()
  377. if len(set(df_bms[df_bms['data_status']=='drive']['data_split_by_status_time'])) > 0:
  378. res_record['drive'] = (res_record['drive'])/len(set(df_bms[df_bms['data_status']=='drive']['data_split_by_status_time']))
  379. if len(set(df_bms[df_bms['data_status']=='charge']['data_split_by_status_time'])) > 0:
  380. res_record['charge'] = (res_record['charge'])/len(set(df_bms[df_bms['data_status']=='charge']['data_split_by_status_time']))
  381. if len(set(df_bms[df_bms['data_status']=='stand']['data_split_by_status_time'])) > 0:
  382. res_record['stand'] = (res_record['stand'])/len(set(df_bms[df_bms['data_status']=='stand']['data_split_by_status_time']))
  383. if len(set(df_bms[df_bms['data_status']=='none']['data_split_by_status_time'])) > 0:
  384. res_record['none'] = (res_record['none'])/len(set(df_bms[df_bms['data_status']=='none']['data_split_by_status_time']))
  385. return df_bms, df_gps, res_record
  386. def data_gps_judge_after_combine(self, df_bms, df_gps, time_diff_thre=600, odo_sum_thre=200, drive_spd_thre=80, parking_spd_thre=2):
  387. '''
  388. GPS数据可靠性判断函数2 (基于combine后的分段) 判别方式同data_gps_judge
  389. '''
  390. df_gps['时间戳'] = pd.to_datetime(df_gps['时间戳'])
  391. res_record = {'not charge':0, 'charge':0, 'total':0} # 不可靠的比例
  392. rely_list = []
  393. df_gps['odo_after_combine'] = [None] * len(df_gps)
  394. df_gps['speed_after_combine'] = [None] * len(df_gps)
  395. data_number_list = sorted(list(set(df_bms['data_split_by_status_after_combine'])))
  396. for data_number in data_number_list[:]:
  397. df_sel = df_bms[df_bms['data_split_by_status_after_combine'] == data_number]
  398. df_sel = df_sel.reset_index(drop=True)
  399. # 尝试采用drive段的开始和结束时间选择GPS数据,因为stand时GPS数据可能存在丢失,影响里程的计算
  400. df_sel_drive = df_sel[df_sel['data_status']=='drive'] #
  401. df_sel_drive = df_sel_drive.reset_index(drop=True)
  402. if df_sel_drive.empty:
  403. df_sel_1 = df_sel
  404. else:
  405. df_sel_1 = df_sel_drive
  406. df_sel_gps = df_gps[(df_gps['时间戳']>=df_sel_1.loc[0,'时间戳']) & (df_gps['时间戳']<=df_sel_1.loc[len(df_sel_1)-1,'时间戳'])]
  407. origin_index = list(df_sel_gps.index)
  408. df_sel_gps = df_sel_gps.reset_index(drop=True)
  409. # 如果当前段数据对应的地理位置数据少于2个
  410. if len(df_sel_gps) <= 1:
  411. rely_list.extend([-1]*len(df_sel))
  412. res_record[str(df_sel.loc[0, 'data_status_after_combine'])] = res_record[str(df_sel.loc[0, 'data_status_after_combine'])] + 1
  413. continue
  414. # 如果GPS 起止时间段和BMS数据相差超过阈值
  415. if abs(df_sel_gps.loc[0, '时间戳'] - df_sel_1.loc[0,'时间戳']).total_seconds() > time_diff_thre or \
  416. abs(df_sel_gps.loc[len(df_sel_gps)-1, '时间戳'] - df_sel_1.loc[len(df_sel_1)-1,'时间戳']).total_seconds() > time_diff_thre:
  417. rely_list.extend([-2]*len(df_sel))
  418. res_record[str(df_sel.loc[0, 'data_status_after_combine'])] = res_record[str(df_sel.loc[0, 'data_status_after_combine'])] + 1
  419. continue
  420. # 计算该段数据每两点之间的里程以及速度
  421. dis_array, speed_array = self._cal_odo_speed(df_sel_gps['纬度'], df_sel_gps['经度'], df_sel_gps['时间戳'])
  422. # 如果 累积里程异常 或 平均车速异常 或两点间车速异常
  423. avg_speed = np.sum(dis_array) *3600.0 / abs(df_sel_gps.loc[0, '时间戳'] - df_sel_gps.loc[len(df_sel_gps)-1, '时间戳']).total_seconds()
  424. if np.sum(dis_array) > odo_sum_thre or avg_speed > drive_spd_thre or (speed_array > drive_spd_thre).any():
  425. rely_list.extend([-3]*len(df_sel))
  426. res_record[str(df_sel.loc[0, 'data_status_after_combine'])] = res_record[str(df_sel.loc[0, 'data_status_after_combine'])] + 1
  427. continue
  428. # 如果充电,且 平均时速超过阈值,则不可靠
  429. if str(df_sel.loc[0, 'data_status_after_combine']) == 'charge' and avg_speed > parking_spd_thre:
  430. rely_list.extend([-4]*len(df_sel))
  431. res_record[str(df_sel.loc[0, 'data_status_after_combine'])] = res_record[str(df_sel.loc[0, 'data_status_after_combine'])] + 1
  432. continue
  433. # 剩下的记录为可靠
  434. rely_list.extend([1]*len(df_sel))
  435. df_gps.loc[origin_index[1:], 'odo_after_combine'] = dis_array
  436. df_gps.loc[origin_index[1:], 'speed_after_combine'] = speed_array
  437. df_bms['gps_rely_after_combine'] = rely_list
  438. res_record['total'] = (res_record['not charge'] + res_record['charge'])/df_bms['data_split_by_status_after_combine'].max()
  439. if len(set(df_bms[df_bms['data_status_after_combine']=='not charge']['data_split_by_status_after_combine'])) > 0:
  440. res_record['not charge'] = (res_record['not charge'])/len(set(df_bms[df_bms['data_status_after_combine']=='not charge']['data_split_by_status_after_combine']))
  441. if len(set(df_bms[df_bms['data_status_after_combine']=='charge']['data_split_by_status_after_combine'])) > 0 :
  442. res_record['charge'] = (res_record['charge'])/len(set(df_bms[df_bms['data_status_after_combine']=='charge']['data_split_by_status_after_combine']))
  443. return df_bms, df_gps, res_record