Ignore:
Timestamp:
Jan 10, 2018 5: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:
e147ce2
Parents:
eae226b
git-author:
Piotr Rozyczko <rozyczko@…> (01/10/18 05:05:35)
git-committer:
Piotr Rozyczko <rozyczko@…> (01/10/18 05:15:53)
Message:

Added constraints to the fitter

File:
1 edited

Legend:

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

    reae226b rd3c0b95  
    462462        self.options_widget.plot_signal.connect(self.onOptionsUpdate) 
    463463 
     464    def modelName(self): 
     465        """ 
     466        Returns model name, by default M<tab#>, e.g. M1, M2 
     467        """ 
     468        return "M%i" % self.tab_id 
     469 
     470    def nameForFittedData(self, name): 
     471        """ 
     472        Generate name for the current fit 
     473        """ 
     474        if self.is2D: 
     475            name += "2d" 
     476        name = "%s [%s]" % (self.modelName(), name) 
     477        return name 
     478 
    464479    def showModelContextMenu(self, position): 
    465  
    466         #rows = len([s.row() for s in self.lstParams.selectionModel().selectedRows()]) 
     480        """ 
     481        Show context specific menu in the parameter table. 
     482        When clicked on parameter(s): fitting/constraints options 
     483        When clicked on white space: model description 
     484        """ 
    467485        rows = [s.row() for s in self.lstParams.selectionModel().selectedRows()] 
    468486        menu = self.showModelDescription() if not rows else self.modelContextMenu(rows) 
     
    475493    def modelContextMenu(self, rows): 
    476494        """ 
    477         Create context menu for the given model 
     495        Create context menu for the parameter selection 
    478496        """ 
    479497        menu = QtWidgets.QMenu() 
     
    482500        param_string = "parameter " if num_rows==1 else "parameters " 
    483501        to_string = "to its current value" if num_rows==1 else "to their current values" 
     502        has_constraints = any([self.rowHasConstraint(i) for i in rows]) 
    484503 
    485504        self.actionSelect = QtWidgets.QAction(self) 
     
    511530        menu.addSeparator() 
    512531 
    513         if self.rowHasConstraint(rows[0]): 
     532        if has_constraints: 
    514533            menu.addAction(self.actionRemoveConstraint) 
     534            #if num_rows == 1: 
     535            #    menu.addAction(self.actionEditConstraint) 
    515536        else: 
    516537            menu.addAction(self.actionConstrain) 
    517         if num_rows == 2: 
    518             menu.addAction(self.actionMutualMultiConstrain) 
     538            if num_rows == 2: 
     539                menu.addAction(self.actionMutualMultiConstrain) 
    519540 
    520541        # Define the callbacks 
     
    549570        item = QtGui.QStandardItem() 
    550571        item.setData(constraint) 
     572 
    551573        # Which row is the constrained parameter in? 
    552  
    553574        row = self.rowFromName(constraint.param) 
    554575        self._model_model.item(row, 1).setChild(0, item) 
    555576        #self.constraintAddedSignal.emit([row]) 
     577 
    556578        # Show visual hints for the constraint 
    557579        font = QtGui.QFont() 
     
    560582        self.modifyViewOnRow(row, font=font, brush=brush) 
    561583 
    562         # Pass the constraint to the parser 
     584        # Notify the user 
    563585        self.communicate.statusBarUpdateSignal.emit('Constraints added') 
    564586 
    565587    def rowFromName(self, name): 
    566588        """ 
    567         given parameter name get the row number in self._model_model 
     589        Given parameter name get the row number in self._model_model 
    568590        """ 
    569591        for row in range(self._model_model.rowCount()): 
     
    596618        Adds a constraint on a single parameter. 
    597619        """ 
     620        min_col = self.lstParams.itemDelegate().param_min 
     621        max_col = self.lstParams.itemDelegate().param_max 
    598622        for row in self.selectedParameters(): 
    599623            param = self._model_model.item(row, 0).text() 
    600624            value = self._model_model.item(row, 1).text() 
    601             min = self._model_model.item(row, 2).text() 
    602             max = self._model_model.item(row, 3).text() 
     625            min = self._model_model.item(row, min_col).text() 
     626            max = self._model_model.item(row, max_col).text() 
    603627            # Create a Constraint object 
    604628            constraint = Constraint(param=param, value=value, min=min, max=max) 
     
    608632            self._model_model.item(row, 1).setChild(0, item) 
    609633            # 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) 
     634            self._model_model.item(row, min_col).setText(value) 
     635            self._model_model.item(row, max_col).setText(value) 
    612636            #self.constraintAddedSignal.emit([row]) 
    613637            # Show visual hints for the constraint 
     
    623647        Delete constraints from selected parameters. 
    624648        """ 
     649        min_col = self.lstParams.itemDelegate().param_min 
     650        max_col = self.lstParams.itemDelegate().param_max 
    625651        for row in self.selectedParameters(): 
    626652            # Get the Constraint object from of the model item 
    627653            item = self._model_model.item(row, 1) 
     654            if not item.hasChildren(): 
     655                continue 
    628656            constraint = item.child(0).data() 
     657            if constraint is None: 
     658                continue 
     659            if not isinstance(constraint, Constraint): 
     660                continue 
    629661            # Retrieve old values and put them on the model 
    630662            if constraint.min is not None: 
    631                 self._model_model.item(row, 2).setText(constraint.min) 
     663                self._model_model.item(row, min_col).setText(constraint.min) 
    632664            if constraint.max is not None: 
    633                 self._model_model.item(row, 3).setText(constraint.max) 
     665                self._model_model.item(row, max_col).setText(constraint.max) 
    634666            # Remove constraint item 
    635667            item.removeRow(0) 
     
    639671        pass 
    640672 
     673    def getConstraintForRow(self, row): 
     674        """ 
     675        For the given row, return its constraint, if any 
     676        """ 
     677        try: 
     678            item = self._model_model.item(row, 1) 
     679            return item.child(0).data() 
     680        except AttributeError: 
     681            # return none when no constraints 
     682            return None 
     683 
    641684    def rowHasConstraint(self, row): 
    642685        """ 
     
    648691    def selectParameters(self): 
    649692        """ 
    650         Selected parameters are chosen for fitting 
     693        Selected parameter is chosen for fitting 
    651694        """ 
    652695        status = QtCore.Qt.Checked 
     
    662705    def selectedParameters(self): 
    663706        """ Returns list of selected (highlighted) parameters """ 
    664         return [s.row() for s in self.lstParams.selectionModel().selectedRows() if self.isCheckable(s.row())] 
     707        return [s.row() for s in self.lstParams.selectionModel().selectedRows() 
     708                if self.isCheckable(s.row())] 
    665709 
    666710    def setParameterSelection(self, status=QtCore.Qt.Unchecked): 
     
    671715        for row in self.selectedParameters(): 
    672716            self._model_model.item(row, 0).setCheckState(status) 
    673         pass 
     717        pass # debugger hook 
     718 
     719    def getConstraintsForModel(self): 
     720        """ 
     721        Return a list of tuples. Each tuple contains constraints mapped as 
     722        ('constrained parameter', 'function to constrain') 
     723        e.g. [('sld','5*sld_solvent')] 
     724        """ 
     725        model_name = self.modelName() 
     726        self.kernel_module.name = model_name 
     727        param_number = self._model_model.rowCount() 
     728        params = [(self._model_model.item(s, 0).text(), 
     729                    model_name+"."+self._model_model.item(s, 1).child(0).data().func) 
     730                    for s in range(param_number) if self.rowHasConstraint(s)] 
     731        return params 
    674732 
    675733    def showModelDescription(self): 
     
    936994        # These should be updating somehow? 
    937995        fit_id = 0 
    938         constraints = [] 
     996        constraints = self.getConstraintsForModel() 
    939997        smearer = None 
    940998        page_id = [210] 
     
    16071665                if isChecked(row_index)] 
    16081666 
    1609     def nameForFittedData(self, name): 
    1610         """ 
    1611         Generate name for the current fit 
    1612         """ 
    1613         if self.is2D: 
    1614             name += "2d" 
    1615         name = "M%i [%s]" % (self.tab_id, name) 
    1616         return name 
    1617  
    16181667    def createNewIndex(self, fitted_data): 
    16191668        """ 
Note: See TracChangeset for help on using the changeset viewer.