Changeset eae226b in sasview for src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
- 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)
- File:
-
- 1 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)
Note: See TracChangeset
for help on using the changeset viewer.