source: sasview/src/sas/qtgui/Perspectives/Fitting/ComplexConstraint.py @ 14ec91c5

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 14ec91c5 was 14ec91c5, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More code review related fixes

  • Property mode set to 100644
File size: 7.3 KB
Line 
1"""
2Widget for multi-model constraints.
3"""
4import os
5from numpy import *
6
7from PyQt5 import QtCore
8from PyQt5 import QtGui
9from PyQt5 import QtWidgets
10import webbrowser
11
12import sas.qtgui.Utilities.GuiUtils as GuiUtils
13ALLOWED_OPERATORS = ['=','<','>','>=','<=']
14
15# Local UI
16from sas.qtgui.Perspectives.Fitting.UI.ComplexConstraintUI import Ui_ComplexConstraintUI
17from sas.qtgui.Perspectives.Fitting.Constraint import Constraint
18
19class ComplexConstraint(QtWidgets.QDialog, Ui_ComplexConstraintUI):
20    def __init__(self, parent=None, tabs=None):
21        super(ComplexConstraint, self).__init__()
22
23        self.setupUi(self)
24        self.setModal(True)
25
26        # Useful globals
27        self.tabs = tabs
28        self.params = None
29        self.tab_names = None
30        self.operator = '='
31
32        self.setupData()
33        self.setupWidgets()
34        self.setupSignals()
35        self.setupTooltip()
36
37        self.setFixedSize(self.minimumSizeHint())
38
39        # Default focus is on OK
40        self.cmdOK.setFocus()
41
42    def setupData(self):
43        """
44        Digs into self.tabs and pulls out relevant info
45        """
46        self.tab_names = [tab.kernel_module.name for tab in self.tabs]
47        self.params = [tab.getParamNames() for tab in self.tabs]
48        pass
49
50    def setupSignals(self):
51        """
52        Signals from various elements
53        """
54        self.cmdOK.clicked.connect(self.accept)
55        self.cmdHelp.clicked.connect(self.onHelp)
56        self.cmdRevert.clicked.connect(self.onRevert)
57        self.txtConstraint.editingFinished.connect(self.validateFormula)
58
59        self.cbParam1.currentIndexChanged.connect(self.onParamIndexChange)
60        self.cbParam2.currentIndexChanged.connect(self.onParamIndexChange)
61        self.cbOperator.currentIndexChanged.connect(self.onOperatorChange)
62
63    def setupWidgets(self):
64        """
65        Setup widgets based on current parameters
66        """
67        self.txtName1.setText(self.tab_names[0])
68        self.txtName2.setText(self.tab_names[1])
69
70        # Show only parameters not already constrained
71        self.cbParam1.clear()
72        items = [param for i,param in enumerate(self.params[0]) if not self.tabs[0].rowHasConstraint(i)]
73        self.cbParam1.addItems(items)
74        self.cbParam2.clear()
75        items = [param for i,param in enumerate(self.params[1]) if not self.tabs[1].rowHasConstraint(i)]
76        self.cbParam2.addItems(items)
77
78        self.txtParam.setText(self.tab_names[0] + ":" + self.cbParam1.currentText())
79
80        self.cbOperator.clear()
81        self.cbOperator.addItems(ALLOWED_OPERATORS)
82        self.txtOperator.setText(self.cbOperator.currentText())
83
84        self.txtConstraint.setText(self.tab_names[1]+"."+self.cbParam2.currentText())
85
86    def setupTooltip(self):
87        """
88        Tooltip for txtConstraint
89        """
90        p1 = self.tab_names[0] + ":" + self.cbParam1.currentText()
91        p2 = self.tab_names[1]+"."+self.cbParam2.currentText()
92        tooltip = "E.g.\n%s = 2.0 * (%s)\n" %(p1, p2)
93        tooltip += "%s = sqrt(%s) + 5"%(p1, p2)
94        self.txtConstraint.setToolTip(tooltip)
95
96    def onParamIndexChange(self, index):
97        """
98        Respond to parameter combo box changes
99        """
100        # Find out the signal source
101        source = self.sender().objectName()
102        if source == "cbParam1":
103            self.txtParam.setText(self.tab_names[0] + ":" + self.cbParam1.currentText())
104        else:
105            self.txtConstraint.setText(self.tab_names[1] + "." + self.cbParam2.currentText())
106        pass
107
108    def onOperatorChange(self, index):
109        """
110        Respond to operator combo box changes
111        """
112        self.txtOperator.setText(self.cbOperator.currentText())
113
114    def onRevert(self):
115        """
116        switch M1 <-> M2
117        """
118        # Switch parameters
119        self.params[1], self.params[0] = self.params[0], self.params[1]
120        self.tab_names[1], self.tab_names[0] = self.tab_names[0], self.tab_names[1]
121        self.tabs[1], self.tabs[0] = self.tabs[0], self.tabs[1]
122        # Try to swap parameter names in the line edit
123        current_text = self.txtConstraint.text()
124        new_text = current_text.replace(self.cbParam1.currentText(), self.cbParam2.currentText())
125        self.txtConstraint.setText(new_text)
126        # Update labels and tooltips
127        index1 = self.cbParam1.currentIndex()
128        index2 = self.cbParam2.currentIndex()
129        indexOp = self.cbOperator.currentIndex()
130        self.setupWidgets()
131
132        # Original indices
133        self.cbParam1.setCurrentIndex(index2)
134        self.cbParam2.setCurrentIndex(index1)
135        self.cbOperator.setCurrentIndex(indexOp)
136        self.setupTooltip()
137
138    def validateFormula(self):
139        """
140        Add visual cues when formula is incorrect
141        """
142        formula_is_valid = False
143        formula_is_valid = self.validateConstraint(self.txtConstraint.text())
144        if not formula_is_valid:
145            self.cmdOK.setEnabled(False)
146            self.txtConstraint.setStyleSheet("QLineEdit {background-color: red;}")
147        else:
148            self.cmdOK.setEnabled(True)
149            self.txtConstraint.setStyleSheet("QLineEdit {background-color: white;}")
150
151    def validateConstraint(self, constraint_text):
152        """
153        Ensure the constraint has proper form
154        """
155        # 0. none or empty
156        if not constraint_text or not isinstance(constraint_text, str):
157            return False
158
159        # M1.scale  --> model_str='M1', constraint_text='scale'
160        param_str = self.cbParam2.currentText()
161        constraint_text = constraint_text.strip()
162        model_str = self.txtName2.text()
163
164        # 0. Has to contain the model name
165        if model_str != self.txtName2.text():
166            return False
167
168        # Remove model name from constraint
169        constraint_text = constraint_text.replace(model_str+".",'')
170
171        # 1. just the parameter
172        if param_str == constraint_text:
173            return True
174
175        # 2. ensure the text contains parameter name
176        parameter_string_start = constraint_text.find(param_str)
177        if parameter_string_start < 0:
178            return False
179        parameter_string_end = parameter_string_start + len(param_str)
180
181        # 3. replace parameter name with "1" and try to evaluate the expression
182        try:
183            expression_to_evaluate = constraint_text.replace(param_str, "1.0")
184            eval(expression_to_evaluate)
185        except Exception:
186            # Too many cases to cover individually, just a blanket
187            # Exception should be sufficient
188            # Note that in current numpy things like sqrt(-1) don't
189            # raise but just return warnings
190            return False
191
192        return True
193
194    def constraint(self):
195        """
196        Return the generated constraint as tuple (model1, param1, operator, constraint)
197        """
198        return (self.txtName1.text(), self.cbParam1.currentText(), self.cbOperator.currentText(), self.txtConstraint.text())
199
200    def onHelp(self):
201        """
202        Display related help section
203        """
204        try:
205            help_location = GuiUtils.HELP_DIRECTORY_LOCATION + \
206            "/user/sasgui/perspectives/fitting/fitting_help.html#simultaneous-fits-with-constraints"
207            webbrowser.open('file://' + os.path.realpath(help_location))
208        except AttributeError:
209            # No manager defined - testing and standalone runs
210            pass
211
212
213
Note: See TracBrowser for help on using the repository browser.