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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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=['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. df = pd.read_csv(filepath)
  17. q1 = df[(df['status']=='in') & (df['x'] >= 0) & (df['y'] >= 0)]
  18. # in q2 is the frame which status is 'in' and value of x is over 0, y is under 0.
  19. q2 = df[(df['status']=='in') & (df['x'] >= 0) & (df['y'] < 0)]
  20. # in q3 is the frame which status is 'in' and value of x, y is under 0.
  21. q3 = df[(df['status']=='in') & (df['x'] < 0) & (df['y'] < 0)]
  22. # in q4 is the frame which status is 'in' and value of x is under 0, y is over 0.
  23. q4 = df[(df['status']=='in') & (df['x'] < 0) & (df['y'] >= 0)]
  24. q1_stroke = []
  25. q1_frame = 0
  26. for x in q1.index:
  27. #find the nearest status 'out' frame before next 'in' frame.
  28. q1_out = df[(df['status']=='out') & (df.index > x)].index[0]
  29. q1_stroke.append((x, q1_out))
  30. q1_frame += q1_out - x
  31. q2_stroke = []
  32. q2_frame = 0
  33. for x in q2.index:
  34. #find the nearest status 'out' frame before next 'in' frame.
  35. q2_out = df[(df['status']=='out') & (df.index > x)].index[0]
  36. q2_stroke.append((x, q2_out))
  37. q2_frame += q2_out - x
  38. q3_stroke = []
  39. q3_frame = 0
  40. for x in q1.index:
  41. #find the nearest status 'out' frame before next 'in' frame.
  42. q3_out = df[(df['status']=='out') & (df.index > x)].index[0]
  43. q3_stroke.append((x, q3_out))
  44. q3_frame += q3_out - x
  45. q4_stroke = []
  46. q4_frame = 0
  47. for x in q4.index:
  48. #find the nearest status 'out' frame before next 'in' frame.
  49. q4_out = df[(df['status']=='out') & (df.index > x)].index[0]
  50. q4_stroke.append((x, q4_out))
  51. q4_frame += q4_out - x
  52. q1_uptime = q1_frame / len(df)
  53. q2_uptime = q2_frame / len(df)
  54. q3_uptime = q3_frame / len(df)
  55. q4_uptime = q4_frame / len(df)
  56. #get 'velocity' of each frame of q1_stroke
  57. q1_velocity = []
  58. for x in q1_stroke:
  59. q1_velocity.append(df['velocity'][x[0]:x[1]].mean())
  60. q1_velocity_mean = np.mean(q1_velocity)
  61. q1_velocity_var = np.var(q1_velocity)
  62. q2_velocity = []
  63. for x in q2_stroke:
  64. q2_velocity.append(df['velocity'][x[0]:x[1]].mean())
  65. q2_velocity_mean = np.mean(q2_velocity)
  66. q2_velocity_var = np.var(q2_velocity)
  67. q3_velocity = []
  68. for x in q3_stroke:
  69. q3_velocity.append(df['velocity'][x[0]:x[1]].mean())
  70. q3_velocity_mean = np.mean(q3_velocity)
  71. q3_velocity_var = np.var(q3_velocity)
  72. q4_velocity = []
  73. for x in q4_stroke:
  74. q4_velocity.append(df['velocity'][x[0]:x[1]].mean())
  75. q4_velocity_mean = np.mean(q4_velocity)
  76. q4_velocity_var = np.var(q4_velocity)
  77. df_20 = np.array_split(df, 20)
  78. #count the number of not 'idle' status in each part
  79. active_rate_20 = []
  80. stroke_20 = []
  81. for x in df_20:
  82. active_rate_20.append(len(x[x['status']!='idle']))
  83. #count the number of 'in' status in each part
  84. stroke_20.append(len(x[x['status']=='in']))
  85. #divde each number by the number of all frames in each part
  86. active_rate_20 = np.array(active_rate_20) / len(df)
  87. dis_20 = []
  88. for x in df_20:
  89. dis_20.append(x['positionvectorvalue'].diff().abs().sum())
  90. stroke_20_sum = np.array(dis_20) / len(df_20)
  91. #make a list of all variables
  92. all_variables = [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]
  93. all_variables.extend(dis_20)
  94. all_variables.extend(active_rate_20)
  95. all_variables.extend(stroke_20_sum)
  96. #add a list to df_all
  97. df_all.loc[len(df_all)] = all_variables
  98. except Exception as e:
  99. print(e)
  100. continue
  101. #save df_all to csv file
  102. df_all.to_csv('kpi.csv', index=False)
  103. if __name__ == '__main__':
  104. try:
  105. main(sys.argv)
  106. except Exception as e:
  107. print(e)
  108. sys.exit(1)