some fixes calling autopylot (figures=None), added log for autoTuner

This commit is contained in:
Marcel Paffrath 2017-05-15 17:21:22 +02:00
parent a2ab98c202
commit cae5c961b0
7 changed files with 252 additions and 93 deletions

View File

@ -1181,6 +1181,7 @@ class MainWindow(QMainWindow):
if not self.tap:
self.tap = TuneAutopicker(self)
self.update_autopicker()
self.tap.update.connect(self.update_autopicker)
else:
self.tap.fill_eventbox()
@ -1761,10 +1762,18 @@ class Project(object):
return
for item in eventlist:
event = Event(item)
if not event in self.eventlist:
if not event.path in self.getPaths():
self.eventlist.append(event)
else:
print('Skipping event with path {}. Already part of project.'.format(event.path))
self.setDirty()
def getPaths(self):
paths = []
for event in self.eventlist:
paths.append(event.path)
return paths
def setDirty(self):
self.dirty = True

View File

@ -29,7 +29,7 @@ from pylot.core.util.version import get_git_version as _getVersionString
__version__ = _getVersionString()
def autoPyLoT(input_dict=None, parameter=None, inputfile=None, fnames=None, savepath=None, iplot=0):
def autoPyLoT(input_dict=None, parameter=None, inputfile=None, fnames=None, savepath=None, station='all', iplot=0):
"""
Determine phase onsets automatically utilizing the automatic picking
algorithms by Kueperkoch et al. 2010/2012.
@ -55,15 +55,20 @@ def autoPyLoT(input_dict=None, parameter=None, inputfile=None, fnames=None, save
***********************************'''.format(version=_getVersionString())
print(splash)
locflag = 1
if input_dict:
if input_dict.has_key('parameter'):
parameter = input_dict['parameter']
if input_dict.has_key('fig_dict'):
fig_dict = input_dict['fig_dict']
if input_dict.has_key('station'):
station = input_dict['station']
if input_dict.has_key('fnames'):
fnames = input_dict['fnames']
if input_dict.has_key('iplot'):
iplot = input_dict['iplot']
if input_dict.has_key('locflag'):
locflag = input_dict['locflag']
if not parameter:
if inputfile:
@ -103,8 +108,7 @@ def autoPyLoT(input_dict=None, parameter=None, inputfile=None, fnames=None, save
datastructure.setExpandFields(exf)
# check if default location routine NLLoc is available
if parameter['nllocbin']:
locflag = 1
if parameter['nllocbin'] and locflag:
# get NLLoc-root path
nllocroot = parameter.get('nllocroot')
# get path to NLLoc executable
@ -168,12 +172,17 @@ def autoPyLoT(input_dict=None, parameter=None, inputfile=None, fnames=None, save
now.minute)
parameter.setParam(eventID=evID)
wfdat = data.getWFData() # all available streams
if not station == 'all':
wfdat = wfdat.select(station=station)
if not wfdat:
print('Could not find station {}. STOP!'.format(station))
return
wfdat = remove_underscores(wfdat)
metadata = read_metadata(parameter.get('invdir'))
corr_dat = restitute_data(wfdat.copy(), *metadata)
print('Working on event %s' % event)
print(data)
print('Working on event %s. Stations: %s' % (event, station))
print(wfdat)
##########################################################
# !automated picking starts here!
picks = autopickevent(wfdat, parameter, iplot=iplot, fig_dict=fig_dict)
@ -243,7 +252,7 @@ def autoPyLoT(input_dict=None, parameter=None, inputfile=None, fnames=None, save
print("autoPyLoT: Number of maximum iterations reached, stop iterative picking!")
break
print("autoPyLoT: Starting with iteration No. %d ..." % nlloccounter)
picks, _ = iteratepicker(wfdat, nllocfile, picks, badpicks, parameter)
picks = iteratepicker(wfdat, nllocfile, picks, badpicks, parameter, fig_dict=fig_dict)
# write phases to NLLoc-phase file
nll.export(picks, phasefile, parameter)
# remove actual NLLoc-location file to keep only the last

View File

@ -1 +1 @@
4d9c-dirty
a2ab-dirty

View File

@ -186,11 +186,15 @@ class AutoPickParameter(object):
if not is_type == expect_type and not is_type == tuple:
message = 'Type check failed for param: {}, is type: {}, expected type:{}'
message = message.format(param, is_type, expect_type)
raise TypeError(message)
print(Warning(message))
def setParam(self, param, value):
def setParamKV(self, param, value):
self.__setitem__(param, value)
def setParam(self, **kwargs):
for key in kwargs:
self.__setitem__(key, kwargs[key])
@staticmethod
def _printParameterError(errmsg):
print('ParameterError:\n non-existent parameter %s' % errmsg)

View File

@ -233,6 +233,10 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
# get prelimenary onset time from AIC-HOS-CF using subclass AICPicker
# of class AutoPicking
key = 'aicFig'
if fig_dict:
fig = fig_dict[key]
else:
fig = None
aicpick = AICPicker(aiccf, tsnrz, pickwinP, iplot, None, tsmoothP, fig=fig_dict[key])
##############################################################
if aicpick.getpick() is not None:
@ -248,10 +252,14 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
'{1}'.format(minsiglength, minsiglength / 2)
if verbose: print(msg)
key = 'slength'
if fig_dict:
fig = fig_dict[key]
else:
fig = None
Pflag = checksignallength(zne, aicpick.getpick(), tsnrz,
minsiglength / 2,
nfacsl, minpercent, iplot,
fig_dict[key])
fig)
else:
# filter and taper horizontal traces
trH1_filt = edat.copy()
@ -266,11 +274,14 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
trH2_filt.taper(max_percentage=0.05, type='hann')
zne += trH1_filt
zne += trH2_filt
key = 'slength'
if fig_dict:
fig = fig_dict['slength']
else:
fig = None
Pflag = checksignallength(zne, aicpick.getpick(), tsnrz,
minsiglength,
nfacsl, minpercent, iplot,
fig_dict[key])
fig)
if Pflag == 1:
# check for spuriously picked S onset
@ -281,9 +292,12 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
if verbose: print(msg)
else:
if iplot>1:
key = 'checkZ4s'
if fig_dict:
fig = fig_dict['checkZ4s']
else:
fig = None
Pflag = checkZ4S(zne, aicpick.getpick(), zfac,
tsnrz[3], iplot, fig_dict[key])
tsnrz[3], iplot, fig)
if Pflag == 0:
Pmarker = 'SinsteadP'
Pweight = 9
@ -330,18 +344,24 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
'correctly: maybe the algorithm name ({algoP}) is ' \
'corrupted'.format(
algoP=algoP)
key = 'refPpick'
if fig_dict:
fig = fig_dict['refPpick']
else:
fig = None
refPpick = PragPicker(cf2, tsnrz, pickwinP, iplot, ausP, tsmoothP,
aicpick.getpick(), fig_dict[key])
aicpick.getpick(), fig)
mpickP = refPpick.getpick()
#############################################################
if mpickP is not None:
# quality assessment
# get earliest/latest possible pick and symmetrized uncertainty
if iplot:
key = 'el_Ppick'
if fig_dict:
fig = fig_dict['el_Ppick']
else:
fig = None
epickP, lpickP, Perror = earllatepicker(z_copy, nfacP, tsnrz,
mpickP, iplot, fig=fig_dict[key])
mpickP, iplot, fig=fig)
else:
epickP, lpickP, Perror = earllatepicker(z_copy, nfacP, tsnrz,
mpickP, iplot)
@ -366,8 +386,11 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
# certain quality required
if Pweight <= minfmweight and SNRP >= minFMSNR:
if iplot:
key = 'fm_picker'
FM = fmpicker(zdat, z_copy, fmpickwin, mpickP, iplot, fig_dict[key])
if fig_dict:
fig = fig_dict['fm_picker']
else:
fig = None
FM = fmpicker(zdat, z_copy, fmpickwin, mpickP, iplot, fig)
else:
FM = fmpicker(zdat, z_copy, fmpickwin, mpickP, iplot)
else:
@ -468,9 +491,12 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
##############################################################
# get prelimenary onset time from AIC-HOS-CF using subclass AICPicker
# of class AutoPicking
key = 'aicARHfig'
if fig_dict:
fig = fig_dict['aicARHfig']
else:
fig = None
aicarhpick = AICPicker(haiccf, tsnrh, pickwinS, iplot, None,
aictsmoothS, fig = fig_dict[key])
aictsmoothS, fig=fig)
###############################################################
# go on with processing if AIC onset passes quality control
if (aicarhpick.getSlope() >= minAICSslope and
@ -524,9 +550,12 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
addnoise) # instance of ARHcf
# get refined onset time from CF2 using class Picker
key = 'refSpick'
if fig_dict:
fig = fig_dict['refSpick']
else:
fig = None
refSpick = PragPicker(arhcf2, tsnrh, pickwinS, iplot, ausS,
tsmoothS, aicarhpick.getpick(), fig_dict[key])
tsmoothS, aicarhpick.getpick(), fig)
mpickS = refSpick.getpick()
#############################################################
if mpickS is not None:
@ -534,11 +563,14 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
# get earliest/latest possible pick and symmetrized uncertainty
h_copy[0].data = trH1_filt.data
if iplot:
key = 'el_S1pick'
if fig_dict:
fig = fig_dict['el_S1pick']
else:
fig = None
epickS1, lpickS1, Serror1 = earllatepicker(h_copy, nfacS,
tsnrh,
mpickS, iplot,
fig_dict[key])
fig=fig)
else:
epickS1, lpickS1, Serror1 = earllatepicker(h_copy, nfacS,
tsnrh,
@ -546,11 +578,14 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
h_copy[0].data = trH2_filt.data
if iplot:
key = 'el_S2pick'
if fig_dict:
fig = fig_dict['el_S2pick']
else:
fig = None
epickS2, lpickS2, Serror2 = earllatepicker(h_copy, nfacS,
tsnrh,
mpickS, iplot,
fig_dict[key])
fig=fig)
else:
epickS2, lpickS2, Serror2 = earllatepicker(h_copy, nfacS,
tsnrh,
@ -841,7 +876,7 @@ def autopickstation(wfstream, pickparam, verbose=False, iplot=0, fig_dict=None):
return picks
def iteratepicker(wf, NLLocfile, picks, badpicks, pickparameter):
def iteratepicker(wf, NLLocfile, picks, badpicks, pickparameter, fig_dict=None):
'''
Repicking of bad onsets. Uses theoretical onset times from NLLoc-location file.
@ -916,7 +951,7 @@ def iteratepicker(wf, NLLocfile, picks, badpicks, pickparameter):
print("zfac: %f => %f" % (zfac_old, pickparameter.get('zfac')))
# repick station
newpicks, fig = autopickstation(wf2pick, pickparameter)
newpicks = autopickstation(wf2pick, pickparameter, fig_dict=fig_dict)
# replace old dictionary with new one
picks[badpicks[i][0]] = newpicks
@ -931,4 +966,4 @@ def iteratepicker(wf, NLLocfile, picks, badpicks, pickparameter):
pickparameter.setParam(noisefactor=noisefactor_old)
pickparameter.setParam(zfac=zfac_old)
return picks, fig
return picks

View File

@ -39,39 +39,57 @@ class AutoPickThread(QThread):
class Thread(QThread):
def __init__(self, parent, func, arg=None, progressText=None):
message = Signal(str)
def __init__(self, parent, func, arg=None, progressText=None, pb_widget=None, redirect_stdout=False):
QThread.__init__(self, parent)
self.func = func
self.arg = arg
self.progressText = progressText
self.pbdlg = None
self.pb_widget = pb_widget
self.redirect_stdout = redirect_stdout
self.finished.connect(self.hideProgressbar)
self.showProgressbar()
def run(self):
if self.arg:
self.data = self.func(self.arg)
else:
self.data = self.func()
if self.redirect_stdout:
sys.stdout = self
try:
if self.arg:
self.data = self.func(self.arg)
else:
self.data = self.func()
self._executed = True
except Exception as e:
self._executed = False
self._executedError = e
print(e)
sys.stdout = sys.__stdout__
def __del__(self):
self.wait()
def showProgressbar(self):
if self.progressText:
self.pbdlg = QDialog(self.parent())
self.pbdlg.setModal(True)
if not self.pb_widget:
self.pb_widget = QDialog(self.parent())
self.pb_widget.setWindowFlags(Qt.SplashScreen)
self.pb_widget.setModal(True)
vl = QVBoxLayout()
pb = QProgressBar()
pb.setRange(0, 0)
vl.addWidget(pb)
vl.addWidget(QLabel(self.progressText))
self.pbdlg.setLayout(vl)
self.pbdlg.setWindowFlags(Qt.SplashScreen)
self.pbdlg.show()
self.pb_widget.setLayout(vl)
self.pb_widget.show()
def hideProgressbar(self):
if self.pbdlg:
self.pbdlg.hide()
if self.pb_widget:
self.pb_widget.hide()
def write(self, text):
self.message.emit(text)
def flush(self):
pass

View File

@ -1289,11 +1289,14 @@ class TuneAutopicker(QWidget):
self.init_eventlist()
self.init_stationlist()
self.init_figure_tabs()
self.add_parameter()
self.init_pbwidget()
self.add_parameters()
self.add_buttons()
self.add_log()
self.set_stretch()
self.resize(1280, 720)
self.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
self.figure_tabs.setCurrentIndex(0)
#self.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
#self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
def init_main_layouts(self):
@ -1309,12 +1312,15 @@ class TuneAutopicker(QWidget):
def init_eventlist(self):
self.eventBox = self.parent.createEventBox()
self.fill_eventbox()
self.eventBox.setCurrentIndex(1)
self.trace_layout.addWidget(self.eventBox)
self.eventBox.activated.connect(self.fill_stationbox)
self.eventBox.activated.connect(self.update_eventID)
def init_stationlist(self):
self.stationBox = QtGui.QComboBox()
self.trace_layout.addWidget(self.stationBox)
self.fill_stationbox()
def fill_stationbox(self):
fnames = self.parent.getWFFnames_from_eventbox(eventbox=self.eventBox)
@ -1325,19 +1331,23 @@ class TuneAutopicker(QWidget):
station = trace.stats.station
if not station in stations:
stations.append(str(station))
stations.sort()
for station in stations:
self.stationBox.addItem(str(station))
def init_figure_tabs(self):
self.main_tabs = QtGui.QTabWidget()
self.p_tabs = QtGui.QTabWidget()
self.s_tabs = QtGui.QTabWidget()
self.tune_layout.insertWidget(0, self.main_tabs)
self.init_tab_names()
def add_parameter(self):
def init_figure_tabs(self):
self.figure_tabs = QtGui.QTabWidget()
self.fill_figure_tabs()
def init_pbwidget(self):
self.pb_widget = QtGui.QWidget()
def add_parameters(self):
self.parameters = AutoPickParaBox(self.ap)
self.parameters.set_tune_mode(True)
self.update_eventID()
self.parameter_layout.addWidget(self.parameters)
self.parameter_layout.addWidget(self.pb_widget)
self.tune_layout.insertLayout(1, self.parameter_layout)
def add_buttons(self):
@ -1346,29 +1356,83 @@ class TuneAutopicker(QWidget):
self.trace_layout.addWidget(self.pick_button)
self.trace_layout.setStretch(0, 1)
def add_log(self):
self.listWidget = QtGui.QListWidget()
self.figure_tabs.insertTab(3, self.listWidget, 'log')
def fill_figure_tabs(self):
self.p_tabs = QtGui.QTabWidget()
self.s_tabs = QtGui.QTabWidget()
self.tune_layout.insertWidget(0, self.figure_tabs)
self.init_tab_names()
def fill_eventbox(self):
self.parent.fill_eventbox(self.eventBox, 'ref')
def get_current_event(self):
return self.eventBox.currentText().split('/')[-1]
def update_eventID(self):
self.parameters.boxes['eventID'].setText(self.get_current_event())
def add_log_item(self, text):
self.listWidget.addItem(text)
self.listWidget.scrollToBottom()
def call_picker(self):
self.ap = self.update_params()
self.ap = self.params_from_gui()
station = str(self.stationBox.currentText())
if not station:
self._warn('No station selected')
return
args = {'parameter': self.ap,
'station': station,
'fnames': 'None',
'iplot': 2,
'fig_dict': self.fig_dict}
self.ap_thread = Thread(self, autoPyLoT, arg=args, progressText='Picking trace...')
'fig_dict': self.fig_dict,
'locflag': 0}
for key in self.fig_dict.keys():
self.fig_dict[key].clear()
self.ap_thread = Thread(self, autoPyLoT, arg=args,
progressText='Picking trace...', pb_widget=self.pb_widget,
redirect_stdout=True)
self.enable(False)
self.ap_thread.message.connect(self.add_log_item)
self.ap_thread.finished.connect(self.finish_picker)
self.figure_tabs.setCurrentIndex(3)
self.ap_thread.start()
#picks = autoPyLoT(self.ap, fnames='None', iplot=2, fig_dict=self.fig_dict)
def finish_picker(self):
self.enable(True)
if not self.ap_thread._executed:
self._warn('Could not execute picker:\n{}'.format(
self.ap_thread._executedError))
return
self.picks = self.ap_thread.data
self.main_tabs.setParent(None)
self.init_figure_tabs()
if not self.picks:
self._warn('No picks found. See terminal output.')
return
#renew tabs
self.overview.setParent(None)
self.p_tabs.setParent(None)
self.s_tabs.setParent(None)
self.fill_figure_tabs()
self.set_stretch()
self.update.emit('Update')
self.figure_tabs.setCurrentIndex(0)
def update_params(self):
ap = self.parameters.update_params()
def enable(self, bool):
self.pick_button.setEnabled(bool)
self.parameters.setEnabled(bool)
self.eventBox.setEnabled(bool)
self.stationBox.setEnabled(bool)
self.overview.setEnabled(bool)
self.p_tabs.setEnabled(bool)
self.s_tabs.setEnabled(bool)
def params_from_gui(self):
ap = self.parameters.params_from_gui()
if self.parent:
self.parent._inputs = ap
return ap
@ -1382,10 +1446,11 @@ class TuneAutopicker(QWidget):
self.stb_names = ['aicARHfig', 'refSpick', 'el_S1pick', 'el_S2pick']
def fill_tabs(self, canvas_dict):
id = self.main_tabs.addTab(self.gen_tab_widget('Overview', canvas_dict['mainFig']), 'Overview')
self.main_tabs.setTabEnabled(id, bool(self.fig_dict['mainFig'].axes))
self.main_tabs.addTab(self.p_tabs, 'P')
self.main_tabs.addTab(self.s_tabs, 'S')
self.overview = self.gen_tab_widget('Overview', canvas_dict['mainFig'])
id = self.figure_tabs.insertTab(0, self.overview, 'Overview')
#self.figure_tabs.setTabEnabled(id, bool(self.fig_dict['mainFig'].axes))
self.figure_tabs.insertTab(1, self.p_tabs, 'P')
self.figure_tabs.insertTab(2, self.s_tabs, 'S')
self.fill_p_tabs(canvas_dict)
self.fill_s_tabs(canvas_dict)
try:
@ -1418,6 +1483,11 @@ class TuneAutopicker(QWidget):
v_layout.addWidget(NavigationToolbar2QT(canvas, self))
widget.setLayout(v_layout)
return widget
def _warn(self, message):
self.qmb = QtGui.QMessageBox(QtGui.QMessageBox.Icon.Warning,
'Warning', message)
self.qmb.show()
class PropertiesDlg(QDialog):
@ -1701,6 +1771,7 @@ class AutoPickParaBox(QtGui.QWidget):
self.setLayout(self.layout)
self.add_main_parameters_tab()
self.add_special_pick_parameters_tab()
self.params_to_gui()
self._toggle_advanced_settings()
def _init_sublayouts(self):
@ -1728,8 +1799,16 @@ class AutoPickParaBox(QtGui.QWidget):
else:
for b in box:
b.setEnabled(enable)
def init_boxes(self, parameter_names, defaults=False):
def set_tune_mode(self, bool):
keys = ['rootpath', 'datapath', 'database',
'eventID', 'invdir', 'nllocbin',
'nllocroot', 'phasefile',
'ctrfile', 'ttpatter', 'outpatter']
for key in keys:
self.boxes[key].setEnabled(not(bool))
def init_boxes(self, parameter_names):
grid = QtGui.QGridLayout()
for index1, name in enumerate(parameter_names):
@ -1739,22 +1818,15 @@ class AutoPickParaBox(QtGui.QWidget):
tooltip = default_item['tooltip']
tooltip += ' | type: {}'.format(default_item['type'])
if not type(default_item['type']) == tuple:
if defaults:
value = default_item['value']
else:
value = self.ap[name]
typ = default_item['type']
box = self.create_box(value, typ, tooltip)
box = self.create_box(typ, tooltip)
self.boxes[name] = box
elif type(default_item['type']) == tuple:
boxes = []
if defaults:
values = default_item['value']
else:
values = self.ap[name]
values = self.ap[name]
for index2, val in enumerate(values):
typ = default_item['type'][index2]
boxes.append(self.create_box(val, typ, tooltip))
boxes.append(self.create_box(typ, tooltip))
box = self.create_multi_box(boxes)
self.boxes[name] = boxes
label.setToolTip(tooltip)
@ -1762,25 +1834,15 @@ class AutoPickParaBox(QtGui.QWidget):
grid.addWidget(box, index1, 2)
return grid
def create_box(self, value, typ, tooltip):
def create_box(self, typ, tooltip):
if typ == str:
box = QtGui.QLineEdit()
box.setText(value)
elif typ == float:
box = QtGui.QDoubleSpinBox()
box.setMaximum(100*value)
box.setValue(value)
elif typ == int:
box = QtGui.QSpinBox()
box.setMaximum(100*value)
box.setValue(value)
elif typ == bool:
if value == 'True':
value = True
if value == 'False':
value = False
box = QtGui.QCheckBox()
box.setChecked(value)
else:
raise TypeError('Unrecognized type {}'.format(typ))
return box
@ -1847,14 +1909,37 @@ class AutoPickParaBox(QtGui.QWidget):
if seperator:
layout.addWidget(self.gen_h_seperator())
def update_params(self):
def params_from_gui(self):
for param in self.ap.get_all_para_names():
box = self.boxes[param]
value = self.getValue(box)
self.ap.checkValue(param, value)
self.ap.setParam(param, value)
self.ap.setParamKV(param, value)
return self.ap
def params_to_gui(self):
for param in self.ap.get_all_para_names():
box = self.boxes[param]
value = self.ap[param]
#self.ap.checkValue(param, value)
self.setValue(box, value)
def setValue(self, box, value):
if type(box) == QtGui.QLineEdit:
box.setText(value)
elif type(box) == QtGui.QSpinBox or type(box) == QtGui.QDoubleSpinBox:
box.setMaximum(100*value)
box.setValue(value)
elif type(box) == QtGui.QCheckBox:
if value == 'True':
value = True
if value == 'False':
value = False
box.setChecked(value)
elif type(box) == list:
for index, b in enumerate(box):
self.setValue(b, value[index])
def getValue(self, box):
if type(box) == QtGui.QLineEdit:
value = str(box.text())
@ -1869,7 +1954,6 @@ class AutoPickParaBox(QtGui.QWidget):
value = tuple(value)
return value
class ParametersTab(PropTab):
def __init__(self, parent=None, infile=None):
super(ParametersTab, self).__init__(parent)