Repository for M.A.I.L system's analysis server.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

kpi.py 8.5KB

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