forked from QinggangSUN/keras_multiple_target_recognition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_data_shipsear_recognition_mix_s0tos3.py
1152 lines (1013 loc) · 48.3 KB
/
prepare_data_shipsear_recognition_mix_s0tos3.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 9 20:34:30 2018
@author: SUN Qinggang
E-mail: [email protected]
"""
# pylint: disable=no-member
import logging
import os
from error import Error, ParameterError
_SR = 52734
_IS_MONO = True
_FRAME_LENGTH = 10547 # ~200ms 10546.800 000 000 001
_FRAME_SHIFT = 10547
# _FRAME_SHIFT = 2636 # 10547/4 = 2636.7
def get_sr():
"""Return const global variable _SR."""
return _SR
def get_mono():
"""Return const global variable _IS_MONO."""
return _IS_MONO
def get_fl():
"""Return const global variable _FRAME_LENGTH."""
return _FRAME_LENGTH
def get_fs():
"""Return const global variable _FRAME_SHIFT."""
return _FRAME_SHIFT
# output mix features
# _WIN_LENGTH = _FRAME_LENGTH
# _HOP_LENGTH = _FRAME_LENGTH
# _HOP_LENGTH = 2636
_WIN_LENGTH = 1582 # 52734*0.03 = 1582.02
_HOP_LENGTH = 396 # 52734*0.03/4 = 395.505
# _WIN_LENGTH = 1055 # 52734*0.02 = 1054.68
# _HOP_LENGTH = 264 # 52734*0.02/4 = 263.67
# _WIN_LENGTH = 4
# _HOP_LENGTH = 1
def get_win_length():
"""Return const global variable _WIN_LENGTH."""
return _WIN_LENGTH
def get_hop_length():
"""Return const global variable _HOP_LENGTH."""
return _HOP_LENGTH
class PathSourceRoot(object): # pylint: disable=too-many-instance-attributes
"""Path to find sources."""
def __init__(self, path_root, **kwargs):
self._path_root = path_root
for key, value in kwargs.items():
if key == 'path_seg_root':
self._path_seg_root = value
elif key == 'frame_length':
self._frame_length = value
elif key == 'frame_shift':
self._frame_shift = value
elif key == 'path_mix_root':
self._path_mix_root = value
elif key == 'win_length':
self._win_length = value
elif key == 'hop_length':
self._hop_length = value
elif key == 'form_src':
self._form_src = value
elif key == 'sub_set_way':
self.sub_set_way = value
elif key == 'scaler_data':
self._scaler_data = value
elif key == 'path_source':
self._path_source = value
elif key == 'n_mels':
self._n_mels = value
elif key == 'n_mfcc':
self._n_mfcc = value
elif key == 'high':
self._high = value
elif key == 'low':
self._low = value
elif key == 'cutoff':
self._cutoff = value
else:
raise ParameterError('kwargs key invalid')
def get_path_root(self):
"""Get read-only _path_root."""
return self._path_root
def get_path_raw(self):
"""Get path_raw from _path_root."""
return os.path.join(self._path_root, 'raw')
def _set_path_seg_root(self, value):
"""Calculate the 'path_seg_root' property."""
if value:
self._path_seg_root = value
return
if hasattr(self, '_frame_length'):
frame_length = self._frame_length
else:
frame_length = get_fl()
if hasattr(self, '_frame_shift'):
frame_shift = self._frame_shift
else:
frame_shift = get_fs()
self._path_seg_root = os.path.join(self._path_root, str(frame_length)+'_'+str(frame_shift))
def _get_path_seg_root(self):
"""Indirect accessor for 'path_seg_root' property."""
if not hasattr(self, '_path_seg_root'):
self._set_path_seg_root(None)
return self._path_seg_root
def get_path_seg_root(self):
"""Get the 'path_seg_root' property.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> print(PATH_CLASS.path_seg_root)
./10547_10547
"""
return self._get_path_seg_root()
def set_path_seg_root(self, value=None):
"""Set path_seg_root.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> PATH_CLASS.path_seg_root = './seg_root'
>>> print(PATH_CLASS.path_seg_root)
./seg_root
"""
return self._set_path_seg_root(value)
path_seg_root = property(get_path_seg_root, set_path_seg_root)
def get_path_seg(self):
"""Get path_seg from _path_seg_root."""
return os.path.join(self._path_seg_root, 'wavhdf5')
def _set_path_mix_root(self, value):
"""Calculate the 'path_mix_root' property."""
if value:
self._path_mix_root = value
return
self._path_mix_root = os.path.join(self._get_path_seg_root(), 's0tos3', 'mix_1to3')
def _get_path_mix_root(self):
"""Indirect accessor for 'path_mix_root' property."""
if not hasattr(self, '_path_mix_root'):
self._set_path_mix_root(None)
return self._path_mix_root
def get_path_mix_root(self):
"""Get the 'path_mix_root' property.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> print(PATH_CLASS.path_mix_root)
./10547_10547/s0tos3/mix_1to3
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> PATH_CLASS.path_seg_root = './seg_root'
>>> print(PATH_CLASS.path_mix_root)
./seg_root/s0tos3/mix_1to3
"""
return self._get_path_mix_root()
def set_path_mix_root(self, value=None):
"""Set path_mix_root.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> PATH_CLASS.path_mix_root = './mix_root'
>>> print(PATH_CLASS.path_mix_root)
./mix_root
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> PATH_CLASS.path_mix_root = './mix_root'
>>> PATH_CLASS.path_seg_root = './seg_root'
>>> print(PATH_CLASS.path_mix_root)
./mix_root
"""
self._set_path_mix_root(value)
path_mix_root = property(get_path_mix_root, set_path_mix_root)
def _set_path_source_root(self, form_src, **kwargs):
"""Calculate the 'path_source_root' property."""
if form_src:
self._form_src = form_src
if not hasattr(self, '_form_src'):
raise ParameterError(
'Has to create _form_src by __init__ or _set_path_source')
if self._form_src == 'wav':
self._path_source_root = os.path.join(self._get_path_mix_root(
), 'wavmat') # pylint: disable=attribute-defined-outside-init
elif self._form_src in {'magspectrum', 'angspectrum', 'realspectrum', 'imgspectrum'}:
if not hasattr(self, '_win_length'):
self._win_length = get_win_length()
if not hasattr(self, '_hop_length'):
self._hop_length = get_hop_length()
self._path_source_root = os.path.join(
self._get_path_mix_root(), f'{self._form_src}_{self._win_length}_{self._hop_length}') # pylint: disable=attribute-defined-outside-init
elif self._form_src == 'logmelspectrum':
if not hasattr(self, '_win_length'):
self._win_length = get_win_length()
if not hasattr(self, '_hop_length'):
self._hop_length = get_hop_length()
if not hasattr(self, '_n_mels'):
raise ParameterError('need para n_mels')
self._path_source_root = os.path.join(
self._get_path_mix_root(), f'logmelspectrum_{self._win_length}_{self._hop_length}_{self._n_mels}') # pylint: disable=attribute-defined-outside-init
elif self._form_src == 'mfcc':
if not hasattr(self, '_win_length'):
self._win_length = get_win_length()
if not hasattr(self, '_hop_length'):
self._hop_length = get_hop_length()
if not hasattr(self, '_n_mels'):
raise ParameterError('need para n_mels')
if not hasattr(self, '_n_mfcc'):
raise ParameterError('need para n_mfcc')
self._path_source_root = os.path.join(
self._get_path_mix_root(), f'mfcc_{self._win_length}_{self._hop_length}_{self._n_mels}_{self._n_mfcc}')
elif self._form_src == 'demon':
if not hasattr(self, '_high'):
raise ParameterError('need para cutoff')
if not hasattr(self, '_low'):
raise ParameterError('need para cutoff')
if not hasattr(self, '_cutoff'):
raise ParameterError('need para cutoff')
self._path_source_root = os.path.join(
self._get_path_mix_root(), f'demon_{int(self._high)}_{int(self._low)}_{int(self._cutoff)}')
else:
raise ParameterError('form_src invaild')
def _get_path_source_root(self):
"""Indirect accessor for 'path_source_root' property."""
if not hasattr(self, '_path_source_root'):
self._set_path_source_root(None)
return self._path_source_root
def get_path_source_root(self):
"""Get the 'path_source_root' property.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.', form_src='wav')
>>> print(PATH_CLASS.path_source_root)
./10547_10547/s0tos3/mix_1to3/wavmat
example:
>>> PATH_CLASS = PathSourceRoot('.', form_src='wav')
>>> PATH_CLASS.path_mix_root = './mix_root'
>>> print(PATH_CLASS.path_source_root)
./mix_root/wavmat
example:
>>> PATH_CLASS = PathSourceRoot('.', form_src='wav')
>>> PATH_CLASS.path_seg_root = './seg_root'
>>> print(PATH_CLASS.path_source_root)
./seg_root/s0tos3/mix_1to3/wavmat
"""
return self._get_path_source_root()
def set_path_source_root(self, value=None):
"""Set path_source_root.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.', form_src='wav')
>>> PATH_CLASS.path_source_root = './src_root'
>>> print(PATH_CLASS.path_source_root)
ParameterError: form_src invaild
(directly give path_source_root banded)
example:
>>> PATH_CLASS = PathSourceRoot('.', form_src='magspectrum')
>>> PATH_CLASS.path_source_root = 'wav'
>>> print(PATH_CLASS.path_source_root)
./10547_10547/s0tos3/mix_1to3/wavmat
example:
>>> PATH_CLASS = PathSourceRoot('.', form_src='magspectrum')
>>> PATH_CLASS.path_source_root = 'wav'
>>> PATH_CLASS.path_mix_root = './mix_root'
>>> print(PATH_CLASS.path_source_root)
./10547_10547/s0tos3/mix_1to3/wavmat
"""
self._set_path_source_root(value)
path_source_root = property(get_path_source_root, set_path_source_root)
def get_win_length(self):
"""Get _win_length."""
return self._win_length
def get_hop_length(self):
"""Get _hop_length."""
return self._hop_length
def _set_path_source(self, value, form_src, scaler_data, sub_set_way):
"""Calculate the 'path_source' property."""
if value:
self._path_source = value
return
if not hasattr(self, '_path_source_root'):
self._set_path_source_root(form_src)
if scaler_data:
self._scaler_data = scaler_data
if not hasattr(self, '_scaler_data'):
raise ParameterError(
'Has to create _scaler_data by __init__ or _set_path_source')
if sub_set_way:
self.sub_set_way = sub_set_way
if not hasattr(self, 'sub_set_way'):
raise ParameterError(
'Has to create sub_set_way by __init__ or _set_path_source')
if self._scaler_data == 'mm':
self._path_source = os.path.join(self._path_source_root, 'min_max_scaler_'+self.sub_set_way)
elif self._scaler_data == 'or':
self._path_source = os.path.join(self._path_source_root, 'original_'+self.sub_set_way)
else:
raise ParameterError('scaler_data invaild')
def _get_path_source(self):
"""Indirect accessor for 'path_source' property."""
if not hasattr(self, '_path_source'):
self._set_path_source(None, None, None, None)
return self._path_source
def get_path_source(self):
"""Get the 'path_source' property.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot(
'.', form_src='wav', scaler_data='or', sub_set_way='rand')
>>> print(PATH_CLASS.path_source)
./10547_10547/s0tos3/mix_1to3/wavmat/original_rand
example:
>>> PATH_CLASS = PathSourceRoot(
'.', form_src='wav', scaler_data='or', sub_set_way='rand',
frame_length=100, frame_shift=50)
>>> print(PATH_CLASS.path_source)
./100_50/s0tos3/mix_1to3/wavmat/original_rand
"""
return self._get_path_source()
def set_path_source(self, args):
"""Set path_source.
Examples:
example:
>>> PATH_CLASS = PathSourceRoot('.')
>>> PATH_CLASS.path_source = None, 'wav', 'or', 'order'
>>> print(PATH_CLASS.path_source)
./10547_10547/s0tos3/mix_1to3/wavmat/original_order
example:
>>> PATH_CLASS = PathSourceRoot('./root')
>>> PATH_CLASS.path_source = './path_source', None, None, None
>>> print(PATH_CLASS.path_source)
./path_source
example:
>>> PATH_CLASS = PathSourceRoot('./root')
>>> PATH_CLASS.path_mix_root = './mix_root'
>>> PATH_CLASS.path_source = None, 'wav', 'or', 'order'
>>> print(PATH_CLASS.path_source)
./mix_root/wavmat/original_order
example:
>>> PATH_CLASS = PathSourceRoot('./root')
>>> PATH_CLASS.path_source_root = './src_root'
>>> PATH_CLASS.path_source = None, 'wav', 'or', 'order'
>>> print(PATH_CLASS.path_source)
ParameterError: form_src invaild
example:
>>> PATH_CLASS = PathSourceRoot('./root')
>>> PATH_CLASS.path_source_root = 'wav'
>>> PATH_CLASS.path_source = None, 'magspectrum', 'or', 'order'
>>> print(PATH_CLASS.path_source)
./root/10547_10547/s0tos3/mix_1to3/wavmat/original_order
"""
try:
value, form_src, scaler_data, sub_set_way = args
except ValueError:
raise ParameterError("Pass an iterable with four items")
else:
self._set_path_source(value, form_src, scaler_data, sub_set_way)
path_source = property(get_path_source, set_path_source)
def get_scaler_data(self):
"""Get read-only _scaler_data."""
return self._scaler_data
def get_form_src(self):
"""Get read-only _form_src."""
return self._form_src
def read_source(path, file_names, form_src='hdf5', data_type=None):
"""Read data file_names [file_names] from [path]."""
import json
import os
import logging
import numpy as np
import h5py
import scipy.io as sio
logging.warning("DeprecationWarning: The 'read_source' function is deprecated, use 'read_datas' instead")
def str_remove_end(file_names, file_type):
"""Remove files' name extension.
Args:
file_names (list[str]): list of file names.
file_type (str): file name extension.
Returns:
file_names_small (list[str]): list of file names without extension.
"""
logging_debug = False
file_names_small = []
for name_i in file_names:
if name_i.endswith('.'+file_type):
logging_debug = True
file_names_small.append(name_i[:-len('.'+file_type)])
else:
file_names_small.append(name_i)
if logging_debug:
logging.debug('Remove file names extension.')
return file_names_small
if form_src == 'hdf5':
if data_type:
logging.warning('ignore data_type')
file_names = str_remove_end(file_names, form_src)
source_frames = [
h5py.File(os.path.join(path, name_i.rstrip()+'.'+form_src), 'r')['data'] for name_i in file_names]
elif form_src == 'bin':
if data_type is None:
data_type = np.float32
source_frames = [np.fromfile(
os.path.join(path, name_i.rstrip()+'.'+form_src), dtype=data_type).reshape(
np.load(os.path.join(path, name_i.rstrip()+'_shape.npy'))) for name_i in file_names]
elif form_src == 'mat':
if data_type:
logging.warning('ignore data_type')
file_names = str_remove_end(file_names, form_src)
source_frames = [sio.loadmat(
os.path.join(path, name_i.rstrip()+'.'+form_src))['data'] for name_i in file_names]
elif form_src == 'json':
if data_type:
logging.warning('ignore data_type')
source_frames = [json.load(
open(os.path.join(path, name_i.rstrip()+'.'+form_src), 'r')) for name_i in file_names]
else:
raise ParameterError('Invalid form_src keyword.')
return source_frames # return 3D np.array [source][num][feature]
def read_data(path, file_name, form_src='hdf5', dict_key='data', data_type=None, **kwargs):
"""Read data file_name from path.
Args:
path (str): path where to read data.
file_name (str): file name.
form_src (str, optional): file name extension. Defaults to 'hdf5'.
dict_key (str, optional): load data dict keyword. Defaults to 'data'.
data_type ([type], optional): data type after load. Defaults to None.
Raises:
ParameterError: kwargs['mode'], Invalid read_data mode.
ParameterError: form_src, Invalid form_src keyword.
Returns:
data ([type]): data load.
"""
import json
import os
import numpy as np
import h5py
import scipy.io as sio
import tables
def str_remove_end(file_name, file_type):
"""Remove files' name extension.
Args:
file_names (list[str]): list of file names.
file_type (str): file name extension.
Returns:
file_names_small (list[str]): list of file names without extension.
"""
if file_name.endswith('.'+file_type):
logging.debug('Remove file names extension.')
file_name_small = file_name[:-len('.'+file_type)]
else:
file_name_small = file_name
return file_name_small
if form_src == 'hdf5':
if data_type:
logging.warning('ignore data_type')
file_name = str_remove_end(file_name, form_src)
if 'mode' not in kwargs.keys():
data = h5py.File(os.path.join(path, file_name.rstrip()+'.'+form_src), 'r')[dict_key]
else:
if kwargs['mode'] == 'pytables':
data = tables.open_file(os.path.join(path, file_name.rstrip()+'.'+form_src))[dict_key]
else:
raise ParameterError('Invalid read_data mode.')
elif form_src == 'bin':
if data_type is None:
data_type = np.float32
data = np.fromfile(
os.path.join(path, file_name.rstrip()+'.'+form_src), dtype=data_type).reshape(
np.load(os.path.join(path, file_name.rstrip()+'_shape.npy')))
elif form_src == 'mat':
if data_type:
logging.warning('ignore data_type')
file_name = str_remove_end(file_name, form_src)
data = sio.loadmat(os.path.join(path, file_name.rstrip()+'.'+form_src))[dict_key]
elif form_src == 'json':
if data_type:
logging.warning('ignore data_type')
data = json.load(open(os.path.join(path, file_name.rstrip()+'.'+form_src), 'r'))
else:
raise ParameterError('Invalid form_src keyword.')
return data
def read_datas(path, file_names, form_src='hdf5', dict_key='data', data_type=None, **kwargs):
"""Read data file_names [file_names] from [path].
Args:
path (str): path where to read data.
file_names (list[str]): list file names to read.
form_src (str, optional): file name extension. Defaults to 'hdf5'.
dict_key (str, optional): load data dict keyword. Defaults to 'data'.
data_type ([type], optional): data type after load. Defaults to None.
Returns:
datas (list[type]): datas load.
"""
datas = [read_data(path, file_name, form_src, dict_key, data_type, **kwargs) for file_name in file_names]
return datas
def data_save_reshape(data):
"""Reshape data to last dim is not 1.
Args:
data (np.ndarray): data to reshape.
Examples:
data: np.ndarray, shape==(n, 1, fl, 1, 1)
index_not_one = [0, 2]
last_one = 2 != 4
data = data.transpose((0, 1) + (3, 4) + (2, ))
data.shape = (n, 1, 1, 1, fl)
"""
index_not_one = [index for (index, value) in enumerate(data.shape) if value != 1]
if index_not_one:
last_one = index_not_one[-1]
if last_one != data.ndim-1:
data = data.transpose(
tuple(range(last_one))+tuple(range(last_one+1, data.ndim))+(last_one, ))
return data
def save_datas(set_dict, path_save, **kwargs):
"""Save data dict set_dict[key] to [path_save] using file name [key].
Args:
set_dict (dict{str:np.ndarray}): dictionary of datas to be saved, key is the file name.
path_save (str): path to save files.
Raises:
ParameterError: Invalid kwargs keyword.
ParameterError: Invalid mode_batch keyword.
ParameterError: Invalid form_save keyword.
"""
import json
import os
import pickle
import numpy as np
import h5py
import scipy.io as sio
import tables
form_save = 'hdf5'
dtype = np.dtype('float32')
mode_batch = 'normal'
save_key = 'data'
save_key2 = 'sij'
for key, value in kwargs.items():
if key == 'form_save':
form_save = value
elif key == 'dtype':
dtype = value
elif key == 'mode_batch':
mode_batch = value
elif key == 'file_name':
file_name = value
elif key == 'save_key':
save_key = value
elif key == 'save_key2':
save_key = value
else:
raise ParameterError('Invalid kwargs keyword.')
if form_save == 'hdf5':
if mode_batch == 'normal':
for name_i, data_i in set_dict.items():
with h5py.File(os.path.join(path_save, name_i+'.hdf5'), 'w') as f_w:
f_w.create_dataset(
save_key, data=data_i, dtype=dtype,
chunks=((data_i.ndim-1)*(1,)+data_i.shape[-1:]),
compression="gzip", compression_opts=9)
elif mode_batch == 'batch':
for name_i, data_i in set_dict.items():
file_name_i = os.path.join(path_save, name_i+'.hdf5')
filters = tables.Filters(complevel=9, complib='blosc')
with tables.open_file(file_name_i, 'a') as f_w:
if save_key not in f_w.root:
data_earray = f_w.create_earray(f_w.root, save_key,
tables.Atom.from_dtype(dtype),
((0,)+data_i.shape[1:]),
chunkshape=(data_i.ndim-1)*(1,)+data_i.shape[-1:],
filters=filters)
else:
data_earray = getattr(f_w.root, save_key)
data_earray.append(data_i)
elif mode_batch == 'batch_h5py':
for name_i, data_i in set_dict.items():
file_name_i = os.path.join(path_save, name_i+'.hdf5')
if not os.path.isfile(file_name_i):
with h5py.File(file_name_i, 'w') as f_w:
f_w.create_dataset(
save_key, data=data_i, dtype=dtype,
chunks=(data_i.ndim-1)*(1,)+data_i.shape[-1:],
maxshape=((None,)+data_i.shape[1:]),
compression="gzip", compression_opts=9)
else:
with h5py.File(file_name_i, 'a') as f_a:
f_a[save_key].resize((f_a[save_key].shape[0]+data_i.shape[0]), axis=0)
f_a[save_key][-data_i.shape[0]:] = data_i
elif mode_batch == 'one_file_no_chunk':
full_file_name = os.path.join(path_save, file_name+'.hdf5')
with h5py.File(full_file_name, 'a') as f_a:
for name_i, data_i in set_dict.items():
f_a.create_dataset(name_i, data=data_i, dtype=dtype)
else:
raise ParameterError('Invalid mode_batch keyword.')
elif form_save == 'mat':
for name_i, data_i in set_dict.items():
sio.savemat(os.path.join(path_save, name_i+'.mat'), {save_key2: data_i})
elif form_save == 'npy':
for name_i, data_i in set_dict.items():
np.save(os.path.join(path_save, name_i+'.npy'), {save_key2: data_i})
elif form_save == 'picke':
for name_i, data_i in set_dict.items():
with open(os.path.join(path_save, name_i+'.pickle'), 'wb') as f_wb:
pickle.dump({save_key2: data_i}, f_wb)
elif form_save == 'bin':
for name_i, data_i in set_dict.items():
data_i.tofile(os.path.join(path_save, name_i+'.bin'))
np.save(os.path.join(path_save, name_i+'_shape.npy'), data_i.shape)
with open(os.path.join(path_save, name_i+'_shape.json'), 'w', encoding='utf-8') as f_w:
json.dump({save_key: data_i.shape}, f_w)
elif form_save == 'json':
with open(os.path.join(path_save, file_name+'.json'), 'w', encoding='utf-8') as f_w:
json.dump(set_dict, f_w)
else:
raise ParameterError('Invalid form_save keyword.')
def save_process_batch(data, func, path_save, file_name, batch_num=200, save_key='data', mode_batch='batch',
*args, **kwargs):
"""Process data by batch through func, save to path_save.
Args:
data (np.ndarray,shape==(nsam, - - )): data to save
func (function): function to process data
path_save (str): where to save data
file_name (str): name of the saved file
batch_num (int, optional): each batch process batch_num data
save_key (str, optional): data save keyword. Defaults to 'data'.
mode_batch (str, optional): use pytables(default) or h5py to save data. Defaults to 'batch'.
"""
import h5py
import numpy as np
import tables
dtype = None
for key, value in kwargs.items():
if key == 'dtype':
dtype = value
if dtype is None:
dtype = np.dtype('float32')
for j in range(0, data.shape[0], batch_num):
if j+batch_num > data.shape[0]:
data_j = data[j:]
else:
data_j = data[j:j+batch_num]
data_result = func(data_j, *args, **kwargs)
if mode_batch == 'batch':
with tables.open_file(os.path.join(path_save, file_name+'.hdf5'), 'a') as f_w:
if save_key not in f_w.root:
data_earray = f_w.create_earray(f_w.root, save_key,
tables.Atom.from_dtype(dtype),
((0,)+data_result.shape[1:]),
chunkshape=(data_result.ndim-1)*(1,)+data_result.shape[-1:],
filters=tables.Filters(complevel=9, complib='blosc'))
else:
data_earray = getattr(f_w.root, save_key)
data_earray.append(data_result)
elif mode_batch == 'batch_h5py':
if j == 0:
with h5py.File(os.path.join(path_save, file_name+'.hdf5'), 'w') as f:
f.create_dataset(
save_key, data=data_result,
dtype=dtype,
chunks=((data_result.ndim-1)*(1,)+data_result.shape[-1:]),
maxshape=((None,)+data_result.shape[1:]),
compression="gzip", compression_opts=9)
else:
with h5py.File(os.path.join(path_save, file_name+'.hdf5'), 'a') as f:
f[save_key].resize(
(f[save_key].shape[0] + data_result.shape[0]), axis=0)
f[save_key][-data_result.shape[0]:] = data_result
else:
raise ParameterError('Invalid mode_batch keyword.')
def data_seg_create(path_class):
"""Create and save seg wavmats from raw data .wav files,
you may run this function only onece.
Args:
path_class (object class PathSourceRoot): object of class to compute path.
"""
import json
import numpy as np
from feature_extract import feature_extract
from file_operation import mkdir, read_wavs_to_np, walk_dirs_start_str
# raw data files in dirs e.g. /s0, /s1 /s2 /s3
path_seg_root = path_class.path_seg_root
mkdir(path_seg_root)
path_raw = path_class.get_path_raw()
dir_names = walk_dirs_start_str(path_raw, 's')
n_src = len(dir_names)
source_frames = []
for dir_i in dir_names:
sources_wavi_np = read_wavs_to_np(dir_i, get_sr(), get_mono())
# original wav sampling points
sourceframesi_np = feature_extract(
'sample_np', **{
'sources': sources_wavi_np,
'fl': get_fl(),
'fs': get_fs()}) # 1d list 2darray to 1d list 2darray
# 1d list 2darray to 2darray (n_samples, fl)
source_frames.append(np.vstack(np.asarray(sourceframesi_np)))
logging.debug('source_frames.shape')
for sf_i in source_frames:
logging.debug(sf_i.shape)
dir_names_save = []
for i in range(0, n_src, 1):
dir_names_save.append('s_'+str(i))
with open(os.path.join(path_seg_root, 'dirname.json'), 'w', encoding='utf-8') as f_w:
json.dump({'dirname': dir_names_save}, f_w)
path_seg = path_class.get_path_seg()
mkdir(path_seg)
save_datas(dict(zip(dir_names_save, source_frames)), path_seg, dtype='float64')
def data_mixwav_create(path_class): # pylint: disable=too-many-locals
"""Create and save mixed sources original sampling point wavmat,
you may run this function only onece.
Args:
path_class (object class PathSourceRoot): object of class to compute path.
"""
import json
from itertools import combinations
from file_operation import mkdir, mycopyfile
from prepare_data import balancesets, mixaddframes_np
path_seg_root = path_class.path_seg_root
path_seg = path_class.get_path_seg()
# path_mix_root = path_class.path_mix_root
# mkdir(path_mix_root)
path_source_root = path_class.path_source_root
mkdir(path_source_root)
# read sources
mycopyfile(os.path.join(path_seg_root, 'dirname.json'),
os.path.join(path_source_root, 'dirname.json'))
dir_names = json.load(open(os.path.join(path_source_root, 'dirname.json'), 'r'))['dirname']
source_frames = read_source(path_seg, dir_names)
# balance sources
source_frames = balancesets(source_frames)
n_src = len(source_frames)
path_source_out = os.path.join(path_source_root, 's_hdf5')
mkdir(path_source_out)
dir_names = []
data_list = []
for i in range(1, n_src+1, 1): # mix 1 to 3 without 0
index_ci = [] # e.g.[0,1,2,3] [(1,2)(1,3)(2,3)]
if i == 1:
index_ci = list(combinations(range(n_src), i))
else:
index_ci = list(combinations(range(1, n_src), i))
for index_ci_j in index_ci: # e.g. (1,2)
items = ['s']
for k in index_ci_j:
items.append('_')
items.append(str(k))
pathout_j = ''.join(items)
dir_names.append(pathout_j)
mix_cij_arr = mixaddframes_np(
[source_frames[k] for k in index_ci_j])
mix_cij_arr = mix_cij_arr.reshape(
mix_cij_arr.shape[0:1]+(1,)+mix_cij_arr.shape[-1:])
data_list.append(mix_cij_arr)
with open(os.path.join(path_source_root, 'dirname.json'), 'w', encoding='utf-8') as f_w:
json.dump({'dirname': dir_names}, f_w)
save_datas(dict(zip(dir_names, data_list)), path_source_out)
def data_feature_create(path_class_in, path_class_out, batch_save=0,
form_src='magspectrum', **kwargs): # pylint: disable=too-many-locals
"""Create and save feature sources_frames.
Args:
path_class_in (object class PathSourceRoot): object of class to compute path.
path_class_out (object class PathSourceRoot): object of class to compute path.
batch_save (int, optional): each batch save batch_save samples. Defaults to 0 means save all samples.
form_src (str, optional): feature type to compute and save. Defaults to 'magspectrum'.
Raises:
ParameterError: [description]
Returns:
[type]: [description]
"""
from feature_extract import feature_extract
from file_operation import mkdir, mycopyfile
import json
path_source_in_root = path_class_in.path_source_root
path_source_in = os.path.join(path_source_in_root, 's_hdf5')
path_source_out_root = path_class_out.path_source_root
mkdir(path_source_out_root)
path_source_out = os.path.join(path_source_out_root, 's_hdf5')
mkdir(path_source_out)
mycopyfile(os.path.join(path_source_in_root, 'dirname.json'),
os.path.join(path_source_out_root, 'dirname.json'))
dir_names = json.load(
open(os.path.join(path_source_out_root, 'dirname.json'), 'r'))['dirname']
if 'mode_read' in kwargs.keys():
sources_wavmat = read_datas(path_source_in, dir_names, **{'mode': kwargs['mode_read']})
else:
sources_wavmat = read_datas(path_source_in, dir_names)
def _spectrum_create(sources_wavmat, feature, win_length, hop_length, fix_length=False, window='hamming'):
"""Abstract method for create spectrum feature sources_frames."""
import numpy as np
source_frames = []
for source_i in sources_wavmat:
source_frames.append(feature_extract(
feature, **{
'source': source_i.reshape(-1, ),
'window': window,
'win_length': win_length, 'hop_length': hop_length,
'n_fft': win_length, 'center': False,
'dtype': np.complex64, 'fix_length': fix_length})) # 2D to 3D
return np.asarray(source_frames, dtype=np.float32)
def magspectrum_create(sources_wavmat, win_length, hop_length, fix_length=False, window='hamming'):
"""Create magnitude (amplitude) spectrum feature sources_frames."""
return _spectrum_create(sources_wavmat, 'magspectrum', win_length, hop_length, fix_length, window)
def angspectrum_create(sources_wavmat, win_length, hop_length, fix_length=False, window='hamming'):
"""Create angle (phase) spectrum feature sources_frames."""
return _spectrum_create(sources_wavmat, 'angspectrum', win_length, hop_length, fix_length, window)
def realspectrum_create(sources_wavmat, win_length, hop_length, fix_length=False, window='hamming'):
"""Create real part of spectrum feature sources_frames."""
return _spectrum_create(sources_wavmat, 'realspectrum', win_length, hop_length, fix_length, window)
def imgspectrum_create(sources_wavmat, win_length, hop_length, fix_length=False, window='hamming'):
"""Create image part of spectrum feature sources_frames."""
return _spectrum_create(sources_wavmat, 'imgspectrum', win_length, hop_length, fix_length, window)
def logmelspectrum_create(sources, sr, n_mels, win_length=None, hop_length=None, window=None, mode=0):
"""Create Log-Mel Spectrogram feature sources_frames."""
import numpy as np
source_frames = []
if mode == 0: # input wavmat
for source_i in sources:
source_frames.append(feature_extract(
'logmelspectrum', **{
'source': source_i.reshape(-1, ),
'sr': sr, 'n_mels': n_mels, 'window': window,
'win_length': win_length, 'hop_length': hop_length,
'n_fft': win_length, 'center': False, 'dtype': np.float32})) # 2D to 3D
elif mode == 1: # input stft spectrum
for source_i in sources:
source_frames.append(feature_extract(
'logmelspectrum', **{
'S': source_i.transpose()**2,
'sr': sr, 'n_mels': n_mels})) # 2D to 3D
return np.asarray(source_frames, dtype=np.float32)
def mfcc_create(sources, sr, n_mfcc,
win_length=None, hop_length=None, window=None, n_mels=None, mode=0):
"""Create Log-Mel Spectrogram feature sources_frames."""
import librosa
import numpy as np
source_frames = []
if mode == 0:
for source_i in sources:
source_frames.append(feature_extract(
'mfcc', **{
'source': source_i.reshape(-1, ), 'sr': sr, 'n_mfcc': n_mfcc,
'n_fft': win_length, 'hop_length': hop_length, 'win_length': win_length,
'window': window, 'center': False,
'n_mels': n_mels, 'dtype': np.float32
})) # 2D to 3D
elif mode == 1: # input log-power Mel spectrogram
for source_i in sources:
source_frames.append(feature_extract(
'mfcc', **{
'source': None, 'S': librosa.power_to_db(source_i.transpose()),
'sr': sr, 'n_mfcc': n_mfcc
})) # 2D to 3D
return np.asarray(source_frames, dtype=np.float32)
def demon_create(sources, high=30000, low=20000, cutoff=1000.0, fs=200000, mode='square_law'):
"""Create Log-Mel Spectrogram feature sources_frames."""
import numpy as np
source_frames = []
for source_i in sources:
source_frames.append(feature_extract(
'demon', **{
'source': source_i, 'high': high, 'low': low, 'cutoff': cutoff, 'fs': fs, 'mode': mode
})) # 2D to 3D
return np.asarray(source_frames, dtype=np.float32)
def feature_create(sources, path_class_out, form_src, **kwargs):
if form_src == 'magspectrum':
feature = magspectrum_create(
sources,
path_class_out.get_win_length(),
path_class_out.get_hop_length(), kwargs['fix_length'], kwargs['window'])
elif form_src == 'angspectrum':
feature = angspectrum_create(
sources,
path_class_out.get_win_length(),
path_class_out.get_hop_length(), kwargs['fix_length'], kwargs['window'])
elif form_src == 'realspectrum':
feature = realspectrum_create(
sources,
path_class_out.get_win_length(),
path_class_out.get_hop_length(), kwargs['fix_length'], kwargs['window'])
elif form_src == 'imgspectrum':
feature = imgspectrum_create(
sources,
path_class_out.get_win_length(),
path_class_out.get_hop_length(), kwargs['fix_length'], kwargs['window'])