-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogApproximation.py
179 lines (169 loc) · 7.43 KB
/
dialogApproximation.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
# -*- coding: cp1251 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from lib4db import *
from PyQt5 import uic
from copy import deepcopy
import globalConstants as gl
from importlib import reload
class dDataApproximation(QDialog):
def __init__(self, parent=None, item=None):
reload(gl)
super(QDialog, self).__init__(parent)
uic.loadUi('dialogApproximation.ui', self)
self.lines=[]
self.item=item
self.bApplyForm.clicked.connect(self.updateForm)
self.tParameters.setColumnCount(5)
self.tParameters.setHorizontalHeaderLabels(['Parameter', 'Value', 'Vary', 'min', 'max'])
self.bCalculateParameters.clicked.connect(self.calculateParameters)
et=self.parent().exps[0].type
if et=='t':
et='c'
self.cbApproximationForm.addItems(self.parent().models.get(et,['{A}+{B}*x1']))
self.et=et
self.approximation=0
self.bRedraw.clicked.connect(self.redraw)
self.updateForm()
for i, k in enumerate(self.approximation.expParams):
v=gl.varSymbols[k]
s=('x_{%i}' % (i+1)) + '-'+v
p=QPixmap()
p.loadFromData(renderFormula(s, dpi=100, fontsize=10, convertFormula=False))
l=QLabel()
l.setPixmap(p)
self.hlVariables.addWidget(l)
self.cbApproximationForm.completer().setCompletionMode(QCompleter.PopupCompletion)
self.cbApproximationForm.setDuplicatesEnabled(False)
def updateForm(self):
#self.cbApproximationForm.addItem(self.cbApproximationForm.currentText())
updflag=0
if self.approximation:
v=deepcopy(self.approximation.params)
mn=deepcopy(self.approximation.parsMin)
mx=deepcopy(self.approximation.parsMax)
updflag=1
self.approximation=abstractModel(self.cbApproximationForm.currentText())
if self.parent().exps:
self.approximation.expParams=self.parent().exps[0].data.keys()
self.approximation.ycol=self.parent().conf['yaxis']
if updflag:
for k in v.keys():
if k in self.approximation.params:
self.approximation.params[k]=v[k]
self.approximation.parsMin[k]=mn[k]
self.approximation.parsMax[k]=mx[k]
p=QPixmap()
p.loadFromData(self.approximation.formulaToPng())
self.lFormula.setPixmap(p)
self.updateParametersTable()
self.tableIntervals.clear()
self.tableIntervals.setColumnCount(2)
self.tableIntervals.setRowCount(len(self.approximation.vars))
for i, p in enumerate(self.approximation.vars):
try:
p=list(self.approximation.expParams)[int(p[1:])-1]
s=gl.varSymbols[p]
except:
s=p
b=QPixmap()
b.loadFromData(renderFormula(s, dpi=100, fontsize=10, convertFormula=False))
c1=QTableWidgetItem()
c1.setFlags(c1.flags()^2)
c1.setIcon(QIcon(b))
self.tableIntervals.setItem(i,0,c1)
def updateParametersTable(self):
self.tParameters.setRowCount(len(self.approximation.params))
i=0
for k,v in self.approximation.params.items():
c1=QTableWidgetItem()
c1.setFlags(c1.flags()^2)
c1.setText(k)
self.tParameters.setItem(i,0,c1)
c2=QTableWidgetItem()
c2.setText(str(v))
self.tParameters.setItem(i,1,c2)
c3=QTableWidgetItem()
c3.setFlags(c3.flags()^2)
c3.setCheckState(2 if k in self.approximation.varParams else 0)
self.tParameters.setItem(i,2,c3)
c4=QTableWidgetItem()
c4.setText(str(self.approximation.parsMin[k]))
self.tParameters.setItem(i,3,c4)
c5=QTableWidgetItem()
c5.setText(str(self.approximation.parsMax[k]))
self.tParameters.setItem(i,4,c5)
i+=1
def calculateParameters(self):
self.approximation.varParams=[]
for i in range(self.tParameters.rowCount()):
k=self.tParameters.item(i,0).text()
if k in self.approximation.params:
v=float(self.tParameters.item(i,1).text())
self.approximation.params[k]=v
if self.tParameters.item(i,2).checkState()==2:
self.approximation.varParams.append(k)
t=self.tParameters.item(i,3).text()
self.approximation.parsMin[k]=float(t) if t!='' else ''
t=self.tParameters.item(i,4).text()
self.approximation.parsMax[k]=float(t) if t!='' else ''
try:
self.approximation.approximateData(self.parent().exps,\
ycol=self.parent().conf['yaxis'], method=self.cbMethod.currentText())
except:
mb=QMessageBox(self)
mb.setText(u'Íå óäàëîñü íàéòè ðåøåíèå.\nÏîïðîáóéòå èçìåíèòü íà÷àëüíûå çíà÷åíèÿ\nèëè îãðàíè÷åíèÿ.')
mb.setWindowTitle(u'Ðåøåíèå íå ïîëó÷åíî.')
mb.setIcon(QMessageBox.Critical)
mb.exec_()
return
# self.parent().log.appendPlainText('r^2={:.5f}'.format(self.approximation.r2))
self.tNotes.setPlainText('')
self.tNotes.appendPlainText(u'Ïàðàìåòðû àïïðîêñèìàöèè îïðåäåëåíû íà áàçå ñëåäóþùèõ ýêñïåðèìåíòîâ:')
for e in self.parent().exps:
self.tNotes.appendPlainText(e.name)
self.tNotes.appendPlainText('r^2={:.5f}'.format(self.approximation.r2))
for i in range(self.tParameters.rowCount()):
k=self.tParameters.item(i,0).text()
if k in self.approximation.params:
self.tParameters.item(i,1).setText(str(self.approximation.params[k]))
# print(self.approximation.varParams)
# print(self.approximation.params)
# print(self.approximation.parsMin)
# print(self.approximation.parsMax)
def redraw(self):
if self.lines:
for l in self.lines:
l.remove()
if self.rbOnData.isChecked():
self.lines=self.approximation.plotOnDataPoints(data=self.parent().exps, xcol=self.parent().conf['xaxis'], ax=self.parent().mpl.ax)
else:
intervals=[]
for i in range(self.tableIntervals.rowCount()):
intervals.append(str2interval(self.tableIntervals.item(i,1).text()))
xcol=self.parent().conf['xaxis']
if xcol>self.parent().conf['yaxis']:
xcol-=1
self.lines=self.approximation.plotOnIntervalPoints(intervals=intervals, xcol=xcol, ax=self.parent().mpl.ax)
self.parent().mplRedraw()
def accept(self):
self.approximation.comment=self.tNotes.toPlainText()
i=QTreeWidgetItem(type=1006)
i.setToolTip(0, splitStrByNSymbols(self.approximation.comment, gl.ttN))
i.setText(0,self.approximation.form)
i.setData(0,100,self.approximation)
if not self.et in self.parent().models.keys():
self.parent().models[self.et]=[]
if not self.approximation.form in self.parent().models[self.et]:
self.parent().models[self.et].append(self.approximation.form)
self.item.addChild(i)
for l in self.lines:
l.remove()
self.parent().mplRedraw()
self.done(1)
def reject(self):
for l in self.lines:
l.remove()
self.parent().mplRedraw()
self.done(0)