Changeset eae226b in sasview for src/sas/qtgui/Perspectives/Fitting
- Timestamp:
- Jan 10, 2018 5:15:53 AM (7 years ago)
- Branches:
- ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
- Children:
- d3c0b95
- Parents:
- 0595bb7
- git-author:
- Piotr Rozyczko <rozyczko@…> (01/09/18 03:44:55)
- git-committer:
- Piotr Rozyczko <rozyczko@…> (01/10/18 05:15:53)
- Location:
- src/sas/qtgui/Perspectives/Fitting
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
r0595bb7 reae226b 41 41 from sas.qtgui.Perspectives.Fitting.ViewDelegate import PolyViewDelegate 42 42 from sas.qtgui.Perspectives.Fitting.ViewDelegate import MagnetismViewDelegate 43 from sas.qtgui.Perspectives.Fitting.Constraints import Constraint 44 from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint 43 45 44 46 … … 80 82 Main widget for selecting form and structure factor models 81 83 """ 82 constraintAddedSignal = QtCore.pyqtSignal(list)84 #constraintAddedSignal = QtCore.pyqtSignal(list) 83 85 def __init__(self, parent=None, data=None, tab_id=1): 84 86 … … 453 455 # Respond to change in parameters from the UI 454 456 self._model_model.itemChanged.connect(self.onMainParamsChange) 455 self.constraintAddedSignal.connect(self.modifyViewOnConstraint)457 #self.constraintAddedSignal.connect(self.modifyViewOnConstraint) 456 458 self._poly_model.itemChanged.connect(self.onPolyModelChange) 457 459 self._magnet_model.itemChanged.connect(self.onMagnetModelChange) … … 462 464 def showModelContextMenu(self, position): 463 465 464 rows = len([s.row() for s in self.lstParams.selectionModel().selectedRows()]) 466 #rows = len([s.row() for s in self.lstParams.selectionModel().selectedRows()]) 467 rows = [s.row() for s in self.lstParams.selectionModel().selectedRows()] 465 468 menu = self.showModelDescription() if not rows else self.modelContextMenu(rows) 466 469 try: … … 471 474 472 475 def modelContextMenu(self, rows): 476 """ 477 Create context menu for the given model 478 """ 473 479 menu = QtWidgets.QMenu() 474 480 num_rows = len(rows) 475 481 # Select for fitting 482 param_string = "parameter " if num_rows==1 else "parameters " 483 to_string = "to its current value" if num_rows==1 else "to their current values" 484 476 485 self.actionSelect = QtWidgets.QAction(self) 477 486 self.actionSelect.setObjectName("actionSelect") 478 self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select parameterfor fitting"))487 self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting")) 479 488 # Unselect from fitting 480 489 self.actionDeselect = QtWidgets.QAction(self) 481 490 self.actionDeselect.setObjectName("actionDeselect") 482 self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select parameterfrom fitting"))491 self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting")) 483 492 484 493 self.actionConstrain = QtWidgets.QAction(self) 485 494 self.actionConstrain.setObjectName("actionConstrain") 486 self.actionConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain parameter to current value")) 495 self.actionConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain "+param_string + to_string)) 496 497 self.actionRemoveConstraint = QtWidgets.QAction(self) 498 self.actionRemoveConstraint.setObjectName("actionRemoveConstrain") 499 self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove constraint")) 487 500 488 501 self.actionMultiConstrain = QtWidgets.QAction(self) … … 494 507 self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of selected parameters...")) 495 508 496 #action.setDefaultWidget(label)497 509 menu.addAction(self.actionSelect) 498 510 menu.addAction(self.actionDeselect) 499 511 menu.addSeparator() 500 512 501 if rows == 1: 513 if self.rowHasConstraint(rows[0]): 514 menu.addAction(self.actionRemoveConstraint) 515 else: 502 516 menu.addAction(self.actionConstrain) 503 elif rows == 2: 504 menu.addAction(self.actionMultiConstrain) 517 if num_rows == 2: 505 518 menu.addAction(self.actionMutualMultiConstrain) 506 elif rows > 2:507 menu.addAction(self.actionMultiConstrain)508 519 509 520 # Define the callbacks 510 521 self.actionConstrain.triggered.connect(self.addSimpleConstraint) 522 self.actionRemoveConstraint.triggered.connect(self.deleteConstraint) 511 523 self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint) 512 524 self.actionSelect.triggered.connect(self.selectParameters) … … 518 530 Show the constraint widget and receive the expression 519 531 """ 520 from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint521 from .Constraints import Constraint522 532 selected_rows = self.lstParams.selectionModel().selectedRows() 533 assert(len(selected_rows), 2) 523 534 524 535 params_list = [s.data() for s in selected_rows] 536 # Create and display the widget for param1 and param2 525 537 mc_widget = MultiConstraint(self, params=params_list) 526 mc_widget.exec_() 538 if mc_widget.exec_() != QtWidgets.QDialog.Accepted: 539 return 540 527 541 constraint = Constraint() 528 542 c_text = mc_widget.txtConstraint.text() 529 # Pass the constraint to the parser 530 531 self.communicate.statusBarUpdateSignal.emit('Constraints added')532 # Change the colour of the row533 pass 534 535 def modifyViewOnConstraint(self, row):536 """537 Add visual cues that the parameter is constrained538 """ 539 value = self._model_model.item(row, 1).text()540 # Set min/max to the value constrained541 self._model_model.item(row,2).setText(value)542 self._model_model.item(row,3).setText(value)543 544 # widget.params[0] is the parameter we're constraining 545 constraint.param = mc_widget.params[0] 546 constraint.func = c_text 547 548 # Create a new item and add the Constraint object as a child 549 item = QtGui.QStandardItem() 550 item.setData(constraint) 551 # Which row is the constrained parameter in? 552 553 row = self.rowFromName(constraint.param) 554 self._model_model.item(row, 1).setChild(0, item) 555 #self.constraintAddedSignal.emit([row]) 556 # Show visual hints for the constraint 543 557 font = QtGui.QFont() 544 558 font.setItalic(True) 545 559 brush = QtGui.QBrush(QtGui.QColor('blue')) 560 self.modifyViewOnRow(row, font=font, brush=brush) 561 562 # Pass the constraint to the parser 563 self.communicate.statusBarUpdateSignal.emit('Constraints added') 564 565 def rowFromName(self, name): 566 """ 567 given parameter name get the row number in self._model_model 568 """ 569 for row in range(self._model_model.rowCount()): 570 row_name = self._model_model.item(row).text() 571 if row_name == name: 572 return row 573 return None 574 575 def modifyViewOnRow(self, row, font=None, brush=None): 576 """ 577 Chage how the given row of the main model is shown 578 """ 579 fields_enabled = False 580 if font is None: 581 font = QtGui.QFont() 582 fields_enabled = True 583 if brush is None: 584 brush = QtGui.QBrush() 585 fields_enabled = True 546 586 self._model_model.blockSignals(True) 547 587 # Modify font and foreground of affected rows … … 549 589 self._model_model.item(row, column).setForeground(brush) 550 590 self._model_model.item(row, column).setFont(font) 551 self._model_model.item(row, column).setEditable( False)591 self._model_model.item(row, column).setEditable(fields_enabled) 552 592 self._model_model.blockSignals(False) 553 593 … … 556 596 Adds a constraint on a single parameter. 557 597 """ 558 from .Constraints import Constraint559 598 for row in self.selectedParameters(): 560 599 param = self._model_model.item(row, 0).text() 561 600 value = self._model_model.item(row, 1).text() 562 constraint = Constraint(param=param, value=value) 601 min = self._model_model.item(row, 2).text() 602 max = self._model_model.item(row, 3).text() 603 # Create a Constraint object 604 constraint = Constraint(param=param, value=value, min=min, max=max) 605 # Create a new item and add the Constraint object as a child 563 606 item = QtGui.QStandardItem() 564 607 item.setData(constraint) 565 608 self._model_model.item(row, 1).setChild(0, item) 609 # Set min/max to the value constrained 610 self._model_model.item(row,2).setText(value) 611 self._model_model.item(row,3).setText(value) 566 612 #self.constraintAddedSignal.emit([row]) 567 self.modifyViewOnConstraint(row) 613 # Show visual hints for the constraint 614 font = QtGui.QFont() 615 font.setItalic(True) 616 brush = QtGui.QBrush(QtGui.QColor('blue')) 617 self.modifyViewOnRow(row, font=font, brush=brush) 568 618 self.communicate.statusBarUpdateSignal.emit('Constraint added') 569 619 pass 620 621 def deleteConstraint(self): 622 """ 623 Delete constraints from selected parameters. 624 """ 625 for row in self.selectedParameters(): 626 # Get the Constraint object from of the model item 627 item = self._model_model.item(row, 1) 628 constraint = item.child(0).data() 629 # Retrieve old values and put them on the model 630 if constraint.min is not None: 631 self._model_model.item(row, 2).setText(constraint.min) 632 if constraint.max is not None: 633 self._model_model.item(row, 3).setText(constraint.max) 634 # Remove constraint item 635 item.removeRow(0) 636 #self.constraintAddedSignal.emit([row]) 637 self.modifyViewOnRow(row) 638 self.communicate.statusBarUpdateSignal.emit('Constraint removed') 639 pass 640 641 def rowHasConstraint(self, row): 642 """ 643 Finds out if row of the main model has a constraint child 644 """ 645 item = self._model_model.item(row,1) 646 return True if (item.hasChildren() and isinstance(item.child(0).data(), Constraint)) else False 570 647 571 648 def selectParameters(self): … … 964 1041 np.any(res.pvec is None) or \ 965 1042 not np.all(np.isfinite(res.pvec)): 966 msg = "Fitting did not converge! !!"1043 msg = "Fitting did not converge!" 967 1044 self.communicate.statusBarUpdateSignal.emit(msg) 968 1045 logging.error(msg) -
src/sas/qtgui/Perspectives/Fitting/MultiConstraint.py
r0595bb7 reae226b 36 36 switch M1 <-> M2 37 37 """ 38 # Switch parameters 38 39 self.params[1], self.params[0] = self.params[0], self.params[1] 40 # Try to swap parameter names in the line edit 41 current_text = self.txtConstraint.text() 42 new_text = current_text.replace(self.params[0], self.params[1]) 43 self.txtConstraint.setText(new_text) 44 # Update labels and tooltips 39 45 self.setupLabels() 40 46 self.setupTooltip() … … 88 94 # 2. ensure the text contains parameter name 89 95 parameter_string_start = constraint_text.find(param_str) 90 has_parameter_name = (parameter_string_start > -1) 91 if not has_parameter_name: 96 if parameter_string_start < 0: 92 97 return False 93 98 parameter_string_end = parameter_string_start + len(param_str) 94 99 95 # 3. parameter name should be a separate word, but can have "()[]*+-/ " around100 # 3. parameter name should be a separate word, but can have "()[]*+-/ " around 96 101 valid_neighbours = "()[]*+-/ " 97 102 has_only_parameter = False -
src/sas/qtgui/Perspectives/Fitting/UI/MultiConstraintUI.ui
r7fd20fc reae226b 10 10 <x>0</x> 11 11 <y>0</y> 12 <width>3 35</width>12 <width>369</width> 13 13 <height>143</height> 14 14 </rect> … … 158 158 </widget> 159 159 <resources/> 160 <connections/> 160 <connections> 161 <connection> 162 <sender>cmdOK</sender> 163 <signal>clicked()</signal> 164 <receiver>MultiConstraintUI</receiver> 165 <slot>accept()</slot> 166 <hints> 167 <hint type="sourcelabel"> 168 <x>321</x> 169 <y>121</y> 170 </hint> 171 <hint type="destinationlabel"> 172 <x>184</x> 173 <y>71</y> 174 </hint> 175 </hints> 176 </connection> 177 </connections> 161 178 </ui>
Note: See TracChangeset
for help on using the changeset viewer.