-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_mining.py
More file actions
1031 lines (845 loc) · 45.4 KB
/
data_mining.py
File metadata and controls
1031 lines (845 loc) · 45.4 KB
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
from lib import bvh2glo_simple, SL_dict
import os
import sys
import copy
import collections
import similaritymeasures
import numpy as np
import pickle as pk
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy import signal, interpolate, spatial
from dtaidistance import dtw
from dtaidistance import dtw_ndim
from dtw import dtw as dtw_slower
from sdtw import SoftDTW
from sdtw.distance import SquaredEuclidean
from sklearn.metrics.pairwise import manhattan_distances
from timeit import default_timer as timer
def load(paths):
try:
with open(paths, 'r') as pth:
paths_list = pth.readlines()
except:
print('Path list directory not found.')
bvh_dir = paths_list[0].rstrip("\n") # all bvh files takes and dictionaries
bvh_dict = paths_list[1].rstrip("\n") # bvh files with separate words signed
source_dir = paths_list[2].rstrip("\n") # data converted from angular to global positions
path_jointlist = paths_list[3].rstrip("\n") # path to the joint_list.txt file
path_chosen_joints = paths_list[4].rstrip("\n") # path to the chosen joints indexes from joint_list.txt file
path_dictionary = paths_list[5].rstrip("\n") # path to the ultimate_dictionary2.txt file
path_metadata = paths_list[6].rstrip("\n") # path to the meta.pkl file
path_trajectory = paths_list[7].rstrip("\n") # path to the traj.pkl file
path_output = paths_list[8] # path to the output_files folder
with open(path_jointlist, 'r') as f:
joint_list = f.readlines() # the list of markers (tracked body parts)
for i in range(len(joint_list)):
joint_list[i] = joint_list[i].rstrip("\n")
with open(path_metadata, 'rb') as pf:
meta = pk.load(pf) # metadata: meaning, the initial file, anotation
with open(path_trajectory, 'rb') as pf:
traj = pk.load(pf) # trajektory [item, frame, joint, channel]
return bvh_dir,bvh_dict,source_dir,path_jointlist,path_chosen_joints,path_dictionary,path_metadata,path_trajectory,path_output,joint_list,meta,traj
def mine_data(in_directory, out_directory):
"""
converts data from angular BVH to global positions (npy matrix)
:param in_directory:
:param out_directory:
:return:
"""
bvhfile_list = [l for l in os.listdir(in_directory) if '.bvh' in l]
for tmp_file_name in bvhfile_list:
tmp_file_base_name = os.path.splitext(tmp_file_name)[0]
tmp_data_glo = bvh2glo_simple.calculate(os.path.join(bvh_dir, tmp_file_name))
np.save(os.path.join(out_directory,tmp_file_base_name + '.npy'), tmp_data_glo[1])
def create_trajectory_matrix(dictionary_data):
"""
creates sign numpy dictionary
:param dictionary_data:
:return:
"""
trajectory_list = []
metadata_list = []
for i, item in enumerate(dictionary_data):
# vyřazuje položky ve slovníku, které nejsou zpracovane
if all(['annotation_Filip_bvh_frame' in item.keys(), item['sign_id'] != '', '!' not in item['sign_id']]):
anot = item['annotation_Filip_bvh_frame']
npy_name = (os.path.splitext(item['src_mocap'])[0] + '.npy')
if 'predlozky' in npy_name: # bastl ! oprava rozdilneho nazvu souboru "predlozky_a_spojky" -> "predlozky_spojky"
npy_name = npy_name.replace('_a_', '_')
# tmp_trajectory = np.load(os.path.join(source_dir, os.path.splitext(item['src_mocap'])[0] + '.npy'))
tmp_trajectory = np.load(os.path.join(source_dir, npy_name))[anot[0]:anot[1], :, :]
metadata_list.append([item['sign_id'], item['src_mocap'], item['annotation_Filip_bvh_frame']])
trajectory_list.append(tmp_trajectory)
print('{:.2f} %'.format(float(i) / len(dictionary_data) * 100))
with open(path_metadata, 'wb') as pf:
pk.dump(metadata_list, pf)
with open(path_trajectory, 'wb') as pf:
pk.dump(trajectory_list, pf)
def get_chosen_joints(path_chosen_joints):
with open(path_chosen_joints, 'r') as pth: # loads chosen joints idxs from the chosen_joints.txt file
selected_joints_idxs = pth.readlines()
selected_joints_idxs = [int(item.rstrip("\n")) for item in selected_joints_idxs]
return selected_joints_idxs
def get_jointlist(path_jointlist):
"""Returns joint list
Args:
path_jointlist (string) = path to the joint_list.txt file in pc
Returns:
[list]: List of joints
"""
file_joints = open(path_jointlist, 'r')
joints = file_joints.readlines()
joints = [f.rstrip() for f in joints]
return joints
def prepare_trajectories(word1, word2, path_chosen_joints):
"""Prepares 2 signals for comparison
Args:
word1 (list): First trajectory signal to compare in format [frame,joint,channel]
word2 (list): Second trajectory signal to compare in format [frame,joint,channel]
path_chosen_joints (string): A path to the chosen_joints.txt file, for example '/data/chosen_joints.txt'
Returns:
[list]: A list of prepared values for each chosen joint separately in format [channel,frame]
"""
chosen_joints = get_chosen_joints(path_chosen_joints)
data_prepared = {}
for i in range(len(chosen_joints)):
seq1 = np.transpose(np.array(word1[:,chosen_joints[i],:]) - np.array(word1[:,1,:]))
seq1 = seq1-np.mean(seq1)
seq2 = np.transpose(np.array(word2[:,chosen_joints[i],:]) - np.array(word2[:,1,:]))
seq2 = seq2-np.mean(seq2)
data_prepared[chosen_joints[i]] = [seq1,seq2]
return data_prepared
def compute_dtw(data_prepared, alg_type):
"""Computes 2 types of DTW algorithm on given data from words_preparation fcn
Args:
data_prepared (dictionary) = prepare_trajectories fcn output
Returns:
[double]: Mean of distances for separate joints counted between 2 instances of words
"""
dtw_dist = list()
for _, val in data_prepared.items():
if alg_type == 'softdtw':
D = SquaredEuclidean(val[0].T, val[1].T)
sdtw = SoftDTW(D, gamma=1.0)
dist = sdtw.compute()
dtw_dist.append(dist)
else:
dtw_dist.append(dtw_ndim.distance_fast(np.transpose(val[0]),np.transpose(val[1])))
return np.mean(dtw_dist)
def compute_one_word(word, path_jointlist, number_of_mins, alg_type = 'dtw', order='toShorter', resample_method = 'interpolation', int_method = 'linear', distance_method = 'euclidean', graph=1):
"""Computes DTW distance between 1 word and all other words
Args:
word (string) = a given word
path_jointlist (string) = a path to the joint_list.txt file in pc
number_of_mins (int) = a number of minimum values that are given as output
alg_type (string) = dtw or softdtw algorithm
graph (boolean) = yes or no to display a graph of distance occurences in sorted words data set
Returns:
[list]: List of [number_of_mins] minimum values found with information
"""
frame_range_list = [m[2] for m in meta if m[0]==word]
sign_name_list = [m[0] for m in meta]
try:
idx = sign_name_list.index(word)
except:
print('Word not found.')
sys.exit()
occurences = sign_name_list.count(word)
print('{} instances of word "{}"'.format(occurences, word))
word_index = input('Index of word instance to test (0,{}): '.format(occurences-1))
word_traj = traj[idx+int(word_index)]
distance = np.zeros((len(traj)))
for i in range(len(traj)):
if alg_type == 'dtw': # Classic DTW algorithm
prepared_trajectories = prepare_trajectories(traj[i], word_traj, path_chosen_joints)
distance[i] = (compute_dtw(prepared_trajectories, 'dtw'))
elif alg_type == 'softdtw':# Differentiable SoftDTW version of DTW
prepared_trajectories = prepare_trajectories(traj[i], word_traj, path_chosen_joints)
distance[i] = (compute_dtw(prepared_trajectories, 'softdtw'))
elif alg_type == 'method_combination': # Signal resample and distance computation separately
if resample_method == 'interpolation':
prepared_trajectories = prepare_trajectories(traj[i], word_traj, path_chosen_joints)
resampled_trajectories,_ = resample(path_chosen_joints, prepared_trajectories, order, resample_method, int_method, 0)
dist_output = compare(resampled_trajectories, dist = distance_method)
elif resample_method == 'fourier':
prepared_trajectories = prepare_trajectories(traj[i], word_traj, path_chosen_joints)
resampled_trajectories,_ = resample(path_chosen_joints, prepared_trajectories, order, resample_method, 0)
dist_output = compare(resampled_trajectories, dist = distance_method)
else:
print('Wrong resample method entered.')
distance[i] = dist_output
if (distance_method == 'pearson'): # bigger value -> better result
matrix_sorted = (-distance).argsort()
else: # smaller value -> better result
matrix_sorted = distance.argsort()
sorted_words = [meta[item][0] for item in matrix_sorted]
sorted_frame_ranges = [[meta[item][2],item] for item in matrix_sorted]
# indexing specially the word duplicate meanings to compare their final order between multiple methods - from arr_graph
idx_of_oneofnearest = -1
for item in sorted_frame_ranges:
idx_of_oneofnearest += 1
if sorted_words[idx_of_oneofnearest] == word:
idx = frame_range_list.index(item[0])
meta[item[1]][0] = meta[item[1]][0]+' #'+str(idx+1)
bestentered = matrix_sorted[:number_of_mins-1]
best100_occurences = sorted_words[:99].count(word)
best500_occurences = sorted_words[:499].count(word)
if graph:
distance_copy = distance.copy()
if distance_method == 'pearson':
best_traj = np.zeros(shape=[15], dtype=object)
best_traj1 = np.zeros(shape=[15], dtype=object)
for k in range(15):
max_idx = np.argmax(distance_copy)
distance_copy[max_idx] = 0
max_idx = np.argmax(distance_copy)
tested_traj = np.array(word_traj[:, 5, :])
best_traj[k] = np.array(traj[max_idx][:, 5, :]) # plotting one joint
tested_traj1 = np.array(word_traj[:, 3, :])
best_traj1[k] = np.array(traj[max_idx][:, 3, :]) # plotting one joint
else:
best_traj = np.zeros(shape=[15], dtype=object)
best_traj1 = np.zeros(shape=[15], dtype=object)
for k in range(15):
min_idx = np.argmin(distance_copy)
distance_copy[min_idx] = 10000
min_idx = np.argmin(distance_copy)
tested_traj = np.array(word_traj[:, 5, :])
best_traj[k] = np.array(traj[min_idx][:, 5, :]) # plotting one joint
tested_traj1 = np.array(word_traj[:, 3, :])
best_traj1[k] = np.array(traj[min_idx][:, 3, :]) # plotting one joint
point_distance = np.zeros(shape=[15])
for q in range(15):
point_distance[q] = dtw_ndim.distance_fast(tested_traj,best_traj[q])
print(point_distance)
print('\n{}'.format(np.mean(point_distance)))
fig = plt.figure(figsize=plt.figaspect(2))
ax = fig.add_subplot(2, 1, 1, projection='3d')
ax.plot(tested_traj[:,0],tested_traj[:,1],tested_traj[:,2], marker='*', color = 'k', linewidth=0, markersize=4)
ax.plot(best_traj[0][:,0],best_traj[0][:,1],best_traj[0][:,2], marker='*', color = 'r', linewidth=0, markersize=4)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Pravé zápěstí')
ax.xaxis.set_tick_params(labelsize=7, rotation=-20)
ax.yaxis.set_tick_params(labelsize=7, rotation=20)
ax.zaxis.set_tick_params(labelsize=7)
ax.view_init(25, 60)
ax = fig.add_subplot(2, 1, 2, projection='3d')
ax.plot(tested_traj1[:,0],tested_traj1[:,1],tested_traj1[:,2], marker='*', color = 'k', linewidth=0, markersize=4)
ax.plot(best_traj1[0][:,0],best_traj1[0][:,1],best_traj1[0][:,2], marker='*', color = 'r', linewidth=0, markersize=4)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Pravá pažní kost')
ax.xaxis.set_tick_params(labelsize=7, rotation=-20)
ax.yaxis.set_tick_params(labelsize=7, rotation=20)
ax.zaxis.set_tick_params(labelsize=7)
ax.view_init(25, 60)
#fig.legend(['1. instance slova {}'.format(word),'2. instance slova {}'.format(word)])
fig.legend(['1. instance slova zítra','2. instance slova zítra'])
hist_data = [int(item == word) for item in sorted_words]
hist_data_plot = list()
idx1 = 0
idx2 = 36
while idx2<=len(hist_data):
hist_data_plot.append(hist_data[idx1:idx2].count(1))
idx1 += 36
idx2 += 36
plt.figure()
plt.barh(np.arange(len(hist_data_plot)).tolist(),hist_data_plot)
plt.xlabel('Vyskytu znaku "{}" v oblasti'.format(word))
plt.ylabel('Seřazený dataset vzdáleností rozdělen do 37 oblastí')
plt.xticks(np.arange(0, max(hist_data_plot), 1))
plt.title('Rozložení znaků s významem "{}" v seřazeném datasetu'.format(word))
plt.grid()
plt.show()
print('\nSorted results from {} of word "{}":'.format(alg_type, word))
print('Occurences of word in {} best matches: {}'.format(100,best100_occurences))
print('Occurences of word in {} best matches: {}'.format(500,best500_occurences))
print('Best {} matches with {}.instance of word: {}'.format(number_of_mins,word_index,word))
arr_graph = []
for item in bestentered:
print('{}: {}'.format(meta[item], distance[item]))
arr_graph.append(meta[item][0])
print(arr_graph)
return bestentered
def compute(path_output, path_trajectory, path_chosen_joints, alg_type = 'dtw', order = 'toShorter', resample_method = 'interpolation', int_method = 'linear', distance_method = 'euclidean', graph = 0, word_amount = None, occurence_lower_limit = None):
"""Computes distance between given number of words and all others
Args:
word_amount [int]: a number of words to count the distance, takes all if the number equals -1
alg_type [string]: 'dtw', 'softdtw' or 'method_combination' options, continues with this type of algorithm
resample_method [string]: 'interpolation' or 'fourier' resample
int_method [string]: if 'interpolation' resample_method is chosen, options are 'linear', 'quadratic' and 'cubic' interpolation
distance_method [string]: type of metrics to count distance by: 'euclidean', 'hamming', 'minkowsky', 'mahalanobis', 'pearson', 'correlationDistance', 'canberra', 'braycurtis', 'chebychev', 'fréchet'
graph [boolean]: yes or no to display a cmap='hot' graph of all words distances
Returns:
[list]: A list of all distances between the given number of words and all others
"""
with open(path_trajectory, 'rb') as pf:
traj = pk.load(pf) # trajektory [item, frame, joint, channel]
"""
# GUI to choose used method and metric
print('CHOOSE ALGORITHM TYPE:')
while(True):
alg_type = input('Enter 1: DTW, 2: SoftDTW, 3: Resample and comparison: ')
if alg_type == '1':
print('Computing DTW ... ...')
alg_type = 'dtw'
break
elif alg_type == '2':
print('Computing SoftDTW ... ...')
alg_type = 'softdtw'
break
elif alg_type == '3':
alg_type = 'method_combination'
print('CHOOSE RESAMPLE METHOD:')
while(True):
resample_method = input('Enter 1: Interpolation, 2: Fourier transform: ')
if resample_method == '1':
resample_method = 'interpolation'
print('CHOOSE INTERPOLATION METHOD:')
while(True):
int_method = input('Enter 1: Linear, 2: Quadratic, 3: Cubic: ')
if int_method == '1':
int_method = 'linear'
break
elif int_method == '2':
int_method = 'quadratic'
break
elif int_method == '3':
int_method = 'cubic'
break
else:
print('Please enter one of the offered options.')
break
elif resample_method == '2':
resample_method = 'fourier'
break
else:
print('Please enter one of the offered options.')
print('CHOOSE METRICS:')
while(True):
distance_method = input('Enter 1: Euclidean\n 2: Hamming\n 3: Minkowsky\n 4: Mahalanobis\n 5: Canberra\n 6: Chebychev\n 7: BrayCurtis\n 8:Pearson correlation coefficient\n 9:Area between curves\n')
if distance_method == '1':
distance_method = 'euclidean'
break
elif distance_method == '2':
distance_method = 'hamming'
break
elif distance_method == '3':
distance_method = 'minkowsky'
break
elif distance_method == '4':
distance_method = 'mahalanobis'
break
elif distance_method == '5':
distance_method = 'canberra'
break
elif distance_method == '6':
distance_method = 'chebychev'
break
elif distance_method == '7':
distance_method = 'braycurtis'
break
elif distance_method == '8':
distance_method = 'pearson'
break
elif distance_method == '9':
distance_method = 'area'
break
else:
print('Please enter one of the offered options.')
if resample_method == 'interpolation':
clear = lambda: os.system('cls')
clear()
print('Computing {} {}, {} ... ...'.format(int_method, resample_method, distance_method))
elif resample_method == 'fourier':
clear = lambda: os.system('cls')
clear()
print('Computing {}, {} ... ...'.format(resample_method, distance_method))
break
else:
print('Please enter one of the offered options.')
"""
if word_amount == None: # computes with all words that appears to have more occurences than given limit
distance = np.zeros((int(count_limit(occurence_lower_limit)[0]), len(traj)))
elif word_amount == -1: # computes with all words
distance = np.zeros((len(traj), len(traj)))
DTW_differences = np.zeros((len(traj), len(traj)))
elif word_amount != None: # computes with given number of words
distance = np.zeros((int(word_amount), len(traj)))
start = timer()
for i in range(len(distance)):
if (i+1)%100 == 0:
print_time = timer()
print('Currently computing {}. row of distance matrix, time: {}'.format(i+1,print_time))
for j in range(len(distance[0])):
if i == j:
distance[i, j] = 0
else:
if alg_type == 'dtw': # Classic DTW algorithm
prepared_trajectories = prepare_trajectories(traj[i], traj[j], path_chosen_joints)
distance[i, j] = (compute_dtw(prepared_trajectories, 'dtw'))
try:
distance[j, i] = distance[i, j]
except:
pass
elif alg_type == 'softdtw':# Differentiable SoftDTW version of DTW
prepared_trajectories = prepare_trajectories(traj[i], traj[j], path_chosen_joints)
distance[i, j] = (compute_dtw(prepared_trajectories, 'softdtw'))
try:
distance[j, i] = distance[i, j]
except:
pass
elif alg_type == 'method_combination': # Signal resample and distance computation separately
if resample_method == 'interpolation':
prepared_trajectories = prepare_trajectories(traj[i], traj[j], path_chosen_joints)
resampled_trajectories, DTW_difference = resample(path_chosen_joints, prepared_trajectories, order, resample_method, int_method, graph=0)
dist_output = compare(resampled_trajectories, dist = distance_method)
elif resample_method == 'fourier':
prepared_trajectories = prepare_trajectories(traj[i], traj[j], path_chosen_joints)
resampled_trajectories, DTW_difference = resample(path_chosen_joints, prepared_trajectories, order, resample_method, graph=0)
dist_output = compare(resampled_trajectories, dist = distance_method)
else:
print('Wrong resample method entered.')
distance[i, j] = dist_output
DTW_differences[i, j] = DTW_difference
try:
distance[j, i] = distance[i, j]
DTW_differences[i, j] = DTW_difference
except:
pass
else:
print('Wrong algorithm type entered.')
# Save output data, graph and time info
end = timer()
time = end-start
DTW_validator = np.mean(DTW_differences)
if (alg_type == 'dtw') or (alg_type == 'softdtw'):
with open(os.path.join(path_output, 'time_{}.txt'.format(alg_type)),"w") as file:
file.write(str(time))
with open(os.path.join(path_output, 'out_matrix_{}.pkl'.format(alg_type)), 'wb') as pk_out:
pk.dump(distance, pk_out)
if graph:
plt.imshow(distance, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.savefig(os.path.join(path_output, 'Figure_{}.eps'.format(alg_type)), dpi=300)
plt.savefig(os.path.join(path_output, 'Figure_{}.png'.format(alg_type)), dpi=300)
else:
if resample_method == 'fourier': # If the Fourier transform is computed in the name of output file will be fourier, else there will be the type of interpolation
int_method = 'fourier'
with open(os.path.join(path_output, 'time_{}_{}.txt'.format(int_method, distance_method)),"w") as file:
file.write(str(time))
file.write("\n{} efficiency: {}".format(int_method, DTW_validator))
with open(os.path.join(path_output, 'out_matrix_{}_{}.pkl'.format(int_method, distance_method)), 'wb') as pk_out:
pk.dump(distance, pk_out)
if graph:
plt.imshow(distance, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.savefig(os.path.join(path_output, 'Figure_{}_{}.eps'.format(int_method, distance_method)), dpi=300)
plt.savefig(os.path.join(path_output, 'Figure_{}_{}.png'.format(int_method, distance_method)), dpi=300)
return distance
def resample(path_chosen_joints, data_prepared, order = 'notImportant', method = 'interpolation', int_method = 'linear', graph = 0):
"""Resamples word2 signal to the length of the word1 signal
Args:
data_prepared [dictionary]: prepared data from prepare_trajectories fcn
method [string]: the method of resampling used, 'interpolation', 'fourier'
int_method [string]: the method of interpolation if the method 'interpolation' is selected
graph [boolean]: yes or no to display graph with comparison of old and resampled signal
Returns:
[dictionary]: Dictionary with the same format as data_prepared with resampled lists
"""
if method == 'fourier':
data_out = {}
chosen_joints = get_chosen_joints(path_chosen_joints)
len_words = [len(data_prepared[chosen_joints[0]][0][0]), len(data_prepared[chosen_joints[0]][1][0])]
word_to_resample = -1
word_second = -1
if order == 'toLonger': # shorter to longer
if len_words[0] <= len_words[1]:
word_to_resample = 0
word_second = 1
if len_words[0] > len_words[1]:
word_to_resample = 1
word_second = 0
elif order == 'toShorter': # longer to shorter
if len_words[0] >= len_words[1]:
word_to_resample = 0
word_second = 1
if len_words[0] < len_words[1]:
word_to_resample = 1
word_second = 0
else:
word_to_resample = 0
word_second = 1
# Computation
DTW_difference = []
for key, val in data_prepared.items():
item_new = np.zeros(shape=(2,3,len_words[word_second]))
item_old = val[word_to_resample] # for resample validation by DTW alg.
for i in range(3):
item_new[word_to_resample][i] = signal.resample(val[word_to_resample][i], len_words[word_second])
item_new[word_second][i] = val[word_second][i]
# Resample validation
DTW_difference.append(dtw_ndim.distance_fast(np.transpose(item_old),np.transpose(item_new[word_to_resample])))
data_out[key] = item_new
if graph:
x_initial = np.linspace(0, len_words[word_second], len_words[word_to_resample])
x_resampled = np.linspace(0, len_words[word_second], len_words[word_second])
data_to_graph_initial = data_prepared[chosen_joints[0]] # graphing only first chosen joint for example
data_to_graph_resampled = data_out[chosen_joints[0]]
x_resampled_line = np.linspace(0, len_words[word_second], len_words[word_second]*10)
data_to_graph_line = np.zeros(shape=(3,len_words[word_second]*10))
for i in range(3):
data_to_graph_line[i] = signal.resample(data_prepared[chosen_joints[0]][word_to_resample][i], len_words[word_second]*10)
mpl.style.use('seaborn')
fig, ax = plt.subplots(3, 1, sharex=True)
ax[0].plot(x_initial, data_to_graph_initial[word_to_resample][0], marker='D', color = 'k', linewidth=0, markersize=4)
ax[0].plot(x_resampled_line[0:1], data_to_graph_line[0][0:1], marker='*', color = 'r', linewidth=0.3, markersize=6)
ax[0].plot(x_resampled_line, data_to_graph_line[0], color = 'r', linewidth=0.3, markersize=0)
ax[0].plot(x_resampled, data_to_graph_resampled[word_to_resample][0], marker='*', color = 'r', linewidth=0, markersize=6)
ax[1].plot(x_initial, data_to_graph_initial[word_to_resample][1],marker='D', color = 'k', linewidth=0, markersize=4)
ax[1].plot(x_resampled_line[0:1], data_to_graph_line[1][0:1], marker='*', color = 'r', linewidth=0.3, markersize=6)
ax[1].plot(x_resampled_line, data_to_graph_line[1], color = 'r', linewidth=0.3, markersize=0)
ax[1].plot(x_resampled, data_to_graph_resampled[word_to_resample][1], marker='*', color = 'r', linewidth=0, markersize=6)
ax[2].plot(x_initial, data_to_graph_initial[word_to_resample][2],marker='D', color = 'k', linewidth=0, markersize=4)
ax[2].plot(x_resampled_line[0:1], data_to_graph_line[2][0:1], marker='*', color = 'r', linewidth=0.3, markersize=6)
ax[2].plot(x_resampled_line, data_to_graph_line[2], color = 'r', linewidth=0.3, markersize=0)
ax[2].plot(x_resampled, data_to_graph_resampled[word_to_resample][2], marker='*', color = 'r', linewidth=0, markersize=6)
ax[2].set_title('Interpolovaná osa Z')
ax[2].set_xlabel('čas [snímek]')
ax[1].set_ylabel('vzdálenost od počátku [cm]')
fig.legend(['Původní signál','Interpolovaný signál'], loc='upper right')
plt.show()
return data_out, np.mean(DTW_difference)
if method == 'interpolation':
data_out = {}
chosen_joints = get_chosen_joints(path_chosen_joints)
len_words = [len(data_prepared[chosen_joints[0]][0][0]), len(data_prepared[chosen_joints[0]][1][0])]
word_to_resample = -1
word_second = -1
if order == 'toLonger': # shorter to longer
if len_words[0] <= len_words[1]:
word_to_resample = 0
word_second = 1
if len_words[0] > len_words[1]:
word_to_resample = 1
word_second = 0
elif order == 'toShorter': # longer to shorter
if len_words[0] >= len_words[1]:
word_to_resample = 0
word_second = 1
if len_words[0] < len_words[1]:
word_to_resample = 1
word_second = 0
else:
word_to_resample = 0
word_second = 1
# Computation
DTW_difference = []
for key, val in data_prepared.items():
item_new = np.zeros(shape=(2,3,len_words[word_second]))
item_old = val[word_to_resample] # for interpolation validation by DTW alg.
for i in range(3):
item_new[word_to_resample][i] = interpolate_signal(val[word_to_resample][i], len_words[word_second], int_method)
item_new[word_second][i] = val[word_second][i]
# Interpolation validation
DTW_difference.append(dtw_ndim.distance_fast(np.transpose(item_old),np.transpose(item_new[word_to_resample])))
data_out[key] = item_new
if graph:
x_initial = np.linspace(0, len_words[word_second], len_words[word_to_resample])
x_resampled = np.linspace(0, len_words[word_second], len_words[word_second])
data_to_graph_initial = data_prepared[chosen_joints[0]] # graphing only first chosen joint for example
data_to_graph_resampled = data_out[chosen_joints[0]]
mpl.style.use('seaborn')
fig, ax = plt.subplots(3,1, sharex=True)
ax[0].plot(x_initial, data_to_graph_initial[word_to_resample][0], marker='D', color = 'k', linewidth=0, markersize=4)
ax[0].plot(x_initial[0:1], data_to_graph_initial[word_to_resample][0][0:1], marker='*', color = 'r', linewidth=0.3, markersize=6)
ax[0].plot(x_initial, data_to_graph_initial[word_to_resample][0], color = 'r', linewidth=0.3, markersize=0)
ax[0].plot(x_resampled, data_to_graph_resampled[word_to_resample][0], marker='*', color = 'r', linewidth=0, markersize=6)
ax[1].plot(x_initial, data_to_graph_initial[word_to_resample][1],marker='D', color = 'k', linewidth=0, markersize=4)
ax[1].plot(x_initial[0:1], data_to_graph_initial[word_to_resample][1][0:1], marker='*', color = 'r', linewidth=0.3, markersize=6)
ax[1].plot(x_initial, data_to_graph_initial[word_to_resample][1], color = 'r', linewidth=0.3, markersize=0)
ax[1].plot(x_resampled, data_to_graph_resampled[word_to_resample][1], marker='*', color = 'r', linewidth=0, markersize=6)
ax[2].plot(x_initial, data_to_graph_initial[word_to_resample][2],marker='D', color = 'k', linewidth=0, markersize=4)
ax[2].plot(x_initial[0:1], data_to_graph_initial[word_to_resample][2][0:1], marker='*', color = 'r', linewidth=0.3, markersize=6)
ax[2].plot(x_initial, data_to_graph_initial[word_to_resample][2], color = 'r', linewidth=0.3, markersize=0)
ax[2].plot(x_resampled, data_to_graph_resampled[word_to_resample][2], marker='*', color = 'r', linewidth=0, markersize=6)
ax[2].set_title('Interpolovaná osa Z')
ax[2].set_xlabel('čas [snímek]')
ax[1].set_ylabel('vzdálenost od počátku [cm]')
fig.legend(['Původní signál','Interpolovaný signál'], loc='upper right')
plt.show()
return data_out, np.mean(DTW_difference)
def interpolate_signal(signal, final_length, int_method = 'linear'):
"""Interpolates given signal to given final_length
Args:
signal [list]: Signal that is being interpolated
final_length [int]: A final length to interpolate signal to
int_method [string]: A given method of interpolation, 'linear', 'quadratic', 'cubic' etc.
Returns:
[list]: An interpolated signal with values in list
"""
x = np.r_[0:len(signal)-1:complex(len(signal),1)]
f = interpolate.interp1d(x,signal,kind=int_method)
to_interpolate = np.r_[0:len(signal)-1:complex(final_length,1)]
signal_interpolated = f(to_interpolate)
return signal_interpolated
def compare(data_prepared, dist = 'euclidean'):
"""Counts the distance between 2 3D signals using one of implemented metrics
Args:
word1 [list]: The first signal for the computation
word2 [list]: The second signal for the computation
dist [string]: A metrics used for distance computation
Returns:
[double]: The distance between 2 given signals
"""
distances = np.zeros(shape=len(data_prepared))
joint_counter = -1
# extracting the minkowsky p parameter from string dist
dist_given = copy.copy(dist)
dist = ''.join(i for i in dist_given if i.isalpha())
if dist == 'minkowsky':
p = int(''.join(filter(str.isdigit, dist_given)))
for _, val in data_prepared.items():
joint_counter += 1
if (dist != 'pearson') and (dist != 'area'):
for i in range(len(val[0])):
if dist == 'euclidean':
distances[joint_counter] += spatial.distance.euclidean(val[0][:,i],val[1][:,i])
elif dist == 'hamming':
distances[joint_counter] += manhattan_distances([val[0][:,i]],[val[1][:,i]])
elif dist == 'minkowsky':
distances[joint_counter] += spatial.distance.minkowski(val[0][:,i],val[1][:,i], p=p)
elif dist == 'mahalanobis':
S = np.random.randn(3, 3)
S = np.dot(S, S.T) #ensure positive semi-definite
distances[joint_counter] += spatial.distance.mahalanobis(val[0][:,i],val[1][:,i], S)
elif dist == 'canberra':
distances[joint_counter] += spatial.distance.canberra(val[0][:,i],val[1][:,i])
elif dist == 'braycurtis':
distances[joint_counter] += spatial.distance.braycurtis(val[0][:,i],val[1][:,i])
elif dist == 'chebyshev':
distances[joint_counter] += spatial.distance.chebyshev(val[0][:,i],val[1][:,i])
else:
print('Wrong distance metrics entered.')
sys.exit()
elif dist == 'pearson':
correlation = np.zeros(shape=len(val[0]))
for i in range(len(val[0])):
correlation[i] = np.corrcoef(val[0][i],val[1][i])[0][1]
distances[joint_counter] = np.mean(correlation)
elif dist =='area':
distances[joint_counter] = similaritymeasures.area_between_two_curves(val[0],val[1])
else:
print('Wrong distance metrics entered.')
return np.mean(distances)
def count_limit(limit):
"""Counts the sum of words instances that appears in more that given number
Args:
limit [int]: a minimum value for the word to be
Returns:
[int]: The sum of instances of chosen words
[dictionary]: The number of instances for each chosen word separately
"""
temp = []
for i in range(len(meta)):
temp.append(meta[i][0])
meta_series = pd.Series(temp)
counts = meta_series.value_counts().to_dict()
sum_counts = sum([item for key, item in counts.items() if item >= limit])
return [sum_counts,counts]
def analyze_result(tested_metrics, method_matrix, noOfminimuminstances, graph = 0):
"""Analysis of given output matrix from one algorithm type
Args:
method_matrix [list]: an output matrix from fcn compute
noOfminimuminstances [int]: how many minimum instances of one word have to exist to include the word into analysis
graph [boolean]: Yes or No to display a graph of analysis
Returns:
[words_data]: The result for all words separately
"""
[noOfWords, words_counts_dict] = count_limit(noOfminimuminstances)
matrix_chosen = method_matrix[0:noOfWords]
top_counts = [1,3,5,10,20,30]
words_data = np.empty(shape=(noOfWords,len(top_counts)), dtype=object)
if (tested_metrics == 'pearson'): # bigger value -> better result
matrix_sorted = (-matrix_chosen).argsort()
else: # smaller value -> better result
matrix_sorted = matrix_chosen.argsort()
counts_dict = {}
for i in range(noOfWords):
tops = np.zeros(len(top_counts))
tested = matrix_sorted[i].tolist()
if tested[0] == i: # pop if first is the same instance to each other
tested.pop(0)
for j in range(len(tested)):
word_main = meta[i][0]
word_compared = meta[tested[j]][0]
if word_main == word_compared:
if j < top_counts[5]:
tops[5]+=1
if j < top_counts[4]:
tops[4]+=1
if j < top_counts[3]:
tops[3]+=1
if j < top_counts[2]:
tops[2]+=1
if j < top_counts[1]:
tops[1]+=1
if j < top_counts[0]:
tops[0]+=1
if not word_main in counts_dict.keys():
counts_dict[word_main] = tops
elif word_main in counts_dict.keys():
counts_dict[word_main] = [sum(x) for x in zip(counts_dict[word_main], tops)]
words_data[i] = tops
method_results = np.zeros(len(top_counts))
for key, val in counts_dict.items():
counts_dict[key] = (np.array(val)/words_counts_dict[key]).tolist()
for i in range(len(method_results)):
method_results[i] = method_results[i] + counts_dict[key][i]
method_results = np.array(method_results)/len(counts_dict)
if graph:
mpl.style.use('seaborn')
#graph_data1 = []
graph_data1 = []
graph_data2 = []
x = []
for l in range(len(top_counts)):
graph_data1.append(method_results[l]/top_counts[l]*100)
graph_data2.append(100)
x.append(str(top_counts[l]))
plt.figure()
plt.grid(True)
plt.plot(0,0, markersize=0, linewidth=0)
plt.bar(x,graph_data2, color='red', alpha=1, width=0.4, edgecolor='black', linewidth=1)
plt.bar(x,graph_data1, color='green', alpha=1, width=0.4, edgecolor='black', linewidth=1)
plt.xlabel('Počet nejbližších projevů [znak]')
plt.ylabel('Zastoupení projevu se stejným významem [%]')
for index,data in enumerate(graph_data1):
plt.text(x=index-0.18, y=data+1, s="{:.2f}".format(data) , fontdict=dict(fontsize=11), fontweight='bold', color='w')
plt.text(x=index+0.22, y=data+1, s="%" , fontdict=dict(fontsize=11), fontweight='bold', color='k')
plt.legend(['','Rozdíl významu', 'Shoda významu'], bbox_to_anchor=(-0.1,1.02,0.6,0.2), loc="lower left", mode="expand", borderaxespad=0, ncol=3)
#plt.show()
return words_data
def compare_to_DTW(DTW_matrix, method_matrix):
max_DTW = np.max(DTW_matrix)
DTW_normalized = np.array(DTW_matrix)/max_DTW
max_method_matrix = np.max(method_matrix)
method_normalized = np.array(method_matrix)/max_method_matrix
diff = np.subtract(DTW_normalized, method_normalized)
D_matrix = diff.clip(min=0)
D1 = sum(sum(D_matrix))
D_matrix = diff.clip(max=0)
D2 = sum(sum(D_matrix))
return D1,D2, abs(D1)+abs(D2)
if __name__ == '__main__':
paths = 'Sign_Language_BP/data/paths.txt'
bvh_dir,bvh_dict,source_dir,path_jointlist,path_chosen_joints,path_dictionary,path_metadata,path_trajectory,path_output,joint_list,meta,traj = load(paths)
# converts data from angular BVH to global positions (npy matrix)
mine = False
if mine:
mine_data(bvh_dir, source_dir)
# creates sign numpy dictionary
create = False
if create:
dict_items = SL_dict.read_dictionary(path_dictionary, 'dictionary_items')
dict_takes = SL_dict.read_dictionary(path_dictionary, 'dictionary_takes')
create_trajectory_matrix(dict_items + dict_takes)
flexing = False
if flexing: # access to data examples
print('Ukázka, jak vypadá položka v proměnné "meta": {}'.format(meta[0]))
print('Proměnná "traj" je list o délce {}, což je počet všech dat nehledě na význam'.format(len(traj)))
print('první 3 položky v traj mají následující dimenze:\n{}\n{}\n{}'.format(np.shape(traj[0]), np.shape(traj[1]), np.shape(traj[2])))
print('Takže dimenze jsou [frame, joint, kanál]')
# Výběr kloubu pro vizualizaci
joint = 'RightHand\n'
# tohle vrací celý list všech nalezených jointů, obsahujících řetězec joint, takže to vrací jednorozměrné pole, proto je tam ta [0], jakože vyberu první prvek z toho pole (abych neměl list, ale int)
joint_id = [i for i, n in enumerate(joint_list) if joint in n][0]
print(joint, joint_id)
# výběr znaku podle id (takže to vrátí všechny se stejným id)
query = 'teplo'
list_of_same_signs = [e for e, s in enumerate(meta) if s[0] == query]
plt.plot(traj[list_of_same_signs[0]][:, joint_id, :])
plt.title('Takhle zhruba vypadá trajektorie znaku')
plt.figure()
plt.plot(traj[list_of_same_signs[0]][:, joint_id, 1])
plt.title('Takhle to vypadá v zoomu pro jeden kanál')
plt.show()
sorting = False
if sorting: # example of data sort
sign_name_list = [m[0] for m in meta]
unique_sign_list_unordered = set(sign_name_list)
unique_count = []
for item in unique_sign_list_unordered:
matches = [m for m in sign_name_list if m == item]
unique_count.append([item, len(matches)])
unique_count.sort(key=lambda x: x[1], reverse=True)
print((unique_count))
# DTW of one word to all others
test_one_word = False
if test_one_word:
word = 'zitra'
alg_type = 'dtw'
resample_method = 'fourier'
int_method = 'linear'
distance_method = 'hamming'
order = 'toShorter'
compute_one_word(word, path_jointlist, 41, alg_type, order, resample_method, int_method, distance_method, graph=1)
# Testing fcn of resample only
test_resample = False
if test_resample:
joint = 3
word1 = traj[191]
word1_meta = meta[191]
word2 = traj[250]
word2_meta = meta[250]
prepared_trajectories = prepare_trajectories(word1,word2,path_chosen_joints)
resample_out = resample(path_chosen_joints, prepared_trajectories, 'toLonger', 'interpolation', 'cubic', graph=1)
# Testing fcn of signal comparison
test_metrics = False
if test_metrics:
joint = 3
word1 = traj[900]
word1_meta = meta[900]
word2 = traj[200]
word2_meta = meta[200]
prepared_trajectories = prepare_trajectories(word1, word2, path_chosen_joints)
resample_out = resample(path_chosen_joints, prepared_trajectories, 'toShorter', 'interpolation', 'linear', graph=0)
kind = 'chebyshev'
distance = compare(resample_out, dist = kind)
print('{} counted over \'{}\' and \'{}\': {}'.format(kind, word1_meta[0], word2_meta[0], distance))
# Analysis of one method output matrix from compute fcn
method_analyze = True
if method_analyze:
tested_metrics1 = 'eucl'
tested_metrics2 = 'chebych'