-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
executable file
·130 lines (119 loc) · 5.33 KB
/
util.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
from __future__ import division
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import os
from data_reader import Config
from detect_peaks import detect_peaks
import logging
def detect_peaks_all_classes(i, pred, fname=None, result_dir=None, args=None):
distance = Config().Y_shape[0] # allow to return only the maximum peak probability
if args is None:
itp = detect_peaks(pred[i,:,0,1], mph=0.5, mpd=distance, show=False)
its = detect_peaks(pred[i,:,0,2], mph=0.5, mpd=distance/Config().dt, show=False)
else:
itp = detect_peaks(pred[i,:,0,1], mph=args.tp_prob, mpd=distance, show=False)
its = detect_peaks(pred[i,:,0,2], mph=args.ts_prob, mpd=distance, show=False)
prob_p = pred[i,itp,0,1]
prob_s = pred[i,its,0,2]
if (fname is not None) and (result_dir is not None):
try:
np.savez(os.path.join(result_dir, fname[i]), pred=pred[i], itp=itp, its=its, prob_p=prob_p, prob_s=prob_s)
except FileNotFoundError:
os.makedirs(os.path.dirname(os.path.join(result_dir, fname[i])), exist_ok=True)
np.savez(os.path.join(result_dir, fname[i]), pred=pred[i], itp=itp, its=its, prob_p=prob_p, prob_s=prob_s)
return [(itp, prob_p), (its, prob_s)]
def plot_results(i, pred, X, Y=None, itp_pred=None, its_pred=None, fname=None, figure_dir=None):
dt = Config().dt
t = np.arange(0, pred.shape[1]) * dt
box = dict(boxstyle='round', facecolor='white', alpha=1)
text_loc = [0.05, 0.77]
plt.figure(i)
plt.subplot(211)
plt.plot(t, X[i, :, 0, 0], 'k', label='channel_Z', linewidth=0.5)
plt.autoscale(enable=True, axis='x', tight=True)
tmp_min = np.min(X[i, :, 0, 0])
tmp_max = np.max(X[i, :, 0, 0])
if (itp_pred is not None) and (its_pred is not None):
for j in range(len(itp_pred)):
plt.plot([itp_pred[j]*dt, itp_pred[j]*dt], [tmp_min, tmp_max], '--g', linewidth=0.5)
for j in range(len(its_pred)):
plt.plot([its_pred[j]*dt, its_pred[j]*dt], [tmp_min, tmp_max], '-.m', linewidth=0.5)
plt.ylabel('Amplitude')
plt.legend(loc='upper right', fontsize='small')
plt.gca().set_xticklabels([])
plt.text(text_loc[0], text_loc[1], '(i)', horizontalalignment='center',
transform=plt.gca().transAxes, fontsize="small", fontweight="normal", bbox=box)
plt.subplot(212)
if Y is not None:
plt.plot(t, Y[i, :, 0, 1], 'b', label='P', linewidth=0.5)
plt.plot(t, Y[i, :, 0, 2], 'r', label='S', linewidth=0.5)
plt.plot(t, pred[i, :, 0, 1], '--g', label='$\hat{P}$', linewidth=0.5)
plt.plot(t, pred[i, :, 0, 2], '-.m', label='$\hat{S}$', linewidth=0.5)
plt.autoscale(enable=True, axis='x', tight=True)
if (itp_pred is not None) and (its_pred is not None):
for j in range(len(itp_pred)):
plt.plot([itp_pred[j]*dt, itp_pred[j]*dt], [-0.1, 1.1], '--g', linewidth=0.5)
for j in range(len(its_pred)):
plt.plot([its_pred[j]*dt, its_pred[j]*dt], [-0.1, 1.1], '-.m', linewidth=0.5)
plt.ylim([-0.05, 1.05])
plt.text(text_loc[0], text_loc[1], '(iv)', horizontalalignment='center',
transform=plt.gca().transAxes, fontsize="small", fontweight="normal", bbox=box)
plt.legend(loc='upper right', fontsize='small')
plt.xlabel('Time (s)')
plt.ylabel('Probability')
plt.tight_layout()
plt.gcf().align_labels()
try:
plt.savefig(os.path.join(figure_dir,
fname[i].rstrip('.npz')+'.png'),
bbox_inches='tight')
except FileNotFoundError:
os.makedirs(os.path.dirname(os.path.join(figure_dir, fname[i])), exist_ok=True)
plt.savefig(os.path.join(figure_dir,
fname[i].rstrip('.npz')+'.png'),
bbox_inches='tight')
plt.close(i)
return 0
def metrics(TP, nP, nT):
'''
TP: true positive
nP: number of positive picks
nT: number of true picks
'''
precision = TP / nP
recall = TP / nT
F1 = 2* precision * recall / (precision + recall)
return [precision, recall, F1]
def correct_picks(picks, true_p, true_s):
dt = Config().dt
tol = Config().tol
if len(true_p) != len(true_s):
print("The length of true P and S pickers are not the same")
num = len(true_p)
TP_p = 0; TP_s = 0; nP_p = 0; nP_s = 0; nT_p = 0; nT_s = 0
for i in range(num):
nT_p += len(true_p[i])
nT_s += len(true_s[i])
nP_p += len(picks[i][0][0])
nP_s += len(picks[i][1][0])
if len(true_p[i]) > 1 or len(true_s[i]) > 1:
print(i, picks[i], true_p[i], true_s[i])
tmp_p = np.array(picks[i][0][0]) - np.array(true_p[i])[:,np.newaxis]
tmp_s = np.array(picks[i][1][0]) - np.array(true_s[i])[:,np.newaxis]
TP_p += np.sum(np.abs(tmp_p) < tol/dt)
TP_s += np.sum(np.abs(tmp_s) < tol/dt)
return [TP_p, TP_s, nP_p, nP_s, nT_p, nT_s]
def calculate_metrics(picks, itp, its):
TP_p, TP_s, nP_p, nP_s, nT_p, nT_s = correct_picks(picks, itp, its)
precision_p, recall_p, f1_p = metrics(TP_p, nP_p, nT_p)
precision_s, recall_s, f1_s = metrics(TP_s, nP_s, nT_s)
logging.info("Total records: {}".format(len(picks)))
logging.info("P-phase:")
logging.info("True={}, Predict={}, TruePositive={}".format(nT_p, nP_p, TP_p))
logging.info("Precision={:.3f}, Recall={:.3f}, F1={:.3f}".format(precision_p, recall_p, f1_p))
logging.info("S-phase:")
logging.info("True={}, Predict={}, TruePositive={}".format(nT_s, nP_s, TP_s))
logging.info("Precision={:.3f}, Recall={:.3f}, F1={:.3f}".format(precision_s, recall_s, f1_s))
return [precision_p, recall_p, f1_p], [precision_s, recall_s, f1_s]