Changeset 1396 for trunk/GSASIIgrid.py


Ignore:
Timestamp:
Jun 25, 2014 6:20:41 PM (8 years ago)
Author:
toby
Message:

fix bugs in multieq seq fits; print results better

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIgrid.py

    r1390 r1396  
    113113
    114114[ wxID_INSTPRMRESET,wxID_CHANGEWAVETYPE,wxID_INSTCOPY, wxID_INSTFLAGCOPY, wxID_INSTLOAD,
    115     wxID_INSTSAVE,
    116 ] = [wx.NewId() for item in range(6)]
     115    wxID_INSTSAVE, wxID_INSTONEVAL
     116] = [wx.NewId() for item in range(7)]
    117117
    118118[ wxID_UNDO,wxID_LSQPEAKFIT,wxID_LSQONECYCLE,wxID_RESETSIGGAM,wxID_CLEARPEAKS,wxID_AUTOSEARCH,
     
    413413
    414414    def _onLoseFocus(self,event):
     415        # wx 2.9 patch: may be unregistered changes since backspace is not seen
     416        i,j= wx.__version__.split('.')[0:2]
     417        if int(i)+int(j)/10. > 2.8:
     418            if self.Validator: self.Validator.TestValid(self)
     419        # end patch
    415420        if self.evaluated:
    416421            self.EvaluateExpression()
     
    422427                                      **self.OnLeaveArgs)
    423428
    424     def _onLeaveWindow(self,event):
    425         if self.evaluated:
    426             self.EvaluateExpression()
    427         elif self.result is not None: # show formatted result, as Bob wants
    428             self._setValue(self.result[self.key])
     429    # def _onLeaveWindow(self,event):
     430    #     if self.evaluated:
     431    #         self.EvaluateExpression()
     432    #     elif self.result is not None: # show formatted result, as Bob wants
     433    #         self._setValue(self.result[self.key])
    429434
    430435    def EvaluateExpression(self):
     
    25922597#        self.InstEdit.Append(help='Change radiation type (Ka12 - synch)',
    25932598#            id=wxID_CHANGEWAVETYPE, kind=wx.ITEM_NORMAL,text='Change radiation')
     2599        self.InstEdit.Append(id=wxID_INSTONEVAL, kind=wx.ITEM_NORMAL,text='Set one value',
     2600            help='Set one instrument parameter value across multiple histograms')
     2601
    25942602        self.PostfillDataMenu()
    25952603       
     
    40454053    def EnableParFitEqMenus():
    40464054        'Enables or disables the Parametric Fit menu items that require existing defs'
    4047         if Controls['SeqParFitEqs']:
     4055        if Controls['SeqParFitEqList']:
    40484056            val = True
    40494057        else:
     
    40714079        calcObjList = [] # expression objects, ready to go for each data point
    40724080        if eqObj is not None:
    4073             eqObjDict = {eqObj.expression:eqObj}
     4081            eqObjList = [eqObj,]
    40744082        else:
    4075             eqObjDict = Controls['SeqParFitEqs']
    4076         for expr in eqObjDict.keys():
    4077             obj = eqObjDict[expr]
     4083            eqObjList = Controls['SeqParFitEqList']
     4084        for obj in eqObjList:
     4085            expr = obj.expression
    40784086            # assemble refined vars for this equation
    40794087            varyValueDict.update({var:val for var,val in obj.GetVariedVarVal()})
     
    41024110        if not varyList:
    41034111            print 'no variables to refine!'
    4104         else:
    4105             try:
    4106                 print 'Fit Results'
    4107                 result = so.leastsq(ParEqEval,varyValues,full_output=True,   #ftol=Ftol,
    4108                                     args=(calcObjList,varyList)
    4109                                     )
    4110                 values = result[0]
    4111                 covar = result[1]
    4112                 if covar is None:
    4113                     raise Exception
    4114                 for i,(var,val) in enumerate(zip(varyList,values)):
    4115                     print '  ',var,' =',G2mth.ValEsd(val,np.sqrt(covar[i,i]))
    4116             except:
    4117                 print 'Fit failed'
    4118                 return
     4112            return
     4113        try:
     4114            result = so.leastsq(ParEqEval,varyValues,full_output=True,   #ftol=Ftol,
     4115                                args=(calcObjList,varyList)
     4116                                )
     4117            values = result[0]
     4118            covar = result[1]
     4119            if covar is None:
     4120                raise Exception
     4121            esdDict = {}
     4122            for i,avar in enumerate(varyList):
     4123                esdDict[avar] = np.sqrt(covar[i,i])
     4124        except:
     4125            print('====> Fit failed')
     4126            return
     4127        print('==== Fit Results ====')
     4128        for obj in eqObjList:
     4129            ind = '      '
     4130            print('  '+obj.GetDepVar()+' = '+obj.expression)
     4131            for var in obj.assgnVars:
     4132                print(ind+var+' = '+obj.assgnVars[var])
     4133            for var in obj.freeVars:
     4134                avar = "::"+obj.freeVars[var][0]
     4135                val = obj.freeVars[var][1]
     4136                if obj.freeVars[var][2]:
     4137                    print(ind+var+' = '+avar + " = " + G2mth.ValEsd(val,esdDict[avar]))
     4138                else:
     4139                    print(ind+var+' = '+avar + " =" + G2mth.ValEsd(val,0))
    41194140        # create a plot for each parametric variable
    4120         for fitnum,expr in enumerate(sorted(eqObjDict)):
    4121             obj = eqObjDict[expr]
     4141        for fitnum,obj in enumerate(eqObjList):
    41224142            obj.UpdateVariedVars(varyList,values)
    41234143            calcobj = G2obj.ExpressionCalcObj(obj)
     
    41424162    def DelParFitEq(event):
    41434163        'Ask the user to select function to delete'
    4144         choices = sorted(Controls['SeqParFitEqs'].keys())
    4145         txtlst = [Controls['SeqParFitEqs'][i].GetDepVar()+' = '+i for i in choices]
     4164        txtlst = [obj.GetDepVar()+' = '+obj.expression for obj in Controls['SeqParFitEqList']]
    41464165        selected = ItemSelector(
    41474166            txtlst,G2frame.dataFrame,
    41484167            multiple=True,
    4149             title='Select a parametric equation to remove',
     4168            title='Select a parametric equation(s) to remove',
    41504169            header='Delete equation')
    41514170        if selected is None: return
    4152         for item in selected:
    4153             del Controls['SeqParFitEqs'][choices[item]]
     4171        Controls['SeqParFitEqList'] = [obj for i,obj in enumerate(Controls['SeqParFitEqList']) if i not in selected]
    41544172        EnableParFitEqMenus()
    4155         if Controls['SeqParFitEqs']: DoParEqFit(event)
     4173        if Controls['SeqParFitEqList']: DoParEqFit(event)
    41564174       
    41574175    def EditParFitEq(event):
    41584176        'Edit an existing parametric equation'
    4159         choices = sorted(Controls['SeqParFitEqs'].keys())
    4160         txtlst = [Controls['SeqParFitEqs'][i].GetDepVar()+' = '+i for i in choices]
    4161         if len(choices) == 1:
     4177        txtlst = [obj.GetDepVar()+' = '+obj.expression for obj in Controls['SeqParFitEqList']]
     4178        if len(txtlst) == 1:
    41624179            selected = 0
    41634180        else:
     
    41704187            dlg = G2exG.ExpressionDialog(
    41714188                G2frame.dataDisplay,indepVarDict,
    4172                 Controls['SeqParFitEqs'][choices[selected]],
     4189                Controls['SeqParFitEqList'][selected],
    41734190                depVarDict=depVarDict,
    41744191                header="Edit the formula for this minimization function",
     
    41774194            if newobj:
    41784195                calcobj = G2obj.ExpressionCalcObj(newobj)
    4179                 del Controls['SeqParFitEqs'][choices[selected]]
    4180                 Controls['SeqParFitEqs'][calcobj.eObj.expression] = newobj
     4196                Controls['SeqParFitEqList'][selected] = newobj
    41814197                EnableParFitEqMenus()
    4182                 if Controls['SeqParFitEqs']: DoParEqFit(event)
     4198            if Controls['SeqParFitEqList']: DoParEqFit(event)
    41834199
    41844200    def AddNewParFitEq(event):
    41854201        'Create a new parametric equation to be fit to sequential results'
    41864202
    4187         # compile the variable names in previous freevars to avoid accidental name collisions
     4203        # compile the variable names used in previous freevars to avoid accidental name collisions
    41884204        usedvarlist = []
    4189         for eq,obj in Controls['SeqParFitEqs'].items():
     4205        for obj in Controls['SeqParFitEqList']:
     4206            eq = obj.expression           
    41904207            for var in obj.freeVars:
    41914208                if obj.freeVars[var][0] not in usedvarlist: usedvarlist.append(obj.freeVars[var][0])
     
    42004217        dlg.Destroy()
    42014218        if obj:
    4202             calcobj = G2obj.ExpressionCalcObj(obj)
    4203             Controls['SeqParFitEqs'][calcobj.eObj.expression] = obj
     4219            Controls['SeqParFitEqList'].append(obj)
    42044220            EnableParFitEqMenus()
    4205             if Controls['SeqParFitEqs']: DoParEqFit(event)
     4221            if Controls['SeqParFitEqList']: DoParEqFit(event)
    42064222                                           
    42074223    def GridSetToolTip(row,col):
     
    42654281    # create a place to store Pseudo Vars & Parametric Fit functions, if needed
    42664282    if 'SeqPseudoVars' not in Controls: Controls['SeqPseudoVars'] = {}
    4267     if 'SeqParFitEqs' not in Controls: Controls['SeqParFitEqs'] = {}
     4283    if 'SeqParFitEqList' not in Controls: Controls['SeqParFitEqList'] = []
    42684284    histNames = data['histNames']
    42694285    if G2frame.dataDisplay:
Note: See TracChangeset for help on using the changeset viewer.