[676f137] | 1 | import os |
---|
| 2 | import sys |
---|
| 3 | |
---|
[116dd4c1] | 4 | from twisted.internet import threads |
---|
| 5 | |
---|
[676f137] | 6 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[14ec91c5] | 7 | import sas.qtgui.Utilities.LocalConfig as LocalConfig |
---|
| 8 | |
---|
[676f137] | 9 | from PyQt5 import QtGui, QtCore, QtWidgets |
---|
| 10 | |
---|
[116dd4c1] | 11 | from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit |
---|
[676f137] | 12 | |
---|
[116dd4c1] | 13 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
[676f137] | 14 | from sas.qtgui.Perspectives.Fitting.UI.ConstraintWidgetUI import Ui_ConstraintWidgetUI |
---|
[be8f4b0] | 15 | from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget |
---|
[116dd4c1] | 16 | from sas.qtgui.Perspectives.Fitting.FitThread import FitThread |
---|
| 17 | from sas.qtgui.Perspectives.Fitting.ConsoleUpdate import ConsoleUpdate |
---|
[2d466e4] | 18 | from sas.qtgui.Perspectives.Fitting.ComplexConstraint import ComplexConstraint |
---|
[14ec91c5] | 19 | from sas.qtgui.Perspectives.Fitting.Constraint import Constraint |
---|
[676f137] | 20 | |
---|
| 21 | class ConstraintWidget(QtWidgets.QWidget, Ui_ConstraintWidgetUI): |
---|
| 22 | """ |
---|
[be8f4b0] | 23 | Constraints Dialog to select the desired parameter/model constraints. |
---|
[676f137] | 24 | """ |
---|
| 25 | |
---|
[14ec91c5] | 26 | def __init__(self, parent=None): |
---|
[676f137] | 27 | super(ConstraintWidget, self).__init__() |
---|
| 28 | self.parent = parent |
---|
| 29 | self.setupUi(self) |
---|
| 30 | self.currentType = "FitPage" |
---|
[116dd4c1] | 31 | # Page id for fitting |
---|
| 32 | # To keep with previous SasView values, use 300 as the start offset |
---|
[14ec91c5] | 33 | self.page_id = 301 |
---|
[676f137] | 34 | |
---|
[47d7d2d] | 35 | # Remember previous content of modified cell |
---|
| 36 | self.current_cell = "" |
---|
| 37 | |
---|
[ba01ad1] | 38 | # Tabs used in simultaneous fitting |
---|
| 39 | # tab_name : True/False |
---|
| 40 | self.tabs_for_fitting = {} |
---|
| 41 | |
---|
[be8f4b0] | 42 | # Set up the widgets |
---|
| 43 | self.initializeWidgets() |
---|
| 44 | |
---|
[676f137] | 45 | # Set up signals/slots |
---|
| 46 | self.initializeSignals() |
---|
| 47 | |
---|
| 48 | # Create the list of tabs |
---|
[be8f4b0] | 49 | self.initializeFitList() |
---|
| 50 | |
---|
| 51 | def acceptsData(self): |
---|
| 52 | """ Tells the caller this widget doesn't accept data """ |
---|
| 53 | return False |
---|
| 54 | |
---|
| 55 | def initializeWidgets(self): |
---|
| 56 | """ |
---|
| 57 | Set up various widget states |
---|
| 58 | """ |
---|
[47d7d2d] | 59 | labels = ['FitPage', 'Model', 'Data', 'Mnemonic'] |
---|
[be8f4b0] | 60 | # tab widget - headers |
---|
[47d7d2d] | 61 | self.editable_tab_columns = [labels.index('Mnemonic')] |
---|
[be8f4b0] | 62 | self.tblTabList.setColumnCount(len(labels)) |
---|
| 63 | self.tblTabList.setHorizontalHeaderLabels(labels) |
---|
| 64 | self.tblTabList.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) |
---|
| 65 | |
---|
| 66 | self.tblTabList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
---|
| 67 | self.tblTabList.customContextMenuRequested.connect(self.showModelContextMenu) |
---|
| 68 | |
---|
| 69 | # disabled constraint |
---|
| 70 | labels = ['Constraint'] |
---|
| 71 | self.tblConstraints.setColumnCount(len(labels)) |
---|
| 72 | self.tblConstraints.setHorizontalHeaderLabels(labels) |
---|
| 73 | self.tblConstraints.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) |
---|
| 74 | self.tblConstraints.setEnabled(False) |
---|
| 75 | |
---|
| 76 | self.tblConstraints.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
---|
| 77 | self.tblConstraints.customContextMenuRequested.connect(self.showConstrContextMenu) |
---|
[676f137] | 78 | |
---|
| 79 | def initializeSignals(self): |
---|
| 80 | """ |
---|
| 81 | Set up signals/slots for this widget |
---|
| 82 | """ |
---|
[47d7d2d] | 83 | # simple widgets |
---|
[676f137] | 84 | self.btnSingle.toggled.connect(self.onFitTypeChange) |
---|
| 85 | self.btnBatch.toggled.connect(self.onFitTypeChange) |
---|
[be8f4b0] | 86 | self.cbCases.currentIndexChanged.connect(self.onSpecialCaseChange) |
---|
[676f137] | 87 | self.cmdFit.clicked.connect(self.onFit) |
---|
| 88 | self.cmdHelp.clicked.connect(self.onHelp) |
---|
[47d7d2d] | 89 | |
---|
| 90 | # QTableWidgets |
---|
[ba01ad1] | 91 | self.tblTabList.cellChanged.connect(self.onTabCellEdit) |
---|
[47d7d2d] | 92 | self.tblTabList.cellDoubleClicked.connect(self.onTabCellEntered) |
---|
| 93 | self.tblConstraints.cellChanged.connect(self.onConstraintChange) |
---|
| 94 | |
---|
| 95 | # External signals |
---|
[a90c9c5] | 96 | self.parent.tabsModifiedSignal.connect(self.initializeFitList) |
---|
[be8f4b0] | 97 | |
---|
| 98 | def updateSignalsFromTab(self, tab=None): |
---|
| 99 | """ |
---|
| 100 | Intercept update signals from fitting tabs |
---|
| 101 | """ |
---|
| 102 | if tab is not None: |
---|
| 103 | ObjectLibrary.getObject(tab).constraintAddedSignal.connect(self.initializeFitList) |
---|
| 104 | ObjectLibrary.getObject(tab).newModelSignal.connect(self.initializeFitList) |
---|
[676f137] | 105 | |
---|
| 106 | def onFitTypeChange(self, checked): |
---|
| 107 | """ |
---|
| 108 | Respond to the fit type change |
---|
| 109 | single fit/batch fit |
---|
| 110 | """ |
---|
[ba01ad1] | 111 | source = self.sender().objectName() |
---|
| 112 | self.currentType = "BatchPage" if source == "btnBatch" else "FitPage" |
---|
| 113 | self.initializeFitList() |
---|
[676f137] | 114 | |
---|
| 115 | def onSpecialCaseChange(self, index): |
---|
| 116 | """ |
---|
| 117 | Respond to the combobox change for special case constraint sets |
---|
| 118 | """ |
---|
| 119 | pass |
---|
| 120 | |
---|
[14ec91c5] | 121 | def getTabsForFit(self): |
---|
| 122 | """ |
---|
| 123 | Returns list of tab names selected for fitting |
---|
| 124 | """ |
---|
| 125 | return [tab for tab in self.tabs_for_fitting if self.tabs_for_fitting[tab]] |
---|
| 126 | |
---|
[676f137] | 127 | def onFit(self): |
---|
| 128 | """ |
---|
| 129 | Perform the constrained/simultaneous fit |
---|
| 130 | """ |
---|
[116dd4c1] | 131 | # Find out all tabs to fit |
---|
[14ec91c5] | 132 | tabs_to_fit = self.getTabsForFit() |
---|
[116dd4c1] | 133 | |
---|
| 134 | # Single fitter for the simultaneous run |
---|
| 135 | sim_fitter = Fit() |
---|
| 136 | sim_fitter.fitter_id = self.page_id |
---|
| 137 | |
---|
[14ec91c5] | 138 | # Notify the parent about fitting started |
---|
| 139 | self.parent.fittingStartedSignal.emit(tabs_to_fit) |
---|
| 140 | |
---|
[116dd4c1] | 141 | # prepare fitting problems for each tab |
---|
| 142 | # |
---|
| 143 | page_ids = [] |
---|
| 144 | fitter_id = 0 |
---|
| 145 | sim_fitter=[sim_fitter] |
---|
[c6343a5] | 146 | # Prepare the fitter object |
---|
| 147 | try: |
---|
| 148 | for tab in tabs_to_fit: |
---|
| 149 | tab_object = ObjectLibrary.getObject(tab) |
---|
| 150 | if tab_object is None: |
---|
| 151 | # No such tab! |
---|
| 152 | return |
---|
| 153 | sim_fitter, fitter_id = tab_object.prepareFitters(fitter=sim_fitter[0], fit_id=fitter_id) |
---|
| 154 | page_ids.append([tab_object.page_id]) |
---|
| 155 | except ValueError as ex: |
---|
| 156 | # No parameters selected in one of the tabs |
---|
| 157 | no_params_msg = "Fitting can not be performed.\n" +\ |
---|
| 158 | "Not all tabs chosen for fitting have parameters selected for fitting." |
---|
| 159 | reply = QtWidgets.QMessageBox.question(self, |
---|
| 160 | 'Warning', |
---|
| 161 | no_params_msg, |
---|
| 162 | QtWidgets.QMessageBox.Ok) |
---|
| 163 | |
---|
| 164 | return |
---|
[116dd4c1] | 165 | |
---|
| 166 | # Create the fitting thread, based on the fitter |
---|
| 167 | completefn = self.onBatchFitComplete if self.currentType=='BatchPage' else self.onFitComplete |
---|
| 168 | |
---|
[14ec91c5] | 169 | if LocalConfig.USING_TWISTED: |
---|
| 170 | handler = None |
---|
| 171 | updater = None |
---|
| 172 | else: |
---|
| 173 | handler = ConsoleUpdate(parent=self.parent, |
---|
| 174 | manager=self, |
---|
| 175 | improvement_delta=0.1) |
---|
| 176 | updater = handler.update_fit |
---|
[116dd4c1] | 177 | |
---|
| 178 | batch_inputs = {} |
---|
| 179 | batch_outputs = {} |
---|
| 180 | |
---|
| 181 | # new fit thread object |
---|
| 182 | calc_fit = FitThread(handler=handler, |
---|
| 183 | fn=sim_fitter, |
---|
| 184 | batch_inputs=batch_inputs, |
---|
| 185 | batch_outputs=batch_outputs, |
---|
| 186 | page_id=page_ids, |
---|
| 187 | updatefn=updater, |
---|
| 188 | completefn=completefn) |
---|
| 189 | |
---|
[14ec91c5] | 190 | if LocalConfig.USING_TWISTED: |
---|
| 191 | # start the trhrhread with twisted |
---|
| 192 | calc_thread = threads.deferToThread(calc_fit.compute) |
---|
| 193 | calc_thread.addCallback(completefn) |
---|
| 194 | calc_thread.addErrback(self.onFitFailed) |
---|
| 195 | else: |
---|
| 196 | # Use the old python threads + Queue |
---|
| 197 | calc_fit.queue() |
---|
| 198 | calc_fit.ready(2.5) |
---|
[116dd4c1] | 199 | |
---|
| 200 | |
---|
| 201 | #disable the Fit button |
---|
| 202 | self.cmdFit.setText('Running...') |
---|
| 203 | self.parent.communicate.statusBarUpdateSignal.emit('Fitting started...') |
---|
| 204 | self.cmdFit.setEnabled(False) |
---|
[676f137] | 205 | |
---|
| 206 | def onHelp(self): |
---|
| 207 | """ |
---|
[c6343a5] | 208 | Show the "Fitting" section of help |
---|
[676f137] | 209 | """ |
---|
[c6343a5] | 210 | tree_location = "/user/sasgui/perspectives/fitting/" |
---|
| 211 | |
---|
| 212 | helpfile = "fitting_help.html#simultaneous-fit-mode" |
---|
| 213 | help_location = tree_location + helpfile |
---|
| 214 | |
---|
| 215 | # OMG, really? Crawling up the object hierarchy... |
---|
| 216 | self.parent.parent.showHelp(help_location) |
---|
[676f137] | 217 | |
---|
[ba01ad1] | 218 | def onTabCellEdit(self, row, column): |
---|
[47d7d2d] | 219 | """ |
---|
[ba01ad1] | 220 | Respond to check/uncheck and to modify the model moniker actions |
---|
[47d7d2d] | 221 | """ |
---|
[ba01ad1] | 222 | item = self.tblTabList.item(row, column) |
---|
| 223 | if column == 0: |
---|
| 224 | # Update the tabs for fitting list |
---|
| 225 | tab_name = item.text() |
---|
| 226 | self.tabs_for_fitting[tab_name] = (item.checkState() == QtCore.Qt.Checked) |
---|
| 227 | # Enable fitting only when there are models to fit |
---|
| 228 | self.cmdFit.setEnabled(any(self.tabs_for_fitting.values())) |
---|
| 229 | |
---|
[47d7d2d] | 230 | if column not in self.editable_tab_columns: |
---|
| 231 | return |
---|
| 232 | new_moniker = item.data(0) |
---|
| 233 | |
---|
| 234 | # The new name should be validated on the fly, with QValidator |
---|
| 235 | # but let's just assure it post-factum |
---|
| 236 | is_good_moniker = self.validateMoniker(new_moniker) |
---|
| 237 | if not is_good_moniker: |
---|
[eb1a386] | 238 | self.tblTabList.blockSignals(True) |
---|
[47d7d2d] | 239 | item.setBackground(QtCore.Qt.red) |
---|
[eb1a386] | 240 | self.tblTabList.blockSignals(False) |
---|
[47d7d2d] | 241 | self.cmdFit.setEnabled(False) |
---|
[ba01ad1] | 242 | return |
---|
| 243 | self.tblTabList.blockSignals(True) |
---|
| 244 | item.setBackground(QtCore.Qt.white) |
---|
| 245 | self.tblTabList.blockSignals(False) |
---|
| 246 | self.cmdFit.setEnabled(True) |
---|
| 247 | if not self.current_cell: |
---|
| 248 | return |
---|
| 249 | # Remember the value |
---|
| 250 | if self.current_cell not in self.available_tabs: |
---|
| 251 | return |
---|
| 252 | temp_tab = self.available_tabs[self.current_cell] |
---|
| 253 | # Remove the key from the dictionaries |
---|
| 254 | self.available_tabs.pop(self.current_cell, None) |
---|
| 255 | # Change the model name |
---|
| 256 | model = temp_tab.kernel_module |
---|
| 257 | model.name = new_moniker |
---|
| 258 | # Replace constraint name |
---|
| 259 | temp_tab.replaceConstraintName(self.current_cell, new_moniker) |
---|
[0764593] | 260 | # Replace constraint name in the remaining tabs |
---|
| 261 | for tab in self.available_tabs.values(): |
---|
| 262 | tab.replaceConstraintName(self.current_cell, new_moniker) |
---|
[ba01ad1] | 263 | # Reinitialize the display |
---|
| 264 | self.initializeFitList() |
---|
[47d7d2d] | 265 | |
---|
| 266 | def onConstraintChange(self, row, column): |
---|
| 267 | """ |
---|
[116dd4c1] | 268 | Modify the constraint's "active" instance variable. |
---|
[47d7d2d] | 269 | """ |
---|
[ba01ad1] | 270 | item = self.tblConstraints.item(row, column) |
---|
| 271 | if column == 0: |
---|
| 272 | # Update the tabs for fitting list |
---|
| 273 | constraint = self.available_constraints[row] |
---|
| 274 | constraint.active = (item.checkState() == QtCore.Qt.Checked) |
---|
[47d7d2d] | 275 | |
---|
| 276 | def onTabCellEntered(self, row, column): |
---|
| 277 | """ |
---|
| 278 | Remember the original tab list cell data. |
---|
| 279 | Needed for reverting back on bad validation |
---|
| 280 | """ |
---|
| 281 | if column != 3: |
---|
| 282 | return |
---|
| 283 | self.current_cell = self.tblTabList.item(row, column).data(0) |
---|
| 284 | |
---|
[116dd4c1] | 285 | def onFitComplete(self, result): |
---|
| 286 | """ |
---|
| 287 | Respond to the successful fit complete signal |
---|
| 288 | """ |
---|
[17968c3] | 289 | #re-enable the Fit button |
---|
| 290 | self.cmdFit.setText("Fit") |
---|
| 291 | self.cmdFit.setEnabled(True) |
---|
| 292 | |
---|
[14ec91c5] | 293 | # Notify the parent about completed fitting |
---|
| 294 | self.parent.fittingStoppedSignal.emit(self.getTabsForFit()) |
---|
| 295 | |
---|
[c6343a5] | 296 | # get the elapsed time |
---|
| 297 | elapsed = result[1] |
---|
| 298 | |
---|
| 299 | # result list |
---|
| 300 | results = result[0][0] |
---|
| 301 | |
---|
| 302 | # Find out all tabs to fit |
---|
| 303 | tabs_to_fit = [tab for tab in self.tabs_for_fitting if self.tabs_for_fitting[tab]] |
---|
| 304 | |
---|
| 305 | # update all involved tabs |
---|
| 306 | for i, tab in enumerate(tabs_to_fit): |
---|
| 307 | tab_object = ObjectLibrary.getObject(tab) |
---|
| 308 | if tab_object is None: |
---|
| 309 | # No such tab. removed while job was running |
---|
| 310 | return |
---|
| 311 | # Make sure result and target objects are the same (same model moniker) |
---|
| 312 | if tab_object.kernel_module.name == results[i].model.name: |
---|
| 313 | tab_object.fitComplete(([[results[i]]], elapsed)) |
---|
[116dd4c1] | 314 | |
---|
[17968c3] | 315 | msg = "Fitting completed successfully in: %s s.\n" % GuiUtils.formatNumber(elapsed) |
---|
| 316 | self.parent.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 317 | |
---|
[116dd4c1] | 318 | def onBatchFitComplete(self, result): |
---|
| 319 | """ |
---|
| 320 | Respond to the successful batch fit complete signal |
---|
| 321 | """ |
---|
[17968c3] | 322 | #re-enable the Fit button |
---|
| 323 | self.cmdFit.setText("Fit") |
---|
| 324 | self.cmdFit.setEnabled(True) |
---|
| 325 | |
---|
[14ec91c5] | 326 | # Notify the parent about completed fitting |
---|
| 327 | self.parent.fittingStoppedSignal.emit(self.getTabsForFit()) |
---|
| 328 | |
---|
[17968c3] | 329 | # get the elapsed time |
---|
| 330 | elapsed = result[1] |
---|
| 331 | |
---|
| 332 | # ADD THE BATCH FIT VIEW HERE |
---|
| 333 | # |
---|
| 334 | |
---|
| 335 | msg = "Fitting completed successfully in: %s s.\n" % GuiUtils.formatNumber(elapsed) |
---|
| 336 | self.parent.communicate.statusBarUpdateSignal.emit(msg) |
---|
| 337 | |
---|
[116dd4c1] | 338 | pass |
---|
| 339 | |
---|
| 340 | def onFitFailed(self, reason): |
---|
| 341 | """ |
---|
[17968c3] | 342 | Respond to fitting failure. |
---|
[116dd4c1] | 343 | """ |
---|
[17968c3] | 344 | #re-enable the Fit button |
---|
| 345 | self.cmdFit.setText("Fit") |
---|
| 346 | self.cmdFit.setEnabled(True) |
---|
| 347 | |
---|
[14ec91c5] | 348 | # Notify the parent about completed fitting |
---|
| 349 | self.parent.fittingStoppedSignal.emit(self.getTabsForFit()) |
---|
| 350 | |
---|
[17968c3] | 351 | msg = "Fitting failed: %s s.\n" % reason |
---|
| 352 | self.parent.communicate.statusBarUpdateSignal.emit(msg) |
---|
[116dd4c1] | 353 | pass |
---|
| 354 | |
---|
[be8f4b0] | 355 | def isTabImportable(self, tab): |
---|
[676f137] | 356 | """ |
---|
[be8f4b0] | 357 | Determines if the tab can be imported and included in the widget |
---|
[676f137] | 358 | """ |
---|
[da9a0722] | 359 | if not isinstance(tab, str): return False |
---|
[be8f4b0] | 360 | if not self.currentType in tab: return False |
---|
| 361 | object = ObjectLibrary.getObject(tab) |
---|
| 362 | if not isinstance(object, FittingWidget): return False |
---|
| 363 | if object.data is None: return False |
---|
| 364 | return True |
---|
| 365 | |
---|
| 366 | def showModelContextMenu(self, position): |
---|
| 367 | """ |
---|
| 368 | Show context specific menu in the tab table widget. |
---|
| 369 | """ |
---|
| 370 | menu = QtWidgets.QMenu() |
---|
| 371 | rows = [s.row() for s in self.tblTabList.selectionModel().selectedRows()] |
---|
| 372 | num_rows = len(rows) |
---|
| 373 | if num_rows <= 0: |
---|
[676f137] | 374 | return |
---|
[be8f4b0] | 375 | # Select for fitting |
---|
| 376 | param_string = "Fit Page " if num_rows==1 else "Fit Pages " |
---|
| 377 | to_string = "to its current value" if num_rows==1 else "to their current values" |
---|
[676f137] | 378 | |
---|
[be8f4b0] | 379 | self.actionSelect = QtWidgets.QAction(self) |
---|
| 380 | self.actionSelect.setObjectName("actionSelect") |
---|
| 381 | self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting")) |
---|
| 382 | # Unselect from fitting |
---|
| 383 | self.actionDeselect = QtWidgets.QAction(self) |
---|
| 384 | self.actionDeselect.setObjectName("actionDeselect") |
---|
| 385 | self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting")) |
---|
| 386 | |
---|
| 387 | self.actionRemoveConstraint = QtWidgets.QAction(self) |
---|
| 388 | self.actionRemoveConstraint.setObjectName("actionRemoveConstrain") |
---|
| 389 | self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove all constraints on selected models")) |
---|
| 390 | |
---|
| 391 | self.actionMutualMultiConstrain = QtWidgets.QAction(self) |
---|
| 392 | self.actionMutualMultiConstrain.setObjectName("actionMutualMultiConstrain") |
---|
| 393 | self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of parameters in selected models...")) |
---|
| 394 | |
---|
| 395 | menu.addAction(self.actionSelect) |
---|
| 396 | menu.addAction(self.actionDeselect) |
---|
| 397 | menu.addSeparator() |
---|
| 398 | |
---|
| 399 | if num_rows >= 2: |
---|
| 400 | menu.addAction(self.actionMutualMultiConstrain) |
---|
[676f137] | 401 | |
---|
[be8f4b0] | 402 | # Define the callbacks |
---|
[47d7d2d] | 403 | self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint) |
---|
[be8f4b0] | 404 | self.actionSelect.triggered.connect(self.selectModels) |
---|
| 405 | self.actionDeselect.triggered.connect(self.deselectModels) |
---|
| 406 | try: |
---|
| 407 | menu.exec_(self.tblTabList.viewport().mapToGlobal(position)) |
---|
| 408 | except AttributeError as ex: |
---|
| 409 | logging.error("Error generating context menu: %s" % ex) |
---|
| 410 | return |
---|
| 411 | |
---|
| 412 | def showConstrContextMenu(self, position): |
---|
[676f137] | 413 | """ |
---|
[be8f4b0] | 414 | Show context specific menu in the tab table widget. |
---|
[676f137] | 415 | """ |
---|
[be8f4b0] | 416 | menu = QtWidgets.QMenu() |
---|
| 417 | rows = [s.row() for s in self.tblConstraints.selectionModel().selectedRows()] |
---|
| 418 | num_rows = len(rows) |
---|
| 419 | if num_rows <= 0: |
---|
| 420 | return |
---|
| 421 | # Select for fitting |
---|
| 422 | param_string = "constraint " if num_rows==1 else "constraints " |
---|
| 423 | to_string = "to its current value" if num_rows==1 else "to their current values" |
---|
| 424 | |
---|
| 425 | self.actionSelect = QtWidgets.QAction(self) |
---|
| 426 | self.actionSelect.setObjectName("actionSelect") |
---|
| 427 | self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting")) |
---|
| 428 | # Unselect from fitting |
---|
| 429 | self.actionDeselect = QtWidgets.QAction(self) |
---|
| 430 | self.actionDeselect.setObjectName("actionDeselect") |
---|
| 431 | self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting")) |
---|
[676f137] | 432 | |
---|
[be8f4b0] | 433 | self.actionRemoveConstraint = QtWidgets.QAction(self) |
---|
| 434 | self.actionRemoveConstraint.setObjectName("actionRemoveConstrain") |
---|
| 435 | self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove "+param_string)) |
---|
| 436 | |
---|
| 437 | menu.addAction(self.actionSelect) |
---|
| 438 | menu.addAction(self.actionDeselect) |
---|
| 439 | menu.addSeparator() |
---|
| 440 | menu.addAction(self.actionRemoveConstraint) |
---|
| 441 | |
---|
| 442 | # Define the callbacks |
---|
| 443 | self.actionRemoveConstraint.triggered.connect(self.deleteConstraint) |
---|
| 444 | self.actionSelect.triggered.connect(self.selectConstraints) |
---|
| 445 | self.actionDeselect.triggered.connect(self.deselectConstraints) |
---|
| 446 | try: |
---|
| 447 | menu.exec_(self.tblConstraints.viewport().mapToGlobal(position)) |
---|
| 448 | except AttributeError as ex: |
---|
| 449 | logging.error("Error generating context menu: %s" % ex) |
---|
| 450 | return |
---|
| 451 | |
---|
| 452 | def selectConstraints(self): |
---|
| 453 | """ |
---|
| 454 | Selected constraints are chosen for fitting |
---|
| 455 | """ |
---|
| 456 | status = QtCore.Qt.Checked |
---|
| 457 | self.setRowSelection(self.tblConstraints, status) |
---|
| 458 | |
---|
| 459 | def deselectConstraints(self): |
---|
| 460 | """ |
---|
| 461 | Selected constraints are removed for fitting |
---|
| 462 | """ |
---|
| 463 | status = QtCore.Qt.Unchecked |
---|
| 464 | self.setRowSelection(self.tblConstraints, status) |
---|
| 465 | |
---|
| 466 | def selectModels(self): |
---|
| 467 | """ |
---|
| 468 | Selected models are chosen for fitting |
---|
| 469 | """ |
---|
| 470 | status = QtCore.Qt.Checked |
---|
| 471 | self.setRowSelection(self.tblTabList, status) |
---|
| 472 | |
---|
| 473 | def deselectModels(self): |
---|
| 474 | """ |
---|
| 475 | Selected models are removed for fitting |
---|
| 476 | """ |
---|
| 477 | status = QtCore.Qt.Unchecked |
---|
| 478 | self.setRowSelection(self.tblTabList, status) |
---|
| 479 | |
---|
| 480 | def selectedParameters(self, widget): |
---|
| 481 | """ Returns list of selected (highlighted) parameters """ |
---|
| 482 | return [s.row() for s in widget.selectionModel().selectedRows()] |
---|
| 483 | |
---|
| 484 | def setRowSelection(self, widget, status=QtCore.Qt.Unchecked): |
---|
| 485 | """ |
---|
| 486 | Selected models are chosen for fitting |
---|
| 487 | """ |
---|
| 488 | # Convert to proper indices and set requested enablement |
---|
| 489 | for row in self.selectedParameters(widget): |
---|
| 490 | widget.item(row, 0).setCheckState(status) |
---|
| 491 | |
---|
| 492 | def deleteConstraint(self):#, row): |
---|
| 493 | """ |
---|
| 494 | Delete all selected constraints. |
---|
| 495 | """ |
---|
[47d7d2d] | 496 | # Removing rows from the table we're iterating over, |
---|
| 497 | # so prepare a list of data first |
---|
[be8f4b0] | 498 | constraints_to_delete = [] |
---|
| 499 | for row in self.selectedParameters(self.tblConstraints): |
---|
| 500 | constraints_to_delete.append(self.tblConstraints.item(row, 0).data(0)) |
---|
| 501 | for constraint in constraints_to_delete: |
---|
| 502 | moniker = constraint[:constraint.index(':')] |
---|
| 503 | param = constraint[constraint.index(':')+1:constraint.index('=')].strip() |
---|
| 504 | tab = self.available_tabs[moniker] |
---|
| 505 | tab.deleteConstraintOnParameter(param) |
---|
| 506 | # Constraints removed - refresh the table widget |
---|
| 507 | self.initializeFitList() |
---|
| 508 | |
---|
[47d7d2d] | 509 | def uneditableItem(self, data=""): |
---|
| 510 | """ |
---|
| 511 | Returns an uneditable Table Widget Item |
---|
| 512 | """ |
---|
| 513 | item = QtWidgets.QTableWidgetItem(data) |
---|
| 514 | item.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) |
---|
| 515 | return item |
---|
| 516 | |
---|
[be8f4b0] | 517 | def updateFitLine(self, tab): |
---|
| 518 | """ |
---|
| 519 | Update a single line of the table widget with tab info |
---|
| 520 | """ |
---|
[ba01ad1] | 521 | fit_page = ObjectLibrary.getObject(tab) |
---|
| 522 | model = fit_page.kernel_module |
---|
[be8f4b0] | 523 | if model is None: |
---|
| 524 | return |
---|
| 525 | tab_name = tab |
---|
| 526 | model_name = model.id |
---|
| 527 | moniker = model.name |
---|
[ba01ad1] | 528 | model_data = fit_page.data |
---|
[be8f4b0] | 529 | model_filename = model_data.filename |
---|
[ba01ad1] | 530 | self.available_tabs[moniker] = fit_page |
---|
[be8f4b0] | 531 | |
---|
| 532 | # Update the model table widget |
---|
| 533 | pos = self.tblTabList.rowCount() |
---|
| 534 | self.tblTabList.insertRow(pos) |
---|
[47d7d2d] | 535 | item = self.uneditableItem(tab_name) |
---|
| 536 | item.setFlags(item.flags() ^ QtCore.Qt.ItemIsUserCheckable) |
---|
[ba01ad1] | 537 | if tab_name in self.tabs_for_fitting: |
---|
| 538 | state = QtCore.Qt.Checked if self.tabs_for_fitting[tab_name] else QtCore.Qt.Unchecked |
---|
| 539 | item.setCheckState(state) |
---|
| 540 | else: |
---|
| 541 | item.setCheckState(QtCore.Qt.Checked) |
---|
| 542 | self.tabs_for_fitting[tab_name] = True |
---|
| 543 | |
---|
[be8f4b0] | 544 | self.tblTabList.setItem(pos, 0, item) |
---|
[47d7d2d] | 545 | self.tblTabList.setItem(pos, 1, self.uneditableItem(model_name)) |
---|
| 546 | self.tblTabList.setItem(pos, 2, self.uneditableItem(model_filename)) |
---|
| 547 | # Moniker is editable, so no option change |
---|
| 548 | item = QtWidgets.QTableWidgetItem(moniker) |
---|
| 549 | # Disable signals so we don't get infinite call recursion |
---|
| 550 | self.tblTabList.blockSignals(True) |
---|
| 551 | self.tblTabList.setItem(pos, 3, item) |
---|
| 552 | self.tblTabList.blockSignals(False) |
---|
[be8f4b0] | 553 | |
---|
| 554 | # Check if any constraints present in tab |
---|
[ba01ad1] | 555 | constraint_names = fit_page.getConstraintsForModel() |
---|
| 556 | constraints = fit_page.getConstraintObjectsForModel() |
---|
[be8f4b0] | 557 | if not constraints: |
---|
| 558 | return |
---|
| 559 | self.tblConstraints.setEnabled(True) |
---|
[ba01ad1] | 560 | for constraint, constraint_name in zip(constraints, constraint_names): |
---|
[be8f4b0] | 561 | # Create the text for widget item |
---|
[ba01ad1] | 562 | label = moniker + ":"+ constraint_name[0] + " = " + constraint_name[1] |
---|
| 563 | pos = self.tblConstraints.rowCount() |
---|
| 564 | self.available_constraints[pos] = constraint |
---|
[be8f4b0] | 565 | |
---|
| 566 | # Show the text in the constraint table |
---|
[ba01ad1] | 567 | item = self.uneditableItem(label) |
---|
| 568 | item.setFlags(item.flags() ^ QtCore.Qt.ItemIsUserCheckable) |
---|
[be8f4b0] | 569 | item.setCheckState(QtCore.Qt.Checked) |
---|
| 570 | self.tblConstraints.insertRow(pos) |
---|
| 571 | self.tblConstraints.setItem(pos, 0, item) |
---|
| 572 | |
---|
| 573 | def initializeFitList(self): |
---|
| 574 | """ |
---|
| 575 | Fill the list of model/data sets for fitting/constraining |
---|
| 576 | """ |
---|
| 577 | # look at the object library to find all fit tabs |
---|
| 578 | # Show the content of the current "model" |
---|
| 579 | objects = ObjectLibrary.listObjects() |
---|
| 580 | |
---|
| 581 | # Tab dict |
---|
| 582 | # moniker -> (kernel_module, data) |
---|
| 583 | self.available_tabs = {} |
---|
| 584 | # Constraint dict |
---|
| 585 | # moniker -> [constraints] |
---|
| 586 | self.available_constraints = {} |
---|
| 587 | |
---|
| 588 | # Reset the table widgets |
---|
| 589 | self.tblTabList.setRowCount(0) |
---|
| 590 | self.tblConstraints.setRowCount(0) |
---|
| 591 | |
---|
| 592 | # Fit disabled |
---|
| 593 | self.cmdFit.setEnabled(False) |
---|
| 594 | |
---|
| 595 | if not objects: |
---|
| 596 | return |
---|
[676f137] | 597 | |
---|
[be8f4b0] | 598 | tabs = [tab for tab in ObjectLibrary.listObjects() if self.isTabImportable(tab)] |
---|
| 599 | for tab in tabs: |
---|
| 600 | self.updateFitLine(tab) |
---|
| 601 | self.updateSignalsFromTab(tab) |
---|
| 602 | # We have at least 1 fit page, allow fitting |
---|
| 603 | self.cmdFit.setEnabled(True) |
---|
[47d7d2d] | 604 | |
---|
| 605 | def validateMoniker(self, new_moniker=None): |
---|
| 606 | """ |
---|
| 607 | Check new_moniker for correctness. |
---|
| 608 | It must be non-empty. |
---|
| 609 | It must not be the same as other monikers. |
---|
| 610 | """ |
---|
| 611 | if not new_moniker: |
---|
| 612 | return False |
---|
| 613 | |
---|
| 614 | for existing_moniker in self.available_tabs: |
---|
| 615 | if existing_moniker == new_moniker and existing_moniker != self.current_cell: |
---|
| 616 | return False |
---|
| 617 | |
---|
| 618 | return True |
---|
| 619 | |
---|
[c5a2722f] | 620 | def getObjectByName(self, name): |
---|
| 621 | for object_name in ObjectLibrary.listObjects(): |
---|
| 622 | object = ObjectLibrary.getObject(object_name) |
---|
| 623 | if isinstance(object, FittingWidget): |
---|
| 624 | try: |
---|
| 625 | if object.kernel_module.name == name: |
---|
| 626 | return object |
---|
| 627 | except AttributeError: |
---|
| 628 | # Disregard atribute errors - empty fit widgets |
---|
| 629 | continue |
---|
| 630 | return None |
---|
| 631 | |
---|
[47d7d2d] | 632 | def showMultiConstraint(self): |
---|
| 633 | """ |
---|
| 634 | Invoke the complex constraint editor |
---|
| 635 | """ |
---|
[2d466e4] | 636 | selected_rows = self.selectedParameters(self.tblTabList) |
---|
| 637 | assert(len(selected_rows)==2) |
---|
| 638 | |
---|
| 639 | tab_list = [ObjectLibrary.getObject(self.tblTabList.item(s, 0).data(0)) for s in selected_rows] |
---|
| 640 | # Create and display the widget for param1 and param2 |
---|
| 641 | cc_widget = ComplexConstraint(self, tabs=tab_list) |
---|
| 642 | if cc_widget.exec_() != QtWidgets.QDialog.Accepted: |
---|
| 643 | return |
---|
| 644 | |
---|
[c5a2722f] | 645 | constraint = Constraint() |
---|
| 646 | model1, param1, operator, constraint_text = cc_widget.constraint() |
---|
| 647 | |
---|
| 648 | constraint.func = constraint_text |
---|
[ba01ad1] | 649 | constraint.param = param1 |
---|
[c5a2722f] | 650 | # Find the right tab |
---|
| 651 | constrained_tab = self.getObjectByName(model1) |
---|
| 652 | if constrained_tab is None: |
---|
| 653 | return |
---|
| 654 | |
---|
| 655 | # Find the constrained parameter row |
---|
| 656 | constrained_row = constrained_tab.getRowFromName(param1) |
---|
| 657 | |
---|
| 658 | # Update the tab |
---|
| 659 | constrained_tab.addConstraintToRow(constraint, constrained_row) |
---|