-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEqT_utils_S.py
1193 lines (909 loc) · 42.1 KB
/
EqT_utils_S.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import numpy as np
import h5py
import matplotlib
matplotlib.use('agg')
from tqdm import tqdm
import tensorflow
import os
os.environ['KERAS_BACKEND']='tensorflow'
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import add, Activation, LSTM, Conv1D, InputSpec
from tensorflow.keras.layers import MaxPooling1D, UpSampling1D, Cropping1D, SpatialDropout1D, Bidirectional, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from obspy.signal.trigger import trigger_onset
import matplotlib
from numpy import NAN
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
class DataGenerator(tensorflow.keras.utils.Sequence):
"""
Keras generator with preprocessing
Parameters
----------
list_IDsx: str
List of trace names.
file_name: str
Name of hdf5 file containing waveforms data.
dim: tuple
Dimension of input traces.
batch_size: int, default=32
Batch size.
n_channels: int, default=3
Number of channels.
phase_window: int, fixed=40
The number of samples (window) around each phaset.
shuffle: bool, default=True
Shuffeling the list.
norm_mode: str, default=max
The mode of normalization, 'max' or 'std'.
label_type: str, default=gaussian
Labeling type: 'gaussian', 'triangle', or 'box'.
augmentation: bool, default=True
If True, half of each batch will be augmented version of the other half.
add_event_r: {float, None}, default=None
Chance for randomly adding a second event into the waveform.
add_gap_r: {float, None}, default=None
Add an interval with zeros into the waveform representing filled gaps.
coda_ratio: {float, 0.4}, default=0.4
% of S-P time to extend event/coda envelope past S pick.
shift_event_r: {float, None}, default=0.9
Rate of augmentation for randomly shifting the event within a trace.
add_noise_r: {float, None}, default=None
Chance for randomly adding Gaussian noise into the waveform.
drop_channe_r: {float, None}, default=None
Chance for randomly dropping some of the channels.
scale_amplitude_r: {float, None}, default=None
Chance for randomly amplifying the waveform amplitude.
pre_emphasis: bool, default=False
If True, waveforms will be pre emphasized.
Returns
--------
Batches of two dictionaries: {'input': X}: pre-processed waveform as input {'picker_S': y3}: outputs including three separate numpy arrays as labels for S.
"""
def __init__(self,
list_IDs,
file_name,
dim,
batch_size=32,
n_channels=3,
phase_window= 40,
shuffle=True,
norm_mode = 'max',
label_type = 'gaussian',
augmentation = False,
add_event_r = None,
add_gap_r = None,
coda_ratio = 0.4,
shift_event_r = None,
add_noise_r = None,
drop_channe_r = None,
scale_amplitude_r = None,
pre_emphasis = True,
**kwargs):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.phase_window = phase_window
self.list_IDs = list_IDs
self.file_name = file_name
self.n_channels = n_channels
self.shuffle = shuffle
self.on_epoch_end()
self.norm_mode = norm_mode
self.label_type = label_type
self.augmentation = augmentation
self.add_event_r = add_event_r
self.add_gap_r = add_gap_r
self.coda_ratio = coda_ratio
self.shift_event_r = shift_event_r
self.add_noise_r = add_noise_r
self.drop_channe_r = drop_channe_r
self.scale_amplitude_r = scale_amplitude_r
self.pre_emphasis = pre_emphasis
def __len__(self):
'Denotes the number of batches per epoch'
if self.augmentation:
return 2*int(np.floor(len(self.list_IDs) / self.batch_size))
else:
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
if self.augmentation:
indexes = self.indexes[index*self.batch_size//2:(index+1)*self.batch_size//2]
indexes = np.append(indexes, indexes)
else:
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_temp = [self.list_IDs[k] for k in indexes]
X, y3 = self.__data_generation(list_IDs_temp)
return ({'input': X}, {'picker_S': y3})
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def _normalize(self, data, mode = 'max'):
'Normalize waveforms in each batch'
data -= np.mean(data, axis=0, keepdims=True)
if mode == 'max':
max_data = np.max(data, axis=0, keepdims=True)
assert(max_data.shape[-1] == data.shape[-1])
max_data[max_data == 0] = 1
data /= max_data
elif mode == 'std':
std_data = np.std(data, axis=0, keepdims=True)
assert(std_data.shape[-1] == data.shape[-1])
std_data[std_data == 0] = 1
data /= std_data
return data
def _scale_amplitude(self, data, rate):
'Scale amplitude or waveforms'
tmp = np.random.uniform(0, 1)
if tmp < rate:
data *= np.random.uniform(1, 3)
elif tmp < 2*rate:
data /= np.random.uniform(1, 3)
return data
def _drop_channel(self, data, snr, rate):
'Randomly replace values of one or two components to zeros in earthquake data'
data = np.copy(data)
if np.random.uniform(0, 1) < rate and all(snr >= 10.0):
c1 = np.random.choice([0, 1])
c2 = np.random.choice([0, 1])
c3 = np.random.choice([0, 1])
if c1 + c2 + c3 > 0:
data[..., np.array([c1, c2, c3]) == 0] = 0
return data
def _drop_channel_noise(self, data, rate):
'Randomly replace values of one or two components to zeros in noise data'
data = np.copy(data)
if np.random.uniform(0, 1) < rate:
c1 = np.random.choice([0, 1])
c2 = np.random.choice([0, 1])
c3 = np.random.choice([0, 1])
if c1 + c2 + c3 > 0:
data[..., np.array([c1, c2, c3]) == 0] = 0
return data
def _add_gaps(self, data, rate):
'Randomly add gaps (zeros) of different sizes into waveforms'
data = np.copy(data)
gap_start = np.random.randint(0, 4000)
gap_end = np.random.randint(gap_start, 5500)
if np.random.uniform(0, 1) < rate:
data[gap_start:gap_end,:] = 0
return data
def _add_noise(self, data, snr, rate):
'Randomly add Gaussian noie with a random SNR into waveforms'
data_noisy = np.empty((data.shape))
if np.random.uniform(0, 1) < rate and all(snr >= 10.0):
data_noisy = np.empty((data.shape))
data_noisy[:, 0] = data[:,0] + np.random.normal(0, np.random.uniform(0.01, 0.15)*max(data[:,0]), data.shape[0])
data_noisy[:, 1] = data[:,1] + np.random.normal(0, np.random.uniform(0.01, 0.15)*max(data[:,1]), data.shape[0])
data_noisy[:, 2] = data[:,2] + np.random.normal(0, np.random.uniform(0.01, 0.15)*max(data[:,2]), data.shape[0])
else:
data_noisy = data
return data_noisy
def _adjust_amplitude_for_multichannels(self, data):
'Adjust the amplitude of multichaneel data'
tmp = np.max(np.abs(data), axis=0, keepdims=True)
assert(tmp.shape[-1] == data.shape[-1])
if np.count_nonzero(tmp) > 0:
data *= data.shape[-1] / np.count_nonzero(tmp)
return data
def _label(self, a=0, b=20, c=40):
'Used for triangolar labeling'
z = np.linspace(a, c, num = 2*(b-a)+1)
y = np.zeros(z.shape)
y[z <= a] = 0
y[z >= c] = 0
first_half = np.logical_and(a < z, z <= b)
y[first_half] = (z[first_half]-a) / (b-a)
second_half = np.logical_and(b < z, z < c)
y[second_half] = (c-z[second_half]) / (c-b)
return y
def _add_event(self, data, addp, adds, coda_end, snr, rate):
'Add a scaled version of the event into the empty part of the trace'
added = np.copy(data)
additions = None
spt_secondEV = None
sst_secondEV = None
if addp and adds:
s_p = adds - addp
if np.random.uniform(0, 1) < rate and all(snr>=10.0) and (data.shape[0]-s_p-21-coda_end) > 20:
secondEV_strt = np.random.randint(coda_end, data.shape[0]-s_p-21)
scaleAM = 1/np.random.randint(1, 10)
space = data.shape[0]-secondEV_strt
added[secondEV_strt:secondEV_strt+space, 0] += data[addp:addp+space, 0]*scaleAM
added[secondEV_strt:secondEV_strt+space, 1] += data[addp:addp+space, 1]*scaleAM
added[secondEV_strt:secondEV_strt+space, 2] += data[addp:addp+space, 2]*scaleAM
spt_secondEV = secondEV_strt
if spt_secondEV + s_p + 21 <= data.shape[0]:
sst_secondEV = spt_secondEV + s_p
if spt_secondEV and sst_secondEV:
additions = [spt_secondEV, sst_secondEV]
data = added
return data, additions
def _shift_event(self, data, addp, adds, coda_end, snr, rate):
'Randomly rotate the array to shift the event location'
org_len = len(data)
data2 = np.copy(data)
addp2 = adds2 = coda_end2 = None;
if np.random.uniform(0, 1) < rate:
nrotate = int(np.random.uniform(1, int(org_len - coda_end)))
data2[:, 0] = list(data[:, 0])[-nrotate:] + list(data[:, 0])[:-nrotate]
data2[:, 1] = list(data[:, 1])[-nrotate:] + list(data[:, 1])[:-nrotate]
data2[:, 2] = list(data[:, 2])[-nrotate:] + list(data[:, 2])[:-nrotate]
if addp+nrotate >= 0 and addp+nrotate < org_len:
addp2 = addp+nrotate;
else:
addp2 = None;
if adds+nrotate >= 0 and adds+nrotate < org_len:
adds2 = adds+nrotate;
else:
adds2 = None;
if coda_end+nrotate < org_len:
coda_end2 = coda_end+nrotate
else:
coda_end2 = org_len
if addp2 and adds2:
data = data2;
addp = addp2;
adds = adds2;
coda_end= coda_end2;
return data, addp, adds, coda_end
def _pre_emphasis(self, data, pre_emphasis=0.97):
'apply the pre_emphasis'
for ch in range(self.n_channels):
bpf = data[:, ch]
data[:, ch] = np.append(bpf[0], bpf[1:] - pre_emphasis * bpf[:-1])
return data
def __data_generation(self, list_IDs_temp):
'read the waveforms'
X = np.zeros((self.batch_size, self.dim, self.n_channels))
y3 = np.zeros((self.batch_size, self.dim, 1))
fl = h5py.File(self.file_name, 'r')
# Generate data
for i, ID in enumerate(list_IDs_temp):
additions = None
dataset = fl.get(str(ID))
#print(ID)
if ID.split('_')[-1] == 'EV':
data = np.array(dataset['data'])
spt = int(dataset.attrs['p_arrival_sample']);
sst = int(dataset.attrs['s_arrival_sample']);
coda_end = int(dataset.attrs['coda_end_sample']);
snr = dataset.attrs['snr_db'];
elif ID.split('_')[-1] == 'NO':
data = np.array(dataset['data'])
## augmentation
if self.augmentation == True:
if i <= self.batch_size//2:
if self.shift_event_r and dataset.attrs['trace_category'] == 'earthquake_local':
data, spt, sst, coda_end = self._shift_event(data, spt, sst, coda_end, snr, self.shift_event_r/2);
if self.norm_mode:
data = self._normalize(data, self.norm_mode)
else:
if dataset.attrs['trace_category'] == 'earthquake_local':
if self.shift_event_r:
data, spt, sst, coda_end = self._shift_event(data, spt, sst, coda_end, snr, self.shift_event_r);
if self.add_event_r:
data, additions = self._add_event(data, spt, sst, coda_end, snr, self.add_event_r);
if self.add_noise_r:
data = self._add_noise(data, snr, self.add_noise_r);
if self.drop_channe_r:
data = self._drop_channel(data, snr, self.drop_channe_r);
data = self._adjust_amplitude_for_multichannels(data)
if self.scale_amplitude_r:
data = self._scale_amplitude(data, self.scale_amplitude_r);
if self.pre_emphasis:
data = self._pre_emphasis(data)
if self.norm_mode:
data = self._normalize(data, self.norm_mode)
elif dataset.attrs['trace_category'] == 'noise':
if self.drop_channe_r:
data = self._drop_channel_noise(data, self.drop_channe_r);
if self.add_gap_r:
data = self._add_gaps(data, self.add_gap_r)
if self.norm_mode:
data = self._normalize(data, self.norm_mode)
elif self.augmentation == False:
if self.shift_event_r and dataset.attrs['trace_category'] == 'earthquake_local':
data, spt, sst, coda_end = self._shift_event(data, spt, sst, coda_end, snr, self.shift_event_r/2);
if self.norm_mode:
data = self._normalize(data, self.norm_mode)
X[i, :, :] = data
## labeling
if self.label_type == 'triangle' and dataset.attrs['trace_category'] == 'earthquake_local':
sd = None
if spt and sst:
sd = sst - spt
if sst and (sst-20 >= 0) and (sst+21 < self.dim):
y3[i, sst-20:sst+21, 0] = self._label()
elif sst and (sst+21 < self.dim):
y3[i, 0:sst+sst+1, 0] = self._label(a=0, b=sst, c=2*sst)
elif sst and (sst-20 >= 0):
sdif = self.dim - sst
y3[i, sst-sdif-1:self.dim, 0] = self._label(a=sst-sdif, b=sst, c=2*sdif)
if additions:
add_spt = additions[0];
add_sst = additions[1];
add_sd = None
if add_spt and add_sst:
add_sd = add_sst - add_spt
if add_sst and (add_sst-20 >= 0) and (add_sst+21 < self.dim):
y3[i, add_sst-20:add_sst+21, 0] = self._label()
elif add_sst and (add_sst+21 < self.dim):
y3[i, 0:add_sst+add_sst+1, 0] = self._label(a=0, b=add_sst, c=2*add_sst)
elif add_sst and (add_sst-20 >= 0):
sdif = self.dim - add_sst
y3[i, add_sst-sdif-1:self.dim, 0] = self._label(a=add_sst-sdif, b=add_sst, c=2*sdif)
fl.close()
return X, y3.astype('float32')
class DataGeneratorTest(keras.utils.Sequence):
"""
Keras generator with preprocessing. For testing.
Parameters
----------
list_IDsx: str
List of trace names.
file_name: str
Path to the input hdf5 file.
dim: tuple
Dimension of input traces.
batch_size: int, default=32
Batch size.
n_channels: int, default=3
Number of channels.
norm_mode: str, default=max
The mode of normalization, 'max' or 'std'.
Returns
--------
Batches of two dictionaries: {'input': X}: pre-processed waveform as input {'picker_S': y3}: outputs including three separate numpy arrays as labels for S.
"""
def __init__(self,
list_IDs,
file_name,
dim,
batch_size=32,
n_channels=3,
norm_mode = 'max'):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.list_IDs = list_IDs
self.file_name = file_name
self.n_channels = n_channels
self.on_epoch_end()
self.norm_mode = norm_mode
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_temp = [self.list_IDs[k] for k in indexes]
X = self.__data_generation(list_IDs_temp)
return ({'input': X})
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
def normalize(self, data, mode = 'max'):
'Normalize waveforms in each batch'
data -= np.mean(data, axis=0, keepdims=True)
if mode == 'max':
max_data = np.max(data, axis=0, keepdims=True)
assert(max_data.shape[-1] == data.shape[-1])
max_data[max_data == 0] = 1
data /= max_data
elif mode == 'std':
std_data = np.std(data, axis=0, keepdims=True)
assert(std_data.shape[-1] == data.shape[-1])
std_data[std_data == 0] = 1
data /= std_data
return data
def __data_generation(self, list_IDs_temp):
'readint the waveforms'
X = np.zeros((self.batch_size, self.dim, self.n_channels))
fl = h5py.File(self.file_name, 'r')
# Generate data
for i, ID in enumerate(list_IDs_temp):
if ID.split('_')[-1] == 'EV':
dataset = fl.get(str(ID))
data = np.array(dataset['data'])
elif ID.split('_')[-1] == 'NO':
dataset = fl.get(str(ID))
data = np.array(dataset['data'])
if self.norm_mode:
data = self.normalize(data, self.norm_mode)
X[i, :, :] = data
fl.close()
return X
def data_reader( list_IDs,
file_name,
dim=6000,
n_channels=3,
norm_mode='max',
augmentation=False,
add_event_r=None,
add_gap_r=None,
coda_ratio=0.4,
shift_event_r=None,
add_noise_r=None,
drop_channe_r=None,
scale_amplitude_r=None,
pre_emphasis=True,
**kwargs):
"""
For pre-processing and loading of data into memory.
Parameters
----------
list_IDsx: str
List of trace names.
file_name: str
Path to the input hdf5 datasets.
dim: int, default=6000
Dimension of input traces, in sample.
n_channels: int, default=3
Number of channels.
norm_mode: str, default=max
The mode of normalization, 'max' or 'std'.
augmentation: bool, default=True
If True, half of each batch will be augmented version of the other half.
add_event_r: {float, None}, default=None
Chance for randomly adding a second event into the waveform.
add_gap_r: {float, None}, default=None
Add an interval with zeros into the waveform representing filled gaps.
coda_ratio: {float, 0.4}, default=0.4
% of S-P time to extend event/coda envelope past S pick.
shift_event_r: {float, None}, default=0.9
Rate of augmentation for randomly shifting the event within a trace.
add_noise_r: {float, None}, default=None
Chance for randomly adding Gaussian noise into the waveform.
drop_channe_r: {float, None}, default=None
Chance for randomly dropping some of the channels.
scale_amplitude_r: {float, None}, default=None
Chance for randomly amplifying the waveform amplitude.
pre_emphasis: bool, default=False
If True, waveforms will be pre emphasized.
Returns
--------
Batches of two dictionaries: {'input': X}: pre-processed waveform as input {'picker_S': y3}: outputs including three separate numpy arrays as labels for S.
Note
-----
Label type is fixed to box.
"""
def _normalize( data, mode = 'max'):
'Normalize waveforms in each batch'
data -= np.mean(data, axis=0, keepdims=True)
if mode == 'max':
max_data = np.max(data, axis=0, keepdims=True)
assert(max_data.shape[-1] == data.shape[-1])
max_data[max_data == 0] = 1
data /= max_data
elif mode == 'std':
std_data = np.std(data, axis=0, keepdims=True)
assert(std_data.shape[-1] == data.shape[-1])
std_data[std_data == 0] = 1
data /= std_data
return data
def _scale_amplitude( data, rate):
'Scale amplitude or waveforms'
tmp = np.random.uniform(0, 1)
if tmp < rate:
data *= np.random.uniform(1, 3)
elif tmp < 2*rate:
data /= np.random.uniform(1, 3)
return data
def _drop_channel( data, snr, rate):
'Randomly replace values of one or two components to zeros in earthquake data'
data = np.copy(data)
if np.random.uniform(0, 1) < rate and all(snr >= 10):
c1 = np.random.choice([0, 1])
c2 = np.random.choice([0, 1])
c3 = np.random.choice([0, 1])
if c1 + c2 + c3 > 0:
data[..., np.array([c1, c2, c3]) == 0] = 0
return data
def _drop_channel_noise(data, rate):
'Randomly replace values of one or two components to zeros in noise data'
data = np.copy(data)
if np.random.uniform(0, 1) < rate:
c1 = np.random.choice([0, 1])
c2 = np.random.choice([0, 1])
c3 = np.random.choice([0, 1])
if c1 + c2 + c3 > 0:
data[..., np.array([c1, c2, c3]) == 0] = 0
return data
def _add_gaps(data, rate):
'Randomly add gaps (zeros) of different sizes into waveforms'
data = np.copy(data)
gap_start = np.random.randint(0, 4000)
gap_end = np.random.randint(gap_start, 5500)
if np.random.uniform(0, 1) < rate:
data[gap_start:gap_end,:] = 0
return data
def _add_noise(data, snr, rate):
'Randomly add Gaussian noie with a random SNR into waveforms'
data_noisy = np.empty((data.shape))
if np.random.uniform(0, 1) < rate and all(snr >= 10.0):
data_noisy = np.empty((data.shape))
data_noisy[:, 0] = data[:,0] + np.random.normal(0, np.random.uniform(0.01, 0.15)*max(data[:,0]), data.shape[0])
data_noisy[:, 1] = data[:,1] + np.random.normal(0, np.random.uniform(0.01, 0.15)*max(data[:,1]), data.shape[0])
data_noisy[:, 2] = data[:,2] + np.random.normal(0, np.random.uniform(0.01, 0.15)*max(data[:,2]), data.shape[0])
else:
data_noisy = data
return data_noisy
def _adjust_amplitude_for_multichannels(data):
'Adjust the amplitude of multichaneel data'
tmp = np.max(np.abs(data), axis=0, keepdims=True)
assert(tmp.shape[-1] == data.shape[-1])
if np.count_nonzero(tmp) > 0:
data *= data.shape[-1] / np.count_nonzero(tmp)
return data
def _label(a=0, b=20, c=40):
'Used for triangolar labeling'
z = np.linspace(a, c, num = 2*(b-a)+1)
y = np.zeros(z.shape)
y[z <= a] = 0
y[z >= c] = 0
first_half = np.logical_and(a < z, z <= b)
y[first_half] = (z[first_half]-a) / (b-a)
second_half = np.logical_and(b < z, z < c)
y[second_half] = (c-z[second_half]) / (c-b)
return y
def _add_event(data, addp, adds, coda_end, snr, rate):
'Add a scaled version of the event into the empty part of the trace'
added = np.copy(data)
additions = spt_secondEV = sst_secondEV = None
if addp and adds:
s_p = adds - addp
if np.random.uniform(0, 1) < rate and all(snr >= 10.0) and (data.shape[0]-s_p-21-coda_end) > 20:
secondEV_strt = np.random.randint(coda_end, data.shape[0]-s_p-21)
scaleAM = 1/np.random.randint(1, 10)
space = data.shape[0]-secondEV_strt
added[secondEV_strt:secondEV_strt+space, 0] += data[addp:addp+space, 0]*scaleAM
added[secondEV_strt:secondEV_strt+space, 1] += data[addp:addp+space, 1]*scaleAM
added[secondEV_strt:secondEV_strt+space, 2] += data[addp:addp+space, 2]*scaleAM
spt_secondEV = secondEV_strt
if spt_secondEV + s_p + 21 <= data.shape[0]:
sst_secondEV = spt_secondEV + s_p
if spt_secondEV and sst_secondEV:
additions = [spt_secondEV, sst_secondEV]
data = added
return data, additions
def _shift_event(data, addp, adds, coda_end, snr, rate):
'Randomly rotate the array to shift the event location'
org_len = len(data)
data2 = np.copy(data)
addp2 = adds2 = coda_end2 = None;
if np.random.uniform(0, 1) < rate:
nrotate = int(np.random.uniform(1, int(org_len - coda_end)))
data2[:, 0] = list(data[:, 0])[-nrotate:] + list(data[:, 0])[:-nrotate]
data2[:, 1] = list(data[:, 1])[-nrotate:] + list(data[:, 1])[:-nrotate]
data2[:, 2] = list(data[:, 2])[-nrotate:] + list(data[:, 2])[:-nrotate]
if addp+nrotate >= 0 and addp+nrotate < org_len:
addp2 = addp+nrotate;
else:
addp2 = None;
if adds+nrotate >= 0 and adds+nrotate < org_len:
adds2 = adds+nrotate;
else:
adds2 = None;
if coda_end+nrotate < org_len:
coda_end2 = coda_end+nrotate
else:
coda_end2 = org_len
if addp2 and adds2:
data = data2;
addp = addp2;
adds = adds2;
coda_end= coda_end2;
return data, addp, adds, coda_end
def _pre_emphasis( data, pre_emphasis=0.97):
'apply the pre_emphasis'
for ch in range(n_channels):
bpf = data[:, ch]
data[:, ch] = np.append(bpf[0], bpf[1:] - pre_emphasis * bpf[:-1])
return data
fl = h5py.File(file_name, 'r')
if augmentation:
X = np.zeros((2*len(list_IDs), dim, n_channels))
y3 = np.zeros((2*len(list_IDs), dim, 1))
else:
X = np.zeros((len(list_IDs), dim, n_channels))
y3 = np.zeros((len(list_IDs), dim, 1))
# Generate data
pbar = tqdm(total=len(list_IDs))
for i, ID in enumerate(list_IDs):
pbar.update()
additions = None
dataset = fl.get(str(ID))
if ID.split('_')[-1] == 'EV':
data = np.array(dataset['data'])
spt = int(dataset.attrs['p_arrival_sample']);
sst = int(dataset.attrs['s_arrival_sample']);
coda_end = int(dataset.attrs['coda_end_sample']);
snr = dataset.attrs['snr_db'];
elif ID.split('_')[-1] == 'NO':
data = np.array(dataset['data'])
if augmentation:
if dataset.attrs['trace_category'] == 'earthquake_local':
data, spt, sst, coda_end = _shift_event(data, spt, sst, coda_end, snr, shift_event_r/2);
if norm_mode:
data1 = _normalize(data, norm_mode)
if dataset.attrs['trace_category'] == 'earthquake_local':
if shift_event_r and spt:
data, spt, sst, coda_end = _shift_event(data, spt, sst, coda_end, snr, shift_event_r);
if add_event_r:
data, additions = _add_event(data, spt, sst, coda_end, snr, add_event_r);
if drop_channe_r:
data = _drop_channel(data, snr, drop_channe_r);
# data = _adjust_amplitude_for_multichannels(data);
if scale_amplitude_r:
data = _scale_amplitude(data, scale_amplitude_r);
if pre_emphasis:
data = _pre_emphasis(data);
if add_noise_r:
data = _add_noise(data, snr, add_noise_r);
if norm_mode:
data2 = _normalize(data, norm_mode);
if dataset.attrs['trace_category'] == 'noise':
if drop_channe_r:
data = _drop_channel_noise(data, drop_channe_r);
if add_gap_r:
data = _add_gaps(data, add_gap_r)
if norm_mode:
data2 = _normalize(data, norm_mode)
X[i, :, :] = data1
X[len(list_IDs)+i, :, :] = data2
if dataset.attrs['trace_category'] == 'earthquake_local':
if sst and (sst-20 >= 0) and (sst+21 < dim):
y3[i, sst-20:sst+21, 0] = _label()
y3[len(list_IDs)+i, sst-20:sst+21, 0] = _label()
elif sst and (sst+21 < dim):
y3[i, 0:sst+sst+1, 0] = _label(a=0, b=sst, c=2*sst)
y3[len(list_IDs)+i, 0:sst+sst+1, 0] = _label(a=0, b=sst, c=2*sst)
elif sst and (sst-20 >= 0):
sdif = dim - sst
y3[i, sst-sdif-1:dim, 0] = _label(a=sst-sdif, b=sst, c=2*sdif)
y3[len(list_IDs)+i, sst-sdif-1:dim, 0] = _label(a=sst-sdif, b=sst, c=2*sdif)
sd = sst - spt
if additions:
add_spt = additions[0];
print(add_spt)
add_sst = additions[1];
add_sd = add_sst - add_spt
if add_sst and (add_sst-20 >= 0) and (add_sst+21 < dim):
y3[len(list_IDs)+i, add_sst-20:add_sst+21, 0] = _label()
elif add_sst and (add_sst+21 < dim):
y3[len(list_IDs)+i, 0:add_sst+add_sst+1, 0] = _label(a=0, b=add_sst, c=2*add_sst)
elif add_sst and (add_sst-20 >= 0):
sdif = dim - add_sst
y3[len(list_IDs)+i, add_sst-sdif-1:dim, 0] = _label(a=add_sst-sdif, b=add_sst, c=2*sdif)
fl.close()
return X.astype('float32'), y3.astype('float32')
def _detect_peaks(x, mph=None, mpd=1, threshold=0, edge='rising', kpsh=False, valley=False):
"""
Detect peaks in data based on their amplitude and other features.
Parameters
----------
x : 1D array_like
data.
mph : {None, number}, default=None
detect peaks that are greater than minimum peak height.
mpd : int, default=1
detect peaks that are at least separated by minimum peak distance (in number of data).
threshold : int, default=0
detect peaks (valleys) that are greater (smaller) than `threshold in relation to their immediate neighbors.
edge : str, default=rising
for a flat peak, keep only the rising edge ('rising'), only the falling edge ('falling'), both edges ('both'), or don't detect a flat peak (None).
kpsh : bool, default=False
keep peaks with same height even if they are closer than `mpd`.
valley : bool, default=False
if True (1), detect valleys (local minima) instead of peaks.
Returns
---------
ind : 1D array_like
indeces of the peaks in `x`.
Modified from
----------------
.. [1] http://nbviewer.ipython.org/github/demotu/BMC/blob/master/notebooks/DetectPeaks.ipynb
"""
x = np.atleast_1d(x).astype('float64')
if x.size < 3:
return np.array([], dtype=int)
if valley:
x = -x
# find indices of all peaks
dx = x[1:] - x[:-1]
# handle NaN's
indnan = np.where(np.isnan(x))[0]
if indnan.size:
x[indnan] = np.inf
dx[np.where(np.isnan(dx))[0]] = np.inf
ine, ire, ife = np.array([[], [], []], dtype=int)
if not edge:
ine = np.where((np.hstack((dx, 0)) < 0) & (np.hstack((0, dx)) > 0))[0]
else:
if edge.lower() in ['rising', 'both']:
ire = np.where((np.hstack((dx, 0)) <= 0) & (np.hstack((0, dx)) > 0))[0]
if edge.lower() in ['falling', 'both']:
ife = np.where((np.hstack((dx, 0)) < 0) & (np.hstack((0, dx)) >= 0))[0]
ind = np.unique(np.hstack((ine, ire, ife)))
# handle NaN's
if ind.size and indnan.size:
# NaN's and values close to NaN's cannot be peaks
ind = ind[np.in1d(ind, np.unique(np.hstack((indnan, indnan-1, indnan+1))), invert=True)]
# first and last values of x cannot be peaks
if ind.size and ind[0] == 0:
ind = ind[1:]
if ind.size and ind[-1] == x.size-1:
ind = ind[:-1]
# remove peaks < minimum peak height
if ind.size and mph is not None:
ind = ind[x[ind] >= mph]
# remove peaks - neighbors < threshold
if ind.size and threshold > 0:
dx = np.min(np.vstack([x[ind]-x[ind-1], x[ind]-x[ind+1]]), axis=0)
ind = np.delete(ind, np.where(dx < threshold)[0])
# detect small peaks closer than minimum peak distance
if ind.size and mpd > 1:
ind = ind[np.argsort(x[ind])][::-1] # sort ind by peak height
idel = np.zeros(ind.size, dtype=bool)
for i in range(ind.size):
if not idel[i]:
# keep peaks with the same height if kpsh is True
idel = idel | (ind >= ind[i] - mpd) & (ind <= ind[i] + mpd) \
& (x[ind[i]] > x[ind] if kpsh else True)
idel[i] = 0 # Keep current peak
# remove the small peaks and sort back the indices by their occurrence
ind = np.sort(ind[~idel])
return ind
def picker(args, yh3, yh3_std, spt=None):
"""
Performs detection and picking.
Parameters
----------
args : dic
A dictionary containing all of the input parameters.
yh3 : 1D array
S arrival probabilities.
yh3_std : 1D array
S arrival standard deviations.
spt : {int, None}, default=None