Bläddra i källkod

update path of modules

V1.2
Jeong Geol Kim 3 år sedan
förälder
incheckning
cad8c920d5
5 ändrade filer med 224 tillägg och 1 borttagningar
  1. 143
    0
      modules/coordsi.py
  2. 34
    0
      modules/wavacc.py
  3. 1
    0
      script/createhandletrace.py
  4. 44
    0
      toysnippet/moduleimprot.ipynb
  5. 2
    1
      toysnippet/sandbox.ipynb

+ 143
- 0
modules/coordsi.py Visa fil

1
+import os, sys
2
+import pandas as pd
3
+import numpy as np
4
+import sklearn as sk
5
+from sklearn import preprocessing
6
+import math
7
+'''
8
+coordsi.py is a module for convert coordinates to SI unit.
9
+
10
+Warning: This module convert coordinates with heuristic method.
11
+This moudle will be deprecated
12
+'''
13
+
14
+def readCam (filepath):
15
+    '''
16
+    Create a dataframe from cam*.csv file.
17
+    You must specify camera id. default is 1.
18
+    '''
19
+    try:
20
+        df = pd.read_csv(filepath, header=0, dtype={'x': np.float64, 'y': np.float64, 'z': np.float64, 'frame': np.int64})
21
+        return df
22
+    except Exception as e:
23
+        print(e)
24
+
25
+def summaryOriginCoord (root, camid=0):
26
+    '''
27
+    Show brief information of listed surgeon coordinate.
28
+    '''
29
+    d = root
30
+    try:
31
+        if camid == 1 or camid == 2 :
32
+            df = pd.DataFrame(columns=['approx_total_frame', 'x_min', 'x_max', 'y_min', 'y_max', 'z_min', 'z_max'])
33
+            subdirs = [x[0] for x in os.walk(d)]
34
+            for d in subdirs[1:]:
35
+                row = []
36
+                #get a list of files in directory
37
+                files = os.listdir(d)
38
+                count_svo = len([s for s in files if "_1.csv" in s])
39
+                filpath = os.path.join(d,'cam'+str(camid)+'.csv')
40
+                if os.path.isfile(filpath):
41
+                    arr = readCam(os.path.join(d,'cam'+str(camid)+'.csv'))
42
+                    row = [3600 *count_svo, arr['x'].min(), arr['x'].max(), arr['y'].min(), arr['y'].max(), arr['z'].min(), arr['z'].max()]
43
+                    df.loc[d] = row
44
+                else:
45
+                    print('Warning: '+filpath+' is not exist.')
46
+                    continue
47
+            return df
48
+        else:
49
+            print("Error: Camera ID must be 1 or 2. type camid=1 or camid=2 after dirpath")
50
+    except Exception as e:
51
+        print(e)
52
+
53
+    
54
+
55
+
56
+def convertToSI (origin, w, l, h):
57
+    '''
58
+    Waring: This function will be deprecated!
59
+    Convert coordinates to SI unit with w, l, h.
60
+    w, l, h are heuristic value. It will be changed after calibration and each surgeon room will geat unique value.
61
+    '''
62
+    try:
63
+        df = origin.copy()
64
+        #normailze dataframe[0] from -1 to 1
65
+        df['x'] = preprocessing.minmax_scale(df['x'], feature_range=(-(w/2), (w/2)), axis=0, copy=True)
66
+        #normailze dataframe[1] from -0.75 to 0.75
67
+        df['y'] = preprocessing.minmax_scale(df['y'], feature_range=(-(l/2), (l/2)), axis=0, copy=True)
68
+        #subtract dataframe[z] from int(4) and normailze from 0.93m to 1.8m (0 as floor)
69
+        df['z'] = h - df['z']
70
+        df['z'] = preprocessing.minmax_scale(df['z'], feature_range=(0.93,1.8), axis=0, copy=True)
71
+        #change column order x, y to y, x
72
+        df = df[['y', 'x', 'z', 'frame']]
73
+        #change column name y to X, x to Y
74
+        df = df.rename(columns={'y': 'x', 'x': 'y'})
75
+        return df
76
+    except Exception as e:
77
+        print(e)
78
+
79
+def getVelocity (dataframe):
80
+    try:
81
+        df  = dataframe
82
+        df['positionvectorvalue'] = (df['x']**2 + df['y']**2 + df['z']**2)
83
+        buffer1, buffer2 = df['positionvectorvalue'].copy(), df['positionvectorvalue'].copy()
84
+        buffer1, buffer2 = buffer1.to_frame(), buffer2.to_frame()
85
+        buffer1, buffer2 = list(buffer1.loc[:, 'positionvectorvalue']), list(buffer2.loc[:, 'positionvectorvalue'])
86
+        buffer3 = [0.0]
87
+        for x in range(0, len(buffer2)-1):
88
+            buffer3.append(buffer2[x])
89
+        buffer3  = np.array(buffer1) - np.array(buffer3)
90
+        for x in range(0, len(buffer3)):
91
+            buffer3[x] = math.sqrt(abs(buffer3[x]))
92
+        buffer3[0] = 0.0
93
+        df['velocity'] = buffer3
94
+        return df
95
+    except Exception as e:
96
+        print(e)
97
+
98
+def getAcceleration (dataframe):
99
+    try:
100
+        df = dataframe
101
+        buffer1, buffer2 = df['velocity'].copy(), df['velocity'].copy()
102
+        buffer1, buffer2 = buffer1.to_frame(), buffer2.to_frame()
103
+        buffer1, buffer2 = list(buffer1.loc[:, 'velocity']), list(buffer2.loc[:, 'velocity'])
104
+        buffer3 = [0.0]
105
+        for x in range(0, len(buffer2)-1):
106
+            buffer3.append(buffer2[x])
107
+        buffer3  = np.array(buffer1) - np.array(buffer3)
108
+        buffer3[1] = 0.0
109
+        df['acceleration'] = buffer3
110
+        return df
111
+    except Exception as e:
112
+        print(e)
113
+
114
+def getFullTrace (dataframe, approx_total_frame):
115
+    try:
116
+        df = dataframe
117
+        total_frame = int(approx_total_frame)
118
+        handletrace = pd.DataFrame(columns=['x', 'y', 'z', 'frame', 'positionvectorvalue', 'velocity', 'acceleration', 'isValid'])
119
+        handletrace = pd.concat([handletrace, pd.DataFrame(np.zeros((total_frame,8)), columns=['x', 'y', 'z', 'frame', 'positionvectorvalue','velocity', 'acceleration', 'isValid'])], axis=0)
120
+        for x in range(0, len(df)):
121
+            handletrace.loc[df.loc[x, 'frame']] = df.loc[x]
122
+        handletrace['isValid'] = handletrace['isValid'].fillna(0)
123
+        return handletrace
124
+    except Exception as e:
125
+        print(e)
126
+
127
+def addValidDetection (dataframe, threshold = 25.0):
128
+    '''
129
+    threshold value is suitable for only 1st surgeon room(Seoul 801).
130
+    We can make threshold in every surgeon room by using epipolar geometry.(but needs more cameras with different positions)
131
+    '''
132
+    try:
133
+        df = dataframe
134
+        top = threshold
135
+        bottom = -threshold
136
+        for x in range(0, len(df)):
137
+            if bottom < df['acceleration'][x] < top:
138
+                df['isValid'][x] = 1
139
+            else:
140
+                df['isValid'][x] = 0
141
+        return df
142
+    except Exception as e:
143
+        print(e)        

+ 34
- 0
modules/wavacc.py Visa fil

1
+import sys, os, math, time
2
+import numpy as np
3
+import pandas as pd
4
+'''
5
+wavacc.py for interpolate accelerometer data and extract feature as stroke.
6
+Denosing step is not implemented this module. See addValidDetection function in coordsi.py for denoising.
7
+'''
8
+def interpolation(df):
9
+    try:
10
+        #implement data here
11
+    except Exception as e:
12
+        print(e)
13
+        return df
14
+    
15
+def decomposition(df):
16
+    try:
17
+        #implemnet decompositino with wavelet transform here
18
+    except Exception as e:
19
+        print(e)
20
+        return df
21
+
22
+def getStroke(df):
23
+    try:
24
+        #implement get stroke here
25
+    except Exception as e:
26
+        print(e)
27
+        return df
28
+
29
+def mergeToDataFrame(df):
30
+    try:
31
+        #implement merge to dataframe here
32
+    except Exception as e:
33
+        print(e)
34
+        return df

+ 1
- 0
script/createhandletrace.py Visa fil

1
 import os, sys
1
 import os, sys
2
 import pandas as pd
2
 import pandas as pd
3
 import numpy as np
3
 import numpy as np
4
+sys.path.append('..')
4
 import modules.coordsi as csi
5
 import modules.coordsi as csi
5
 import math
6
 import math
6
 import time
7
 import time

+ 44
- 0
toysnippet/moduleimprot.ipynb Visa fil

1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 1,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import sys\n",
10
+    "sys.path.append('..')\n",
11
+    "import modules.coordsi as csi"
12
+   ]
13
+  },
14
+  {
15
+   "cell_type": "code",
16
+   "execution_count": null,
17
+   "metadata": {},
18
+   "outputs": [],
19
+   "source": []
20
+  }
21
+ ],
22
+ "metadata": {
23
+  "kernelspec": {
24
+   "display_name": "Python 3",
25
+   "language": "python",
26
+   "name": "python3"
27
+  },
28
+  "language_info": {
29
+   "codemirror_mode": {
30
+    "name": "ipython",
31
+    "version": 3
32
+   },
33
+   "file_extension": ".py",
34
+   "mimetype": "text/x-python",
35
+   "name": "python",
36
+   "nbconvert_exporter": "python",
37
+   "pygments_lexer": "ipython3",
38
+   "version": "3.8.0"
39
+  },
40
+  "orig_nbformat": 4
41
+ },
42
+ "nbformat": 4,
43
+ "nbformat_minor": 2
44
+}

+ 2
- 1
toysnippet/sandbox.ipynb Visa fil

9
     "import os, sys\n",
9
     "import os, sys\n",
10
     "import pandas as pd\n",
10
     "import pandas as pd\n",
11
     "import numpy as np\n",
11
     "import numpy as np\n",
12
-    "import coordsi as csi\n",
12
+    "sys.path.append('../')\n",
13
+    "import modules.coordsi as csi\n",
13
     "import math\n",
14
     "import math\n",
14
     "import time"
15
     "import time"
15
    ]
16
    ]

Laddar…
Avbryt
Spara