-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Run_EqT.py
More file actions
executable file
·107 lines (100 loc) · 4.22 KB
/
2-Run_EqT.py
File metadata and controls
executable file
·107 lines (100 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on March 2023
@authors: Hamzeh Mohammadigheymasi, Xiao Zhuowei
"""
"""
Runs EQT on the prepared data from the step 1- Data_preparation, and detect P and S phases for all the stationsConvert the OnDisk data format from the SeisComp format to the EQT format
"""
import warnings
warnings.filterwarnings("ignore")
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
import sys
sys.path.append('./src/S_EqT_codes/src/EqT_libs')
sys.path.append('./src/S_EqT_codes/src')
from misc import convert_csv_to_real, xml2REAL_sta
from hdf5_maker import preprocessor
from predictor import predictor
from EqT_utils import DataGeneratorPrediction, picker, generate_arrays_from_file
from EqT_utils import f1, SeqSelfAttention, FeedForward, LayerNormalization
from keras.models import load_model
from keras.optimizers import Adam
import json
import os
import argparse
from pathlib import Path
import tensorflow as tf
def set_gpu_memory_fraction(gpu_fraction):
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
tf.config.set_logical_device_configuration(
gpus[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=gpus[0].memory_limit * gpu_fraction)])
logical_gpus = tf.config.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Virtual devices must be set at program startup
print(e)
def convert(cfgs):
mseed_dir = cfgs['EqT']['mseed_dir']
sta_json_path = cfgs['EqT']['sta_json_path']
overlap = cfgs['EqT']['overlap']
n_processor = cfgs['EqT']['n_processor']
preprocessor(mseed_dir=mseed_dir,
stations_json=sta_json_path,
overlap=overlap,n_processor=n_processor)
return 1
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='2-run_EqT')
parser.add_argument('--config-file', dest='config_file', type=str, help='Configuration file path',
default='./Configuration_Parameters.json')
args = parser.parse_args()
with open(args.config_file, 'r') as f:
cfgs = json.load(f)
task_dir = './' + cfgs['Project'] + '/'
os.chdir(task_dir)
# earthquake detection and phase picking by the EqT model
os.environ['CUDA_VISIBLE_DEVICES'] = cfgs['EqT']['gpuid']
set_gpu_memory_fraction(0.5)
convert(cfgs)
print(' *** Loading the model ...', flush=True)
model = load_model(cfgs['EqT']['model_path'],
custom_objects={'SeqSelfAttention': SeqSelfAttention,
'FeedForward': FeedForward,
'LayerNormalization': LayerNormalization,
'f1': f1
})
model.compile(loss = ['binary_crossentropy', 'binary_crossentropy', 'binary_crossentropy'],
loss_weights = [0.03, 0.40, 0.58],
optimizer = Adam(lr = 0.001),
metrics = [f1])
print('Done Loading Model')
predictor(input_dir= cfgs['EqT']['mseed_dir'] + '_processed_hdfs/',
input_model=model,
output_dir=cfgs['EqT']['det_res'],
estimate_uncertainty=False,
output_probabilities=False,
number_of_sampling=cfgs['EqT']['number_of_sampling'],
loss_weights=[0.02, 0.40, 0.58],
detection_threshold=cfgs['EqT']['EQ_threshold'],
P_threshold=cfgs['EqT']['P_threshold'],
S_threshold=cfgs['EqT']['S_threshold'],
number_of_plots=0,
plot_mode='time',
batch_size=500,gpuid=0,
number_of_cpus=4,
keepPS=True,
spLimit=60)
# convert xml and csv files for S-EqT and REAL
xml2REAL_sta(cfgs)
det_folder = Path(cfgs['EqT']['det_res'])
for outfolder in det_folder.glob('*_outputs'):
csv_name = str(outfolder) + '/X_prediction_results.csv'
state = convert_csv_to_real(csv_name, cfgs)
if state == 0:
print('Empty {}'.format(outfolder.name))
else:
print('Success On {}'.format(outfolder.name))