Repository for M.A.I.L system's analysis server.
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.

mailkpi.py 10KB

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