-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_preprocess.py
1783 lines (1760 loc) · 75.2 KB
/
lib_preprocess.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
"""
Preprocess module:
Utility functions of sound processing & feature preprocessing
Author: Yu Shiu
Date: May 24, 2019
"""
import numpy as np
import os
import sys
import glob
from TonalClass import tonal
# from utilities.features import ztransform, non_ztransform, rocca
# from scipy.signal import butter, lfilter
# import multiprocessing as mp
# from itertools import repeat
#
# from species_classifier.load_feature_model import load_fea_model
# warnings.filterwarnings("error")
def bin_extract(bin_dir, sound_dir, species_name):
"""
Extract whistle contour information from bin files
:param bin_dir: the folder that has the bin files
:param sound_dir: the folder that has the sound files
:param species_name: a dict that has the list of species names
:return file_contour_pair: a dict with filename as the key. The value
consists of (i) species class label; (ii) list of [time & freq sequence]
:return bin_wav_pair: a dict with filename as the key to the sound files.
"""
bin_wav_pair = dict()
file_contour_pair = dict()
# for species in [species_name[0]]:
for species in species_name:
print(species)
bin_file_list = glob.glob(os.path.join(bin_dir, species, '*.bin'))
bin_file_list.sort()
if len(bin_file_list) == 0:
print('Tonal files were not found. ')
sys.exit()
for bb in bin_file_list:
file_base = os.path.splitext(os.path.basename(bb))[0]
file_name = file_base + '.wav'
file_path = os.path.join(sound_dir, species, file_name)
if os.path.isfile(file_path): # bin & wav match!
print('Process ' + file_name)
bin_wav_pair[bb] = file_path
# Extract contours
tonal0 = tonal(bb)
contour_list = []
while True:
try:
contour_list.append(tonal0.__next__())
except StopIteration:
print("End of the bin file of " + file_base + ".bin")
break
file_contour_pair[file_base] = [species, contour_list]
return file_contour_pair, bin_wav_pair
def contour_target_retrieve(contour_target, bin_dir_target, time_reso):
'''
Retrieve whistle contours from .bin files
:param contour_target:
:param bin_dir_target:
:param conf:
:return:
'''
# time_reso = conf['time_reso']
# duration_thre = conf["duration_thre"]
# transform = conf['transform']
# species_id = conf['species_id']
duration_max = 0
count_all = 0
count_long = 0
data_list = []
df_ff_fea_list = []
df_ff_list = []
contour_target_list = []
# iterate over files
for ff in sorted(list(contour_target.keys())):
print(ff)
file_contour = contour_target[ff][1]
label_contour = contour_target[ff][0]
len_contour = len(file_contour)
if len_contour >= 1:
# Retrieve contours
contour_target_ff, count_all, duration_max = contour_retrieve(file_contour, count_all, duration_max, time_reso)
contour_target_list.append([ff, label_contour, contour_target_ff])
return contour_target_list
def contour_retrieve(file_contour, count_all, duration_max, time_reso):
print('Retrieving contours...')
contour_target_ff = []
contour_dur = np.zeros(len(file_contour))
len_contour = len(file_contour)
# read contours into the var contour_target_ff
for cc in range(len_contour):
count_all += 1
time_contour = file_contour[cc]['Time']
freq_contour = file_contour[cc]['Freq']
duration = time_contour[-1] - time_contour[0]
contour_dur[cc] = duration
duration_max = max([duration_max, duration])
# linear interpolation
time_contour_interp = np.arange(time_contour[0], time_contour[-1],
time_reso)
freq_contour_interp = np.interp(time_contour_interp, time_contour,
freq_contour)
contour_target_ff_cc = dict()
contour_target_ff_cc['Time'] = time_contour_interp
contour_target_ff_cc['Freq'] = freq_contour_interp
contour_target_ff.append(contour_target_ff_cc)
return contour_target_ff, count_all, duration_max
# def butter_bandpass(lowcut, highcut, fs, order=5):
# nyq = 0.5 * fs
# low = lowcut / nyq
# high = highcut / nyq
# b, a = butter(order, [low, high], btype='band')
# return b, a
#
#
# def butter_bandpass_filter(data, lowcut, highcut, fs, order=10):
# b, a = butter_bandpass(lowcut, highcut, fs, order=order)
# y = lfilter(b, a, data)
# return y
#
#
# def calc_cepstrum(samples, sample_rate, win_size, step_size, fft_size, filt_num=64, low_freq=5000, high_freq=23500):
# num_frame = int(np.floor((samples.shape[0]-win_size)/step_size))
# fea_cepstrum_list = []
# for ii in range(num_frame):
# samp_frame = samples[ii*step_size:ii*step_size+win_size] # windowing
# samp_fft = np.zeros(fft_size)
# samp_fft[:win_size] = samp_frame # zero-padding
#
# # samp_fft2 = butter_bandpass_filter(samp_fft, 5000, 23500, sample_rate, order=64)
# # ceps = real_cepstrum(samp_fft, fft_size)
# ceps = filtered_cepstrum(samp_fft, fft_size, sr=sample_rate,
# num_fil=filt_num, low_freq=low_freq, high_freq=high_freq)
# fea_cepstrum_list.append(ceps)
# fea_cepstrum = np.vstack(fea_cepstrum_list)
#
# return fea_cepstrum # array (num_sample, dim_fea)
#
#
# def calc_cepstrum_parallel(samples, config):
# sample_rate = config['sample_rate']
# win_size = config['input_size']
# step_size = int(win_size/2)
# fft_size = win_size
# filt_num = config['filt_num'] # 64
# low_freq = config['low_freq'] # 5000
# high_freq = config['high_freq'] # 23500
#
# fea_cep_raw = calc_cepstrum(samples, sample_rate, win_size, step_size,
# fft_size, filt_num=64, low_freq=5000,
# high_freq=23500)
# mid_point = int(fea_cep_raw.shape[0] / 2)
# fea_cep_0 = fea_cep_raw[
# mid_point - 5:mid_point + 5] # center of the contour
#
# return fea_cep_0 # array (num_sample, dim_fea)
#
#
# def calc_energy(samples, sample_rate, win_size, step_size, fft_size, filt_num=64, low_freq=5000, high_freq=23500):
# num_frame = int(np.floor((samples.shape[0]-win_size)/step_size))
# fea_cepstrum_list = []
# for ii in range(num_frame):
# samp_frame = samples[ii*step_size:ii*step_size+win_size] # windowing
# samp_fft = np.zeros(fft_size)
# samp_fft[:win_size] = samp_frame # zero-padding
#
# # samp_fft2 = butter_bandpass_filter(samp_fft, 5000, 23500, sample_rate, order=64)
# # ceps = real_cepstrum(samp_fft, fft_size)
# # ceps = filtered_cepstrum(samp_fft, fft_size, sr=sample_rate,
# # num_fil=filt_num, low_freq=low_freq, high_freq=high_freq)
# ceps = filtered_energy(samp_fft, fft_size, sr=sample_rate,
# num_fil=filt_num, low_freq=low_freq, high_freq=high_freq)
# fea_cepstrum_list.append(ceps)
# fea_cepstrum = np.vstack(fea_cepstrum_list)
#
# return fea_cepstrum # array (num_sample, dim_fea)
#
#
#
#
# def real_cepstrum(x, fft_size):
# r"""Compute the real cepstrum of a real sequence.
# x : ndarray
# Real sequence to compute real cepstrum of.
# n : {None, int}, optional
# Length of the Fourier transform.
# Returns
# """
# if x.shape[0] != fft_size:
# raise Exception('Sample length needs to be the same as FFT size.')
# spectrum = np.fft.fft(x, n=fft_size)
# ceps = np.fft.ifft(np.log(np.abs(spectrum))).real
#
# return ceps
#
#
# def filtered_cepstrum(x, fft_size, sr, num_fil, low_freq, high_freq):
# r"""Compute the real cepstrum of a real sequence.
# x : ndarray
# Real sequence to compute real cepstrum of.
# n : {None, int}, optional
# Length of the Fourier transform.
# Returns
# """
# if x.shape[0] != fft_size:
# raise Exception('Sample length needs to be the same as FFT size.')
# spectrum = np.fft.fft(x, n=fft_size)
#
# win_half_size = (high_freq-low_freq)*1.0 / (num_fil+1)/sr*fft_size
# win_full_size = int(2.0*win_half_size)
# if win_full_size % 2 == 1: # check if odd
# win_full_size -= 1 # make it even
#
# win_half_size_rounded = win_full_size/2
# win_half = np.arange(win_half_size_rounded)/win_half_size_rounded
# win_full = np.concatenate((win_half, 1.0-win_half))
#
# ind_start = (win_half_size*np.arange(num_fil*1.0)).astype(int)
# filtered_sepctrum = np.array([(np.sqrt((np.abs(spectrum[ii:ii+win_full_size])**2)*win_full)).sum() for ii in ind_start])
# # ceps = np.fft.ifft(np.log(np.abs(filtered_sepctrum))).real # original one
# ceps = np.fft.ifft(np.log(filtered_sepctrum**2.)).real
#
# return ceps
#
#
# def filtered_energy(x, fft_size, sr, num_fil, low_freq, high_freq):
# r"""Compute the real cepstrum of a real sequence.
# x : ndarray
# Real sequence to compute real cepstrum of.
# n : {None, int}, optional
# Length of the Fourier transform.
# Returns
# """
# if x.shape[0] != fft_size:
# raise Exception('Sample length needs to be the same as FFT size.')
# spectrum = np.fft.fft(x, n=fft_size)
#
# win_half_size = (high_freq-low_freq)*1.0 / (num_fil+1)/sr*fft_size
# win_full_size = int(2.0*win_half_size)
# if win_full_size % 2 == 1: # check if odd
# win_full_size -= 1 # make it even
#
# win_half_size_rounded = win_full_size/2
# win_half = np.arange(win_half_size_rounded)/win_half_size_rounded
# win_full = np.concatenate((win_half, 1.0-win_half))
#
# ind_start = (win_half_size*np.arange(num_fil*1.0)).astype(int)
# filtered_sepctrum = np.array([(np.sqrt((np.abs(spectrum[ii:ii+win_full_size])**2)*win_full)).sum() for ii in ind_start])
# # ceps = np.fft.ifft(np.log(np.abs(filtered_sepctrum))).real
#
# return filtered_sepctrum
#
#
# def power_law_calc(spectro_mat, nu1=2.0, nu2=1.0, gamma=2.0):
# """
# The preprocessing part of Generalized power-law (GPL) energy detector. It
# preprocesses the spectrogram so that the tonal sounds are emphasized
# whereas short-duration time impulse and narrow-band noises are diminished.
#
# :param spectro_mat: spectrogram matrix that GPL is applied on
# :param nu1: Weight parameter to the power of freq
# :param nu2: Weight parameter to the power of time
# :param gamma:
# :return power_law_mat: spectrogram preprocessed with GPL
# """
# dim_f, dim_t = spectro_mat.shape
# mu_k = [power_law_find_mu(spectro_mat[ff, :]) for ff in range(dim_f)]
#
# mat0 = spectro_mat ** gamma - np.array(mu_k).reshape(dim_f, 1) * np.ones(
# (1, dim_t))
# mat_a_denom = [(np.sum(mat0[:, tt] ** 2.)) ** .5 for tt in range(dim_t)]
# mat_a = mat0 / (np.ones((dim_f, 1)) * np.array(mat_a_denom).reshape(1, dim_t))
# mat_b_denom = [(np.sum(mat0[ff, :] ** 2.)) ** .5 for ff in range(dim_f)]
# mat_b = mat0 / (np.array(mat_b_denom).reshape(dim_f, 1) * np.ones((1, dim_t)))
#
# mat_a = mat_a * (mat_a > 0) # set negative values into zero
# mat_b = mat_b * (mat_b > 0)
# power_law_mat = (mat_a**nu1)*(mat_b**nu2)
# # power_law_mat = (mat_a ** (2.0 * nu1)) * (mat_b ** (2.0 * nu2))
# # PowerLawTFunc = np.sum((mat_a**nu1)*(mat_b**nu2), axis=0)
#
# return power_law_mat
#
#
# def power_law_find_mu(spectro_target):
# """
# Function used in Generalized Power Law (GPL) to find the mu for each
# frequency bin
#
# :param spectro_target: input spectrogram
# :return an array of mu where one for each frequency:
# """
# spec_sorted = np.sort(spectro_target)
# spec_half_len = int(np.floor(spec_sorted.shape[0]*.5))
# ind_j = np.argmin(spec_sorted[spec_half_len:spec_half_len*2] - spec_sorted[0:spec_half_len])
# mu = np.mean(spec_sorted[ind_j:ind_j+spec_half_len])
#
# return mu
#
#
# def bin_extract(bin_dir, sound_dir, species_name):
# """
# Extract whistle contour information from bin files
# :param bin_dir: the folder that has the bin files
# :param sound_dir: the folder that has the sound files
# :param species_name: a dict that has the list of species names
# :return file_contour_pair: a dict with filename as the key. The value
# consists of (i) species class label; (ii) list of [time & freq sequence]
# :return bin_wav_pair: a dict with filename as the key to the sound files.
# """
# bin_wav_pair = dict()
# file_contour_pair = dict()
# # for species in [species_name[0]]:
# for species in species_name:
# print(species)
# bin_file_list = glob.glob(os.path.join(bin_dir, species, '*.bin'))
# bin_file_list.sort()
# if len(bin_file_list) == 0:
# print('Tonal files were not found. ')
# sys.exit()
# for bb in bin_file_list:
# file_base = os.path.splitext(os.path.basename(bb))[0]
# file_name = file_base + '.wav'
# file_path = os.path.join(sound_dir, species, file_name)
# if os.path.isfile(file_path): # bin & wav match!
# print('Process ' + file_name)
# bin_wav_pair[bb] = file_path
# # Extract contours
# tonal0 = tonal(bb)
# contour_list = []
# while True:
# try:
# contour_list.append(tonal0.__next__())
# except StopIteration:
# print("End of the bin file of " + file_base + ".bin")
# break
# file_contour_pair[file_base] = [species, contour_list]
# return file_contour_pair, bin_wav_pair
#
#
# def timestep_info(contour_all, timestep_file, percentile_list):
# """
# Measure the information of time steps in each whistle contours
# :param contour_all: output from bin_extraction. It has all the info
# contained in bin files
# :param timestep_file: the output file that time step info is written to
# :param percentile_list: the levels of percentiles we target at. E.g.
# [0, 25, 50, 75, 100] has the quartiles as well as min and max.
# """
# contour_count = 0
# contour_fixed_step = 0
# f = open(timestep_file, "w")
# for ff in contour_all.keys():
# # print(ff)
# f.write(ff+"\n")
# file_contour = contour_all[ff][1]
# for cc in range(len(file_contour)):
# # print('Contour: '+str(cc))
# f.write('Contour: '+str(cc)+' Length: '+str(len(file_contour[cc]['Time']))+'\t')
# timestep_prctile = np.percentile(np.diff(file_contour[cc]['Time']),
# percentile_list)
# contour_count += 1
# if timestep_prctile[0] == timestep_prctile[4]: # max - min
# contour_fixed_step += 1
# for ii in timestep_prctile.tolist():
# f.write(str(ii)+"\t")
# f.write("\n")
# f.close()
# print("contour_count: "+str(contour_count))
# print("contour_fixed_step: "+str(contour_fixed_step))
#
#
# def fea_cqt_powerlaw(samples, samplerate):
# nu_1 = 3.0
# nu_2 = 1.0
# gamma = 1.0
#
# # cqt
# spectro_cqt = np.abs( librosa.cqt(samples, sr=samplerate, hop_length=1000,
# n_bins=12 * 3 * 4,
# bins_per_octave=12 * 4,
# fmin=6000))
# spectro_cqt_ud = np.flipud(spectro_cqt)
# spectro_cqt_pl = power_law_calc(spectro_cqt_ud, nu_1, nu_2, gamma)
# fea = ((spectro_cqt_pl - spectro_cqt_pl.min()) / (spectro_cqt_pl.max())).T
#
# return fea
#
#
# def contour_repeat_score(contour_target_ff, len_contour, time_reso,
# overlap_time_thre=0.05, std_norm_thre=0.05):
# contour_count = 0
# contour_ind = np.ones(len_contour)
# for cc1 in range(len_contour):
# # print("cc1: " + str(cc1) + " length " + str(
# # contour_target_ff[cc1]['Time'].shape[0]))
# for cc2 in range(cc1 + 1, len_contour):
# a1 = contour_target_ff[cc1]['Time'][0]
# b1 = contour_target_ff[cc1]['Time'][-1]
# a2 = contour_target_ff[cc2]['Time'][0]
# b2 = contour_target_ff[cc2]['Time'][-1]
# if (b1 - a2 > 0) & (b2 - a1 > 0): # there is overlap greater than 0.1 sec
# if a1 <= a2:
# time_start = a2
# seq_ind = 2
# else:
# time_start = a1
# seq_ind = 1
# if b1 <= b2:
# time_end = b1
# else:
# time_end = b2
#
# if time_end - time_start >= overlap_time_thre:
# if seq_ind == 1:
# ind_list = list(range(0, int(
# round((time_end - a1) / time_reso))))
# freq_seq_1 = [contour_target_ff[cc1]['Freq'][tt] for
# tt in ind_list]
# freq_seq_2 = [contour_target_ff[cc2]['Freq'][
# tt + int(round((time_start - a2) / time_reso))]
# for tt in ind_list]
# else: # seq_ind 2
# ind_list = list(range(0, int(
# round((time_end - a2) / time_reso))))
# freq_seq_2 = [contour_target_ff[cc2]['Freq'][tt] for
# tt in ind_list]
# freq_seq_1 = [contour_target_ff[cc1]['Freq'][tt + int(round((time_start - a1) / time_reso))] for tt in ind_list]
#
# ratio_seq = np.array(freq_seq_1) / np.array(freq_seq_2)
# try:
# std_norm = ratio_seq.std() / ratio_seq.mean()
# except RuntimeWarning:
# print()
#
# if std_norm <= std_norm_thre: # small freq one will lose a point
# if np.array(freq_seq_1).mean() <= np.array(
# freq_seq_2).mean():
# contour_ind[cc2] -= 1
# else:
# contour_ind[cc1] -= 1
#
# # print('Duration steps: ' + str(int(
# # round((time_end - time_start) / time_reso + 1.0))))
# # print('')
# contour_count += 1
# print('Remaining contours: ' + str((contour_ind >= 1.0).sum())+' / '+str(len_contour))
# return contour_ind, contour_count
#
#
# def fea_freq_label_extract(contour_target_ff, contour_ind, fea_list, label_list, species_label, count_long, duration_thre, transform):
# contour_ind_list = np.where(contour_ind >= 1.0)[0].tolist()
# fea_dict = dict()
# for cc in contour_ind_list:
# freq_contour_interp = contour_target_ff[cc]['Freq']
# time_contour_interp = contour_target_ff[cc]['Time']
# duration = time_contour_interp[-1] - time_contour_interp[0]
#
# if duration >= duration_thre:
# count_long += 1
# if transform == "zscorederiv":
# fea_contour = ztransform(freq_contour_interp)
# elif transform == "non_z":
# fea_contour = non_ztransform(freq_contour_interp)
# elif transform == "rocca":
# fea_dict = rocca(freq_contour_interp, time_contour_interp)
# # fea_contour = np.array(list(fea_dict.values()))
# fea_contour = np.array([fea_dict[kk] for kk in sorted(fea_dict.keys())])
# else: # pass freq contour directly
# fea_contour = freq_contour_interp
# fea_list.append(fea_contour)
# label_list.append(species_label)
#
# if transform == "rocca":
# fea_name = sorted(fea_dict.keys())
# else:
# fea_name = None
# return fea_list, label_list, count_long, fea_name
#
#
# def fea_context_base_extract(contour_target_ff, contour_ind, ff, species_name_this, species_label, bin_wav_pair,
# bin_dir_target, duration_thre, conf, fea='rocca_cep'):
# '''
# Extract features for each file contour_target_ff
# :param contour_target_ff:
# :param contour_ind:
# :param ff:
# :param species_name_this:
# :param species_label:
# :param bin_wav_pair:
# :param bin_dir_target:
# :param duration_thre:
# :param conf:
# :param fea:
# :return:
# '''
# contour_ind_list = np.where(contour_ind >= 1.0)[0].tolist()
# fea_dict = dict()
# fea_rocca_list = []
# fea_cep_list = []
# label_list = []
# count_long = 0
# if (fea == 'cep') or (fea == 'rocca_cep'):
# model_fea = load_fea_model(model_dir)
# sample_list = []
# sound_file = bin_wav_pair[os.path.join(bin_dir_target, species_name_this,
# ff + '.bin')]
# # # extract time of contours
# # time_start = []
# # time_stop = []
# # for cc in contour_ind_list:
# # time_contour_interp = contour_target_ff[cc]['Time']
# # time_start.append(time_contour_interp[0])
# # time_stop.append(time_contour_interp[-1])
# # df_contour = pd.DataFrame(list(zip(time_start, time_stop)), columns=['start_time', 'end_time'])
# timestamp = []
# for cc in contour_ind_list:
# freq_contour_interp = contour_target_ff[cc]['Freq']
# time_contour_interp = contour_target_ff[cc]['Time']
# time_start = time_contour_interp[0]
# time_stop = time_contour_interp[-1]
# duration = time_stop - time_start
# time_center = (time_stop + time_start)*.5
#
# if duration >= duration_thre:
# # print('cc: '+str(cc))
# timestamp.append(time_center)
# count_long += 1
#
# # feature "rocca":
# if fea == 'rocca' or fea == 'rocca_cep':
# fea_dict = rocca(freq_contour_interp, time_contour_interp)
# fea_rocca = np.array(
# [fea_dict[kk] for kk in sorted(fea_dict.keys())])
#
# t1 = (np.isinf(fea_rocca)).sum()
# if t1 > 0:
# print('Number of infinity is' + str(t1))
# print()
# else:
# fea_rocca = None
#
# # feature cep
# if fea == 'cep' or fea == 'rocca_cep':
# samples, samplerate = librosa.load(sound_file,
# offset=time_contour_interp[0],
# duration=duration,
# sr=conf['sample_rate'],
# mono=True)
# else:
# # fea_model_cep_flatten = []
# # fea_cep_0 = []
# samples = None
#
# # Add the newly calculated features into a list
# fea_rocca_list.append(fea_rocca)
# # fea_cep_list.append(fea_model_cep_flatten)
# sample_list.append(samples)
# # fea_cep_list.append(fea_cep_0)
# label_list.append(species_label)
# print('Remaining contours after duration check: : ' +
# str(count_long) + ' / ' + str(len(contour_ind_list)))
#
# if fea == 'rocca' or fea == 'rocca_cep':
# if len(fea_rocca_list) != 0:
# fea_rocca = np.stack(fea_rocca_list)
# else:
# fea_rocca = None
# else:
# fea_rocca = None
# if fea == 'cep' or fea == 'rocca_cep':
# # if len(fea_cep_list) == not 0:
# if len(sample_list) != 0:
# # calculate cepstral coeff in parallel
# pool_cep = mp.Pool(processes=4)
# fea_cep_list = pool_cep.starmap(calc_cepstrum_parallel,
# zip(sample_list, repeat(conf)))
# pool_cep.close()
# pool_cep.join()
#
# # convert cepstral coeff into feature vectors through pre-trained LSTM model
# fea_cep = np.stack(fea_cep_list)
# fea_cep = np.expand_dims(fea_cep, axis=3)
# fea_cep = np.expand_dims(fea_cep, axis=4)
# fea_model_cep = model_fea.predict(fea_cep)
# # fea_model_cep_flatten = fea_model_cep1.flatten()
# else:
# fea_model_cep = None
# else:
# fea_model_cep = None
# fea_name = sorted(fea_dict.keys())
#
# return fea_rocca, fea_model_cep, label_list, count_long, fea_name, timestamp
#
#
# def fea_context_ae_extract(contour_target_ff, contour_ind, ff,
# species_name_this, species_label, bin_wav_pair,
# bin_dir_target, duration_thre, conf, encoder, min_fea,
# max_fea, dur_max_ind, fea):
# '''
# extract context features based on autoencoder
# :param contour_target_ff:
# :param contour_ind:
# :param ff:
# :param species_name_this:
# :param species_label:
# :param bin_wav_pair:
# :param bin_dir_target:
# :param duration_thre:
# :param conf:
# :param fea:
# :return:
# '''
# contour_ind_list = np.where(contour_ind >= 1.0)[0].tolist()
# fea_dict = dict()
# # fea_rocca_list = []
# # fea_cep_list = []
# label_list = []
# count_long = 0
# sample_list = []
# sound_file = bin_wav_pair[os.path.join(bin_dir_target, species_name_this,
# ff + '.bin')]
# # # extract time of contours
# timestamp = []
# contour_list = []
# for cc in contour_ind_list:
# freq_contour_interp = contour_target_ff[cc]['Freq']
# time_contour_interp = contour_target_ff[cc]['Time']
# time_start = time_contour_interp[0]
# time_stop = time_contour_interp[-1]
# duration = time_stop - time_start
# time_center = (time_stop + time_start)*.5
#
# if duration >= duration_thre:
# # print('cc: '+str(cc))
# timestamp.append(time_center)
# contour_list.append(freq_contour_interp)
# count_long += 1
# print('Remaining contours after duration check: : ' +
# str(count_long) + ' / ' + str(len(contour_ind_list)))
#
# label_list = []
# fea_name = []
# if count_long >= 1:
# max_minus_min_fea = max_fea - min_fea
# if (fea == 'ae') or (fea == 'vae'):
# fea_list_train_norm = []
# for cc in range(len(contour_list)):
# fea_list_train_norm.append((contour_list[cc] - min_fea) / max_minus_min_fea)
# fea_arr_train = pad_sequences(fea_list_train_norm,
# maxlen=dur_max_ind,
# dtype='float')
# fea_arr_train_3d = np.expand_dims(fea_arr_train, axis=2)
# if fea == 'vae':
# encoded_fea, _, _ = encoder.predict(fea_arr_train_3d)
# else:
# encoded_fea = encoder.predict(fea_arr_train_3d)
#
# for ll in range(encoded_fea.shape[0]):
# label_list.append(species_label)
# for ee in range(encoded_fea.shape[1]):
# fea_name.append('ae'+str(ee+1))
# else:
# encoded_fea = None
#
# return encoded_fea, label_list, count_long, fea_name, timestamp
#
#
# def fea_context(df_ff, df_ff_fea, timestamp, conf, fea_name):
# context_win = conf['context_win']
# df_ff['Time'] = np.array(timestamp) # add center time into meta info dataframe
# # select contours within context windows
# df_ff_tot = pd.concat([df_ff, df_ff_fea], axis=1)
#
# fea_context_list = []
# fea_rocca_mean = [ff+'_mean' for ff in fea_name[0:]]
# fea_rocca_std = [ff + '_std' for ff in fea_name[0:]]
# fea_name_context = ['num_contour', 'num_contour_half', 'num_contour_quarter',
# 'dur_mean', 'dur_std', 'dur_5',
# 'dur_25', 'dur_50', 'dur_75', 'dur_95',
# 'time_diff_mean', 'time_diff_std', 'time_diff_5',
# 'time_diff_25', 'time_diff_50', 'time_diff_75',
# 'time_diff_95',
# 'dur_wei_mean', 'dur_wei_std', 'dur_wei_5',
# 'dur_wei_25', 'dur_wei_50', 'dur_wei_75', 'dur_wei_95']\
# + fea_rocca_mean + fea_rocca_std
# for _, row in df_ff_tot.iterrows():
# fea_context = []
# df_context = df_ff_tot.loc[np.abs(df_ff_tot['Time']-row['Time'])<context_win]
#
# # number of contours within context window
# fea_context.append(df_context.shape[0])
# # number of contours within a half context window
# fea_context.append((df_ff_tot.loc[np.abs(df_ff_tot['Time']-row['Time'])<context_win*.5]).shape[0] / df_context.shape[0])
# # number of contours within a quarter context window
# fea_context.append((df_ff_tot.loc[np.abs(df_ff_tot['Time']-row['Time'])<context_win*.25]).shape[0] / df_context.shape[0])
#
# # duration
# fea_dur = np.array(df_context['dur'])
# fea_dur_percent = np.percentile(fea_dur, [5, 25, 50, 75, 95])
# fea_context.append(fea_dur.mean())
# fea_context.append(fea_dur.std())
# for ff in fea_dur_percent:
# fea_context.append(ff)
#
# # time diff
# fea_time_diff = np.array((df_context['Time']-row['Time']).abs())
# fea_time_diff_percent = np.percentile(fea_time_diff, [5, 25, 50, 75, 95])
# fea_context.append(fea_time_diff.mean())
# fea_context.append(fea_time_diff.std())
# for ff in fea_time_diff_percent:
# fea_context.append(ff)
#
# # weighted duration w.r.t. time diff
# fea_dur_weighted = np.array(df_context['dur'])*(1. - (np.array((df_context['Time']-row['Time']).abs())/conf['context_win']))
# fea_dur_weighted_percent = np.percentile(fea_dur_weighted, [5, 25, 50, 75, 95])
# fea_context.append(fea_dur_weighted.mean())
# fea_context.append(fea_dur_weighted.std())
# for ff in fea_dur_weighted_percent:
# fea_context.append(ff)
#
# # means of individual rocca features
# # original mean
# fea_rocca_mean = (np.array(df_context.iloc[:,4:])).mean(axis=0)
# for ff in fea_rocca_mean:
# fea_context.append(ff)
# # weighted mean
# # fea_rocca_weight = np.array(df_context.iloc[:, 4:]).T*(1. - (np.array((df_context['Time']-row['Time']).abs())/conf['context_win']))
# # fea_rocca_mean_weight = fea_rocca_weight.mean(axis=1)
# # for ff in fea_rocca_mean_weight:
# # fea_context.append(ff)
#
# # std of individual rocca features
# fea_rocca_std = (np.array(df_context.iloc[:,4:])).mean(axis=0)
# for ff in fea_rocca_std:
# fea_context.append(ff)
# # fea_rocca_std_weight = fea_rocca_weight.std(axis=1)
# # for ff in fea_rocca_std_weight:
# # fea_context.append(ff)
#
# fea_context_list.append(fea_context)
# df_context_fea = pd.DataFrame(fea_context_list, columns=fea_name_context)
#
# return df_context_fea
#
#
# def fea_context_ae(df_ff, df_ff_fea, timestamp, conf, fea_name):
# context_win = conf['context_win']
# df_ff['Time'] = np.array(timestamp) # add center time into meta info dataframe
# # select contours within context windows
# df_ff_tot = pd.concat([df_ff, df_ff_fea], axis=1)
#
# fea_name_context = []
# for aa in range(conf['ae_latent_dim']):
# fea_name_context.append('ae_mean'+str(aa+1))
# for aa in range(conf['ae_latent_dim']):
# fea_name_context.append('ae_std'+str(aa+1))
#
# fea_context_list = []
# for _, row in df_ff_tot.iterrows():
# fea_context = []
# df_context = df_ff_tot.loc[np.abs(df_ff_tot['Time']-row['Time'])<context_win]
#
# # ae
# fea_ae = np.array((df_context.iloc[:, 4:]))
# fea_ae_mean = fea_ae.mean(axis=0)
# fea_ae_std = fea_ae.std(axis=0)
# for ff in fea_ae_mean:
# fea_context.append(ff)
# for ff in fea_ae_std:
# fea_context.append(ff)
#
# fea_context_list.append(fea_context)
# df_context_fea = pd.DataFrame(fea_context_list, columns=fea_name_context)
#
# return df_context_fea
#
#
# def fea_freq_cep_label_extract(contour_target_ff, contour_ind, ff,
# species_name_this, species_label, bin_wav_pair,
# bin_dir_target, duration_thre,
# model_dir, conf, fea='rocca_cep'):
# '''
# :param contour_target_ff:
# :param contour_ind:
# :param ff:
# :param species_name_this:
# :param species_label:
# :param bin_wav_pair:
# :param bin_dir_target:
# :param count_long:
# :param duration_thre:
# :param fea: 'rocca_cep', 'rocca' or 'cep'
# :return:
# '''
# contour_ind_list = np.where(contour_ind >= 1.0)[0].tolist()
# fea_dict = dict()
# fea_rocca_list = []
# fea_cep_list = []
# label_list = []
# count_long = 0
# if fea != 'rocca':
# model_fea = load_fea_model(model_dir)
# sample_list = []
# sound_file = bin_wav_pair[os.path.join(bin_dir_target,
# species_name_this, ff + '.bin')]
#
# for cc in contour_ind_list:
# freq_contour_interp = contour_target_ff[cc]['Freq']
# time_contour_interp = contour_target_ff[cc]['Time']
# duration = time_contour_interp[-1] - time_contour_interp[0]
#
# if duration >= duration_thre:
# print('cc: '+str(cc))
# count_long += 1
#
# # feature "rocca":
# if fea == 'rocca' or fea == 'rocca_cep':
# fea_dict = rocca(freq_contour_interp, time_contour_interp)
# fea_rocca = np.array(
# [fea_dict[kk] for kk in sorted(fea_dict.keys())])
#
# t1 = (np.isinf(fea_rocca)).sum()
# if t1 > 0:
# print('Number of infinity is' + str(t1))
# print()
#
# else:
# fea_rocca = []
#
# # feature cep
# if fea == 'cep' or fea == 'rocca_cep':
# samples, samplerate = librosa.load(sound_file,
# offset=time_contour_interp[0],
# duration=duration,
# sr=conf['sample_rate'],
# mono=True)
#
# # conf['input_size'] = input_size
# # conf['sample_rate'] = samplerate
# # pool_cep = mp.Pool(processes=4)
# # results = pool_cep.starmap(fft_sample_to_spectro,
# # zip(SampList, repeat(config)))
# # # fea_cep_raw = calc_cepstrum(samples, samplerate, win_size=input_size, step_size=int(input_size/2), fft_size=input_size, filt_num=conf['filt_num'],
# # # low_freq=conf['low_freq'], high_freq=conf['high_freq'])
# #
# # # fea_cep_1 = fea_cep_raw[:10] # beginning of the contour
# # mid_point = int(fea_cep_raw.shape[0]/2)
# # fea_cep_2 = fea_cep_raw[mid_point-5:mid_point+5] # center of the contour
# # # fea_cep_3 = fea_cep_raw[-10:] # end of the contour
# #
# # # fea_cep = np.stack([fea_cep_1, fea_cep_2, fea_cep_3], axis=0)
# # fea_cep_0 = fea_cep_2
# # fea_cep = np.expand_dims(fea_cep, axis=0)
# # fea_cep = np.expand_dims(fea_cep, axis=3)
# # fea_cep = np.expand_dims(fea_cep, axis=4)
#
# # fea_model_cep1 = model_fea.predict(fea_cep)
# # fea_model_cep_flatten = fea_model_cep1.flatten()
# # fea_model_cep_flatten = fea_model_cep1
# else:
# # fea_model_cep_flatten = []
# # fea_cep_0 = []
# samples = []
#
# # Add the newly calculated features into a list
# fea_rocca_list.append(fea_rocca)
# # fea_cep_list.append(fea_model_cep_flatten)
# sample_list.append(samples)
# # fea_cep_list.append(fea_cep_0)
# label_list.append(species_label)
# print('Remaining contours after duration check: : ' +
# str(count_long) + ' / ' + str(len(contour_ind_list)))
#
# if fea == 'rocca' or fea == 'rocca_cep':
# if len(fea_rocca_list) != 0:
# fea_rocca = np.stack(fea_rocca_list)
# else:
# fea_rocca = []
# else:
# fea_rocca = []
# if fea == 'cep' or fea == 'rocca_cep':
# # if len(fea_cep_list) == not 0:
# if len(sample_list) != 0:
# # calculate cepstral coeff in parallel
# pool_cep = mp.Pool(processes=4)
# fea_cep_list = pool_cep.starmap(calc_cepstrum_parallel,
# zip(sample_list, repeat(conf)))
# pool_cep.close()
# pool_cep.join()
#
# # convert cepstral coeff into feature vectors through pre-trained LSTM model
# fea_cep = np.stack(fea_cep_list)
# fea_cep = np.expand_dims(fea_cep, axis=3)
# fea_cep = np.expand_dims(fea_cep, axis=4)
# fea_model_cep = model_fea.predict(fea_cep)
# # fea_model_cep_flatten = fea_model_cep1.flatten()
# else:
# fea_model_cep = []
# else:
# fea_model_cep_flatten = []
# fea_name = sorted(fea_dict.keys())
#
# return fea_rocca, fea_model_cep, label_list, count_long, fea_name
#
#
# def fea_cep_label_extract(contour_target_ff, contour_ind, ff,
# species_name_this, species_label, bin_wav_pair,
# bin_dir_target, count_long, duration_thre):
# contour_ind_list = np.where(contour_ind >= 1.0)[0].tolist()
# fea_list = []
# label_list = []
# for cc in contour_ind_list:
# # for cc in contour_ind_list[:10]:
# freq_contour_interp = contour_target_ff[cc]['Freq']
# time_contour_interp = contour_target_ff[cc]['Time']
# duration = time_contour_interp[-1] - time_contour_interp[0]
#
# if duration >= duration_thre:
# print('Extracting features of '+str(cc)+':')
# count_long += 1
# sound_file = bin_wav_pair[
# os.path.join(bin_dir_target, species_name_this, ff + '.bin')]
# samples, samplerate = librosa.load(sound_file,
# offset=time_contour_interp[0],
# duration=duration,
# sr=192000, mono=True)
# # cepstrum calculation
# input_size = 4096 # 21.33 msec given sampling rate 192,000 Hz
# fea_contour = calc_cepstrum(samples, samplerate, win_size=input_size, step_size=int(input_size/2), fft_size=input_size, filt_num=64, low_freq=5000., high_freq=23500)
# # calc_cepstrum(samples, sample_rate, win_size, step_size, fft_size,
# # filt_num=64, low_freq=5000., high_freq=23500):
# fea_list.append(fea_contour)
# label_list.append(species_label)
#
# return fea_list, label_list, count_long
#
#
# def fea_energy_label_extract(contour_target_ff, contour_ind, ff,
# species_name_this, species_label, bin_wav_pair,
# bin_dir_target, count_long, duration_thre):
# contour_ind_list = np.where(contour_ind >= 1.0)[0].tolist()
# fea_list = []
# label_list = []
# for cc in contour_ind_list:
# # for cc in contour_ind_list[:10]:
# freq_contour_interp = contour_target_ff[cc]['Freq']
# time_contour_interp = contour_target_ff[cc]['Time']
# duration = time_contour_interp[-1] - time_contour_interp[0]
#
# if duration >= duration_thre:
# print('Extracting features of '+str(cc)+':')
# count_long += 1
# sound_file = bin_wav_pair[
# os.path.join(bin_dir_target, species_name_this, ff + '.bin')]
# samples, samplerate = librosa.load(sound_file,
# offset=time_contour_interp[0],
# duration=duration,
# sr=192000, mono=True)
# # cepstrum calculation
# input_size = 4096 # 21.33 msec given sampling rate 192,000 Hz
# fea_contour = calc_energy(samples, samplerate, win_size=input_size, step_size=int(input_size/2), fft_size=input_size, filt_num=64, low_freq=5000., high_freq=23500)
# # calc_cepstrum(samples, sample_rate, win_size, step_size, fft_size,
# # filt_num=64, low_freq=5000., high_freq=23500):
# fea_list.append(fea_contour)
# label_list.append(species_label)
#
# return fea_list, label_list, count_long
#
#
# def fea_label_generate(contour_target, bin_wav_pair, duration_thre,
# percentile_list, bin_dir_target, species_id, gap=0.0):
# count_long = 0
# count_all = 0
# duration_list = []
# fea_list = []
# label_list = []
# duration_max = 0
# for ff in contour_target.keys():
# print(ff)