From 221743fe20f6fdcb4d631fea7df1518162dab799 Mon Sep 17 00:00:00 2001 From: Sebastian Wehling-Benatelli Date: Mon, 10 Apr 2023 19:14:23 +0200 Subject: [PATCH] bugfix: correct erroneous and add new doctests --- PyLoT.py | 15 +-------- pylot/core/pick/utils.py | 4 +-- pylot/core/util/utils.py | 64 +++++++++++++++++++++++++++++--------- pylot/core/util/widgets.py | 40 ++---------------------- 4 files changed, 55 insertions(+), 68 deletions(-) diff --git a/PyLoT.py b/PyLoT.py index 6e881d77..ac6ff10c 100755 --- a/PyLoT.py +++ b/PyLoT.py @@ -496,6 +496,7 @@ class MainWindow(QMainWindow): icon=eventlist_xml_icon, tip='Create an Eventlist from a XML File') self.eventlist_xml_action.setEnabled(False) + printAction = self.createAction(self, "&Print event ...", self.show_event_information, QKeySequence.Print, print_icon, @@ -1403,8 +1404,6 @@ class MainWindow(QMainWindow): for id, event in enumerate(self.project.eventlist): event_path = event.path - #phaseErrors = {'P': self._inputs['timeerrorsP'], - # 'S': self._inputs['timeerrorsS']} man_au_picks = {'manual': event.pylot_picks, 'auto': event.pylot_autopicks} @@ -1473,7 +1472,6 @@ class MainWindow(QMainWindow): for picktype, item_np in [('manual', item_nmp), ('auto', item_nap)]: npicks_str = f"{npicks[picktype]['P']}|{npicks[picktype]['S']}" - #npicks_str += f"({npicks_total[picktype]['P']}/{npicks_total[picktype]['S']})" item_np.setText(npicks_str) item_ref = QStandardItem() # str(event_ref)) @@ -1718,8 +1716,6 @@ class MainWindow(QMainWindow): for tr in self.get_data().wfdata.select(component=ch).traces: traces[tr.stats.station][ch] = tr - names.sort() - a = self.get_current_event() print (self.get_data().wfdata.traces[0]) @@ -1729,14 +1725,6 @@ class MainWindow(QMainWindow): self.tabs.setCurrentIndex(3) figCanvas = test.makeSpecFig(direction=self.dispComponent, height = height, width = width, parent = self.tabs.widget) return figCanvas - #self.spectro_layout.addWidget() - # self.get_data().wfdata.spectrogram() - # self.tabs.addTab(figCanvas, 'Spectrogram') - # self.tabs[3] = figCanvas - # self.refreshTabs() - # test.show() - - def compareMulti(self): if not self.compareoptions: @@ -1896,7 +1884,6 @@ class MainWindow(QMainWindow): # which will read in data input twice. Therefore current tab is changed to 0 # in loadProject before calling this function. self.fill_eventbox() - #print(f'{self.get_current_event()=}') plotted = False if self.tabs.currentIndex() == 2: self.init_event_table() diff --git a/pylot/core/pick/utils.py b/pylot/core/pick/utils.py index e2bb8e7a..def27b8a 100644 --- a/pylot/core/pick/utils.py +++ b/pylot/core/pick/utils.py @@ -275,7 +275,7 @@ def fmpicker(Xraw, Xfilt, pickwin, Pick, iplot=0, fig=None, linecolor='k'): try: P1 = np.polyfit(xslope1, xraw[islope1], 1) datafit1 = np.polyval(P1, xslope1) - except Exception as e: + except ValueError as e: print("fmpicker: Problems with data fit! {}".format(e)) print("Skip first motion determination!") return FM @@ -321,7 +321,7 @@ def fmpicker(Xraw, Xfilt, pickwin, Pick, iplot=0, fig=None, linecolor='k'): try: P2 = np.polyfit(xslope2, xfilt[islope2], 1) datafit2 = np.polyval(P2, xslope2) - except Exception as e: + except ValueError as e: emsg = 'fmpicker: polyfit failed: {}'.format(e) print(emsg) return FM diff --git a/pylot/core/util/utils.py b/pylot/core/util/utils.py index bb64fdda..7cd53465 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -333,18 +333,37 @@ def get_bool(value): """ Convert string representations of bools to their true boolean value :param value: - :type value: str, bool + :type value: str, bool, int, float :return: true boolean value :rtype: bool + + >>> get_bool(True) + True + >>> get_bool(False) + False + >>> get_bool(0) + False + >>> get_bool(0.) + False + >>> get_bool(0.1) + True + >>> get_bool(2) + True + >>> get_bool(-1) + False + >>> get_bool(-0.3) + False """ - if type(value) is bool: + if type(value) == bool: return value elif value in ['True', 'true']: return True elif value in ['False', 'false']: return False + elif value > 0. or value > 0: + return True else: - return bool(value) + return False def four_digits(year): """ @@ -355,8 +374,8 @@ def four_digits(year): :return: four digit year correspondent :rtype: int - >>> four_digits(20) - 1920 + >>> four_digits(75) + 1975 >>> four_digits(16) 2016 >>> four_digits(00) @@ -513,6 +532,11 @@ def is_executable(fn): :param fn: path to the file to be tested :return: True or False :rtype: bool + + >>> is_executable('/bin/ls') + True + >>> is_executable('/var/log/system.log') + False """ return os.path.isfile(fn) and os.access(fn, os.X_OK) @@ -619,13 +643,13 @@ def find_horizontals(data): :param data: waveform data :type data: `obspy.core.stream.Stream` :return: components list - :rtype: list + :rtype: List(str) ..example:: >>> st = read() >>> find_horizontals(st) - [u'N', u'E'] + ['N', 'E'] """ rval = [] for tr in data: @@ -819,6 +843,19 @@ def trim_station_components(data, trim_start=True, trim_end=True): return data +def merge_stream(stream): + gaps = stream.get_gaps() + if gaps: + # list of merged stations (seed_ids) + merged = ['{}.{}.{}.{}'.format(*gap[:4]) for gap in gaps] + stream.merge(method=1) + print('Merged the following stations because of gaps:') + for merged_station in merged: + print(merged_station) + + return stream, gaps + + def check4gapsAndRemove(data): """ check for gaps in Stream and remove them @@ -839,12 +876,12 @@ def check4gapsAndRemove(data): return data -def check_for_gaps_and_merge(data): +def check4gapsAndMerge(data): """ check for gaps in Stream and merge if gaps are found :param data: stream of seismic data :type data: `~obspy.core.stream.Stream` - :return: data stream, gaps returned from obspy get_gaps + :return: data stream :rtype: `~obspy.core.stream.Stream` """ gaps = data.get_gaps() @@ -855,7 +892,7 @@ def check_for_gaps_and_merge(data): for merged_station in merged: print(merged_station) - return data, gaps + return data def check4doubled(data): @@ -927,11 +964,11 @@ def get_possible_pylot_eventfile_extensions(event, fext): def get_stations(data): """ - Get list of all station names in data stream + Get list of all station names in data-stream :param data: stream containing seismic traces :type data: `~obspy.core.stream.Stream` :return: list of all station names in data, no duplicates - :rtype: list of str + :rtype: List(str) """ stations = [] for tr in data: @@ -987,7 +1024,7 @@ def check4rotated(data, metadata=None, verbosity=1): if len(wfs_in) < 3: print(f"Stream {wfs_in=}, has not enough components to rotate.") return wfs_in - + # check if any traces in this station need to be rotated trace_ids = [trace.id for trace in wfs_in] if not rotation_required(trace_ids): @@ -1155,7 +1192,6 @@ def identifyPhase(phase): return False -@lru_cache def identifyPhaseID(phase): """ Returns phase id (capital P or S) diff --git a/pylot/core/util/widgets.py b/pylot/core/util/widgets.py index a9a3e6f4..297d47e3 100644 --- a/pylot/core/util/widgets.py +++ b/pylot/core/util/widgets.py @@ -897,8 +897,6 @@ class WaveformWidgetPG(QtWidgets.QWidget): else: st_select = wfdata - # st_select, gaps = check_for_gaps_and_merge(st_select) #MP MP commented because probably done twice - # list containing tuples of network, station, channel (for sorting) nslc = [] for trace in st_select: @@ -5788,10 +5786,7 @@ class ChooseWaveFormWindow(QWidget): def submit(self): matplotlib.pyplot.close(self.currentSpectro) t = self.chooseBoxTraces.currentText() + " " + self.chooseBoxComponent.currentText() - #self.currentSpectro = self.traces[ - # self.chooseBoxTraces.currentText()[3:]][self.chooseBoxComponent.currentText()].spectrogram(show=False, title=t) - #self.currentSpectro.show() - applyFFT() + self.applyFFT() def applyFFT(self, trace): tra = self.traces[self.chooseBoxTraces.currentText()[3:]]['Z'] @@ -5886,21 +5881,6 @@ class SpectrogramTab(QWidget): colors='m', linestyles='dashed', linewidth=2) - # Different axis settings for visual improvements - # axis[i].set_xlim(left=0, right=end - start) - # axis[i].spines['top'].set_visible(False) - # axis[i].spines['right'].set_visible(False) - # # axis[i].spines['left'].set_visible(False) - - # axis[i].tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False) - # if not (len(self.traces) == i - 1): - # axis[i].spines['bottom'].set_visible(False) - # axis[i].set_yticks([]) - # axis[i].set_ylabel(t, loc='center', rotation='horizontal') - - #ax.axhline(n, color="0.5", lw=0.5) - - grams.append(tra.spectrogram(show=False, axes=axis[i])) i+=1 @@ -5909,24 +5889,8 @@ class SpectrogramTab(QWidget): fC = FigureCanvas(figure) return fC - #for t in self.traces: - # tra = self.traces[t]['Z'] - # transformed = abs(np.fft.rfft(tra.data)) - # axis[i].plot(transformed, label=t) - # # axis[i].tick_params(labelbottom=False) - # axis[i].spines['top'].set_visible(False) - # axis[i].spines['right'].set_visible(False) - # axis[i].spines['left'].set_visible(False) - # if not (len(self.traces) == i - 1): - # axis[i].spines['bottom'].set_visible(False) - # axis[i].set_yticks([]) - # axis[i].set_ylabel(t, loc='center', rotation='horizontal') - # # axis[i].axis('off') - # i += 1 - # # self.applyFFTs(t) - if __name__ == '__main__': import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file