Commit e6c8c753 authored by Ivo Peterek's avatar Ivo Peterek

ENH: check user input for smoothing options. #8

parent d472e1e9
......@@ -39,6 +39,7 @@ class Window(QtWidgets.QDialog):
self.use_logscaleY = False
self.samples_data = []
self.samples_data_lables = []
self.max_data_len = 0
self.samples_ylabel = "Power [W]"
self.min_sample_val = float(sys.maxsize)
self.max_sample_val = float(0)
......@@ -77,7 +78,8 @@ class Window(QtWidgets.QDialog):
self.smoothpolyOrderLabel = QtWidgets.QLabel("Polynomial order:")
self.smoothpolyOrderLabel.setFixedWidth(130)
self.smoothpolyOrderSpinBox = QtWidgets.QSpinBox()
self.smoothpolyOrderSpinBox.setMinimum(2)
self.smoothpolyOrderSpinBox.setMinimum(1)
self.smoothpolyOrderSpinBox.setValue(3)
self.smoothpolyOrderSpinBox.setMaximum(2147483647)
self.smoothButton = QtWidgets.QPushButton('Smooth samples')
self.smoothButton.clicked.connect(self.smoothSamples)
......@@ -206,8 +208,10 @@ class Window(QtWidgets.QDialog):
self.smoothButton.setFixedWidth(130)
hlayout3.addWidget(self.smoothpolyOrderLabel)
hlayout3.addWidget(self.smoothpolyOrderSpinBox)
self.smoothpolyOrderSpinBox.editingFinished.connect(self.spinBoxCheck)
hlayout3.addWidget(self.smoothWindowSizeLabel)
hlayout3.addWidget(self.smoothWindowSizeSpinBox)
self.smoothWindowSizeSpinBox.editingFinished.connect(self.spinBoxCheck)
hlayout3.addWidget(self.smoothButton)
layout.addLayout(hlayout3)
......@@ -248,6 +252,28 @@ class Window(QtWidgets.QDialog):
# self.plot()
self.resize(800,800)
def spinBoxCheck(self):
if self.samples_data:
if self.smoothWindowSizeSpinBox.value() % 2 == 0:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Invalid input', 'Window size must be odd number.',
QtWidgets.QMessageBox.Ok).exec()
self.smoothWindowSizeSpinBox.setValue(self.smoothWindowSizeSpinBox.value()-1)
return 1
if self.max_data_len < self.smoothWindowSizeSpinBox.value():
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Invalid input', 'Maximum window length is number of '
'samples.',
QtWidgets.QMessageBox.Ok).exec()
self.smoothWindowSizeSpinBox.setValue(self.max_data_len)
return 1
if self.smoothWindowSizeSpinBox.value() <= self.smoothpolyOrderSpinBox.value():
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Invalid input', 'Polynomial order must be less than '
'window length.',
QtWidgets.QMessageBox.Ok).exec()
self.smoothpolyOrderSpinBox.setValue(self.smoothWindowSizeSpinBox.value() - 1)
return 1
return 0
def dotSizeChanged(self):
self.plot()
......@@ -264,6 +290,7 @@ class Window(QtWidgets.QDialog):
def clearCanvas(self):
self.samples_data = []
self.samples_data_lables = []
self.max_data_len = 0
self.min_sample_val = float(99999999999999)
self.max_sample_val = float(0)
self.ax.clear()
......@@ -297,44 +324,50 @@ class Window(QtWidgets.QDialog):
self.plot()
def smoothSamples(self):
window_length = self.smoothWindowSizeSpinBox.value()
poly_order = self.smoothpolyOrderSpinBox.value()
smooth_samples_data = []
for line in self.samples_data:
tmp_line = []
sample_numbers = []
sample_vals = []
zero_numbers = []
for sample in line:
# Take out zeros
if sample[1] == 0:
zero_numbers.append((sample[0]))
else:
sample_numbers.append(sample[0])
sample_vals.append(sample[1])
smoothed_sample_vals = signal.savgol_filter(sample_vals, window_length, poly_order)
for i, number in enumerate(sample_numbers):
if smoothed_sample_vals[i] < self.min_sample_val and smoothed_sample_vals[i] != 0:
self.min_sample_val = smoothed_sample_vals[i]
if smoothed_sample_vals[i] > self.max_sample_val:
self.max_sample_val = smoothed_sample_vals[i]
if number - 1 in zero_numbers: # Put zeros back
tmp_line.append((number - 1, 0))
tmp_line.append((number, smoothed_sample_vals[i]))
if number+1 in zero_numbers: # Put zeros back
tmp_line.append((number+1, 0))
smooth_samples_data.append(tmp_line)
self.samples_data = smooth_samples_data
self.plot()
if self.samples_data:
if self.spinBoxCheck() == 0:
window_length = self.smoothWindowSizeSpinBox.value()
poly_order = self.smoothpolyOrderSpinBox.value()
smooth_samples_data = []
for line in self.samples_data:
tmp_line = []
sample_numbers = []
sample_vals = []
zero_numbers = []
for sample in line:
# Take out zeros
if sample[1] == 0:
zero_numbers.append((sample[0]))
else:
sample_numbers.append(sample[0])
sample_vals.append(sample[1])
smoothed_sample_vals = signal.savgol_filter(sample_vals, window_length, poly_order)
for i, number in enumerate(sample_numbers):
if smoothed_sample_vals[i] < self.min_sample_val and smoothed_sample_vals[i] != 0:
self.min_sample_val = smoothed_sample_vals[i]
if smoothed_sample_vals[i] > self.max_sample_val:
self.max_sample_val = smoothed_sample_vals[i]
if number - 1 in zero_numbers: # Put zeros back
tmp_line.append((number - 1, 0))
tmp_line.append((number, smoothed_sample_vals[i]))
if number+1 in zero_numbers: # Put zeros back
tmp_line.append((number+1, 0))
smooth_samples_data.append(tmp_line)
self.samples_data = smooth_samples_data
self.plot()
else:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Invalid input', 'Please add data to plot before '
'smoothing.',
QtWidgets.QMessageBox.Ok).exec()
def round_up_to_odd(self, f):
return int(np.ceil(f) // 2 * 2 + 1)
def round_down_to_odd(self, f):
return int(np.ceil(f) // 2 * 2 + 1 -2)
def clustering(self):
self.clearCanvas()
......@@ -360,8 +393,8 @@ class Window(QtWidgets.QDialog):
increasing = True
# Smooth samples
smooth_window_size = self.round_up_to_odd(int(len(samples)))-2
smooth_window_size_small = self.round_up_to_odd(int(0.1*len(samples)))-2
smooth_window_size = self.round_down_to_odd(int(len(samples)))
smooth_window_size_small = self.round_down_to_odd(int(0.1*len(samples)))
poly_order = 3
smooth_samples = signal.savgol_filter(engs, smooth_window_size_small, poly_order)
smooth_samples = signal.savgol_filter(smooth_samples, smooth_window_size, poly_order)
......@@ -460,6 +493,7 @@ class Window(QtWidgets.QDialog):
def load_samples_from_csv(self, csv_path):
new_samples_data = []
added_zeros_count = 0
csv_file = open(csv_path, "r")
reading_samples = False
next_call = False
......@@ -472,10 +506,8 @@ class Window(QtWidgets.QDialog):
reading_samples = True
continue
if reading_samples:
# iter += 1
# if iter >= 100:
# break
if next_call:
added_zeros_count += 1
line_data = (power_sample_number+1, float(0))
new_samples_data.append(line_data)
line = line.split(",")
......@@ -487,10 +519,14 @@ class Window(QtWidgets.QDialog):
self.max_sample_val = power_sample_value
line_data = (power_sample_number, power_sample_value)
if next_call:
added_zeros_count += 1
new_samples_data.append((power_sample_number-1, float(0)))
next_call = False
new_samples_data.append(line_data)
csv_file.close()
if self.max_data_len < len(new_samples_data)-added_zeros_count: # true length of data
self.max_data_len = len(new_samples_data)-added_zeros_count
self.samples_data.append(new_samples_data)
def plot(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment