-
Notifications
You must be signed in to change notification settings - Fork 2
/
test3.py
175 lines (146 loc) · 10.9 KB
/
test3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 29 00:07:11 2015
@author: Shamir
"""
def CalculateValidData():
# Calculate the number of missing values in the array
number_of_nan = len(readFile.values[m][pandas.isnull(readFile.values[m])])
length_of_array = len(readFile.values[m])
valid_datapoints = length_of_array - number_of_nan
return valid_datapoints
for i in range(len(os.listdir(sourcePath))): # we have 6 files corresponding to 6 gestures
print 'i = ', i
gesture = os.listdir(sourcePath)[i] # Jab, Uppercut, Throw, Jets, Block, Asgard
#dataset = os.listdir(sourcePath + gesture)[0] # Train, Cross Validation, Test
copy = False
AngVel_array = []
for k in range(len(os.listdir(sourcePath + gesture))):
sensor = os.listdir(sourcePath + gesture)[k] # Sensor15, Sensor16, Sensor17, Sensor18, Sensor19
sensorFolder = os.listdir(sourcePath + gesture + backslash + sensor)
print sensorFolder
for l in range(len(sensorFolder)):
csvfile = sourcePath + gesture + backslash + sensor + backslash + sensorFolder[l] # full filepath
readFile = pandas.read_csv(csvfile, header = None)
readFile.values[1:] = readFile.values[1:].astype(float)
velocityAlpha = ['Precession_' + sensor[6:]]
velocityBeta = ['Nutation_' + sensor[6:]]
velocityGamma = ['Spin_' + sensor[6:]]
#print velocityAlpha
velocityAlpha = np.asarray(velocityAlpha)
velocityBeta = np.asarray(velocityBeta)
velocityGamma = np.asarray(velocityGamma)
#time = np.shape(readFile.values)[1] / frequency_euc
if copy == True:
print 'This is the If phase'
for m in range(1, len(readFile.values)): # for every two files ???
## need to add code to check if number_of_rows matches
precession, nutation, spin = 0, 0, 0
for n in range(0, np.shape(readFile.values)[1] - 5, 3):
alpha = n
beta = n + 1
gamma = n + 2
alphaNext = n + 3
betaNext = n + 4
gammaNext = n + 5
try:
precession += euclidean(readFile.values[m, alpha], readFile.values[m, alphaNext])
#print 'precession = ', precession
nutation += euclidean(readFile.values[m, beta], readFile.values[m, betaNext])
spin += euclidean(readFile.values[m, gamma], readFile.values[m, gammaNext])
except ValueError:
#print '1st catch (copy = True) at file, m, n = ', csvfile[-6:], m, n
break
valid_data = CalculateValidData() # Exclude missing values (we exclude 6 more values to remain within a safer margin)
time = valid_data / frequency_euc
precessionVelocity = precession/time
#print 'precessionVelocity = ', precessionVelocity
nutationVelocity = nutation/time
spinVelocity = spin/time
for n in range(0, np.shape(readFile.values)[1] - 3, 3):
alpha = n
beta = n + 1
gamma = n + 2
try:
readFile.values[m, alpha] = (precessionVelocity * np.sin(readFile.values[m, gamma]) * np.sin(readFile.values[m, beta])) + (nutationVelocity * np.cos(readFile.values[m, gamma])) # alpha component
readFile.values[m, beta] = (precessionVelocity * np.cos(readFile.values[m, gamma]) * np.sin(readFile.values[m, beta])) - (nutationVelocity * np.sin(readFile.values[m, gamma])) # beta component
readFile.values[m, beta] = (precessionVelocity * np.cos(readFile.values[m, beta])) * spinVelocity # gamma compomemt
except ValueError:
#print '2nd catch (copy = True) at file, m, n = ', csvfile[-6:], m, n
continue
averageAlpha = np.sum(readFile.values[m, range(0, valid_data, 3)]) / time
averageBeta = np.sum(readFile.values[m, range(1, valid_data, 3)]) / time
averageGamma = np.sum(readFile.values[m, range(2, valid_data, 3)]) / time
velocityAlpha = np.vstack((velocityAlpha, averageAlpha))
#print 'filename, m, velocityAlpha = ', csvfile[-6:], m, velocityAlpha
velocityBeta = np.vstack((velocityBeta, averageBeta))
velocityGamma = np.vstack((velocityGamma, averageGamma))
columnSize = len(velocityAlpha)
angular_velocity = np.zeros((len(velocityAlpha), 3))
angular_velocity = angular_velocity.astype(str) # to avoid string to float conversion error
# Return the column vectors in a single 2D array
angular_velocity[:,0] = velocityAlpha.reshape(1, columnSize)
angular_velocity[:,1] = velocityBeta.reshape (1, columnSize)
angular_velocity[:,2] = velocityGamma.reshape(1, columnSize)
AngVel_array = np.hstack((AngVel_array, angular_velocity))
#print 'AngVel_array = ', AngVel_array
else:
print 'This is the Else phase'
for m in range(1, len(readFile.values)): # for every two files
## need to add code to check if number_of_rows matches
precession, nutation, spin = 0, 0, 0
for n in range(0, np.shape(readFile.values)[1] - 5, 3):
alpha = n
beta = n + 1
gamma = n + 2
alphaNext = n + 3
betaNext = n + 4
gammaNext = n + 5
try:
precession += euclidean(readFile.values[m, alpha], readFile.values[m, alphaNext])
nutation += euclidean(readFile.values[m, beta], readFile.values[m, betaNext])
spin += euclidean(readFile.values[m, gamma], readFile.values[m, gammaNext])
except ValueError:
#print '1st catch (copy = False) at print file, m, n = ', csvfile[-6:], m, n
continue
valid_data = CalculateValidData()
time = valid_data / frequency_euc
precessionVelocity = precession/time
nutationVelocity = nutation/time
spinVelocity = spin/time
#print 'precession,nutation,spinVelocity = ', precessionVelocity, nutationVelocity, spinVelocity
for n in range(0, np.shape(readFile.values)[1] - 3, 3):
alpha = n
beta = n + 1
gamma = n + 2
try:
readFile.values[m, alpha] = (precessionVelocity * np.sin(readFile.values[m, gamma]) * np.sin(readFile.values[m, beta])) + (nutationVelocity * np.cos(readFile.values[m, gamma])) # alpha component
readFile.values[m, beta] = (precessionVelocity * np.cos(readFile.values[m, gamma]) * np.sin(readFile.values[m, beta])) - (nutationVelocity * np.sin(readFile.values[m, gamma])) # beta component
readFile.values[m, beta] = (precessionVelocity * np.cos(readFile.values[m, beta])) * spinVelocity # gamma compomemt
except ValueError:
#print '2nd catch (copy = True) at file, m, n = ', csvfile[-6:], m, n
continue
averageAlpha = np.sum(readFile.values[m, range(0, valid_data, 3)]) / time
#print 'averageAlpha = ', averageAlpha
averageBeta = np.sum(readFile.values[m, range(1, valid_data, 3)]) / time
averageGamma = np.sum(readFile.values[m, range(2, valid_data, 3)]) / time
velocityAlpha = np.vstack((velocityAlpha, averageAlpha))
#print 'filename, m, velocityAlpha = ', csvfile[-6:], m, velocityAlpha
velocityBeta = np.vstack((velocityBeta, averageBeta))
velocityGamma = np.vstack((velocityGamma, averageGamma))
columnSize = len(velocityAlpha)
angular_velocity = np.zeros((len(velocityAlpha), 3))
angular_velocity = angular_velocity.astype(str)
# Return the column vectors in a single 2D array
angular_velocity[:,0] = velocityAlpha.reshape(1, columnSize)
angular_velocity[:,1] = velocityBeta.reshape (1, columnSize)
angular_velocity[:,2] = velocityGamma.reshape(1, columnSize)
AngVel_array = angular_velocity.copy()
#print 'AngVel_array = ', AngVel_array
copy = True
# Create complete file structure/dataframe
if i == 0:
fullFile4 = DataFrame(AngVel_array)
else:
AngVel_array = DataFrame(AngVel_array)
fullFile4 = pandas.concat([fullFile4, AngVel_array], join = 'inner')