-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutilities.py
1706 lines (1419 loc) · 74.2 KB
/
utilities.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
---------------------------------------------------------------------------
utilities.py
---------------------------------------------------------------------------
Copyright 2022 Stanford University and the Authors
Author(s): Antoine Falisse
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial.transform import Rotation as R
import dataman
import pandas as pd
import os
import scipy.interpolate as interpolate
import random
import string
from datasets import getInfodataset
# %% Metrics.
def getMetrics(features, responses, model,
model_type='LSTM', encoder_only=False):
if model_type == 'LSTM':
y_pred2 = model.predict(features)
elif model_type == 'Transformer':
if encoder_only:
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model(features[i,:,:]).numpy()
else:
raise ValueError('Not implemented yet.')
elif model_type == 'linear_regression':
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model.predict(features[i,:,:], verbose=0)
else:
raise ValueError('Not implemented yet.')
mse = np.mean(np.square(y_pred2-responses))
rmse = np.sqrt(mse)
return mse,rmse
def getMetrics_ind(features, responses, model):
y_pred2 = model.predict(features)
mse = np.mean(np.square(y_pred2-responses),axis=0)
rmse = np.sqrt(mse)
return mse,rmse
def getMetrics_unnorm(features, responses, model, heights):
heights_sh = np.tile(heights, (responses.shape[1], 1)).T
responses_unnorm = responses * heights_sh
y_pred2_unnorm = model.predict(features) * heights_sh
mse_unnorm = np.mean(np.square(y_pred2_unnorm-responses_unnorm))
rmse_unnorm = np.sqrt(mse_unnorm)
return mse_unnorm,rmse_unnorm
def getMetrics_unnorm_lstm(features, responses, model, heights,
model_type='LSTM', encoder_only=False):
responses_unnorm = responses * heights
if model_type == 'LSTM':
y_pred2_unnorm = model.predict(features) * heights
elif model_type == 'Transformer':
if encoder_only:
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model(features[i,:,:]).numpy()
y_pred2_unnorm = y_pred2 * heights
else:
raise ValueError('Not implemented yet.')
elif model_type == 'linear_regression':
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model.predict(features[i,:,:], verbose=0)
y_pred2_unnorm = y_pred2 * heights
else:
raise ValueError('Not implemented yet.')
mse_unnorm = np.mean(np.square(y_pred2_unnorm-responses_unnorm))
rmse_unnorm = np.sqrt(mse_unnorm)
return mse_unnorm,rmse_unnorm
def getMPME_unnorm_lstm(features, responses, model, heights,
model_type='LSTM', encoder_only=False):
responses_unnorm = responses * heights
if model_type == 'LSTM':
y_pred2_unnorm = model.predict(features) * heights
elif model_type == 'Transformer':
if encoder_only:
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model(features[i,:,:]).numpy()
y_pred2_unnorm = y_pred2 * heights
else:
raise ValueError('Not implemented yet.')
elif model_type == 'linear_regression':
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model.predict(features[i,:,:], verbose=0)
y_pred2_unnorm = y_pred2 * heights
else:
raise ValueError('Not implemented yet.')
# We know there are three dimensions (x,y,z).
MPMEvec = np.zeros((int(responses_unnorm.shape[2]/3),))
for i in range(int(responses_unnorm.shape[2]/3)):
MPMEvec[i] = np.mean(np.linalg.norm(
y_pred2_unnorm[:,:,i*3:i*3+3] -
responses_unnorm[:,:,i*3:i*3+3],axis = 2))
MPME = np.mean(MPMEvec)
return MPME, MPMEvec
def getMetrics_ind_unnorm_lstm(features, responses, model, heights,
model_type='LSTM', encoder_only=False):
responses_unnorm = responses * heights
responses_unnorm_2D = np.reshape(responses_unnorm,
(responses_unnorm.shape[1],
responses_unnorm.shape[2]))
if model_type == 'LSTM':
y_pred2_unnorm = model.predict(features) * heights
elif model_type == 'Transformer':
if encoder_only:
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model(features[i,:,:]).numpy()
y_pred2_unnorm = y_pred2 * heights
else:
raise ValueError('Not implemented yet.')
elif model_type == 'linear_regression':
y_pred2 = np.zeros((responses.shape[0], responses.shape[1], responses.shape[2]))
for i in range(responses.shape[0]):
y_pred2[i, :, :] = model.predict(features[i,:,:], verbose=0)
y_pred2_unnorm = y_pred2 * heights
else:
raise ValueError('Not implemented yet.')
y_pred2_unnorm_2D = np.reshape(y_pred2_unnorm,
(y_pred2_unnorm.shape[1],
y_pred2_unnorm.shape[2]))
mse = np.mean(np.square(y_pred2_unnorm_2D-responses_unnorm_2D),axis=0)
rmse = np.sqrt(mse)
return mse,rmse
def plotLossOverEpochs(history):
plt.figure()
plt.plot(history["loss"], linewidth=3)
legend = ['Training']
if 'val_loss' in history:
plt.plot(history["val_loss"], linewidth=3)
legend.append('Evaluation')
plt.ylabel('loss (mean squared error)', fontsize=20)
plt.xlabel('Epoch Number', fontsize=20)
plt.xticks(list(range(1, len(history["loss"])+1)), fontsize=18)
plt.yticks(fontsize=18)
plt.legend(legend, fontsize=20)
plt.show()
# Partition dataset.
def getPartition(idxDatasets, scaleFactors, infoData, subjectSplit, idxFold,
partial_selection={"activity":[], "factor":[]}, nScaleFactors=-1):
idxSubject = {'train': {}, 'val': {}, 'test': {}}
partition = {'train': np.array([], dtype=int),
'val': np.array([], dtype=int),
'test': np.array([], dtype=int)}
partition_assert = {'train': np.array([], dtype=int),
'val': np.array([], dtype=int),
'test': np.array([], dtype=int)}
count_s_train = 0
count_s_val = 0
count_s_test = 0
acc_val = 0
for idxDataset in idxDatasets:
c_dataset = "dataset" + str(idxDataset)
infoDataset = getInfodataset(idxDataset)
# Select nScaleFactors out of the scaleFactors available. Different
# selection for each dataset.
if nScaleFactors == -1:
c_scaleFactors = scaleFactors
else:
# Select nScaleFactors from scaleFactors
c_scaleFactors = np.random.choice(scaleFactors, nScaleFactors, replace=False)
for scaleFactor in c_scaleFactors:
# Train
count_s = 0
for c_idx in subjectSplit[c_dataset]["training_" + str(idxFold)]:
c_where = np.argwhere(np.logical_and(
infoData["scalefactors"]==scaleFactor,
np.logical_and(infoData["datasets"]==idxDataset,
infoData["subjects"]==c_idx)))
if infoDataset['activities'] in partial_selection['activity'] and not idxDataset in partial_selection['excluded']:
# Get scale factor
c_idx_activity = partial_selection['activity'].index(infoDataset['activities'])
c_scale = partial_selection['factor'][c_idx_activity]
# Randomly pick 1 in c_scale values from c_where
c_where2 = c_where[np.random.choice(
c_where.shape[0], int(c_where.shape[0]/c_scale), replace=False)]
# Sort c_where
c_where2 = np.sort(c_where2, axis=0)
else:
c_where2 = c_where
partition["train"] = np.append(partition["train"], c_where2)
partition_assert["train"] = np.append(partition_assert["train"], c_where)
idxSubject["train"][count_s_train + count_s] = c_where2
count_s += 1
count_s_train += count_s
# Val
count_s = 0
for c_idx in subjectSplit[c_dataset]["validation_" + str(idxFold)]:
c_where = np.argwhere(np.logical_and(
infoData["scalefactors"]==scaleFactor,
np.logical_and(infoData["datasets"]==idxDataset,
infoData["subjects"]==c_idx)))
if infoDataset['activities'] in partial_selection['activity'] and not idxDataset in partial_selection['excluded']:
# Get scale factor
c_idx_activity = partial_selection['activity'].index(infoDataset['activities'])
c_scale = partial_selection['factor'][c_idx_activity]
# Randomly pick 1 in c_scale values from c_where
c_where2 = c_where[np.random.choice(
c_where.shape[0], int(c_where.shape[0]/c_scale), replace=False)]
# Sort c_where
c_where2 = np.sort(c_where2, axis=0)
else:
c_where2 = c_where
partition["val"] = np.append(partition["val"], c_where2)
partition_assert["val"] = np.append(partition_assert["val"], c_where)
idxSubject["val"][count_s_val + count_s] = c_where2
count_s += 1
count_s_val += count_s
# Test
count_s = 0
for c_idx in subjectSplit[c_dataset]["test"]:
c_where = np.argwhere(np.logical_and(
infoData["scalefactors"]==scaleFactor,
np.logical_and(infoData["datasets"]==idxDataset,
infoData["subjects"]==c_idx)))
if infoDataset['activities'] in partial_selection['activity'] and not idxDataset in partial_selection['excluded']:
# Get scale factor
c_idx_activity = partial_selection['activity'].index(infoDataset['activities'])
c_scale = partial_selection['factor'][c_idx_activity]
# Randomly pick 1 in c_scale values from c_where
c_where2 = c_where[np.random.choice(
c_where.shape[0], int(c_where.shape[0]/c_scale), replace=False)]
# Sort c_where
c_where2 = np.sort(c_where2, axis=0)
else:
c_where2 = c_where
partition["test"] = np.append(partition["test"], c_where2)
partition_assert["test"] = np.append(partition_assert["test"], c_where)
idxSubject["test"][count_s_test + count_s] = c_where2
count_s += 1
count_s_test += count_s
acc_val += np.sum(np.logical_and(
infoData["datasets"] == idxDataset,
infoData["scalefactors"]==scaleFactor))
# Make sure the all the data has been split in the three sets.
sum_partitions = partition_assert["train"].shape[0] + partition_assert["val"].shape[0] + partition_assert["test"].shape[0]
# Relaxing test given possible split per activity
assert (acc_val == sum_partitions), ("missing data")
return partition
# %% Welford's online algorithm:
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
# For a new value newValue, compute the new count, new mean, the new M2.
# mean accumulates the mean of the entire dataset
# M2 aggregates the squared distance from the mean
# count aggregates the number of samples seen so far
def update(existingAggregate, newValue):
(count, mean, M2) = existingAggregate
count += 1
delta = newValue - mean
mean += delta / count
delta2 = newValue - mean
M2 += delta * delta2
return (count, mean, M2)
# Retrieve the mean, variance and sample variance from an aggregate
def finalize(existingAggregate):
(count, mean, M2) = existingAggregate
if count < 2:
return float("nan")
else:
(mean, variance, sampleVariance) = (mean, M2 / count, M2 / (count - 1))
return (mean, variance, sampleVariance)
# %% Markers
# Get all the markers.
def getAllMarkers():
markers = ['Neck_mmpose', 'RShoulder_mmpose', 'LShoulder_mmpose', 'RHip_mmpose', 'LHip_mmpose',
'midHip_mmpose', 'RKnee_mmpose', 'LKnee_mmpose', 'RAnkle_mmpose', 'LAnkle_mmpose',
'RHeel_mmpose', 'LHeel_mmpose', 'RSmallToe_mmpose', 'LSmallToe_mmpose',
'RBigToe_mmpose', 'LBigToe_mmpose', 'RElbow_mmpose', 'LElbow_mmpose', 'RWrist_mmpose',
'LWrist_mmpose', 'Neck_openpose', 'RShoulder_openpose', 'LShoulder_openpose',
'RHip_openpose', 'LHip_openpose', 'midHip_openpose', 'RKnee_openpose',
'LKnee_openpose', 'RAnkle_openpose', 'LAnkle_openpose', 'RHeel_openpose',
'LHeel_openpose', 'RSmallToe_openpose', 'LSmallToe_openpose', 'RBigToe_openpose',
'LBigToe_openpose', 'RElbow_openpose', 'LElbow_openpose', 'RWrist_openpose',
'LWrist_openpose', 'RASIS_augmenter', 'LASIS_augmenter', 'RPSIS_augmenter',
'LPSIS_augmenter', 'RKnee_augmenter', 'RMKnee_augmenter', 'RAnkle_augmenter',
'RMAnkle_augmenter', 'RToe_augmenter', 'R5meta_augmenter', 'RCalc_augmenter',
'LKnee_augmenter', 'LMKnee_augmenter', 'LAnkle_augmenter', 'LMAnkle_augmenter',
'LToe_augmenter', 'LCalc_augmenter', 'L5meta_augmenter', 'RShoulder_augmenter',
'LShoulder_augmenter', 'C7_augmenter', 'RElbow_augmenter', 'RMElbow_augmenter',
'RWrist_augmenter', 'RMWrist_augmenter', 'LElbow_augmenter', 'LMElbow_augmenter',
'LWrist_augmenter', 'LMWrist_augmenter', 'RThigh1_augmenter', 'RThigh2_augmenter',
'RThigh3_augmenter', 'LThigh1_augmenter', 'LThigh2_augmenter', 'LThigh3_augmenter',
'RSh1_augmenter', 'RSh2_augmenter', 'RSh3_augmenter', 'LSh1_augmenter',
'LSh2_augmenter', 'LSh3_augmenter', 'RHJC_augmenter', 'LHJC_augmenter']
return markers
def getAllMarkers_oldData():
feature_markers = [
"Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
"RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
"RBigToe", "LBigToe", "RElbow", "LElbow", "RWrist", "LWrist",
"RSmallToe_mmpose", "LSmallToe_mmpose"]
response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
"r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
"L.PSIS_study", "r_knee_study", "L_knee_study",
"r_mknee_study", "L_mknee_study", "r_ankle_study",
"L_ankle_study", "r_mankle_study", "L_mankle_study",
"r_calc_study", "L_calc_study", "r_toe_study",
"L_toe_study", "r_5meta_study", "L_5meta_study",
"r_lelbow_study", "L_lelbow_study", "r_melbow_study",
"L_melbow_study", "r_lwrist_study", "L_lwrist_study",
"r_mwrist_study", "L_mwrist_study",
"r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
"L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
"r_sh1_study", "r_sh2_study", "r_sh3_study",
"L_sh1_study", "L_sh2_study", "L_sh3_study",
"RHJC_study", "LHJC_study"]
return feature_markers, response_markers
def getArmMarkersPoseDetector(pose_detector):
markers = ['RElbow', 'LElbow', 'RWrist', 'LWrist']
markers = [m + "_" + pose_detector for m in markers]
return markers
def getArmMarkersAugmenter():
markers = [ 'RElbow', 'RMElbow',
'RWrist', 'RMWrist', 'LElbow', 'LMElbow',
'LWrist', 'LMWrist']
markers = [m + "_augmenter" for m in markers]
return markers
def getMarkersPoseDetector(pose_detector, withArms=True):
markers = ['Neck', 'RShoulder', 'LShoulder', 'RHip', 'LHip', 'midHip',
'RKnee', 'LKnee', 'RAnkle', 'LAnkle', 'RHeel', 'LHeel',
'RSmallToe', 'LSmallToe', 'RBigToe', 'LBigToe', 'RElbow',
'LElbow', 'RWrist', 'LWrist']
markers = [m + "_" + pose_detector for m in markers]
if not withArms:
armMarkers = getArmMarkersPoseDetector(pose_detector)
# Remove the arm markers from markers
markers = [m for m in markers if m not in armMarkers]
return markers
def getMarkersAugmenter(withArms=True):
markers = ['RASIS_augmenter', 'LASIS_augmenter', 'RPSIS_augmenter',
'LPSIS_augmenter', 'RKnee_augmenter', 'RMKnee_augmenter', 'RAnkle_augmenter',
'RMAnkle_augmenter', 'RToe_augmenter', 'R5meta_augmenter', 'RCalc_augmenter',
'LKnee_augmenter', 'LMKnee_augmenter', 'LAnkle_augmenter', 'LMAnkle_augmenter',
'LToe_augmenter', 'LCalc_augmenter', 'L5meta_augmenter', 'RShoulder_augmenter',
'LShoulder_augmenter', 'C7_augmenter', 'RElbow_augmenter', 'RMElbow_augmenter',
'RWrist_augmenter', 'RMWrist_augmenter', 'LElbow_augmenter', 'LMElbow_augmenter',
'LWrist_augmenter', 'LMWrist_augmenter', 'RThigh1_augmenter', 'RThigh2_augmenter',
'RThigh3_augmenter', 'LThigh1_augmenter', 'LThigh2_augmenter', 'LThigh3_augmenter',
'RSh1_augmenter', 'RSh2_augmenter', 'RSh3_augmenter', 'LSh1_augmenter',
'LSh2_augmenter', 'LSh3_augmenter', 'RHJC_augmenter', 'LHJC_augmenter']
if not withArms:
armMarkers = getArmMarkersAugmenter()
# Remove the arm markers from markers
markers = [m for m in markers if m not in armMarkers]
return markers
def getMarkersPoseDetector_lowerExtremity(pose_detector, withArms=True):
markers = [
"Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
"RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
"RBigToe", "LBigToe"]
markers = [m + "_" + pose_detector for m in markers]
allMarkers = getMarkersPoseDetector(pose_detector, withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getOpenPoseMarkers_lowerExtremity_oldData():
feature_markers = [
"Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
"RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
"RBigToe", "LBigToe"]
response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
"r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
"L.PSIS_study", "r_knee_study", "L_knee_study",
"r_mknee_study", "L_mknee_study", "r_ankle_study",
"L_ankle_study", "r_mankle_study", "L_mankle_study",
"r_calc_study", "L_calc_study", "r_toe_study",
"L_toe_study", "r_5meta_study", "L_5meta_study",
"r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
"L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
"r_sh1_study", "r_sh2_study", "r_sh3_study",
"L_sh1_study", "L_sh2_study", "L_sh3_study",
"RHJC_study", "LHJC_study"]
all_feature_markers, all_response_markers = getAllMarkers_oldData()
idx_in_all_feature_markers = []
for marker in feature_markers:
idx_in_all_feature_markers.append(
all_feature_markers.index(marker))
idx_in_all_response_markers = []
for marker in response_markers:
idx_in_all_response_markers.append(
all_response_markers.index(marker))
return feature_markers, response_markers, idx_in_all_feature_markers, idx_in_all_response_markers
def getMarkersPoseDetector_upperExtremity(pose_detector, withArms=True):
markers = [
"Neck", "RShoulder", "LShoulder", 'RElbow', 'LElbow',
'RWrist', 'LWrist']
markers = [m + "_" + pose_detector for m in markers]
allMarkers = getMarkersPoseDetector(pose_detector, withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersPoseDetector_feet(pose_detector, withArms=True):
markers = [
"RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
"RBigToe", "LBigToe"]
markers = [m + "_" + pose_detector for m in markers]
allMarkers = getMarkersPoseDetector(pose_detector, withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkers_inMarkersPoseDetector(markers, pose_detector, withArms=True):
allMarkers = getMarkersPoseDetector(pose_detector, withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker+ "_"+ pose_detector))
return idx_in_allMarkers
def getMarkersAugmenter_lowerExtremity(withArms=True):
markers = [
'RASIS_augmenter', 'LASIS_augmenter', 'RPSIS_augmenter',
'LPSIS_augmenter', 'RKnee_augmenter', 'RMKnee_augmenter',
'RAnkle_augmenter', 'RMAnkle_augmenter', 'RToe_augmenter',
'R5meta_augmenter', 'RCalc_augmenter', 'LKnee_augmenter',
'LMKnee_augmenter', 'LAnkle_augmenter', 'LMAnkle_augmenter',
'LToe_augmenter', 'LCalc_augmenter', 'L5meta_augmenter',
'RShoulder_augmenter', 'LShoulder_augmenter', 'C7_augmenter',
'RThigh1_augmenter', 'RThigh2_augmenter', 'RThigh3_augmenter',
'LThigh1_augmenter', 'LThigh2_augmenter', 'LThigh3_augmenter',
'RSh1_augmenter', 'RSh2_augmenter', 'RSh3_augmenter', 'LSh1_augmenter',
'LSh2_augmenter', 'LSh3_augmenter', 'RHJC_augmenter', 'LHJC_augmenter']
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_lowerExtremity_old(withArms=True):
markers = [
"C7_augmenter", "RShoulder_augmenter", "LShoulder_augmenter",
"RASIS_augmenter", "LASIS_augmenter", "RPSIS_augmenter",
"LPSIS_augmenter", "RKnee_augmenter", "LKnee_augmenter",
"RMKnee_augmenter", "LMKnee_augmenter", "RAnkle_augmenter",
"LAnkle_augmenter", "RMAnkle_augmenter", "LMAnkle_augmenter",
"RCalc_augmenter", "LCalc_augmenter", "RToe_augmenter",
"LToe_augmenter", "R5meta_augmenter", "L5meta_augmenter",
"RThigh1_augmenter", "RThigh2_augmenter", "RThigh3_augmenter",
"LThigh1_augmenter", "LThigh2_augmenter", "LThigh3_augmenter",
"RSh1_augmenter", "RSh2_augmenter", "RSh3_augmenter",
"LSh1_augmenter", "LSh2_augmenter", "LSh3_augmenter",
"RHJC_augmenter", "LHJC_augmenter"]
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_lowerExtremityNoFeet(withArms=True):
markers = [
'RASIS_augmenter', 'LASIS_augmenter', 'RPSIS_augmenter',
'LPSIS_augmenter', 'RKnee_augmenter', 'RMKnee_augmenter',
'RAnkle_augmenter', 'RMAnkle_augmenter', 'LKnee_augmenter',
'LMKnee_augmenter', 'LAnkle_augmenter', 'LMAnkle_augmenter',
'RShoulder_augmenter', 'LShoulder_augmenter', 'C7_augmenter',
'RThigh1_augmenter', 'RThigh2_augmenter', 'RThigh3_augmenter',
'LThigh1_augmenter', 'LThigh2_augmenter', 'LThigh3_augmenter',
'RSh1_augmenter', 'RSh2_augmenter', 'RSh3_augmenter', 'LSh1_augmenter',
'LSh2_augmenter', 'LSh3_augmenter', 'RHJC_augmenter', 'LHJC_augmenter']
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_lowerExtremityNoTracking(withArms=True):
markers = [
'RASIS_augmenter', 'LASIS_augmenter', 'RPSIS_augmenter',
'LPSIS_augmenter', 'RKnee_augmenter', 'RMKnee_augmenter',
'RAnkle_augmenter', 'RMAnkle_augmenter', 'RToe_augmenter',
'R5meta_augmenter', 'RCalc_augmenter', 'LKnee_augmenter',
'LMKnee_augmenter', 'LAnkle_augmenter', 'LMAnkle_augmenter',
'LToe_augmenter', 'LCalc_augmenter', 'L5meta_augmenter',
'RShoulder_augmenter', 'LShoulder_augmenter', 'C7_augmenter',
'RHJC_augmenter', 'LHJC_augmenter']
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_lowerExtremityNoTrackingNoFeet(withArms=True):
markers = [
'RASIS_augmenter', 'LASIS_augmenter', 'RPSIS_augmenter',
'LPSIS_augmenter', 'RKnee_augmenter', 'RMKnee_augmenter',
'RAnkle_augmenter', 'RMAnkle_augmenter', 'LKnee_augmenter',
'LMKnee_augmenter', 'LAnkle_augmenter', 'LMAnkle_augmenter',
'RShoulder_augmenter', 'LShoulder_augmenter', 'C7_augmenter',
'RHJC_augmenter', 'LHJC_augmenter']
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_upperExtremity(withArms=True):
markers = [
'RElbow_augmenter', 'RMElbow_augmenter', 'RWrist_augmenter',
'RMWrist_augmenter', 'LElbow_augmenter', 'LMElbow_augmenter',
'LWrist_augmenter', 'LMWrist_augmenter']
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_upperExtremity_old(withArms=True):
markers = [
"RElbow_augmenter", "LElbow_augmenter", "RMElbow_augmenter",
"LMElbow_augmenter", "RWrist_augmenter", "LWrist_augmenter",
"RMWrist_augmenter", "LMWrist_augmenter"]
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def getMarkersAugmenter_feet(withArms=True):
markers = [
'RToe_augmenter', 'R5meta_augmenter', 'RCalc_augmenter',
'LToe_augmenter', 'L5meta_augmenter', 'LCalc_augmenter']
allMarkers = getMarkersAugmenter(withArms)
idx_in_allMarkers = []
for marker in markers:
idx_in_allMarkers.append(allMarkers.index(marker))
return markers, idx_in_allMarkers
def get_idx_in_all_features(augmenter_type, poseDetector, nFeatures, nDim=3,
withArms=True, featureHeight=True,
featureWeight=True):
if augmenter_type == 'lowerExtremity':
markers, idx_in_allMarkers = getMarkersPoseDetector_lowerExtremity(
poseDetector, withArms)
elif augmenter_type == 'lowerExtremityNoTracking':
markers, idx_in_allMarkers = getMarkersPoseDetector_lowerExtremity(
poseDetector, withArms)
elif augmenter_type == 'lowerExtremityNoFeet':
markers, idx_in_allMarkers = getMarkersPoseDetector_lowerExtremity(
poseDetector, withArms)
elif augmenter_type == 'lowerExtremityNoTrackingNoFeet':
markers, idx_in_allMarkers = getMarkersPoseDetector_lowerExtremity(
poseDetector, withArms)
elif augmenter_type == 'upperExtremity':
markers, idx_in_allMarkers = getMarkersPoseDetector_upperExtremity(
poseDetector, withArms)
elif augmenter_type == 'feet':
markers, idx_in_allMarkers = getMarkersPoseDetector_feet(
poseDetector, withArms)
# Each marker has 3 dimensions.
idx_in_all_features = []
for idx in idx_in_allMarkers:
idx_in_all_features.append(idx*nDim)
idx_in_all_features.append(idx*nDim+1)
idx_in_all_features.append(idx*nDim+2)
# Additional features (height and weight).
if featureHeight:
idxFeatureHeight = nFeatures-2
idx_in_all_features.append(idxFeatureHeight)
if featureWeight:
idxFeatureWeight = nFeatures-1
idx_in_all_features.append(idxFeatureWeight)
return idx_in_all_features, len(markers)
def get_idx_in_all_features_oldData(nDim=3, featureHeight=True, featureWeight=True):
feature_markers_all, _ = getAllMarkers_oldData()
feature_markers, _, idx_in_all_feature_markers, _ = getOpenPoseMarkers_lowerExtremity_oldData()
idx_in_all_features = []
for idx in idx_in_all_feature_markers:
idx_in_all_features.append(idx*nDim)
idx_in_all_features.append(idx*nDim+1)
idx_in_all_features.append(idx*nDim+2)
# Additional features (height and weight).
nAddFeatures = 0
if featureHeight:
nAddFeatures += 1
idxFeatureHeight = len(feature_markers_all)*nDim
idx_in_all_features.append(idxFeatureHeight)
if featureWeight:
nAddFeatures += 1
if featureHeight:
idxFeatureWeight = len(feature_markers_all)*nDim + 1
else:
idxFeatureWeight = len(feature_markers_all)*nDim
idx_in_all_features.append(idxFeatureWeight)
return idx_in_all_features, len(feature_markers)
def get_idx_in_all_labels(augmenter_type, nDim=3, withArms=True):
if augmenter_type == 'lowerExtremity':
markers, idx_in_allMarkers = getMarkersAugmenter_lowerExtremity(
withArms)
if augmenter_type == 'lowerExtremityNoTracking':
markers, idx_in_allMarkers = getMarkersAugmenter_lowerExtremityNoTracking(
withArms)
if augmenter_type == 'lowerExtremityNoFeet':
markers, idx_in_allMarkers = getMarkersAugmenter_lowerExtremityNoFeet(
withArms)
if augmenter_type == 'lowerExtremityNoTrackingNoFeet':
markers, idx_in_allMarkers = getMarkersAugmenter_lowerExtremityNoTrackingNoFeet(
withArms)
elif augmenter_type == 'upperExtremity':
markers, idx_in_allMarkers = getMarkersAugmenter_upperExtremity(
withArms)
elif augmenter_type == 'feet':
markers, idx_in_allMarkers = getMarkersAugmenter_feet(
withArms)
# Each marker has 3 dimensions.
idx_in_all_labels = []
for idx in idx_in_allMarkers:
idx_in_all_labels.append(idx*nDim)
idx_in_all_labels.append(idx*nDim+1)
idx_in_all_labels.append(idx*nDim+2)
return idx_in_all_labels, len(markers)
def get_idx_in_all_labels_oldData(nDim=3):
_, response_markers, _, idx_in_all_response_markers = getOpenPoseMarkers_lowerExtremity_oldData()
idx_in_all_responses = []
for idx in idx_in_all_response_markers:
idx_in_all_responses.append(idx*nDim)
idx_in_all_responses.append(idx*nDim+1)
idx_in_all_responses.append(idx*nDim+2)
return idx_in_all_responses, len(response_markers)
def get_reference_marker_value(c_features_all, reference_marker, poseDetector,
nDim=3, withArms=True):
# Express marker position with respect to reference marker.
from utilities import getMarkers_inMarkersPoseDetector
idx_ref_in_allMarkers = getMarkers_inMarkersPoseDetector(
[reference_marker], poseDetector, withArms)
idx_ref_in_all_features = []
for idx_ref_in_allMarker in idx_ref_in_allMarkers:
idx_ref_in_all_features.append(idx_ref_in_allMarker*nDim)
idx_ref_in_all_features.append(idx_ref_in_allMarker*nDim+1)
idx_ref_in_all_features.append(idx_ref_in_allMarker*nDim+2)
c_ref = c_features_all[:,idx_ref_in_all_features]
return c_ref
def subtract_reference_marker_value(c_values, nMarkers, c_ref,
featureHeight=False,
featureWeight=False):
# Replicate reference marker for each marker.
c_ref_all = np.tile(c_ref, (1, nMarkers))
# Add columns of 0s for additional features.
if featureHeight:
c_ref_all = np.concatenate(
(c_ref_all, np.zeros((c_ref_all.shape[0],1))), axis=1)
if featureWeight:
c_ref_all = np.concatenate(
(c_ref_all, np.zeros((c_ref_all.shape[0],1))), axis=1)
# Subtract reference marker from all markers.
c_values -= c_ref_all
return c_values
def get_height(c_features_all):
idxFeatureHeight = c_features_all.shape[1]-2
height = c_features_all[:,idxFeatureHeight][:,None]
return height
def normalize_height(c_values, height, nMarkers, nDim=3,
featureHeight=False, featureWeight=False):
c_height_all = np.tile(height, nMarkers*nDim)
# Add columns of 1s for additional features.
if featureHeight:
c_height_all = np.concatenate(
(c_height_all, np.ones((c_height_all.shape[0],1))), axis=1)
if featureWeight:
c_height_all = np.concatenate(
(c_height_all, np.ones((c_height_all.shape[0],1))), axis=1)
c_values /= c_height_all
return c_values
# # %% Markers
# def getAllMarkers():
# feature_markers = [
# "Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
# "RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
# "RBigToe", "LBigToe", "RElbow", "LElbow", "RWrist", "LWrist",
# "RSmallToe_mmpose", "LSmallToe_mmpose"]
# response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
# "r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
# "L.PSIS_study", "r_knee_study", "L_knee_study",
# "r_mknee_study", "L_mknee_study", "r_ankle_study",
# "L_ankle_study", "r_mankle_study", "L_mankle_study",
# "r_calc_study", "L_calc_study", "r_toe_study",
# "L_toe_study", "r_5meta_study", "L_5meta_study",
# "r_lelbow_study", "L_lelbow_study", "r_melbow_study",
# "L_melbow_study", "r_lwrist_study", "L_lwrist_study",
# "r_mwrist_study", "L_mwrist_study",
# "r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
# "L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
# "r_sh1_study", "r_sh2_study", "r_sh3_study",
# "L_sh1_study", "L_sh2_study", "L_sh3_study",
# "RHJC_study", "LHJC_study"]
# return feature_markers, response_markers
# def getOpenPoseMarkers_fullBody():
# feature_markers = [
# "Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
# "RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
# "RBigToe", "LBigToe", "RElbow", "LElbow", "RWrist", "LWrist"]
# response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
# "r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
# "L.PSIS_study", "r_knee_study", "L_knee_study",
# "r_mknee_study", "L_mknee_study", "r_ankle_study",
# "L_ankle_study", "r_mankle_study", "L_mankle_study",
# "r_calc_study", "L_calc_study", "r_toe_study",
# "L_toe_study", "r_5meta_study", "L_5meta_study",
# "r_lelbow_study", "L_lelbow_study", "r_melbow_study",
# "L_melbow_study", "r_lwrist_study", "L_lwrist_study",
# "r_mwrist_study", "L_mwrist_study",
# "r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
# "L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
# "r_sh1_study", "r_sh2_study", "r_sh3_study",
# "L_sh1_study", "L_sh2_study", "L_sh3_study",
# "RHJC_study", "LHJC_study"]
# all_feature_markers, all_response_markers = getAllMarkers()
# idx_in_all_feature_markers = []
# for marker in feature_markers:
# idx_in_all_feature_markers.append(
# all_feature_markers.index(marker))
# idx_in_all_response_markers = []
# for marker in response_markers:
# idx_in_all_response_markers.append(
# all_response_markers.index(marker))
# return (feature_markers, response_markers, idx_in_all_feature_markers,
# idx_in_all_response_markers)
# def getMMposeMarkers_fullBody():
# feature_markers = [
# "Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
# "RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe_mmpose",
# "LSmallToe_mmpose", "RElbow", "LElbow", "RWrist", "LWrist"]
# response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
# "r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
# "L.PSIS_study", "r_knee_study", "L_knee_study",
# "r_mknee_study", "L_mknee_study", "r_ankle_study",
# "L_ankle_study", "r_mankle_study", "L_mankle_study",
# "r_calc_study", "L_calc_study", "r_toe_study",
# "L_toe_study", "r_5meta_study", "L_5meta_study",
# "r_lelbow_study", "L_lelbow_study", "r_melbow_study",
# "L_melbow_study", "r_lwrist_study", "L_lwrist_study",
# "r_mwrist_study", "L_mwrist_study",
# "r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
# "L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
# "r_sh1_study", "r_sh2_study", "r_sh3_study",
# "L_sh1_study", "L_sh2_study", "L_sh3_study",
# "RHJC_study", "LHJC_study"]
# all_feature_markers, all_response_markers = getAllMarkers()
# idx_in_all_feature_markers = []
# for marker in feature_markers:
# idx_in_all_feature_markers.append(
# all_feature_markers.index(marker))
# idx_in_all_response_markers = []
# for marker in response_markers:
# idx_in_all_response_markers.append(
# all_response_markers.index(marker))
# return (feature_markers, response_markers, idx_in_all_feature_markers,
# idx_in_all_response_markers)
# def getOpenPoseMarkers_lowerExtremity():
# feature_markers = [
# "Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
# "RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe", "LSmallToe",
# "RBigToe", "LBigToe"]
# response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
# "r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
# "L.PSIS_study", "r_knee_study", "L_knee_study",
# "r_mknee_study", "L_mknee_study", "r_ankle_study",
# "L_ankle_study", "r_mankle_study", "L_mankle_study",
# "r_calc_study", "L_calc_study", "r_toe_study",
# "L_toe_study", "r_5meta_study", "L_5meta_study",
# "r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
# "L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
# "r_sh1_study", "r_sh2_study", "r_sh3_study",
# "L_sh1_study", "L_sh2_study", "L_sh3_study",
# "RHJC_study", "LHJC_study"]
# all_feature_markers, all_response_markers = getAllMarkers()
# idx_in_all_feature_markers = []
# for marker in feature_markers:
# idx_in_all_feature_markers.append(
# all_feature_markers.index(marker))
# idx_in_all_response_markers = []
# for marker in response_markers:
# idx_in_all_response_markers.append(
# all_response_markers.index(marker))
# return (feature_markers, response_markers, idx_in_all_feature_markers,
# idx_in_all_response_markers)
# def getMMposeMarkers_lowerExtremity():
# feature_markers = [
# "Neck", "RShoulder", "LShoulder", "RHip", "LHip", "RKnee", "LKnee",
# "RAnkle", "LAnkle", "RHeel", "LHeel", "RSmallToe_mmpose",
# "LSmallToe_mmpose"]
# response_markers = ["C7_study", "r_shoulder_study", "L_shoulder_study",
# "r.ASIS_study", "L.ASIS_study", "r.PSIS_study",
# "L.PSIS_study", "r_knee_study", "L_knee_study",
# "r_mknee_study", "L_mknee_study", "r_ankle_study",
# "L_ankle_study", "r_mankle_study", "L_mankle_study",
# "r_calc_study", "L_calc_study", "r_toe_study",
# "L_toe_study", "r_5meta_study", "L_5meta_study",
# "r_thigh1_study", "r_thigh2_study", "r_thigh3_study",
# "L_thigh1_study", "L_thigh2_study", "L_thigh3_study",
# "r_sh1_study", "r_sh2_study", "r_sh3_study",
# "L_sh1_study", "L_sh2_study", "L_sh3_study",
# "RHJC_study", "LHJC_study"]
# all_feature_markers, all_response_markers = getAllMarkers()
# idx_in_all_feature_markers = []
# for marker in feature_markers:
# idx_in_all_feature_markers.append(
# all_feature_markers.index(marker))
# idx_in_all_response_markers = []
# for marker in response_markers:
# idx_in_all_response_markers.append(
# all_response_markers.index(marker))