-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_withinsubject_handcrafted.py
336 lines (266 loc) · 11.7 KB
/
main_withinsubject_handcrafted.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import torch
from dataset import EMGData
from utils import fix_random_seed
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.svm import SVC
import time
def extract_TD_feats(signals, num_channels):
features = np.zeros((signals.shape[0],num_channels*4),dtype =float)
if torch.is_tensor(signals):
signals = signals.numpy()
features[:,0:num_channels] = getMAVfeat(signals)
features[:,num_channels:2*num_channels] = getZCfeat(signals)
features[:,2*num_channels:3*num_channels] = getSSCfeat(signals)
features[:,3*num_channels:4*num_channels] = getWLfeat(signals)
return features
def extract_TDPSD_feats(signals, num_channels):
# There are 6 features per channel
features = np.zeros((signals.shape[0], num_channels*6), dtype=float)
if torch.is_tensor(signals):
signals = signals.numpy()
# TDPSD feature set adapted from: https://github.com/RamiKhushaba/getTDPSDfeat
# Extract the features from original signal and nonlinear version
ebp = KSM1(signals)
# np.spacing = epsilon (smallest value), done so log does not return inf.
efp = KSM1(np.log(signals**2 + np.spacing(1)))
# Correlation analysis:
num = -2*np.multiply(efp, ebp)
den = np.multiply(efp, efp) + np.multiply(ebp,ebp)
#Feature extraction goes here
features = num-den
return features
def KSM1(signals):
samples = signals.shape[2]
channels = signals.shape[1]
# Root squared zero moment normalized
m0 = np.sqrt(np.sum(signals**2,axis=2))
m0 = m0 ** 0.1 / 0.1
# Prepare derivatives for higher order moments
d1 = np.diff(signals, n=1, axis=2)
d2 = np.diff(d1 , n=1, axis=2)
# Root squared 2nd and 4th order moments normalized
m2 = np.sqrt(np.sum(d1 **2, axis=2)/ (samples-1))
m2 = m2 ** 0.1 / 0.1
m4 = np.sqrt(np.sum(d2**2,axis=2) / (samples-1))
m4 = m4 **0.1/0.1
# Sparseness
sparsi = m0/np.sqrt(np.abs((m0-m2)*(m0-m4)))
# Irregularity factor
IRF = m2/np.sqrt(np.multiply(m0,m4))
# Waveform Length Ratio
WLR = np.sum( np.abs(d1),axis=2)-np.sum(np.abs(d2),axis=2)
Feat = np.concatenate((m0, m0-m2, m0-m4, sparsi, IRF, WLR), axis=1)
Feat = np.log(np.abs(Feat))
return Feat
def extract_LSF4_feats(signals, num_channels):
features = np.zeros((signals.shape[0],num_channels*4),dtype =float)
if torch.is_tensor(signals):
signals = signals.numpy()
features[:,0:num_channels] = getLSfeat(signals)
features[:,num_channels:2*num_channels] = getMFLfeat(signals)
features[:,2*num_channels:3*num_channels] = getMSRfeat(signals)
features[:,3*num_channels:4*num_channels] = getWAMPfeat(signals)
return features
def extract_LSF9_feats(signals, num_channels):
features = np.zeros((signals.shape[0],num_channels*9),dtype =float)
if torch.is_tensor(signals):
signals = signals.numpy()
features[:,0:num_channels] = getLSfeat(signals)
features[:,num_channels:2*num_channels] = getMFLfeat(signals)
features[:,2*num_channels:3*num_channels] = getMSRfeat(signals)
features[:,3*num_channels:4*num_channels] = getWAMPfeat(signals)
features[:,4*num_channels:5*num_channels] = getZCfeat(signals)
features[:,5*num_channels:6*num_channels] = getRMSfeat(signals)
features[:,6*num_channels:7*num_channels] = getIAVfeat(signals)
features[:,7*num_channels:8*num_channels] = getDASDVfeat(signals)
features[:,8*num_channels:9*num_channels] = getVARfeat(signals)
return features
def getMAVfeat(signal):
feat = np.mean(np.abs(signal),2)
return feat
def getZCfeat(signal):
sgn_change = np.diff(np.sign(signal),axis=2)
neg_change = sgn_change == -2
pos_change = sgn_change == 2
feat_a = np.sum(neg_change,2)
feat_b = np.sum(pos_change,2)
return feat_a+feat_b
def getSSCfeat(signal):
d_sig = np.diff(signal,axis=2)
return getZCfeat(d_sig)
def getWLfeat(signal):
feat = np.sum(np.abs(np.diff(signal,axis=2)),2)
return feat
def getLSfeat(signal):
feat = np.zeros((signal.shape[0],signal.shape[1]))
for w in range(0, signal.shape[0],1):
for c in range(0, signal.shape[1],1):
tmp = lmom(np.reshape(signal[w,c,:],(1,signal.shape[2])),2)
feat[w,c] = tmp[0,1]
return feat
def lmom(signal, nL):
# same output to matlab when ones vector of various sizes are input
b = np.zeros((1,nL-1))
l = np.zeros((1,nL-1))
b0 = np.zeros((1,1))
b0[0,0] = np.mean(signal)
n = signal.shape[1]
signal = np.sort(signal, axis=1)
for r in range(1,nL,1):
num = np.tile(np.asarray(range(r+1,n+1)),(r,1)) - np.tile(np.asarray(range(1,r+1)),(1,n-r))
num = np.prod(num,axis=0)
den = np.tile(np.asarray(n),(1,r)) - np.asarray(range(1,r+1))
den = np.prod(den)
b[r-1] = 1/n * np.sum(num / den * signal[0,r:n])
tB = np.concatenate((b0,b))
B = np.flip(tB,0)
for i in range(1, nL, 1):
Spc = np.zeros((B.shape[0]-(i+1),1))
Coeff = np.concatenate((Spc, LegendreShiftPoly(i)))
l[0,i-1] = np.sum(Coeff * B)
L = np.concatenate((b0, l),1)
return L
def LegendreShiftPoly(n):
# Verified: this has identical function to MATLAB function for n = 2:10 (only 2 is used to compute LS feature)
pk = np.zeros((n+1,1))
if n == 0:
pk = 1
elif n == 1:
pk[0,0] = 2
pk[1,0] = -1
else:
pkm2 = np.zeros(n+1)
pkm2[n] = 1
pkm1 = np.zeros(n+1)
pkm1[n] = -1
pkm1[n-1] = 2
for k in range(2,n+1,1):
pk = np.zeros((n+1,1))
for e in range(n-k+1,n+1,1):
pk[e-1] = (4*k-2)*pkm1[e]+ (1-2*k)*pkm1[e-1] + (1-k) * pkm2[e-1]
pk[n,0] = (1-2*k)*pkm1[n] + (1-k)*pkm2[n]
pk = pk/k
if k < n:
pkm2 = pkm1
pkm1 = pk
return pk
def getMFLfeat(signal):
feat = np.log10(np.sum(np.abs(np.diff(signal, axis=2)),axis=2))
return feat
def getMSRfeat(signal):
feat = np.abs(np.mean(np.sqrt(signal.astype('complex')),axis=2))
return feat
def getWAMPfeat(signal,threshold=2e-3): # TODO: add optimization if threshold not passed, need class labels
feat = np.sum(np.abs(np.diff(signal, axis=2)) > threshold, axis=2)
return feat
def getRMSfeat(signal):
feat = np.sqrt(np.mean(np.square(signal),2))
return feat
def getIAVfeat(signal):
feat = np.sum(np.abs(signal),axis=2)
return feat
def getDASDVfeat(signal):
feat = np.sqrt(np.mean(np.diff(np.square(signal.astype('complex')),2),2))
return feat
def getVARfeat(signal):
feat = np.var(signal,axis=2)
return feat
if __name__ == "__main__":
# Fix the random seed -- make results reproducible
# Found in utils.py, this sets the seed for the random, torch, and numpy libraries.
fix_random_seed(1, torch.cuda.is_available())
# Dataset details, packaged together to easily pass them through functions if required.
num_subjects = 10
num_channels = 6
num_motions = 8
motion_list = ["wrist flexion","wrist extension","wrist supination","wrist pronation",
"power grip","pinch grip","hand open","no motion"] # This is the order as listed in the paper, check this
num_reps = 4
num_positions = 16
position_list = ["P1", "P2","P3","P4","P5","P6","P7","P8","P9","P10","P11","P12","P13","P14","P15","P16"]
sampling_frequency = 1000
winsize = 250
wininc = 100
dataset_characteristics = (num_subjects, num_motions, motion_list, num_reps, num_positions, position_list, winsize, wininc, sampling_frequency)
# Handcrafted feature variables:
featuresets = ["TD", "TDPSD","LSF4","LSF9"]
num_featuresets = len(featuresets)
featureset_times = []
# Start within subject cross-validation scheme
# For this eample, train with data from all positions from one subject.
# Leave one repetition out for cross-validation
within_subject_results = np.zeros((num_subjects, num_reps, num_featuresets))
for s in range(num_subjects):
subject_dataset = EMGData(s)
subject_data = subject_dataset.data
subject_class = subject_dataset.class_label
subject_rep = subject_dataset.rep_label
for f in range(num_featuresets):
if s==0:
# Timing starts
t0 = time.perf_counter()
if featuresets[f] == "TD":
features = extract_TD_feats(subject_data, num_channels)
elif featuresets[f] == "TDPSD": # TODO: TDPSD is a common feature set -- should be added to tutorial.
features = extract_TDPSD_feats(subject_data, num_channels)
elif featuresets[f] == "LSF4":
features = extract_LSF4_feats(subject_data, num_channels)
elif featuresets[f] == "LSF9":
features = extract_LSF9_feats(subject_data, num_channels)
else:
print("Unknown featureset given: {}".format(featuresets[f]))
continue
if s == 0:
featureset_times.append((time.perf_counter()-t0)/subject_data.shape[0])
# Now that we have the features extracted, we can partition the dataset into a training and testing set using
# leave-one-repetition-out cross-validation.
for r in range(num_reps):
# The training reps are all reps except rep r
# The test rep is rep r.
train_reps = list(range(1,num_reps+1))
# This pop function returns the popped element and removes the rth element from the training list.
test_rep = [train_reps.pop(r)]
# Get a list of the windows that belong to the test set and training set.
testing_ids = subject_rep.numpy() == test_rep
training_ids = [not elem for elem in testing_ids]
# Partition the training and testing features / labels
train_features = features[training_ids,:]
test_features = features[testing_ids,:]
train_class = subject_class[training_ids]
test_class = subject_class[testing_ids]
mdl = LinearDiscriminantAnalysis()
# If you'd rather use SVM, you can do so using this code instead!
# mdl = SVC(kernel='linear')
mdl.fit(train_features, train_class)
predictions = mdl.predict(test_features)
within_subject_results[s,r,f] = np.sum(predictions == test_class.numpy())/test_features.shape[0] * 100
# I am planning on using the github readme file to keep track of the results of different pipelines, so let's output the results in markup format
subject_accuracy = np.mean(within_subject_results, axis=1)
featureset_accuracy = np.mean(subject_accuracy,axis=0)
featureset_std = np.std(subject_accuracy,axis=0)
print("| Subject | ", end='')
for f in featuresets:
print(" {} |".format(f), end='')
print("")
for c in range(num_featuresets+1):
print("| --- ",end='')
print("|")
for s in range(num_subjects):
print("| S{} |".format(str(s)), end='')
for f in range(num_featuresets):
print(" {} |".format(str(subject_accuracy[s,f])),end='')
print("")
print("| Mean | ",end="")
for f in range(num_featuresets):
print(" {} |".format(str(featureset_accuracy[f])), end="")
print("")
print("| STD | ",end="")
for f in range(num_featuresets):
print(" {} |".format(str(featureset_std[f])), end="")
print("")
print("| TIME (ms) | ",end="")
for f in range(num_featuresets):
print(" {} |".format(str(featureset_times[f]*1000)), end="")
print("")
np.save("Results/withinsubject_handcrafted.npy", within_subject_results)