-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
306 lines (236 loc) · 11.2 KB
/
main.py
File metadata and controls
306 lines (236 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
# License information
# PyQt GPL - https://www.gnu.org/licenses/gpl-3.0.en.html
# Matplotlib, Numpy, Pandas and Jinja2 BSD - https://opensource.org/licenses/BSD-3-Clause
# Python PSF - https://docs.python.org/3/license.html
# GPL will override BSD licenses
# For commercial use obtain a commercial license at https://riverbankcomputing.com/commercial/buy
# NOTE: No need for commercial license for internal use or if
# the sourcecode is made public.
# TODO pyinstaller -> https://stackoverflow.com/questions/5458048/how-can-i-make-a-python-script-standalone-executable-to-run-without-any-dependen
import os
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as Navi
matplotlib.use('Qt5Agg')
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QFileDialog
# Own libs
import calculation as pid
import report_gen as save_report
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
self.fig = Figure(figsize=(width, height), dpi=dpi)
self.fig.set_tight_layout('pad')
self.axes = self.fig.add_subplot(111)
super(MplCanvas, self).__init__(self.fig)
def save(self, path, values):
try:
filename = os.path.splitext((os.path.basename(path)))[0]
self.fig.savefig(f'./report/img/{filename}.png')
save_report.render_html(filename, values)
return [True, filename]
except Exception as e:
return [False, e]
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('GUI/layout.ui', self)
self.show()
# Bindings
self.pB_clear.clicked.connect(self.clear)
self.actionClose.triggered.connect(self.clear)
self.pB_calculate.clicked.connect(self.calculating)
self.pB_reset.clicked.connect(self.reset_onclick)
# self.pB_calculate.clicked.connect(self.get_file)
self.actionImport_CSV_file.triggered.connect(self.get_file)
self.actionExit.triggered.connect(MainWindow.close)
self.MaxValueSlider.valueChanged.connect(self.change_sb_max)
self.ResponseValueSlider.valueChanged.connect(self.change_sb_resp)
self.sB_max.valueChanged.connect(self.change_slider_max)
self.sB_theta.valueChanged.connect(self.change_slider_resp)
self.pB_save.clicked.connect(self.save)
self.sc = MplCanvas(self, width=5, height=4, dpi=100)
# Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second.
self.toolbar = Navi(self.sc, MainWindow)
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.toolbar)
self.layout.addWidget(self.sc)
# Create a placeholder widget to hold our toolbar and canvas.
self.wi_plot.setLayout(self.layout)
self.k = False
self.ti = False
self.first = True
def save(self):
# prepeare a list of values that will be sent to the html rendrer
values = self.step_resp.get_vars()
resp = self.sc.save(self.filname, values)
if resp[0]:
msg = f"Report generated and saved as:\nreport\{resp[1]}.html"
else:
msg = f"Error saving the file: {resp[1]}"
self.write_to_box(msg)
def reset_onclick(self):
self.reset_values()
self.step = pid.read(self.filname)
# Max value
def change_sb_max(self):
self.sB_max.setValue((self.MaxValueSlider.value()/100))
self.calculating()
def change_slider_max(self):
self.MaxValueSlider.setValue(int(self.sB_max.value()*100))
# Theta
def change_sb_resp(self):
self.sB_theta.setValue(self.ResponseValueSlider.value())
def change_slider_resp(self):
self.ResponseValueSlider.setValue(self.sB_theta.value())
self.calculating()
def get_file(self):
# If loading a second CSV clear the last one
if self.k:
self.reset_values()
self.clear()
self.filname = QFileDialog.getOpenFileName(filter="csv (*.csv)")[0]
if self.filname:
self.step = pid.read(self.filname)
self.update_combo_box(self.step.columns)
self.suggest_values()
self.plot_file()
def suggest_values(self):
# Set min and max values to spin box when step var is chosen
self.write_to_box(self.step[str(self.StepVarBox.currentText())].max())
self.sB_from.setValue(int(self.step[str(self.StepVarBox.currentText())].min()))
self.sB_to.setValue(int(self.step[str(self.StepVarBox.currentText())].max()))
time = self.step.index.to_series().diff().value_counts()
self.Sb_sampling_time.setValue(abs(time.index[0].total_seconds()))
def write_to_box(self, msg, cls=False):
self.resultBox.clear()
self.resultBox.insertPlainText(str(msg))
def update_combo_box(self, values):
self.StepVarBox.clear()
self.ResponseVarBox.clear()
self.ResponseVarBox.addItems(values)
self.StepVarBox.addItems(values)
def calculating(self):
# Getting values
# TODO Check if values are set
try:
step_var = str(self.StepVarBox.currentText())
response_var = str(self.ResponseVarBox.currentText())
step_from = int(self.sB_from.value())
step_to = int(self.sB_to.value())
step_done = [step_from, step_to]
samp = int(self.Sb_sampling_time.value())
except Exception as e:
print(f'error {e}')
# Calculating
# First run
try:
if not self.k:
curr_step = pid.find_step(df = self.step, step = step_done, name=step_var)
self.step_resp = pid.step_analytics(df = curr_step, sampling_time = samp, step_from_to = step_done, factor = 0.1)
self.step_resp.measured_value = response_var
self.step_resp.gain = step_var
self.step_resp.calculate(band=False)
self.MaxValueSlider.setMaximum(int(self.step_resp.max_val * 1.1*100))
self.MaxValueSlider.setMinimum(int(self.step_resp.max_val * 0.9*100))
# self.MaxValueSlider.setSliderPosition(int(self.step_resp.max_val))
# Recalc with current values
else:
max_val = self.sB_max.value()
theta = self.sB_theta.value()
self.step_resp.re_calculate(max_val, theta)
except Exception as e:
print(f'error {e}')
try:
# Result
self.plot_calc()
self.sB_theta.setValue(int(self.step_resp.theta))
self.sB_max.setValue(self.step_resp.max_val)
self.k = self.step_resp.k_c
self.sb_kp.setValue(self.step_resp.k_c)
self.sb_ti.setValue(self.step_resp.t_i)
self.write_to_box(self.step_resp)
except Exception as e:
print(f'error {e}')
def plot_calc(self):
# Clear old plot
self.clear_plot()
self.sc.axes.grid(color='oldlace')
x = self.step_resp.step_df.loc[self.step_resp.step_df[self.step_resp.measured_value]==self.step_resp.max_val]
self.sc.axes.plot(self.step_resp.step_df[self.step_resp.measured_value], color="r")
# Polotting point for when we choose step response
# # Plotting vertical and horisontal lines for 63% point
self.sc.axes.hlines(self.step_resp.prosent63[1], xmin=0,
xmax=(-self.step_resp.prosent63[0] - 1) * self.step_resp._sampling_time, color="grey",
linestyles='dashed')
self.sc.axes.vlines((-self.step_resp.prosent63[0] - 1) * self.step_resp._sampling_time,
ymin=self.step_resp.start[1] * 0.99, ymax=self.step_resp.prosent63[1],
color="grey",
linestyles='dashed')
# THETA
self.sc.axes.vlines(((-self.step_resp.response - 1) * self.step_resp._sampling_time),
ymin=self.step_resp.start[1] * 0.99,
ymax=self.step_resp.step_df.iloc[self.step_resp.response][self.step_resp.measured_value],
color="grey", linestyles='dashed')
self.sc.axes.vlines((self.step_resp.step_df.iloc[self.step_resp.start[0]]["Time"]),
ymin=self.step_resp.start[1] * 0.99,
ymax=self.step_resp.start[1] * 1.1,
color="blue", linestyles='dashdot')
self.sc.axes.axhline(y=self.step_resp.max_val, color='blue', linestyle='-')
# self.sc.axes.axhline(y=self.step_resp.dY * 0.95 + self.step_resp.start[1], color='y', linestyle='-')
self.sc.axes.text(-self.step_resp.start[1] * self.step_resp._sampling_time * 1.5, self.step_resp.max_val,
f'Max val = {self.step_resp.max_val:.2f} ', fontsize=6, va="top")
# Value at Theta
self.sc.axes.text(-self.step_resp.response * self.step_resp._sampling_time * 1.2,
self.step_resp.step_df.iloc[self.step_resp.response][self.step_resp.measured_value],
f' {self.step_resp.step_df.iloc[self.step_resp.response][self.step_resp.measured_value]:.2f} ',
fontsize=6, va="center")
self.sc.axes.text((-self.step_resp.response * self.step_resp._sampling_time) + 5, self.step_resp.start[1],
f'ϴ: {self.step_resp.theta:.0f} ', fontsize=6, va="top", ha='right')
self.sc.axes.text(((-self.step_resp.prosent63[0]) * self.step_resp._sampling_time) + 5, self.step_resp.start[1],
f'τ: {self.step_resp.tau:.0f}', fontsize=6, va="bottom", ha='right')
self.sc.axes.text(((-self.step_resp.prosent63[0]) * self.step_resp._sampling_time) - 5,
self.step_resp.prosent63[1], f'63% value\n{self.step_resp.prosent63[1]:.2f}', fontsize=6,
va="top")
self.sc.axes.plot()
self.sc.draw()
def plot_file(self):
# Clear old plot
self.clear_plot()
self.sc.axes.grid(color='oldlace')
self.sc.axes.plot(self.step)
self.sc.draw()
def clear(self):
self.sB_from.clear()
self.sB_to.clear()
self.Sb_sampling_time.clear()
self.clear_plot()
self.StepVarBox.clear()
self.ResponseVarBox.clear()
self.MaxValueSlider.setMaximum(100)
self.MaxValueSlider.setMinimum(0)
self.sB_theta.clear()
self.sB_max.clear()
self.k = False
self.ti = False
self.filname = ""
self.write_to_box("")
def reset_values(self):
self.clear_plot()
self.MaxValueSlider.setMaximum(100)
self.MaxValueSlider.setMinimum(0)
self.sB_theta.clear()
self.sB_max.clear()
self.k = False
self.ti = False
self.step = ""
def clear_plot(self):
self.sc.axes.cla()
self.sc.draw()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui()
sys.exit(app.exec_())