Changeset 1396


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

fix bugs in multieq seq fits; print results better

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIexprGUI.py

    r1378 r1396  
    280280            if val:
    281281                self.varValue[v] = val
    282         wx.CallAfter(self.ShowVars())
     282        wx.CallAfter(self.ShowVars)
    283283
    284284    def Show(self,mode=True):
     
    289289        self.Layout()
    290290        self.mainsizer.Fit(self)
     291        self.SendSizeEvent() # force repaint
    291292        if self.ShowModal() == wx.ID_OK:
    292293            # store the edit results in the object and return it
     
    482483        self.varbox.Refresh()
    483484        self.Layout()
     485        self.mainsizer.Fit(self)
     486        self.SendSizeEvent() # force repaint
    484487        return
    485488
  • 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:
  • trunk/GSASIIplot.py

    r1386 r1396  
    24022402      number (or None) as the X-axis selection
    24032403    '''
    2404     G2frame.seqXselect = SelectX
    2405     try:
    2406         G2frame.seqXaxis
    2407     except:
    2408         G2frame.seqXaxis = None
    2409 
    24102404    def OnMotion(event):
    24112405        if event.xdata and event.ydata:                 #avoid out of frame errors
     
    24202414            Draw()
    24212415           
    2422     if fitnum is None:
    2423         label = 'Sequential refinement'
    2424     else:
    2425         label = 'Parametric fit #'+str(fitnum+1)
    2426     try:
    2427         plotNum = G2frame.G2plotNB.plotList.index(label)
    2428         Page = G2frame.G2plotNB.nb.GetPage(plotNum)
    2429         Page.figure.clf()
    2430         Plot = Page.figure.gca()
    2431         if not Page.IsShown():
    2432             Page.Show()
    2433     except ValueError:
    2434         Plot = G2frame.G2plotNB.addMpl(label).gca()
    2435         plotNum = G2frame.G2plotNB.plotList.index(label)
    2436         Page = G2frame.G2plotNB.nb.GetPage(plotNum)
    2437         Page.canvas.mpl_connect('key_press_event', OnKeyPress)
    2438         Page.canvas.mpl_connect('motion_notify_event', OnMotion)
    2439     Page.Choice = ['s to select plot x-axis',]
    2440     Page.keyPress = OnKeyPress
    2441     Page.seqYaxisList = ColumnList
    2442     Page.seqTableGet = TableGet
    2443     Page.fitvals = fitvals
    2444        
    24452416    def Draw():
    24462417        Page.SetFocus()
     
    24732444        Plot.set_xlabel(xName)
    24742445        Page.canvas.draw()           
     2446           
     2447    G2frame.seqXselect = SelectX
     2448    try:
     2449        G2frame.seqXaxis
     2450    except:
     2451        G2frame.seqXaxis = None
     2452
     2453    if fitnum is None:
     2454        label = 'Sequential refinement'
     2455    else:
     2456        label = 'Parametric fit #'+str(fitnum+1)
     2457    try:
     2458        plotNum = G2frame.G2plotNB.plotList.index(label)
     2459        Page = G2frame.G2plotNB.nb.GetPage(plotNum)
     2460        Page.figure.clf()
     2461        Plot = Page.figure.gca()
     2462        if not Page.IsShown():
     2463            Page.Show()
     2464    except ValueError:
     2465        Plot = G2frame.G2plotNB.addMpl(label).gca()
     2466        plotNum = G2frame.G2plotNB.plotList.index(label)
     2467        Page = G2frame.G2plotNB.nb.GetPage(plotNum)
     2468        Page.canvas.mpl_connect('key_press_event', OnKeyPress)
     2469        Page.canvas.mpl_connect('motion_notify_event', OnMotion)
     2470    Page.Choice = ['s to select plot x-axis',]
     2471    Page.keyPress = OnKeyPress
     2472    Page.seqYaxisList = ColumnList
     2473    Page.seqTableGet = TableGet
     2474    Page.fitvals = fitvals
     2475       
    24752476    Draw()
     2477    G2frame.G2plotNB.nb.SetSelection(plotNum) # raises plot tab
    24762478               
    24772479################################################################################
Note: See TracChangeset for help on using the changeset viewer.