GpsRank.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pandas as pd
  2. import numpy as np
  3. from datetime import datetime
  4. from datetime import timedelta
  5. def cal_gps_score(df):
  6. '''在获取信号,优、良、合格、掉线的比例之后,计算gps的总评分'''
  7. score=0
  8. for index in range(len(df)):
  9. time_percent=df.loc[index,'累计时间占比']
  10. if df.loc[index,'GPS质量']=='优':
  11. score+=time_percent*0
  12. elif df.loc[index,'GPS质量']=='良':
  13. score+=time_percent*0.3
  14. elif df.loc[index,'GPS质量']=='合格':
  15. score+=time_percent*0.5
  16. elif df.loc[index,'GPS质量']=='掉线':
  17. score+=time_percent*1
  18. return (1-score)*100
  19. def gps_rank(df_gps_signal_table,df_gps,signal_rank,dist_factor):
  20. '''gps信号质量分析函数,需要输入表格,df_gps,信号等级,权重'''
  21. gps_signal_condition=(df_gps['gps_signal']==signal_rank)
  22. dist=df_gps.loc[gps_signal_condition,'distance'].values.sum()
  23. deltatime=df_gps.loc[gps_signal_condition,'deltatime'].values.sum()
  24. df_gps_signal_table_condition=(df_gps_signal_table['gps_signal']==signal_rank)
  25. df_gps_signal_table.loc[df_gps_signal_table_condition,'accum_distance']=dist/1000
  26. df_gps_signal_table.loc[df_gps_signal_table_condition,'accum_deltatime']=deltatime
  27. df_gps_signal_table.loc[df_gps_signal_table_condition,'accum_distance_factor']=dist/1000*dist_factor
  28. return df_gps_signal_table
  29. def get_df_gps_score(starttime,endtime,df_gps):
  30. '''对df_gps中的gps质量进行评分,返回一个数值'''
  31. test_start_time=starttime#'2021-05-29 17:16:39'
  32. test_end_time=endtime#'2021-05-29 20:08:08'
  33. test_time_condition=(df_gps['time']>test_start_time)&(df_gps['time']<test_end_time)
  34. df_gps_test=df_gps.loc[test_time_condition,:].copy()
  35. df_gps_test=df_gps_test.reset_index(drop=True)#重置index
  36. #按照deltatime打标签
  37. gps_deltatime_bins=[0,30,60,120,10000]#优-良-合格-掉线
  38. name=['优','良','合格','掉线']
  39. df_gps_test['gps_signal']=pd.cut(df_gps_test['deltatime'], gps_deltatime_bins,labels=name)
  40. df_gps_test['gps_signal'].value_counts()
  41. #声明一个gps信号按类别统计table
  42. df_gps_signal_table=pd.DataFrame()
  43. df_gps_signal_table['gps_signal']=df_gps_test['gps_signal'].value_counts().index.tolist()
  44. df_gps_signal_table['num']=df_gps_test['gps_signal'].value_counts().values.tolist()
  45. #分类进行统计
  46. df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'优',1.00)
  47. df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'良',1.05)
  48. df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'合格',1.2)
  49. df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'掉线',1)
  50. #次数占比,时间占比
  51. all_num=df_gps_signal_table['num'].sum()
  52. df_gps_signal_table['num_percent']=df_gps_signal_table['num']/all_num
  53. all_accum_deltatime=df_gps_signal_table['accum_deltatime'].sum()
  54. df_gps_signal_table['accum_deltatime_percent']=df_gps_signal_table['accum_deltatime']/all_accum_deltatime
  55. #选择参数
  56. df_gps_signal_table=df_gps_signal_table[['gps_signal','num','num_percent','accum_distance',
  57. 'accum_distance_factor','accum_deltatime','accum_deltatime_percent']]
  58. df_gps_signal_table=df_gps_signal_table.rename(columns={'gps_signal':'GPS质量','num':'数量','num_percent':'数量占比',
  59. 'accum_distance':'累计里程','accum_distance_factor':'累计里程修正值',
  60. 'accum_deltatime':'累计时间','accum_deltatime_percent':'累计时间占比'})
  61. df_gps_signal_table.loc[:,['GPS质量','累计时间','累计时间占比']]
  62. gps_score=cal_gps_score(df_gps_signal_table)#调用函数计算gps评分
  63. #输出结果,评分
  64. #print('From '+test_start_time+' to '+test_end_time)
  65. #print('GPS信号质量评分:'+str(gps_score))
  66. return gps_score