File size: 9,531 Bytes
0eb79a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import division
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import os
from data_reader import DataConfig
from detect_peaks import detect_peaks
import logging

class EMA(object):
    def __init__(self, alpha):
        self.alpha = alpha
        self.x = 0.
        self.count = 0

    @property
    def value(self):
        return self.x

    def __call__(self, x):
        if self.count == 0:
            self.x = x
        else:
            self.x = self.alpha * self.x + (1 - self.alpha) * x
        self.count += 1
        return self.x

class LMA(object):
    def __init__(self):
        self.x = 0.
        self.count = 0

    @property
    def value(self):
        return self.x

    def __call__(self, x):
        if self.count == 0:
            self.x = x
        else:
            self.x += (x - self.x)/(self.count+1)
        self.count += 1
        return self.x

def detect_peaks_thread(i, pred, fname=None, result_dir=None, args=None):
  if args is None:
    itp, prob_p = detect_peaks(pred[i,:,0,1], mph=0.5, mpd=0.5/DataConfig().dt, show=False)
    its, prob_s = detect_peaks(pred[i,:,0,2], mph=0.5, mpd=0.5/DataConfig().dt, show=False)
  else:
    itp, prob_p = detect_peaks(pred[i,:,0,1], mph=args.tp_prob, mpd=0.5/DataConfig().dt, show=False)
    its, prob_s = detect_peaks(pred[i,:,0,2], mph=args.ts_prob, mpd=0.5/DataConfig().dt, show=False)
  if (fname is not None) and (result_dir is not None):
#    np.savez(os.path.join(result_dir, fname[i].decode().split('/')[-1]), pred=pred[i], itp=itp, its=its, prob_p=prob_p, prob_s=prob_s)
    try:
      np.savez(os.path.join(result_dir, fname[i].decode()), pred=pred[i], itp=itp, its=its, prob_p=prob_p, prob_s=prob_s)
    except FileNotFoundError:
      #if not os.path.exists(os.path.dirname(os.path.join(result_dir, fname[i].decode()))):
      os.makedirs(os.path.dirname(os.path.join(result_dir, fname[i].decode())), exist_ok=True)
      np.savez(os.path.join(result_dir, fname[i].decode()), pred=pred[i], itp=itp, its=its, prob_p=prob_p, prob_s=prob_s)
  return [(itp, prob_p), (its, prob_s)]

def plot_result_thread(i, pred, X, Y=None, itp=None, its=None, 
                       itp_pred=None, its_pred=None, fname=None, figure_dir=None):
  dt = DataConfig().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.clf()
  # fig_size = plt.gcf().get_size_inches()
  # plt.gcf().set_size_inches(fig_size*[1, 1.2])
  plt.subplot(411)
  plt.plot(t, X[i, :, 0, 0], 'k', label='E', 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 is not None) and (its is not None):
    for j in range(len(itp[i])):
      if j == 0:
        plt.plot([itp[i][j]*dt, itp[i][j]*dt], [tmp_min, tmp_max], 'b', label='P', linewidth=0.5)
      else:
        plt.plot([itp[i][j]*dt, itp[i][j]*dt], [tmp_min, tmp_max], 'b', linewidth=0.5)
    for j in range(len(its[i])):
      if j == 0:
        plt.plot([its[i][j]*dt, its[i][j]*dt], [tmp_min, tmp_max], 'r', label='S', linewidth=0.5)
      else:
        plt.plot([its[i][j]*dt, its[i][j]*dt], [tmp_min, tmp_max], 'r', 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(412)
  plt.plot(t, X[i, :, 0, 1], 'k', label='N', linewidth=0.5)
  plt.autoscale(enable=True, axis='x', tight=True)
  tmp_min = np.min(X[i, :, 0, 1])
  tmp_max = np.max(X[i, :, 0, 1])
  if (itp is not None) and (its is not None):
    for j in range(len(itp[i])):
      plt.plot([itp[i][j]*dt, itp[i][j]*dt], [tmp_min, tmp_max], 'b', linewidth=0.5)
    for j in range(len(its[i])):
      plt.plot([its[i][j]*dt, its[i][j]*dt], [tmp_min, tmp_max], 'r', 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], '(ii)', horizontalalignment='center',
           transform=plt.gca().transAxes, fontsize="small", fontweight="normal", bbox=box)
  plt.subplot(413)
  plt.plot(t, X[i, :, 0, 2], 'k', label='Z', linewidth=0.5)
  plt.autoscale(enable=True, axis='x', tight=True)
  tmp_min = np.min(X[i, :, 0, 2])
  tmp_max = np.max(X[i, :, 0, 2])
  if (itp is not None) and (its is not None):
    for j in range(len(itp[i])):
      plt.plot([itp[i][j]*dt, itp[i][j]*dt], [tmp_min, tmp_max], 'b', linewidth=0.5)
    for j in range(len(its[i])):
      plt.plot([its[i][j]*dt, its[i][j]*dt], [tmp_min, tmp_max], 'r', 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], '(iii)', horizontalalignment='center',
           transform=plt.gca().transAxes, fontsize="small", fontweight="normal", bbox=box)
  plt.subplot(414)
  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].decode().rstrip('.npz')+'.png'), 
                bbox_inches='tight')
  except FileNotFoundError:
  #if not os.path.exists(os.path.dirname(os.path.join(figure_dir, fname[i].decode()))):
    os.makedirs(os.path.dirname(os.path.join(figure_dir, fname[i].decode())), exist_ok=True)
    plt.savefig(os.path.join(figure_dir, 
                fname[i].decode().rstrip('.npz')+'.png'), 
                bbox_inches='tight')
  #plt.savefig(os.path.join(figure_dir, 
  #            fname[i].decode().split('/')[-1].rstrip('.npz')+'.png'), 
  #            bbox_inches='tight')
  # plt.savefig(os.path.join(figure_dir, 
  #             fname[i].decode().split('/')[-1].rstrip('.npz')+'.pdf'), 
  #             bbox_inches='tight')
  plt.close(i)
  return 0

def postprocessing_thread(i, pred, X, Y=None, itp=None, its=None, fname=None, result_dir=None, figure_dir=None, args=None):
  (itp_pred, prob_p), (its_pred, prob_s) = detect_peaks_thread(i, pred, fname, result_dir, args)
  if (fname is not None) and (figure_dir is not None):
    plot_result_thread(i, pred, X, Y, itp, its, itp_pred, its_pred, fname, figure_dir)
  return [(itp_pred, prob_p), (its_pred, prob_s)]


def clean_queue(picks):
  clean = []
  for i in range(len(picks)):
    tmp = []
    for j in picks[i]:
      if j != 0:
        tmp.append(j)
    clean.append(tmp)
  return clean

def clean_queue_thread(picks):
  tmp = []
  for j in picks:
    if j != 0:
      tmp.append(j)
  return tmp


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, tol):
  dt = DataConfig().dt
  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
  diff_p = []; diff_s = []
  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)
    diff_p.append(tmp_p[np.abs(tmp_p) < 0.5/dt])
    diff_s.append(tmp_s[np.abs(tmp_s) < 0.5/dt])

  return [TP_p, TP_s, nP_p, nP_s, nT_p, nT_s, diff_p, diff_s]

def calculate_metrics(picks, itp, its, tol=0.1):
  TP_p, TP_s, nP_p, nP_s,  nT_p, nT_s, diff_p, diff_s = correct_picks(picks, itp, its, tol)
  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]