import os import time import math import sys import numpy as np from numpy.lib.function_base import append import pandas as pd from pandas import DataFrame as df from sklearn.cluster import DBSCAN import pymssql #import matplotlib.pyplot as plt ssd_dir = str(sys.argv[1]) file_path = '/hdd/' + ssd_dir[5:] count = 1 def put_kpi_to_db(kpi): conn = pymssql.connect(server='52.231.39.219',port=1433, user='mc365', password='tkadbrdhmc1!', database='master') cursor = conn.cursor() 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]+")") conn.commit() conn.close() def get_quad(df): if df['x'] >= 0 and df['y'] >= 0: return 1 elif df['x'] >= 0 and df['y'] < 0: return 2 elif df['x'] < 0 and df['y'] < 0: return 3 elif df['x'] < 0 and df['y'] >= 0: return 4 def get_inc_num(df): global count prev_count = 0 if abs(df['diff_diff']) > 0.0008: prev_count = df['inc_num'] df['inc_num'] = count #print(prev_count) return count else: if prev_count != 0: count += 1 df['inc_num'] = 0 prev_count = df['inc_num'] return 0 else: df['inc_num'] = 0 prev_count = df['inc_num'] return 0 def count_inc(df): e= 0 count = 1 prev_count = 0 for x in df['diff_diff']: if abs(x) > 8: df['inc_num'][e] = count prev_count = count e +=1 else: if prev_count != 0: count += 1 df['inc_num'][e] = 0 prev_count = 0 e +=1 else: df['inc_num'][e] = 0 prev_count = 0 e +=1 def stroke_count(df): e = 0 count = 1 for x in df['diff_diff']: if x > 1.2: df['stroke_count'][e] = count e +=1 else: df['stroke_count'][e] = 0 e+=1 def get_sqrt(df): e =0 count = 1 prev_x = 0.0 prev_y = 0.0 prev_z = 0.0 for x in range(len(df)): df['sqrt'][e] = np.sqrt((df['x'][e] - prev_x) **2 + (df['y'][e] - prev_y) ** 2 +(df['z'][e] - prev_z) ** 2) prev_x = df['x'][e] prev_y = df['y'][e] prev_z = df['z'][e] e += 1 def dist(): file = os.path.join(file_path, 'coordinate.csv') norm = pd.read_csv(file) norm = norm.drop(norm.columns[0], axis=1) norm = norm.fillna(0) data = norm[['x', 'y', 'z']] norm['sqrt'] = 0.0 get_sqrt(norm) #norm = norm.apply(lambda x:np.sqrt(x) if x.name in ['sqrt'] else x, axis =1) norm['diff'] = norm.diff()['sqrt'] norm['diff_diff'] = norm.diff()['diff'] norm['quad'] = norm.apply(get_quad, axis =1) norm = norm.fillna(0) norm['inc_num'] = 0 #norm['inc_num'] = norm.apply(get_inc_num, axis = 1) count_inc(norm) norm['stroke_count'] = 0 stroke_count(norm) #print('num of inc') #print(norm['inc_num'].unique()) #print('total stroke') #print(norm['stroke_count'].unique()) #print(norm) #plt.plot(norm['frame'],norm['diff_diff']) #plt.savefig('/sources/diff_diff.png') kpi = pd.DataFrame(index=range(0,1)) quad_count = norm[norm['stroke_count'] != 0] quad1_c = quad_count[quad_count['quad'] == 1] quad2_c = quad_count[quad_count['quad'] == 2] quad3_c = quad_count[quad_count['quad'] == 3] quad4_c = quad_count[quad_count['quad'] == 4] #print(len(quad1_c)) #print(len(quad2_c)) #print(len(quad3_c)) #print(len(quad4_c)) #print(quad1_c) kpi['PK'] = ssd_dir[5:] kpi['qaud1_c'] = len(quad1_c) kpi['qaud2_c'] = len(quad2_c) kpi['qaud3_c'] = len(quad3_c) kpi['qaud4_c'] = len(quad4_c) #FIXME: this values are huristic value. please change this values after research kpi['qaud1_s'] = quad1_c['sqrt'].mean() / 30 kpi['qaud2_s'] = quad2_c['sqrt'].mean() / 30 kpi['qaud3_s'] = quad3_c['sqrt'].mean() / 30 kpi['qaud4_s'] = quad4_c['sqrt'].mean() / 30 kpi['qaud1_d'] = quad1_c['sqrt'].mean() / 45 kpi['qaud2_d'] = quad2_c['sqrt'].mean() / 45 kpi['qaud3_d'] = quad3_c['sqrt'].mean() / 45 kpi['qaud4_d'] = quad4_c['sqrt'].mean() / 45 kpi_1 = { "stroke": [len(quad1_c),len(quad2_c),len(quad3_c),len(quad4_c)], "velocity": [quad1_c['sqrt'].mean() / 30, quad2_c['sqrt'].mean() / 30, quad3_c['sqrt'].mean() / 30, quad4_c['sqrt'].mean() / 30], "depth": [quad1_c['sqrt'].mean() / 45, quad2_c['sqrt'].mean() / 45, quad3_c['sqrt'].mean() / 45, quad4_c['sqrt'].mean() / 45] } #print(kpi) val_list = [] #PK = val_list.append(str(ssd_dir[5:-1])) #t_stroke = val_list.append(sum(kpi_1['stroke'])) #t_vel val_list.append("{:.4f}".format(sum(kpi_1['velocity'])/4)) #t_dep val_list.append("{:.4f}".format(sum(kpi_1['depth'])/4)) #ud_s_rate = 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)) #ud_v_rate = val_list.append("{:.4f}".format((kpi_1['velocity'][0] + kpi_1['velocity'][3] - kpi_1['velocity'][1] - kpi_1['velocity'][2])/2)) #ud_d_rate = val_list.append("{:.4f}".format((kpi_1['depth'][0] + kpi_1['depth'][3] - kpi_1['depth'][1] - kpi_1['depth'][2])/2)) #lr_s_rate = 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)) #lr_v_rate = val_list.append("{:.4f}".format((kpi_1['velocity'][2] + kpi_1['velocity'][3] - kpi_1['velocity'][0] - kpi_1['velocity'][1])/2)) #lr_d_rate = val_list.append("{:.4f}".format((kpi_1['depth'][2] + kpi_1['depth'][3] - kpi_1['depth'][0] - kpi_1['depth'][1])/2)) #q1 s, v, d val_list.append(len(quad1_c)) val_list.append(quad1_c['sqrt'].mean() / 30) val_list.append(quad1_c['sqrt'].mean() / 45) val_list.append(len(quad2_c)) val_list.append(quad2_c['sqrt'].mean() / 30) val_list.append(quad2_c['sqrt'].mean() / 45) val_list.append(len(quad3_c)) val_list.append(quad3_c['sqrt'].mean() / 30) val_list.append(quad3_c['sqrt'].mean() / 45) val_list.append(len(quad4_c)) val_list.append(quad4_c['sqrt'].mean() / 30) val_list.append(quad4_c['sqrt'].mean() / 45) return val_list #print(val_list) if __name__ == "__main__": kpi = dist() kpi_csv = pd.DataFrame(index=range(0,1)) kpi_csv.loc[0] = kpi kpi_csv.to_csv(os.path.join(file_path,'kpi.csv')) put_kpi_to_db(kpi)