-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.py
434 lines (348 loc) · 15.2 KB
/
Process.py
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Process.py
# by Robin Prillwitz
# 11.2.2020
#
import os
import sys
import copy
from pathlib import Path
from PyQt5 import QtGui, QtCore, QtWidgets, QtTest
import pyqtgraph as pg
import pandas as pd
import numpy as np
from scipy import integrate
from scipy.interpolate import interp1d
from scipy.ndimage.filters import gaussian_filter1d
import Config
import Cursor
import ListItem
import ListWidget
# Handles one data file and its processing
# inherits from QWidget to emit signals
class Process(ListItem.ListItem):
def __init__(self, color, config=None, parent=None):
super().__init__(color, config=config, parent=parent)
self.config["operation"] = "Addition"
self.__initSettings()
self.__showListItem()
self.data = None
self.recalculate()
self.updatePlot()
self.__toggleSettings()
self.updateUI()
def __copy__(self):
newPc = Process(self.config["color"], self.config, self.parent)
for item in self.fileList.list:
newPc.fileList.addItem(copy.copy(item))
return newPc
# displays the list item in the file list
def __showListItem(self):
Font = QtGui.QFont()
Font.setItalic(True)
self.box = QtWidgets.QFrame()
self.Hlayout = QtWidgets.QHBoxLayout()
self.Vlayout = QtWidgets.QVBoxLayout()
self.enable = QtWidgets.QCheckBox()
self.enable.setChecked(self.config["enabled"])
self.enable.stateChanged.connect(self.setEnabled)
self.enable.setObjectName("enable_checkbox")
self.enable.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.enable.setStyleSheet(
"background-color: hsv({:d},{:d}%,{:d}%); color: black;".format(
self.config["color"][0], self.config["color"][1], self.config["color"][2]))
self.Hlayout.addWidget(self.enable)
self.label = QtWidgets.QLabel("Prozess")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setFont(Font)
self.Hlayout.addWidget(self.label)
self.Hlayout.addStretch(1)
self.operationBox = QtWidgets.QComboBox()
self.operationBox.addItems(["Addition", "Subtraktion", "Multiplikation", "Division"])
self.operationBox.setCurrentText(self.config["operation"])
self.operationBox.currentTextChanged.connect(lambda x, who="operation": self.applyChange(x, who))
self.Hlayout.addWidget(self.operationBox)
self.settingsBtn = QtWidgets.QPushButton(QtGui.QIcon(Config.getResource("assets/left.png")), "")
self.settingsBtn.clicked.connect(self.__toggleSettings)
self.settingsBtn.setFlat(True)
self.settingsBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.settingsBtn.setIconSize(QtCore.QSize(16, 16))
self.settingsBtn.setFixedSize(30, 30)
self.Hlayout.addWidget(self.settingsBtn)
self.Hlayout.addStrut(40)
self.Hlayout.setAlignment(QtCore.Qt.AlignCenter)
self.Hlayout.setSizeConstraint(QtWidgets.QLayout.SetMinimumSize)
self.Hlayout.setContentsMargins(10,0,10,0)
self.Vlayout.setContentsMargins(0,0,0,0)
self.box.setLayout(self.Hlayout)
self.Vlayout.addWidget(self.box)
self.Vlayout.addWidget(self.settings)
self.fileList = ListWidget.DeselectableListWidget(self)
self.fileList.setObjectName("process-list")
self.fileList.setMinimumHeight(self.Hlayout.sizeHint().height() * 1.5)
self.fileList.setMaximumHeight(self.Hlayout.sizeHint().height() * 2)
self.fileList.setMinimumWidth(200)
self.fileList.sigUpdateUI.connect(self.updateUI)
self.fileList.sigCalc.connect(self.update)
self.Vlayout.addWidget(self.fileList)
self.frame.setLayout(self.Vlayout)
# applies all calculations and interpolation
def recalculate(self):
dlg = pg.ProgressDialog("Berechnung", cancelText=None, busyCursor=False, disable=False, wait=250)
dlg.setValue(0)
if len(self.fileList.list) == 0:
self.ignore = True
self.config["cursorEnabled"] = False
self.modData = self.interpData = pd.DataFrame({'x': [0, 0.00001], 'y': [0, 0]})
return
self.ignore = False
self.config["cursorEnabled"] = True
# find entire range of all items
start = np.Inf
end = -np.Inf
points = 0
dlg += 5
QtTest.QTest.qWait(20)
for index, item in enumerate(self.fileList.list):
dlg += 1
if item.ignore:
break
imin = item.interpData["x"].min()
imax = item.interpData["x"].max()
pts = len(item.interpData["x"])
if imin < start:
start = imin
if imax > end:
end = imax
if pts > points:
points = pts
dlg += 5
if start == np.Inf or end == -np.Inf:
return
# generate x points in range
x = np.linspace(
start,
end,
points
)
y = np.full(len(x), np.nan)
dlg += 5
QtTest.QTest.qWait(20)
for item in self.fileList.list:
dlg += 1
if item.ignore:
break
if isinstance(item, Cursor.Cursor):
values = np.full(len(x), item.config["yOffset"])
else:
if item.config["interpolation"] == "keine" or item.config["interpolation"] == "linear":
interpolation = "slinear"
else:
interpolation = item.config["interpolation"]
dlg += 1
# create spline, ignoreing all out-of-range values
spl = interp1d(item.modData["x"], item.modData["y"],
kind=interpolation, copy=False, assume_sorted=True,
bounds_error = False, fill_value=0)
dlg += 1
values = spl(x)
# apply Operation otherwise just add
dlg += 1
inc = 1 // (20 / len(x))
for i in range(len(x)):
if np.isnan(y[i]):
y[i] = values[i]
else:
y[i] = self.__doOperation(y[i], values[i])
if inc > 0 and i % inc == 0:
dlg += 1
# remove all remaining NANs
y[np.isnan(y)] = 0
dlg += 5
# apply gaussian filter
if self.config["filter"] > 0:
y = gaussian_filter1d(y, sigma=self.config["filter"])
dlg += 5
# calculate numeric integration / differential
if self.config["integrate"] > 0:
for i in range(self.config["integrate"]):
for j, val in enumerate(x):
y[j] = integrate.quad(lambda _: y[j], 0, val)[0]
elif self.config["integrate"] < 0:
for i in range(abs(self.config["integrate"])):
y = np.gradient(y, x[1] - x[0])
dlg += 5
# Add Offsets
x = x + self.config["xOffset"]
y = y + self.config["yOffset"]
# save data
self.interpData = {'x': x, 'y': y}
dlg.setValue(100)
if self.sigCalc:
self.sigCalc.emit()
# from user Aguy at
# https://stackoverflow.com/questions/48900977/find-all-indexes-of-a-numpy-array-closest-to-a-value
def __findValueIndex(self, seq, val):
r = np.where(np.diff(np.sign(seq - val)) != 0)
idx = r + (val - seq[r]) / (seq[r + np.ones_like(r)] - seq[r])
idx = np.append(idx, np.where(seq == val))
idx = np.sort(idx)
return idx
def __doOperation(self, a, b):
if self.config["operation"] == "Addition":
return a + b
elif self.config["operation"] == "Subtraktion":
return a - b
elif self.config["operation"] == "Multiplikation":
return a * b
elif self.config["operation"] == "Division":
return a / b
else:
return -1
def updatePlot(self):
for item in self.fileList.list:
item.updatePlot()
return super().updatePlot()
# reflects updated values in the UI
def updateUI(self):
self.x_offset.setValue(self.config["xOffset"])
self.y_offset.setValue(self.config["yOffset"])
height = 50
for item in self.fileList.list:
height += item.item.sizeHint().height()
self.fileList.setFixedHeight( height )
self.__setSizeHint()
def deselect(self):
super().deselect()
self.fileList.deselectAll()
def getSelected(self):
return self.fileList.getSelected()
def getCount(self, i):
i = self.fileList.getCount(i)
return i + 1
def deleteSelected(self, plot, selected):
# deletes itself and all its children
if self.item == selected:
plot.plt.removeItem(self.plot)
plot.plt.removeItem(self.cursor)
self.fileList.deleteChildren(plot)
del self
return False # may be unreachable
else:
self.fileList.deleteSelected(plot)
return True # = item stays alive
def updateCursor(self, mousePoint):
infoText = super().updateCursor(mousePoint)
for index, item in enumerate(self.fileList.list):
infoText += item.updateCursor(mousePoint)
return infoText
def autoscale(self):
cst = super().autoscale()
for item in self.fileList.list:
icst = item.autoscale()
cst["xmin"] = min(icst["xmin"], cst["xmin"])
cst["xmax"] = max(icst["xmax"], cst["xmax"])
cst["ymin"] = min(icst["ymin"], cst["ymin"])
cst["ymax"] = max(icst["ymax"], cst["ymax"])
return cst
def setZIndex(self, zIndex):
self.config["zIndex"] = zIndex
zIndex = self.fileList.setZIndex(zIndex - 1)
self.updatePlot()
return zIndex
def __toggleSettings(self):
if self.settings.isHidden():
self.settings.show()
self.settingsBtn.setIcon(QtGui.QIcon(Config.getResource("assets/down.png")))
self.__setSizeHint()
else:
self.settings.hide()
self.settingsBtn.setIcon(QtGui.QIcon(Config.getResource("assets/left.png")))
sizeHint = self.Vlayout.sizeHint()
# sizeHint.setHeight(40+7) # strut height + padding (don't ask)
self.__setSizeHint()
self.sigUpdateUI.emit()
def __setSizeHint(self):
sizeHint = self.Vlayout.sizeHint()
sizeHint.setHeight(sizeHint.height() + 7)
self.item.setSizeHint(sizeHint)
def __initSettings(self):
self.settings = QtWidgets.QFrame()
self.settings.setObjectName("settings")
self.settings.setContentsMargins(20, 0, 20, 0)
self.settings.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Maximum)
self.GLayout = QtWidgets.QGridLayout()
# Offsets
self.x_offset = ListItem.SuperSpinner(None)
self.x_offset.setRange(-9999, 9999)
self.x_offset.setValue(self.config["xOffset"])
self.x_offset.setFixedWidth(75)
self.x_offset.valueChanged.connect(lambda x, who="xOffset": self.applyChange(x, who))
self.x_offset_label = QtWidgets.QLabel("x-Offset:")
self.x_offset_label.setBuddy(self.x_offset)
self.x_offset_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.y_offset = ListItem.SuperSpinner(None)
self.y_offset.setRange(-9999, 9999)
self.y_offset.setValue(self.config["yOffset"])
self.y_offset.setFixedWidth(75)
self.y_offset.valueChanged.connect(lambda y, who="yOffset": self.applyChange(y, who))
self.y_offset_label = QtWidgets.QLabel("y-Offset:")
self.y_offset_label.setBuddy(self.y_offset)
self.y_offset_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
# Color Picker
self.colorPickerBtn = QtWidgets.QPushButton(" ")
self.colorPickerBtn.setObjectName("color_select_button")
self.colorPickerBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.colorPickerBtn.setStyleSheet("background-color: hsv({:d},{:d}%,{:d}%); color: black;".format(
self.config["color"][0], self.config["color"][1], self.config["color"][2]))
self.colorPickerBtn.clicked.connect(lambda x, who="color": self.applyChange(x, who))
self.colorPickerLabel = QtWidgets.QLabel("Farbe:")
self.colorPickerLabel.setBuddy(self.colorPickerBtn)
self.colorPickerLabel.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
# Width Spinner
self.widthSpinner = QtWidgets.QSpinBox()
self.widthSpinner.setRange(1, 20)
self.widthSpinner.setValue(self.config["width"])
self.widthSpinner.valueChanged.connect(lambda x, who="width": self.applyChange(x, who))
self.widthLabel = QtWidgets.QLabel("Breite:")
self.widthLabel.setBuddy(self.widthSpinner)
self.widthLabel.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
# Integration
self.integrationBox = QtWidgets.QSpinBox()
self.integrationBox.setRange(-10, 10)
self.integrationBox.setValue(self.config["integrate"])
self.integrationBox.valueChanged.connect(lambda x, who="integrate": self.applyChange(x, who))
self.integrationLabel = QtWidgets.QLabel("Integration:")
self.integrationLabel.setBuddy(self.integrationBox)
self.integrationLabel.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
# Filter
self.filterBox = QtWidgets.QDoubleSpinBox()
self.filterBox.setValue(self.config["filter"])
self.filterBox.valueChanged.connect(lambda x, who="filter": self.applyChange(x, who))
self.filterLabel = QtWidgets.QLabel("Filter:")
self.filterLabel.setBuddy(self.integrationBox)
self.filterLabel.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.GLayout.addWidget(self.x_offset_label, 0, 0)
self.GLayout.addWidget(self.x_offset, 0, 1)
self.GLayout.addWidget(self.y_offset_label, 1, 0)
self.GLayout.addWidget(self.y_offset, 1, 1)
self.GLayout.addWidget(self.colorPickerLabel, 2, 0)
self.GLayout.addWidget(self.colorPickerBtn, 2, 1)
self.GLayout.addWidget(self.widthLabel, 3, 0)
self.GLayout.addWidget(self.widthSpinner, 3, 1)
self.GLayout.addWidget(self.filterLabel, 4, 0)
self.GLayout.addWidget(self.filterBox, 4, 1)
self.GLayout.addWidget(self.integrationLabel, 5, 0)
self.GLayout.addWidget(self.integrationBox, 5, 1)
self.settings.setLayout(self.GLayout)
def toDict(self):
dct = {
"type": "process",
"containing": {
"config": self.config,
"data": self.modData.to_dict(),
"children": []
}
}
for item in self.fileList.list:
dct["containing"]["children"].append(item.toDict())
return dct