Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gaussian picking uncertainty #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,14 @@ def pred_fn(args, data_reader, figure_dir=None, result_dir=None, log_dir=None):
row = "{},[{}],[{}],[{}],[{}]".format(fname_batch[i].decode(), " ".join(map(str,picks_batch[i][0][0])), " ".join(map(str,picks_batch[i][0][1])),
" ".join(map(str,picks_batch[i][1][0])), " ".join(map(str,picks_batch[i][1][1])))
fclog.write(row+"\n")
picks[fname_batch[i].decode()]={"itp":picks_batch[i][0][0], "tp_prob":picks_batch[i][0][1], "its":picks_batch[i][1][0], "ts_prob":picks_batch[i][1][1]}
picks[fname_batch[i].decode()]={
"itp":picks_batch[i][0][0],
"tp_prob":picks_batch[i][0][1],
"its":picks_batch[i][1][0],
"ts_prob":picks_batch[i][1][1],
"sigma_itp":picks_batch[i][0][2],
"sigma_its":picks_batch[i][1][2],
}

if last_batch:
break
Expand All @@ -531,7 +538,14 @@ def pred_fn(args, data_reader, figure_dir=None, result_dir=None, log_dir=None):
row = "{},[{}],[{}],[{}],[{}]".format(fname_batch[i].decode(), " ".join(map(str,picks_batch[i][0][0])), " ".join(map(str,picks_batch[i][0][1])),
" ".join(map(str,picks_batch[i][1][0])), " ".join(map(str,picks_batch[i][1][1])))
fclog.write(row+"\n")
picks[fname_batch[i].decode()]={"itp":picks_batch[i][0][0], "tp_prob":picks_batch[i][0][1], "its":picks_batch[i][1][0], "ts_prob":picks_batch[i][1][1]}
picks[fname_batch[i].decode()]={
"itp":picks_batch[i][0][0],
"tp_prob":picks_batch[i][0][1],
"its":picks_batch[i][1][0],
"ts_prob":picks_batch[i][1][1],
"sigma_itp":picks_batch[i][0][2],
"sigma_its":picks_batch[i][1][2],
}
# fclog.flush()

fclog.close()
Expand Down
28 changes: 25 additions & 3 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
from detect_peaks import detect_peaks
import logging


def gauss_sigma(pred, it):
if len(it) == 0:
return np.empty(0)
# get peak widths to cut `pred` aroud each pick
from scipy.signal import peak_widths
_, _, left, right = peak_widths(pred, it, rel_height=0.8)
sigma = []
for ii, li, ri in zip(it, left, right):
li = int(li)
ri = int(ri)
pred_cut = pred.copy()
pred_cut[:li] = 0
pred_cut[ri:] = 0
samples = np.arange(len(pred_cut))
# get gaussian sigma of `pred_cut`
sigma.append((sum(((samples-ii) * pred_cut)**2)/sum(pred_cut))**0.5)
return np.array(sigma)


def detect_peaks_thread(i, pred, fname=None, result_dir=None, args=None):
if args is None:
itp = detect_peaks(pred[i,:,0,1], mph=0.5, mpd=0.5/Config().dt, show=False)
Expand All @@ -17,6 +37,8 @@ def detect_peaks_thread(i, pred, fname=None, result_dir=None, args=None):
its = detect_peaks(pred[i,:,0,2], mph=args.ts_prob, mpd=0.5/Config().dt, show=False)
prob_p = pred[i,itp,0,1]
prob_s = pred[i,its,0,2]
sigma_itp = gauss_sigma(pred[i,:,0,1], itp)
sigma_its = gauss_sigma(pred[i,:,0,2], its)
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:
Expand All @@ -25,7 +47,7 @@ def detect_peaks_thread(i, pred, fname=None, result_dir=None, args=None):
#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)]
return [(itp, prob_p, sigma_itp), (its, prob_s, sigma_its)]

def plot_result_thread(i, pred, X, Y=None, itp=None, its=None,
itp_pred=None, its_pred=None, fname=None, figure_dir=None):
Expand Down Expand Up @@ -130,10 +152,10 @@ def plot_result_thread(i, pred, X, Y=None, itp=None, its=None,
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)
(itp_pred, prob_p, sigma_itp), (its_pred, prob_s, sigma_its) = 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)]
return [(itp_pred, prob_p, sigma_itp), (its_pred, prob_s, sigma_its)]


def clean_queue(picks):
Expand Down