From d90fd90911f65731c7db8f00f37286660c5389c8 Mon Sep 17 00:00:00 2001 From: tensionhead Date: Thu, 7 Jan 2021 01:23:42 +0100 Subject: [PATCH] fix amplitude envelope window size crash --- pyboat/__init__.py | 2 +- pyboat/core.py | 11 +++++-- pyboat/ui/data_viewer.py | 63 ++++++++++++++++++++-------------------- pyboat/ui/util.py | 2 +- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/pyboat/__init__.py b/pyboat/__init__.py index 160267e..0597c39 100644 --- a/pyboat/__init__.py +++ b/pyboat/__init__.py @@ -3,7 +3,7 @@ import sys, os import argparse -__version__ = '0.9.0' +__version__ = '0.9.1' # the object oriented API from .api import WAnalyzer diff --git a/pyboat/core.py b/pyboat/core.py index da96550..544a342 100644 --- a/pyboat/core.py +++ b/pyboat/core.py @@ -565,9 +565,13 @@ def sliding_window_amplitude(signal, window_size, dt, SGsmooth=True): # get the underlying array vector = np.array(signal) + if window_size > (len(signal) - 1) * dt: + window_size = (len(signal) - 1) * dt + print(f'Warning, setting window_size to {window_size}!') + # window size in sampling interval units window_size = int(window_size / dt) - + # has to be odd if window_size % 2 != 1: window_size = window_size + 1 @@ -619,7 +623,10 @@ def normalize_with_envelope(dsignal, window_size, dt): # mean subtraction signal = dsignal - dsignal.mean() - + if window_size > (len(signal) - 1) * dt: + window_size = (len(signal) - 1) * dt + print(f'Warning, setting window_size to {window_size}!') + # ampl. normalization env = sliding_window_amplitude(signal, window_size, dt) ANsignal = 1 / env * dsignal diff --git a/pyboat/ui/data_viewer.py b/pyboat/ui/data_viewer.py index 3b93e1c..5d37396 100644 --- a/pyboat/ui/data_viewer.py +++ b/pyboat/ui/data_viewer.py @@ -445,17 +445,16 @@ def toggle_trend(self, state): self.doPlot() def toggle_envelope(self, state): - - # trying to plot the trend - if state == Qt.Checked: - window_size = self.get_wsize(self.wsize_edit) - if not window_size: - self.cb_envelope.setChecked(False) - self.cb_use_envelope.setChecked(False) - - + # signal selected? if self.signal_id: + # trying to plot the envelope + if state == Qt.Checked: + window_size = self.get_wsize(self.wsize_edit) + if not window_size: + self.cb_envelope.setChecked(False) + self.cb_use_envelope.setChecked(False) + self.doPlot() # connected to unit_edit @@ -541,15 +540,38 @@ def get_wsize(self, wsize_edit): msgBox = QMessageBox() msgBox.setWindowTitle('Missing Parameter') msgBox.setText( - 'Envelope parameter not set properly, specify a sliding window size!') + 'Amplitude envelope parameter not set, specify a sliding window size!') + msgBox.exec() if self.debug: print("window_size ValueError", window_size) return None + + if window_size / self.dt < 4: + + msgBox = QMessageBox() + msgBox.setWindowTitle("Out of Bounds") + msgBox.setText( + f'''Minimal sliding window size for envelope estimation is {4*self.dt} {self.time_unit}!''') + msgBox.exec() + + return None + + if window_size / self.dt > self.df.shape[0]: + max_window_size = self.df.shape[0] * self.dt + + msgBox = QMessageBox() + msgBox.setWindowTitle("Out of Bounds") + msgBox.setText( + f"Maximal sliding window size for envelope estimation is {max_window_size:.2f} {self.time_unit}!") + msgBox.exec() + + return None if self.debug: print("window_size set to:", window_size) + return window_size def set_initial_periods(self, force=False): @@ -809,27 +831,6 @@ def calc_envelope(self): if self.debug: print("Calculating envelope with window_size = ", window_size) - if window_size / self.dt < 4: - - msgBox = QMessageBox() - msgBox.setWindowTitle("Out of Bounds") - msgBox.setText( - f'''Minimal sliding window size is {4*self.dt}{self.time_unit}!''') - msgBox.exec() - - window_size = None - return - - if window_size / self.dt > self.df.shape[0]: - max_window_size = self.df.shape[0] * self.dt - - msgBox = QMessageBox() - msgBox.setWindowTitle("Out of Bounds") - msgBox.setText( - f"Maximum sliding window size is {max_window_size:.2f} {self.time_unit}!") - msgBox.exec() - - return # cut of frequency set?! if self.get_T_c(self.T_c_edit): diff --git a/pyboat/ui/util.py b/pyboat/ui/util.py index 79a4838..8ec83d3 100644 --- a/pyboat/ui/util.py +++ b/pyboat/ui/util.py @@ -32,7 +32,7 @@ 'window_size' : None, 'Tmin' : None, 'Tmax' : None, - 'nT' : 100, + 'nT' : 200, 'pow_max' : None }