Просмотр исходного кода

create a script for procedure

V1.2
Jeong Geol Kim 3 лет назад
Родитель
Сommit
60c1135e64
1 измененных файлов: 125 добавлений и 0 удалений
  1. 125
    0
      script/createkpi.py

+ 125
- 0
script/createkpi.py Просмотреть файл

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

Загрузка…
Отмена
Сохранить