[2d0e0c1] | 1 | # global |
---|
| 2 | import sys |
---|
| 3 | import os |
---|
[b0c5e8c] | 4 | import types |
---|
| 5 | |
---|
[4992ff2] | 6 | from PyQt5 import QtCore |
---|
| 7 | from PyQt5 import QtGui |
---|
| 8 | from PyQt5 import QtWidgets |
---|
| 9 | from PyQt5 import QtWebKitWidgets |
---|
[2d0e0c1] | 10 | |
---|
| 11 | from sas.qtgui.UI import images_rc |
---|
| 12 | from sas.qtgui.UI import main_resources_rc |
---|
[b0c5e8c] | 13 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
| 14 | |
---|
[2d0e0c1] | 15 | from bumps import fitters |
---|
| 16 | import bumps.options |
---|
| 17 | |
---|
| 18 | from sas.qtgui.Perspectives.Fitting.UI.FittingOptionsUI import Ui_FittingOptions |
---|
| 19 | |
---|
| 20 | # Set the default optimizer |
---|
| 21 | fitters.FIT_DEFAULT_ID = 'lm' |
---|
| 22 | |
---|
[72f4834] | 23 | |
---|
[4992ff2] | 24 | class FittingOptions(QtWidgets.QDialog, Ui_FittingOptions): |
---|
[2d0e0c1] | 25 | """ |
---|
| 26 | Hard-coded version of the fit options dialog available from BUMPS. |
---|
| 27 | This should be make more "dynamic". |
---|
| 28 | bumps.options.FIT_FIELDS gives mapping between parameter names, parameter strings and field type |
---|
| 29 | (double line edit, integer line edit, combo box etc.), e.g. |
---|
| 30 | FIT_FIELDS = dict( |
---|
| 31 | samples = ("Samples", parse_int), |
---|
| 32 | xtol = ("x tolerance", float)) |
---|
| 33 | |
---|
| 34 | bumps.fitters.<algorithm>.settings gives mapping between algorithm, parameter name and default value: |
---|
| 35 | e.g. |
---|
| 36 | settings = [('steps', 1000), ('starts', 1), ('radius', 0.15), ('xtol', 1e-6), ('ftol', 1e-8)] |
---|
| 37 | """ |
---|
[b0c5e8c] | 38 | fit_option_changed = QtCore.pyqtSignal(str) |
---|
[2d0e0c1] | 39 | |
---|
| 40 | def __init__(self, parent=None, config=None): |
---|
| 41 | super(FittingOptions, self).__init__(parent) |
---|
| 42 | self.setupUi(self) |
---|
| 43 | |
---|
| 44 | self.config = config |
---|
| 45 | |
---|
| 46 | # no reason to have this widget resizable |
---|
| 47 | self.setFixedSize(self.minimumSizeHint()) |
---|
| 48 | |
---|
[85487ebd] | 49 | self.setWindowTitle("Fit Algorithms") |
---|
[2d0e0c1] | 50 | |
---|
| 51 | # Fill up the algorithm combo, based on what BUMPS says is available |
---|
| 52 | self.cbAlgorithm.addItems([n.name for n in fitters.FITTERS if n.id in fitters.FIT_ACTIVE_IDS]) |
---|
| 53 | |
---|
| 54 | # Handle the Apply button click |
---|
[4992ff2] | 55 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).clicked.connect(self.onApply) |
---|
[b0c5e8c] | 56 | # handle the Help button click |
---|
[4992ff2] | 57 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Help).clicked.connect(self.onHelp) |
---|
[2d0e0c1] | 58 | |
---|
| 59 | # Handle the combo box changes |
---|
| 60 | self.cbAlgorithm.currentIndexChanged.connect(self.onAlgorithmChange) |
---|
| 61 | |
---|
| 62 | # Set the default index |
---|
| 63 | default_name = [n.name for n in fitters.FITTERS if n.id == fitters.FIT_DEFAULT_ID][0] |
---|
| 64 | default_index = self.cbAlgorithm.findText(default_name) |
---|
| 65 | self.cbAlgorithm.setCurrentIndex(default_index) |
---|
[b0c5e8c] | 66 | |
---|
| 67 | # Assign appropriate validators |
---|
| 68 | self.assignValidators() |
---|
| 69 | |
---|
[72f4834] | 70 | # Set defaults |
---|
[2d0e0c1] | 71 | self.current_fitter_id = fitters.FIT_DEFAULT_ID |
---|
| 72 | |
---|
[72f4834] | 73 | # OK has to be initialized to True, after initial validator setup |
---|
[4992ff2] | 74 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True) |
---|
[72f4834] | 75 | |
---|
[b0c5e8c] | 76 | # Display HTML content |
---|
[4992ff2] | 77 | self.helpView = QtWebKitWidgets.QWebView() |
---|
[b0c5e8c] | 78 | |
---|
| 79 | def assignValidators(self): |
---|
| 80 | """ |
---|
| 81 | Use options.FIT_FIELDS to assert which line edit gets what validator |
---|
| 82 | """ |
---|
[b3e8629] | 83 | for option in bumps.options.FIT_FIELDS.keys(): |
---|
[b0c5e8c] | 84 | (f_name, f_type) = bumps.options.FIT_FIELDS[option] |
---|
| 85 | validator = None |
---|
| 86 | if type(f_type) == types.FunctionType: |
---|
| 87 | validator = QtGui.QIntValidator() |
---|
[72f4834] | 88 | validator.setBottom(0) |
---|
[b3e8629] | 89 | elif f_type == float: |
---|
[d6b8a1d] | 90 | validator = GuiUtils.DoubleValidator() |
---|
[72f4834] | 91 | validator.setBottom(0) |
---|
[b0c5e8c] | 92 | else: |
---|
| 93 | continue |
---|
| 94 | for fitter_id in fitters.FIT_ACTIVE_IDS: |
---|
| 95 | line_edit = self.widgetFromOption(str(option), current_fitter=str(fitter_id)) |
---|
| 96 | if hasattr(line_edit, 'setValidator') and validator is not None: |
---|
| 97 | line_edit.setValidator(validator) |
---|
[72f4834] | 98 | line_edit.textChanged.connect(self.check_state) |
---|
| 99 | line_edit.textChanged.emit(line_edit.text()) |
---|
| 100 | |
---|
| 101 | def check_state(self, *args, **kwargs): |
---|
| 102 | sender = self.sender() |
---|
| 103 | validator = sender.validator() |
---|
| 104 | state = validator.validate(sender.text(), 0)[0] |
---|
| 105 | if state == QtGui.QValidator.Acceptable: |
---|
| 106 | color = '' # default |
---|
[4992ff2] | 107 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True) |
---|
[72f4834] | 108 | else: |
---|
| 109 | color = '#fff79a' # yellow |
---|
[4992ff2] | 110 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False) |
---|
[72f4834] | 111 | |
---|
| 112 | sender.setStyleSheet('QLineEdit { background-color: %s }' % color) |
---|
[2d0e0c1] | 113 | |
---|
| 114 | def onAlgorithmChange(self, index): |
---|
| 115 | """ |
---|
| 116 | Change the page in response to combo box index |
---|
| 117 | """ |
---|
| 118 | # Find the algorithm ID from name |
---|
| 119 | self.current_fitter_id = \ |
---|
| 120 | [n.id for n in fitters.FITTERS if n.name == str(self.cbAlgorithm.currentText())][0] |
---|
| 121 | |
---|
| 122 | # find the right stacked widget |
---|
| 123 | widget_name = "self.page_"+str(self.current_fitter_id) |
---|
| 124 | |
---|
| 125 | # Convert the name into widget instance |
---|
| 126 | widget_to_activate = eval(widget_name) |
---|
[b0c5e8c] | 127 | index_for_this_id = self.stackedWidget.indexOf(widget_to_activate) |
---|
[2d0e0c1] | 128 | |
---|
| 129 | # Select the requested widget |
---|
[b0c5e8c] | 130 | self.stackedWidget.setCurrentIndex(index_for_this_id) |
---|
[2d0e0c1] | 131 | |
---|
| 132 | self.updateWidgetFromBumps(self.current_fitter_id) |
---|
| 133 | |
---|
[b0c5e8c] | 134 | self.assignValidators() |
---|
| 135 | |
---|
[b67bfa7] | 136 | # OK has to be reinitialized to True |
---|
[4992ff2] | 137 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True) |
---|
[b67bfa7] | 138 | |
---|
[2d0e0c1] | 139 | def onApply(self): |
---|
| 140 | """ |
---|
[b0c5e8c] | 141 | Update the fitter object |
---|
[2d0e0c1] | 142 | """ |
---|
[b0c5e8c] | 143 | # Notify the perspective, so the window title is updated |
---|
| 144 | self.fit_option_changed.emit(self.cbAlgorithm.currentText()) |
---|
[2d0e0c1] | 145 | |
---|
[b0c5e8c] | 146 | def bumpsUpdate(option): |
---|
| 147 | """ |
---|
| 148 | Utility method for bumps state update |
---|
| 149 | """ |
---|
[2d0e0c1] | 150 | widget = self.widgetFromOption(option) |
---|
[4992ff2] | 151 | new_value = widget.currentText() if isinstance(widget, QtWidgets.QComboBox) \ |
---|
[b0c5e8c] | 152 | else float(widget.text()) |
---|
[2d0e0c1] | 153 | self.config.values[self.current_fitter_id][option] = new_value |
---|
| 154 | |
---|
[b0c5e8c] | 155 | # Update the BUMPS singleton |
---|
[b3e8629] | 156 | [bumpsUpdate(o) for o in self.config.values[self.current_fitter_id].keys()] |
---|
[b0c5e8c] | 157 | |
---|
| 158 | def onHelp(self): |
---|
| 159 | """ |
---|
| 160 | Show the "Fitting options" section of help |
---|
| 161 | """ |
---|
| 162 | tree_location = GuiUtils.HELP_DIRECTORY_LOCATION + "/user/sasgui/perspectives/fitting/" |
---|
| 163 | |
---|
| 164 | # Actual file anchor will depend on the combo box index |
---|
| 165 | # Note that we can be clusmy here, since bad current_fitter_id |
---|
| 166 | # will just make the page displayed from the top |
---|
| 167 | helpfile = "optimizer.html#fit-" + self.current_fitter_id |
---|
| 168 | help_location = tree_location + helpfile |
---|
| 169 | self.helpView.load(QtCore.QUrl(help_location)) |
---|
| 170 | self.helpView.show() |
---|
| 171 | |
---|
| 172 | def widgetFromOption(self, option_id, current_fitter=None): |
---|
[2d0e0c1] | 173 | """ |
---|
| 174 | returns widget's element linked to the given option_id |
---|
| 175 | """ |
---|
[b0c5e8c] | 176 | if current_fitter is None: |
---|
| 177 | current_fitter = self.current_fitter_id |
---|
[b3e8629] | 178 | if option_id not in list(bumps.options.FIT_FIELDS.keys()): return None |
---|
[b0c5e8c] | 179 | option = option_id + '_' + current_fitter |
---|
| 180 | if not hasattr(self, option): return None |
---|
| 181 | return eval('self.' + option) |
---|
[2d0e0c1] | 182 | |
---|
| 183 | def getResults(self): |
---|
| 184 | """ |
---|
| 185 | Sends back the current choice of parameters |
---|
| 186 | """ |
---|
| 187 | algorithm = self.cbAlgorithm.currentText() |
---|
| 188 | return algorithm |
---|
| 189 | |
---|
| 190 | def updateWidgetFromBumps(self, fitter_id): |
---|
| 191 | """ |
---|
| 192 | Given the ID of the current optimizer, fetch the values |
---|
| 193 | and update the widget |
---|
| 194 | """ |
---|
| 195 | options = self.config.values[fitter_id] |
---|
[b3e8629] | 196 | for option in options.keys(): |
---|
[2d0e0c1] | 197 | # Find the widget name of the option |
---|
| 198 | # e.g. 'samples' for 'dream' is 'self.samples_dream' |
---|
| 199 | widget_name = 'self.'+option+'_'+fitter_id |
---|
[377ade1] | 200 | if option not in bumps.options.FIT_FIELDS: |
---|
| 201 | return |
---|
[2d0e0c1] | 202 | if isinstance(bumps.options.FIT_FIELDS[option][1], bumps.options.ChoiceList): |
---|
| 203 | control = eval(widget_name) |
---|
| 204 | control.setCurrentIndex(control.findText(str(options[option]))) |
---|
| 205 | else: |
---|
| 206 | eval(widget_name).setText(str(options[option])) |
---|
| 207 | |
---|
| 208 | pass |
---|