Changeset eae226b in sasview for src/sas


Ignore:
Timestamp:
Jan 10, 2018 3:15:53 AM (6 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
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 01:44:55)
git-committer:
Piotr Rozyczko <rozyczko@…> (01/10/18 03:15:53)
Message:

More functionality for single model constraints SASVIEW-843

Location:
src/sas/qtgui/Perspectives/Fitting
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Fitting/FittingWidget.py

    r0595bb7 reae226b  
    4141from sas.qtgui.Perspectives.Fitting.ViewDelegate import PolyViewDelegate 
    4242from sas.qtgui.Perspectives.Fitting.ViewDelegate import MagnetismViewDelegate 
     43from sas.qtgui.Perspectives.Fitting.Constraints import Constraint 
     44from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint 
    4345 
    4446 
     
    8082    Main widget for selecting form and structure factor models 
    8183    """ 
    82     constraintAddedSignal = QtCore.pyqtSignal(list) 
     84    #constraintAddedSignal = QtCore.pyqtSignal(list) 
    8385    def __init__(self, parent=None, data=None, tab_id=1): 
    8486 
     
    453455        # Respond to change in parameters from the UI 
    454456        self._model_model.itemChanged.connect(self.onMainParamsChange) 
    455         self.constraintAddedSignal.connect(self.modifyViewOnConstraint) 
     457        #self.constraintAddedSignal.connect(self.modifyViewOnConstraint) 
    456458        self._poly_model.itemChanged.connect(self.onPolyModelChange) 
    457459        self._magnet_model.itemChanged.connect(self.onMagnetModelChange) 
     
    462464    def showModelContextMenu(self, position): 
    463465 
    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()] 
    465468        menu = self.showModelDescription() if not rows else self.modelContextMenu(rows) 
    466469        try: 
     
    471474 
    472475    def modelContextMenu(self, rows): 
     476        """ 
     477        Create context menu for the given model 
     478        """ 
    473479        menu = QtWidgets.QMenu() 
    474  
     480        num_rows = len(rows) 
    475481        # 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 
    476485        self.actionSelect = QtWidgets.QAction(self) 
    477486        self.actionSelect.setObjectName("actionSelect") 
    478         self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select parameter for fitting")) 
     487        self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting")) 
    479488        # Unselect from fitting 
    480489        self.actionDeselect = QtWidgets.QAction(self) 
    481490        self.actionDeselect.setObjectName("actionDeselect") 
    482         self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select parameter from fitting")) 
     491        self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting")) 
    483492 
    484493        self.actionConstrain = QtWidgets.QAction(self) 
    485494        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")) 
    487500 
    488501        self.actionMultiConstrain = QtWidgets.QAction(self) 
     
    494507        self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of selected parameters...")) 
    495508 
    496         #action.setDefaultWidget(label) 
    497509        menu.addAction(self.actionSelect) 
    498510        menu.addAction(self.actionDeselect) 
    499511        menu.addSeparator() 
    500512 
    501         if rows == 1: 
     513        if self.rowHasConstraint(rows[0]): 
     514            menu.addAction(self.actionRemoveConstraint) 
     515        else: 
    502516            menu.addAction(self.actionConstrain) 
    503         elif rows == 2: 
    504             menu.addAction(self.actionMultiConstrain) 
     517        if num_rows == 2: 
    505518            menu.addAction(self.actionMutualMultiConstrain) 
    506         elif rows > 2: 
    507             menu.addAction(self.actionMultiConstrain) 
    508519 
    509520        # Define the callbacks 
    510521        self.actionConstrain.triggered.connect(self.addSimpleConstraint) 
     522        self.actionRemoveConstraint.triggered.connect(self.deleteConstraint) 
    511523        self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint) 
    512524        self.actionSelect.triggered.connect(self.selectParameters) 
     
    518530        Show the constraint widget and receive the expression 
    519531        """ 
    520         from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint 
    521         from .Constraints import Constraint 
    522532        selected_rows = self.lstParams.selectionModel().selectedRows() 
     533        assert(len(selected_rows), 2) 
    523534 
    524535        params_list = [s.data() for s in selected_rows] 
     536        # Create and display the widget for param1 and param2 
    525537        mc_widget = MultiConstraint(self, params=params_list) 
    526         mc_widget.exec_() 
     538        if mc_widget.exec_() != QtWidgets.QDialog.Accepted: 
     539            return 
     540 
    527541        constraint = Constraint() 
    528542        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 row 
    533         pass 
    534  
    535     def modifyViewOnConstraint(self, row): 
    536         """ 
    537         Add visual cues that the parameter is constrained 
    538         """ 
    539         value = self._model_model.item(row, 1).text() 
    540         # Set min/max to the value constrained 
    541         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 
    543557        font = QtGui.QFont() 
    544558        font.setItalic(True) 
    545559        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 
    546586        self._model_model.blockSignals(True) 
    547587        # Modify font and foreground of affected rows 
     
    549589            self._model_model.item(row, column).setForeground(brush) 
    550590            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) 
    552592        self._model_model.blockSignals(False) 
    553593 
     
    556596        Adds a constraint on a single parameter. 
    557597        """ 
    558         from .Constraints import Constraint 
    559598        for row in self.selectedParameters(): 
    560599            param = self._model_model.item(row, 0).text() 
    561600            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 
    563606            item = QtGui.QStandardItem() 
    564607            item.setData(constraint) 
    565608            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) 
    566612            #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) 
    568618        self.communicate.statusBarUpdateSignal.emit('Constraint added') 
    569619        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 
    570647 
    571648    def selectParameters(self): 
     
    9641041            np.any(res.pvec is None) or \ 
    9651042            not np.all(np.isfinite(res.pvec)): 
    966             msg = "Fitting did not converge!!!" 
     1043            msg = "Fitting did not converge!" 
    9671044            self.communicate.statusBarUpdateSignal.emit(msg) 
    9681045            logging.error(msg) 
  • src/sas/qtgui/Perspectives/Fitting/MultiConstraint.py

    r0595bb7 reae226b  
    3636        switch M1 <-> M2 
    3737        """ 
     38        # Switch parameters 
    3839        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 
    3945        self.setupLabels() 
    4046        self.setupTooltip() 
     
    8894        # 2. ensure the text contains parameter name 
    8995        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: 
    9297            return False 
    9398        parameter_string_end = parameter_string_start + len(param_str) 
    9499 
    95         # 3. parameter name should be a separate word, but can have "()[]*+-/" around 
     100        # 3. parameter name should be a separate word, but can have "()[]*+-/ " around 
    96101        valid_neighbours = "()[]*+-/ " 
    97102        has_only_parameter = False 
  • src/sas/qtgui/Perspectives/Fitting/UI/MultiConstraintUI.ui

    r7fd20fc reae226b  
    1010    <x>0</x> 
    1111    <y>0</y> 
    12     <width>335</width> 
     12    <width>369</width> 
    1313    <height>143</height> 
    1414   </rect> 
     
    158158 </widget> 
    159159 <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> 
    161178</ui> 
Note: See TracChangeset for help on using the changeset viewer.