You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

createkpi.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os, sys
  2. sys.path.append('.')
  3. import pandas as pd
  4. import scipy as sp
  5. import numpy as np
  6. def main(input):
  7. if len(input) != 2:
  8. print('Usage: python3 ./scripts/createkpi.py <fulltrace directopry>')
  9. sys.exit(1)
  10. root = input[1]
  11. listdir = os.listdir(root)
  12. df_all = pd.DataFrame(columns=['sid','date','q1','q2','q3','q4','q1_uptime','q2_uptime','q3_uptime','q4_uptime','q1_speed','q2_speed','q3_speed','q4_speed','q1_variance','q2_variance','q3_variance','q4_variance','f01_dis','f02_dis','f03_dis','f04_dis','f05_dis','f06_dis','f07_dis','f08_dis','f09_dis','f10_dis','f11_dis','f12_dis','f13_dis','f14_dis','f15_dis','f16_dis','f17_dis','f18_dis','f19_dis','f20_dis','f01_rate','f02_rate','f03_rate','f04_rate','f05_rate','f06_rate','f07_rate','f08_rate','f09_rate','f10_rate','f11_rate','f12_rate','f13_rate','f14_rate','f15_rate','f16_rate','f17_rate','f18_rate','f19_rate','f20_rate','f01_stroke','f02_stroke','f03_stroke','f04_stroke','f05_stroke','f06_stroke','f07_stroke','f08_stroke','f09_stroke','f10_stroke','f11_stroke','f12_stroke','f13_stroke','f14_stroke','f15_stroke','f16_stroke','f17_stroke','f18_stroke','f19_stroke','f20_stroke'])
  13. for index in listdir:
  14. try:
  15. filepath = os.path.join(root, index)
  16. sid = index[:9]
  17. date = index[9:-4]
  18. df = pd.read_csv(filepath)
  19. q1 = df[(df['status']=='in') & (df['x'] >= 0) & (df['y'] >= 0)]
  20. # in q2 is the frame which status is 'in' and value of x is over 0, y is under 0.
  21. q2 = df[(df['status']=='in') & (df['x'] >= 0) & (df['y'] < 0)]
  22. # in q3 is the frame which status is 'in' and value of x, y is under 0.
  23. q3 = df[(df['status']=='in') & (df['x'] < 0) & (df['y'] < 0)]
  24. # in q4 is the frame which status is 'in' and value of x is under 0, y is over 0.
  25. q4 = df[(df['status']=='in') & (df['x'] < 0) & (df['y'] >= 0)]
  26. q1_stroke = []
  27. q1_frame = 0
  28. for x in q1.index:
  29. #find the nearest status 'out' frame before next 'in' frame.
  30. q1_out = df[(df['status']=='out') & (df.index > x)].index[0]
  31. q1_stroke.append((x, q1_out))
  32. q1_frame += q1_out - x
  33. q2_stroke = []
  34. q2_frame = 0
  35. for x in q2.index:
  36. #find the nearest status 'out' frame before next 'in' frame.
  37. q2_out = df[(df['status']=='out') & (df.index > x)].index[0]
  38. q2_stroke.append((x, q2_out))
  39. q2_frame += q2_out - x
  40. q3_stroke = []
  41. q3_frame = 0
  42. for x in q1.index:
  43. #find the nearest status 'out' frame before next 'in' frame.
  44. q3_out = df[(df['status']=='out') & (df.index > x)].index[0]
  45. q3_stroke.append((x, q3_out))
  46. q3_frame += q3_out - x
  47. q4_stroke = []
  48. q4_frame = 0
  49. for x in q4.index:
  50. #find the nearest status 'out' frame before next 'in' frame.
  51. q4_out = df[(df['status']=='out') & (df.index > x)].index[0]
  52. q4_stroke.append((x, q4_out))
  53. q4_frame += q4_out - x
  54. q1_uptime = q1_frame / len(df)
  55. q2_uptime = q2_frame / len(df)
  56. q3_uptime = q3_frame / len(df)
  57. q4_uptime = q4_frame / len(df)
  58. #get 'velocity' of each frame of q1_stroke
  59. q1_velocity = []
  60. for x in q1_stroke:
  61. q1_velocity.append(df['velocity'][x[0]:x[1]].mean())
  62. q1_velocity_mean = np.mean(q1_velocity)
  63. q1_velocity_var = np.var(q1_velocity)
  64. q2_velocity = []
  65. for x in q2_stroke:
  66. q2_velocity.append(df['velocity'][x[0]:x[1]].mean())
  67. q2_velocity_mean = np.mean(q2_velocity)
  68. q2_velocity_var = np.var(q2_velocity)
  69. q3_velocity = []
  70. for x in q3_stroke:
  71. q3_velocity.append(df['velocity'][x[0]:x[1]].mean())
  72. q3_velocity_mean = np.mean(q3_velocity)
  73. q3_velocity_var = np.var(q3_velocity)
  74. q4_velocity = []
  75. for x in q4_stroke:
  76. q4_velocity.append(df['velocity'][x[0]:x[1]].mean())
  77. q4_velocity_mean = np.mean(q4_velocity)
  78. q4_velocity_var = np.var(q4_velocity)
  79. df_20 = np.array_split(df, 20)
  80. #count the number of not 'idle' status in each part
  81. active_rate_20 = []
  82. stroke_20 = []
  83. for x in df_20:
  84. active_rate_20.append(len(x[x['status']!='idle']))
  85. #count the number of 'in' status in each part
  86. stroke_20.append(len(x[x['status']=='in']))
  87. #divde each number by the number of all frames in each part
  88. active_rate_20 = np.array(active_rate_20) / len(df)
  89. dis_20 = []
  90. for x in df_20:
  91. dis_20.append(x['positionvectorvalue'].diff().abs().sum())
  92. stroke_20_sum = np.array(dis_20) / len(df_20)
  93. #make a list of all variables
  94. all_variables = [sid, date, len(q1), len(q2), len(q3), len(q4), q1_uptime, q2_uptime, q3_uptime, q4_uptime, q1_velocity_mean, q1_velocity_var, q2_velocity_mean, q2_velocity_var, q3_velocity_mean, q3_velocity_var, q4_velocity_mean, q4_velocity_var]
  95. all_variables.extend(dis_20)
  96. all_variables.extend(active_rate_20)
  97. all_variables.extend(stroke_20_sum)
  98. #add a list to df_all
  99. df_all.loc[len(df_all)] = all_variables
  100. except Exception as e:
  101. print(e)
  102. continue
  103. #save df_all to csv file
  104. df_all.to_csv('kpi.csv', index=False)
  105. if __name__ == '__main__':
  106. try:
  107. main(sys.argv)
  108. except Exception as e:
  109. print(e)
  110. sys.exit(1)