Repository for M.A.I.L system's analysis server.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

kpi.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import os
  2. import time
  3. import math
  4. import sys
  5. import numpy as np
  6. from numpy.lib.function_base import append
  7. import pandas as pd
  8. from pandas import DataFrame as df
  9. from sklearn.cluster import DBSCAN
  10. import pymssql
  11. #import matplotlib.pyplot as plt
  12. ssd_dir = str(sys.argv[1])
  13. file_path = '/hdd/' + ssd_dir[5:]
  14. count = 1
  15. def put_kpi_to_db(kpi):
  16. conn = pymssql.connect(server='52.231.39.219',port=1433, user='mc365', password='tkadbrdhmc1!', database='master')
  17. cursor = conn.cursor()
  18. cursor.execute("INSERT INTO tsfmc_mailsystem.dbo.KPI(surgeryID, totalStrokeCount, totalStrokeVelocity, totalStrokeDepth, upDownTotalStrokeCount, upDownTotalStrokeVelocity, upDownTotalStrokeDepth, leftRightTotalStrokeCount, leftRightTotalStrokeVelocity, leftRightTotalStrokeDepth, quadrant1TotalStrokeCount, quadrant1TotalStrokeVelocity, quadrant1TotalStrokeDepth, quadrant2TotalStrokeCount, quadrant2TotalStrokeVelocity, quadrant2TotalStrokeDepth, quadrant3TotalStrokeCount, quadrant3TotalStrokeVelocity, quadrant3TotalStrokeDepth, quadrant4TotalStrokeCount, quadrant4TotalStrokeVelocity, quadrant4TotalStrokeDepth) VALUES ("+str(kpi[0])+", "+kpi[1]+", "+kpi[2]+", "+kpi[3]+", "+kpi[4]+", "+kpi[5]+", "+kpi[6]+", "+kpi[7]+", "+kpi[8]+", "+kpi[9]+", "+kpi[10]+", "+kpi[11]+", "+kpi[12]+", "+kpi[13]+", "+kpi[14]+", "+kpi[15]+", "+kpi[16]+", "+kpi[17]+", "+kpi[18]+", "+kpi[19]+", "+kpi[20]+", "+kpi[21]+")")
  19. conn.commit()
  20. conn.close()
  21. def get_quad(df):
  22. if df['x'] >= 0 and df['y'] >= 0:
  23. return 1
  24. elif df['x'] >= 0 and df['y'] < 0:
  25. return 2
  26. elif df['x'] < 0 and df['y'] < 0:
  27. return 3
  28. elif df['x'] < 0 and df['y'] >= 0:
  29. return 4
  30. def get_inc_num(df):
  31. global count
  32. prev_count = 0
  33. if abs(df['diff_diff']) > 0.0008:
  34. prev_count = df['inc_num']
  35. df['inc_num'] = count
  36. #print(prev_count)
  37. return count
  38. else:
  39. if prev_count != 0:
  40. count += 1
  41. df['inc_num'] = 0
  42. prev_count = df['inc_num']
  43. return 0
  44. else:
  45. df['inc_num'] = 0
  46. prev_count = df['inc_num']
  47. return 0
  48. def count_inc(df):
  49. e= 0
  50. count = 1
  51. prev_count = 0
  52. for x in df['diff_diff']:
  53. if abs(x) > 8:
  54. df['inc_num'][e] = count
  55. prev_count = count
  56. e +=1
  57. else:
  58. if prev_count != 0:
  59. count += 1
  60. df['inc_num'][e] = 0
  61. prev_count = 0
  62. e +=1
  63. else:
  64. df['inc_num'][e] = 0
  65. prev_count = 0
  66. e +=1
  67. def stroke_count(df):
  68. e = 0
  69. count = 1
  70. for x in df['diff_diff']:
  71. if x > 1.2:
  72. df['stroke_count'][e] = count
  73. e +=1
  74. else:
  75. df['stroke_count'][e] = 0
  76. e+=1
  77. def get_sqrt(df):
  78. e =0
  79. count = 1
  80. prev_x = 0.0
  81. prev_y = 0.0
  82. prev_z = 0.0
  83. for x in range(len(df)):
  84. df['sqrt'][e] = np.sqrt((df['x'][e] - prev_x) **2 + (df['y'][e] - prev_y) ** 2 +(df['z'][e] - prev_z) ** 2)
  85. prev_x = df['x'][e]
  86. prev_y = df['y'][e]
  87. prev_z = df['z'][e]
  88. e += 1
  89. def dist():
  90. file = os.path.join(file_path, 'coordinate.csv')
  91. norm = pd.read_csv(file)
  92. norm = norm.drop(norm.columns[0], axis=1)
  93. norm = norm.fillna(0)
  94. data = norm[['x', 'y', 'z']]
  95. norm['sqrt'] = 0.0
  96. get_sqrt(norm)
  97. #norm = norm.apply(lambda x:np.sqrt(x) if x.name in ['sqrt'] else x, axis =1)
  98. norm['diff'] = norm.diff()['sqrt']
  99. norm['diff_diff'] = norm.diff()['diff']
  100. norm['quad'] = norm.apply(get_quad, axis =1)
  101. norm = norm.fillna(0)
  102. norm['inc_num'] = 0
  103. #norm['inc_num'] = norm.apply(get_inc_num, axis = 1)
  104. count_inc(norm)
  105. norm['stroke_count'] = 0
  106. stroke_count(norm)
  107. #print('num of inc')
  108. #print(norm['inc_num'].unique())
  109. #print('total stroke')
  110. #print(norm['stroke_count'].unique())
  111. #print(norm)
  112. #plt.plot(norm['frame'],norm['diff_diff'])
  113. #plt.savefig('/sources/diff_diff.png')
  114. kpi = pd.DataFrame(index=range(0,1))
  115. quad_count = norm[norm['stroke_count'] != 0]
  116. quad1_c = quad_count[quad_count['quad'] == 1]
  117. quad2_c = quad_count[quad_count['quad'] == 2]
  118. quad3_c = quad_count[quad_count['quad'] == 3]
  119. quad4_c = quad_count[quad_count['quad'] == 4]
  120. #print(len(quad1_c))
  121. #print(len(quad2_c))
  122. #print(len(quad3_c))
  123. #print(len(quad4_c))
  124. #print(quad1_c)
  125. kpi['PK'] = ssd_dir[5:]
  126. kpi['qaud1_c'] = len(quad1_c)
  127. kpi['qaud2_c'] = len(quad2_c)
  128. kpi['qaud3_c'] = len(quad3_c)
  129. kpi['qaud4_c'] = len(quad4_c)
  130. #FIXME: this values are huristic value. please change this values after research
  131. kpi['qaud1_s'] = quad1_c['sqrt'].mean() / 30
  132. kpi['qaud2_s'] = quad2_c['sqrt'].mean() / 30
  133. kpi['qaud3_s'] = quad3_c['sqrt'].mean() / 30
  134. kpi['qaud4_s'] = quad4_c['sqrt'].mean() / 30
  135. kpi['qaud1_d'] = quad1_c['sqrt'].mean() / 45
  136. kpi['qaud2_d'] = quad2_c['sqrt'].mean() / 45
  137. kpi['qaud3_d'] = quad3_c['sqrt'].mean() / 45
  138. kpi['qaud4_d'] = quad4_c['sqrt'].mean() / 45
  139. kpi_1 = {
  140. "stroke": [len(quad1_c),len(quad2_c),len(quad3_c),len(quad4_c)],
  141. "velocity": [quad1_c['sqrt'].mean() / 30, quad2_c['sqrt'].mean() / 30, quad3_c['sqrt'].mean() / 30, quad4_c['sqrt'].mean() / 30],
  142. "depth": [quad1_c['sqrt'].mean() / 45, quad2_c['sqrt'].mean() / 45, quad3_c['sqrt'].mean() / 45, quad4_c['sqrt'].mean() / 45]
  143. }
  144. #print(kpi)
  145. val_list = []
  146. #PK =
  147. val_list.append(str(ssd_dir[5:-1]))
  148. #t_stroke =
  149. val_list.append(sum(kpi_1['stroke']))
  150. #t_vel
  151. val_list.append("{:.4f}".format(sum(kpi_1['velocity'])/4))
  152. #t_dep
  153. val_list.append("{:.4f}".format(sum(kpi_1['depth'])/4))
  154. #ud_s_rate =
  155. val_list.append("{:.4f}".format((kpi_1['stroke'][0] + kpi_1['stroke'][3] - kpi_1['stroke'][1] - kpi_1['stroke'][2]) / sum(kpi_1['stroke'])*100))
  156. #ud_v_rate =
  157. val_list.append("{:.4f}".format((kpi_1['velocity'][0] + kpi_1['velocity'][3] - kpi_1['velocity'][1] - kpi_1['velocity'][2])/2))
  158. #ud_d_rate =
  159. val_list.append("{:.4f}".format((kpi_1['depth'][0] + kpi_1['depth'][3] - kpi_1['depth'][1] - kpi_1['depth'][2])/2))
  160. #lr_s_rate =
  161. val_list.append("{:.4f}".format((kpi_1['stroke'][2] + kpi_1['stroke'][3] - kpi_1['stroke'][0] - kpi_1['stroke'][1]) / sum(kpi_1['stroke'])*100))
  162. #lr_v_rate =
  163. val_list.append("{:.4f}".format((kpi_1['velocity'][2] + kpi_1['velocity'][3] - kpi_1['velocity'][0] - kpi_1['velocity'][1])/2))
  164. #lr_d_rate =
  165. val_list.append("{:.4f}".format((kpi_1['depth'][2] + kpi_1['depth'][3] - kpi_1['depth'][0] - kpi_1['depth'][1])/2))
  166. #q1 s, v, d
  167. val_list.append(len(quad1_c))
  168. val_list.append(quad1_c['sqrt'].mean() / 30)
  169. val_list.append(quad1_c['sqrt'].mean() / 45)
  170. val_list.append(len(quad2_c))
  171. val_list.append(quad2_c['sqrt'].mean() / 30)
  172. val_list.append(quad2_c['sqrt'].mean() / 45)
  173. val_list.append(len(quad3_c))
  174. val_list.append(quad3_c['sqrt'].mean() / 30)
  175. val_list.append(quad3_c['sqrt'].mean() / 45)
  176. val_list.append(len(quad4_c))
  177. val_list.append(quad4_c['sqrt'].mean() / 30)
  178. val_list.append(quad4_c['sqrt'].mean() / 45)
  179. return val_list
  180. #print(val_list)
  181. if __name__ == "__main__":
  182. kpi = dist()
  183. put_kpi_to_db(kpi)