-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_case_visualization.py
302 lines (206 loc) · 9.38 KB
/
error_case_visualization.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import firstHarmonicsAnalysis as fh
from scipy.stats import kurtosis
from scipy.stats import iqr
from scipy.stats import skew
from sklearn.utils import shuffle
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
# train test divide
def train_test_divide(x_data, y_data, ratio = 0.7):#mean_data, std_data, skew_data, median_data, amp_acc, amp_lin
num_sample, num_feature = x_data.shape
x_data, y_data = shuffle(x_data, y_data)
train_size = int(num_sample*ratio)
train_x_data = x_data[:train_size, :]
test_x_data = x_data[train_size:, :]
train_y_data = y_data[:train_size]
test_y_data = y_data[train_size:]
return train_x_data, train_y_data, test_x_data, test_y_data
#feature extraction: mean(7), std(7), skewness(7), median(7), sqrt acc(1), sqrt gyro(1)
def feature_extraction(data, starting_idx_rank, ending_idx_rank, sampling_size, weight, bmi):
#first filter the given data
row, col = data.shape
data = fh.data_removal_trial(data)
#data= fh.datameanremoval(data)
data = fh.filter_data(data)
data = data[int(row*starting_idx_rank) : int(row*ending_idx_rank), :]
new_row, new_col = data.shape
num_features = int(new_row / sampling_size)
#we consider mean, std, skewness, median, magnitude for acc and gyro
mean_features = []
std_features = []
skew_features = []
max_data = []
avg_var = []
kurt_features = []
median_features = []
sqrt_acc_features = []
sqrt_gx_features = []
r_cor1 = []
r_cor2 = []
r_cor3 = []
r_cor4 = []
r_cor5 = []
r_cor6 = []
r_cor7 = []
r_cor8 = []
r_cor9 = []
cf = []
pitch = []
roll = []
yaw = []
iqr_data = []
angleacc = []
angler = []
for idx in range(num_features):
cor = data[idx * sampling_size:(idx + 1) * sampling_size, :]
angleacc.append(fh.anglefinderacc(cor, bmi))
angler.append(fh.anglefinder(cor))
r_cor1.append(fh.correlation_feature(cor[:, 0], cor[:, 3]))
r_cor2.append(fh.correlation_feature(cor[:, 1], cor[:, 4]))
r_cor3.append(fh.correlation_feature(cor[:, 2], cor[:, 5]))
r_cor4.append(fh.correlation_feature(cor[:, 0], cor[:, 1]))
r_cor5.append(fh.correlation_feature(cor[:, 1], cor[:, 2]))
r_cor6.append(fh.correlation_feature(cor[:, 2], cor[:, 1]))
r_cor7.append(fh.correlation_feature(cor[:, 3], cor[:, 4]))
r_cor8.append(fh.correlation_feature(cor[:, 4], cor[:, 5]))
r_cor9.append(fh.correlation_feature(cor[:, 5], cor[:, 3]))
pitch.append(fh.pitch_calculation(cor))
roll.append(fh.roll_calculation(cor))
yaw.append(fh.yaw_calculation(cor))
mean_features.append(np.mean(data[idx*sampling_size:(idx+1)*sampling_size, :], axis=0))
_temp = (np.mean(data[idx * sampling_size:(idx + 1) * sampling_size, :], axis=0))
kurt_features.append(kurtosis(data[idx*sampling_size:(idx+1)*sampling_size, :], axis=0))
std_features.append(np.std(data[idx*sampling_size:(idx+1)*sampling_size, :], 0))
skew_features.append(skew(data[idx*sampling_size:(idx+1)*sampling_size, :], axis=0, bias=True))
median_features.append(np.median(data[idx*sampling_size:(idx+1)*sampling_size, :], axis = 0))
square_matrix = np.square(data[idx*sampling_size:(idx+1)*sampling_size, :])
acc_square_matrix = square_matrix[:, [0, 1, 2]]
gx_square_matrix = square_matrix[:,[3, 4, 5]]
acc_square_matrix = np.mean(np.sum(acc_square_matrix, axis = 1))
gx_square_matrix = np.mean(np.sum(gx_square_matrix,axis=1))
sqrt_acc = (np.sqrt(acc_square_matrix)) * weight
sqrt_gx = (np.sqrt(gx_square_matrix))
sqrt_acc_features.append(sqrt_acc)
sqrt_gx_features.append(sqrt_gx)
new_mean_feature = [[x[0], x[3]] for x in mean_features]
new_std_feature = [[x[0], x[3]] for x in std_features]
new_skew_feature = [[x[0], x[3]] for x in skew_features]
new_kurt_feature = [[x[0], x[3]] for x in kurt_features]
new_cf_feature = [[x[0], x[3]] for x in cf]
return [np.array(mean_features),
np.array(std_features),
np.array(skew_features),
np.array(kurt_features),
np.array(sqrt_acc_features).reshape(-1, 1),
np.array(pitch).reshape(-1, 1),
np.array(roll).reshape(-1, 1),
np.array(r_cor1).reshape(-1, 1),
np.array(r_cor2).reshape(-1, 1),
np.array(r_cor3).reshape(-1, 1),
np.array(r_cor4).reshape(-1, 1),
np.array(r_cor5).reshape(-1, 1),
np.array(r_cor6).reshape(-1, 1),
np.array(r_cor7).reshape(-1, 1),
np.array(r_cor8).reshape(-1, 1),
np.array(r_cor9).reshape(-1, 1),
np.array(yaw).reshape(-1,1)]
# feature extraction & labeling"""
def activity_feature_extraction(path, starting_idx_rank, ending_idx_rank, sampling_size):
#path: path to a certain activity
data_file_list = os.listdir(path)
print(data_file_list)
for idx, file_name in enumerate(data_file_list):
data_file_path = os.path.join(path, file_name)
str_one = file_name
k = (str_one.split("_"))
weigh = k[2]
height = k[1]
height = float(height)
height = height/100
weigh = weigh.split(".")
weight = float(weigh[0])
bmi = weight/(height* height)
data = pd.read_csv(data_file_path, delimiter = ',').values[1:, 1:]
data = data.astype(float)
features = feature_extraction(data, starting_idx_rank, ending_idx_rank, sampling_size, weight, bmi)
for feature_idx in range(len(features)):
if feature_idx == 0:
concatenated_features = features[feature_idx]
else:
concatenated_features = np.concatenate((concatenated_features, features[feature_idx]), axis = 1)
if idx == 0:
activity_features = concatenated_features
num_row, _ = features[0].shape
label = np.zeros((num_row, 2))
label[:, 0] = idx
label[:, 1] = range(num_row)
labels = label
else:
activity_features = np.concatenate((activity_features, concatenated_features), axis = 0)
num_row, _ = features[0].shape
label = np.zeros((num_row, 2))
label[:, 0] = idx
label[:, 1] = range(num_row)
labels = np.concatenate((labels, label), axis = 0)
return activity_features, labels
#load feature, label data
walk_features = np.load('walk_features_vis.npy')
walk_labels = np.load('walk_labels_vis.npy')
#set the # of estimators
num_estimators = 500
#divide train and test data (labels)
train_x_data, train_y_data, test_x_data, test_y_data = train_test_divide(walk_features, walk_labels)
train_y_data = train_y_data[:, 0]
test_y_data, test_y_data_idx = test_y_data[:, 0], test_y_data[:, 1]
#number of unique users in our system
num_participants = len(np.unique(walk_labels))
train_y_data = train_y_data.ravel()
test_y_data = test_y_data.ravel()
print('Train Start!')
rfc = RandomForestClassifier(n_estimators=num_estimators, n_jobs = -1)
rfc.fit(train_x_data, train_y_data)
print('Train End!')
train_score = rfc.score(train_x_data, train_y_data)
test_score = rfc.score(test_x_data, test_y_data)
print("rfc train score: ", train_score)
print("rfc test score: ", test_score)
p = rfc.predict(test_x_data)
user_name_list = test_y_data[np.where(p != test_y_data)[0]]
user_data_idx = test_y_data_idx[np.where(p != test_y_data)[0]]
img_save_path = 'img/error'
if not os.path.exists(img_save_path):
os.makedirs(img_save_path)
walk_data_path = 'processed_data/walk'
starting_idx_rank = 0.05# Cuting off the First 5% of Data
ending_idx_rank = 0.95# Cutting off the End 5% of Data
sampling_size = 100#maybe you should use different sampling size according to the type of activities
data_file_list = os.listdir(walk_data_path)
for user_name, data_idx in zip(user_name_list, user_data_idx):
if data_idx >= 3:
file_name = data_file_list[int(user_name)]
data = pd.read_csv(os.path.join(walk_data_path, file_name), delimiter = ',').values[1:, 1:]
data = data.astype(float)
row, col = data.shape
data = fh.data_removal_trial(data)
data = fh.filter_data(data)
data = data[int(row*starting_idx_rank) : int(row*ending_idx_rank), :]
figure, axes = plt.subplots(6, sharex = True)
data_name = ['Acc X', 'Acc Y', 'Acc Z', 'Gyro X', 'Gyro Y', 'Gyro Z']#, 'Heart Rate'
for idx, ax in enumerate(axes):
new_data = data[int((int(data_idx)-2.5))*sampling_size:int((int(data_idx)+2.5))*sampling_size, idx]
ax.plot(new_data)
ax.set_title(data_name[idx])
ax.set_yticks([int(new_data.min()), int(new_data.max())])
ax.axvline(100, color = 'k', linestyle = 'dotted', label = 'Period')
ax.axvline(200, color = 'k', linestyle = 'dotted')
ax.axvline(300, color = 'k', linestyle = 'dotted')
ax.axvline(400, color = 'k', linestyle = 'dotted')
plt.tight_layout()
plt.savefig(os.path.join(img_save_path, file_name.split('.')[0] + '_' + str(data_idx) + '.pdf'))
plt.savefig(os.path.join(img_save_path, file_name.split('.')[0] + '_' + str(data_idx) + '.png'))
plt.close()
#end