source: sasview/src/sas/qtgui/Perspectives/Fitting/ComplexConstraint.py @ 86d3207

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 86d3207 was aed0532, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Updated references to help files

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