Changeset 1812


Ignore:
Timestamp:
Apr 27, 2015 1:22:06 PM (7 years ago)
Author:
vondreele
Message:

make new ranId for phases imported from gpx files
eliminate all raise Exceptions from Refine & seqRefine - now gives a ErrorMessage? popup upon e.g. user abort.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r1809 r1812  
    35043504        Rw = 100.00
    35053505        try:
    3506             Rw = G2stMn.Refine(self.GSASprojectfile,dlg)
     3506            OK,Msg = G2stMn.Refine(self.GSASprojectfile,dlg)
    35073507        finally:
    35083508            dlg.Update(101.) # forces the Auto_Hide; needed after move w/Win & wx3.0
     
    35163516            if parentId:
    35173517                parentName = self.PatternTree.GetItemText(parentId)     #find the current data tree name
    3518         dlg2 = wx.MessageDialog(self,'Load new result?','Refinement results, Rw =%.3f'%(Rw),wx.OK|wx.CANCEL)
    3519         try:
    3520             if dlg2.ShowModal() == wx.ID_OK:
    3521                 Id = 0
    3522                 self.PatternTree.DeleteChildren(self.root)
    3523                 if self.HKL: self.HKL = []
    3524                 if self.G2plotNB.plotList:
    3525                     self.G2plotNB.clear()
    3526                 G2IO.ProjFileOpen(self)
    3527                 item, cookie = self.PatternTree.GetFirstChild(self.root)
    3528                 while item and not Id:
    3529                     name = self.PatternTree.GetItemText(item)
    3530                     if name[:4] in ['PWDR','HKLF']:
    3531                         Id = item
    3532                     item, cookie = self.PatternTree.GetNextChild(self.root, cookie)               
    3533                 if Id:
    3534                     self.PatternTree.SelectItem(Id)
    3535                 if parentName:
    3536                     parentId = G2gd.GetPatternTreeItemId(self, self.root, parentName)
    3537                     if parentId:
    3538                         itemId = G2gd.GetPatternTreeItemId(self, parentId, oldName)
    3539                     else:
    3540                         itemId = G2gd.GetPatternTreeItemId(self, self.root, oldName)
    3541                     self.PatternTree.SelectItem(itemId)
    3542         finally:
    3543             dlg2.Destroy()
     3518        if OK:
     3519            Rw = Msg
     3520            dlg2 = wx.MessageDialog(self,'Load new result?','Refinement results, Rw =%.3f'%(Rw),wx.OK|wx.CANCEL)
     3521            try:
     3522                if dlg2.ShowModal() == wx.ID_OK:
     3523                    Id = 0
     3524                    self.PatternTree.DeleteChildren(self.root)
     3525                    if self.HKL: self.HKL = []
     3526                    if self.G2plotNB.plotList:
     3527                        self.G2plotNB.clear()
     3528                    G2IO.ProjFileOpen(self)
     3529                    item, cookie = self.PatternTree.GetFirstChild(self.root)
     3530                    while item and not Id:
     3531                        name = self.PatternTree.GetItemText(item)
     3532                        if name[:4] in ['PWDR','HKLF']:
     3533                            Id = item
     3534                        item, cookie = self.PatternTree.GetNextChild(self.root, cookie)               
     3535                    if Id:
     3536                        self.PatternTree.SelectItem(Id)
     3537                    if parentName:
     3538                        parentId = G2gd.GetPatternTreeItemId(self, self.root, parentName)
     3539                        if parentId:
     3540                            itemId = G2gd.GetPatternTreeItemId(self, parentId, oldName)
     3541                        else:
     3542                            itemId = G2gd.GetPatternTreeItemId(self, self.root, oldName)
     3543                        self.PatternTree.SelectItem(itemId)
     3544            finally:
     3545                dlg2.Destroy()
     3546        else:
     3547            self.ErrorDialog('Refinement error',Msg)
    35443548
    35453549    def OnSeqRefine(self,event):
  • trunk/GSASIIstrMain.py

    r1800 r1812  
    147147    Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile)
    148148    if not Phases:
    149         print ' *** ERROR - you have no phases! ***'
     149        print ' *** ERROR - you have no phases to refine! ***'
    150150        print ' *** Refine aborted ***'
    151         raise Exception
     151        return False,'No phases'
    152152    if not Histograms:
    153153        print ' *** ERROR - you have no data to refine with! ***'
    154154        print ' *** Refine aborted ***'
    155         raise Exception       
     155        return False,'No data'
    156156    rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
    157157    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
     
    183183        #print 'Errors',errmsg
    184184        #if warnmsg: print 'Warnings',warnmsg
    185         raise Exception(' *** Refine aborted ***')
     185        return False,' Constraint error'
    186186#    print G2mv.VarRemapShow(varyList)
    187187   
     
    189189    print >>printFile,'\n Refinement results:'
    190190    print >>printFile,135*'-'
    191     IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histograms,Phases,restraintDict,
    192         rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifPrint,printFile,dlg)
    193     sigDict = dict(zip(varyList,sig))
    194     newCellDict = G2stMth.GetNewCellParms(parmDict,varyList)
    195     newAtomDict = G2stMth.ApplyXYZshifts(parmDict,varyList)
    196     covData = {'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals,
    197                'varyListStart':varyListStart,
    198                'covMatrix':covMatrix,'title':GPXfile,'newAtomDict':newAtomDict,
    199                'newCellDict':newCellDict,'freshCOV':True}
    200     # add the uncertainties into the esd dictionary (sigDict)
    201     sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict))
    202     G2mv.PrintIndependentVars(parmDict,varyList,sigDict,pFile=printFile)
    203     G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True)
    204     G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile)
    205     G2stIO.SetPhaseData(parmDict,sigDict,Phases,rbIds,covData,restraintDict,printFile)
    206     G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histograms,pFile=printFile)
    207     G2stIO.SetHistogramData(parmDict,sigDict,Histograms,pFile=printFile)
    208     G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,rigidbodyDict,covData)
    209     printFile.close()
    210     print ' Refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst'
    211     print ' ***** Refinement successful *****'
     191    try:
     192        IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histograms,Phases,restraintDict,
     193            rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifPrint,printFile,dlg)
     194        sigDict = dict(zip(varyList,sig))
     195        newCellDict = G2stMth.GetNewCellParms(parmDict,varyList)
     196        newAtomDict = G2stMth.ApplyXYZshifts(parmDict,varyList)
     197        covData = {'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals,
     198                   'varyListStart':varyListStart,
     199                   'covMatrix':covMatrix,'title':GPXfile,'newAtomDict':newAtomDict,
     200                   'newCellDict':newCellDict,'freshCOV':True}
     201        # add the uncertainties into the esd dictionary (sigDict)
     202        sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict))
     203        G2mv.PrintIndependentVars(parmDict,varyList,sigDict,pFile=printFile)
     204        G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True)
     205        G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile)
     206        G2stIO.SetPhaseData(parmDict,sigDict,Phases,rbIds,covData,restraintDict,printFile)
     207        G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histograms,pFile=printFile)
     208        G2stIO.SetHistogramData(parmDict,sigDict,Histograms,pFile=printFile)
     209        G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,rigidbodyDict,covData)
     210        printFile.close()
     211        print ' Refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst'
     212        print ' ***** Refinement successful *****'
     213    except G2stMth.UserAbort:
     214        printFile.close()
     215        return False,'Refinement aborted by user'
    212216   
    213217#for testing purposes!!!
     
    227231
    228232    if dlg:
    229         return Rvals['Rwp']
     233        return True,Rvals['Rwp']
    230234
    231235def SeqRefine(GPXfile,dlg):
     
    378382        print >>printFile,'\n Refinement results for histogram: v'+histogram
    379383        print >>printFile,135*'-'
    380         IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histo,Phases,restraintDict,
    381             rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifPrint,printFile,dlg)
    382 
    383         print '  wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%(
    384             Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2'])
    385         # add the uncertainties into the esd dictionary (sigDict)
    386         sigDict = dict(zip(varyList,sig))
    387         # the uncertainties for dependent constrained parms into the esd dict
    388         sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict))
    389 
    390         # a dict with values & esds for dependent (constrained) parameters
    391         depParmDict = {i:(parmDict[i],sigDict[i]) for i in varyListStart
    392                        if i not in varyList}
    393         newCellDict = copy.deepcopy(G2stMth.GetNewCellParms(parmDict,varyList))
    394         newAtomDict = copy.deepcopy(G2stMth.ApplyXYZshifts(parmDict,varyList))
    395         histRefData = {
    396             'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals,
    397             'varyListStart':varyListStart,
    398             'covMatrix':covMatrix,'title':histogram,'newAtomDict':newAtomDict,
    399             'newCellDict':newCellDict,'depParmDict':depParmDict,
    400             'constraintInfo':constraintInfo,
    401             'parmDict':parmDict}
    402         SeqResult[histogram] = histRefData
    403         G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True)
    404 #        G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile)
    405         G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histo,ifPrint,printFile)
    406         G2stIO.SetHistogramData(parmDict,sigDict,Histo,ifPrint,printFile)
    407         G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histo,Phases,rigidbodyDict,histRefData,makeBack)
    408         makeBack = False
    409         NewparmDict = {}
    410         # make dict of varied parameters in current histogram, renamed to
    411         # next histogram, for use in next refinement.
    412         if Controls['Copy2Next'] and ihst < len(histNames)-1:
    413             hId = Histo[histogram]['hId'] # current histogram
    414             nexthId = Histograms[histNames[ihst+1]]['hId']
    415             for parm in set(list(varyList)+list(varyListStart)):
    416                 items = parm.split(':')
    417                 if len(items) < 3: continue
    418                 if str(hId) in items[1]:
    419                     items[1] = str(nexthId)
    420                     newparm = ':'.join(items)
    421                     NewparmDict[newparm] = parmDict[parm]
     384        try:
     385            IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histo,Phases,restraintDict,
     386                rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifPrint,printFile,dlg)
     387   
     388            print '  wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%(
     389                Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2'])
     390            # add the uncertainties into the esd dictionary (sigDict)
     391            sigDict = dict(zip(varyList,sig))
     392            # the uncertainties for dependent constrained parms into the esd dict
     393            sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict))
     394   
     395            # a dict with values & esds for dependent (constrained) parameters
     396            depParmDict = {i:(parmDict[i],sigDict[i]) for i in varyListStart
     397                           if i not in varyList}
     398            newCellDict = copy.deepcopy(G2stMth.GetNewCellParms(parmDict,varyList))
     399            newAtomDict = copy.deepcopy(G2stMth.ApplyXYZshifts(parmDict,varyList))
     400            histRefData = {
     401                'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals,
     402                'varyListStart':varyListStart,
     403                'covMatrix':covMatrix,'title':histogram,'newAtomDict':newAtomDict,
     404                'newCellDict':newCellDict,'depParmDict':depParmDict,
     405                'constraintInfo':constraintInfo,
     406                'parmDict':parmDict}
     407            SeqResult[histogram] = histRefData
     408            G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True)
     409    #        G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile)
     410            G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histo,ifPrint,printFile)
     411            G2stIO.SetHistogramData(parmDict,sigDict,Histo,ifPrint,printFile)
     412            G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histo,Phases,rigidbodyDict,histRefData,makeBack)
     413            makeBack = False
     414            NewparmDict = {}
     415            # make dict of varied parameters in current histogram, renamed to
     416            # next histogram, for use in next refinement.
     417            if Controls['Copy2Next'] and ihst < len(histNames)-1:
     418                hId = Histo[histogram]['hId'] # current histogram
     419                nexthId = Histograms[histNames[ihst+1]]['hId']
     420                for parm in set(list(varyList)+list(varyListStart)):
     421                    items = parm.split(':')
     422                    if len(items) < 3: continue
     423                    if str(hId) in items[1]:
     424                        items[1] = str(nexthId)
     425                        newparm = ':'.join(items)
     426                        NewparmDict[newparm] = parmDict[parm]
     427        except G2stMth.UserAbort:
     428            printFile.close()
     429            print ' ***** Refinement aborted *****'
     430            return False,' Refinement aborted by user'
    422431    G2stIO.SetSeqResult(GPXfile,Histograms,SeqResult)
    423432    printFile.close()
  • trunk/GSASIIstrMath.py

    r1810 r1812  
    3838twopi = 2.0*np.pi
    3939twopisq = 2.0*np.pi**2
     40
     41class UserAbort(BaseException):
     42    def __init__(self,line):
     43        self.line = line
     44       
    4045
    4146################################################################################
     
    573578                except ValueError:
    574579                    pass
    575                    
    576 #    raise Exception
    577580    return pDerv
    578581
     
    12801283    for item in SHnames:
    12811284        L,N = eval(item.strip('C'))
    1282 #        Kcl = G2lat.GetKcl(L,N,SGData['SGLaue'],phi,beta)
    1283 #        Ksl,x,x = G2lat.GetKsl(L,0,'0',psi,gam)
    1284 #        Lnorm = G2lat.Lnorm(L)
    1285 #        odfCor += parmDict[phfx+item]*Lnorm*Kcl*Ksl
    1286         Kcsl,Lnorm = G2lat.GetKclKsl(L,N,SGData['SGLaue'],psi,phi,beta)
    1287         odfCor += parmDict[phfx+item]*Lnorm*Kcsl
     1285        Kcl = G2lat.GetKcl(L,N,SGData['SGLaue'],phi,beta)
     1286        Ksl,x,x = G2lat.GetKsl(L,0,'0',psi,gam)
     1287        Lnorm = G2lat.Lnorm(L)
     1288        odfCor += parmDict[phfx+item]*Lnorm*Kcl*Ksl
     1289#        Kcsl,Lnorm = G2lat.GetKclKsl(L,N,SGData['SGLaue'],psi,phi,beta)
     1290#        odfCor += parmDict[phfx+item]*Lnorm*Kcsl
    12881291    return np.squeeze(odfCor)
    12891292   
     
    13101313    for item in SHnames:
    13111314        L,N = eval(item.strip('C'))
    1312 #        Kcl = G2lat.GetKcl(L,N,SGData['SGLaue'],phi,beta)
    1313 #        Ksl,x,x = G2lat.GetKsl(L,0,'0',psi,gam)
    1314 #        Lnorm = G2lat.Lnorm(L)
    1315 #        odfCor += parmDict[phfx+item]*Lnorm*Kcl*Ksl
    1316 #        dFdODF[phfx+item] = Kcl*Ksl*Lnorm
    1317         Kcsl,Lnorm = G2lat.GetKclKsl(L,N,SGData['SGLaue'],psi,phi,beta)
    1318         odfCor += parmDict[phfx+item]*Lnorm*Kcsl
    1319         dFdODF[phfx+item] = Kcsl*Lnorm
     1315        Kcl = G2lat.GetKcl(L,N,SGData['SGLaue'],phi,beta)
     1316        Ksl,x,x = G2lat.GetKsl(L,0,'0',psi,gam)
     1317        Lnorm = G2lat.Lnorm(L)
     1318        odfCor += parmDict[phfx+item]*Lnorm*Kcl*Ksl
     1319        dFdODF[phfx+item] = Kcl*Ksl*Lnorm
     1320#        Kcsl,Lnorm = G2lat.GetKclKsl(L,N,SGData['SGLaue'],psi,phi,beta)
     1321#        odfCor += parmDict[phfx+item]*Lnorm*Kcsl
     1322#        dFdODF[phfx+item] = Kcsl*Lnorm
    13201323    return odfCor,dFdODF
    13211324   
     
    24122415    return True
    24132416   
    2414    
    2415    
    24162417def dervHKLF(Histogram,Phase,calcControls,varylist,parmDict,rigidbodyDict):
    24172418    '''Loop over reflections in a HKLF histogram and compute derivatives of the fitting
     
    28002801            parmDict['saved values'] = values
    28012802            dlg.Destroy()
    2802             raise Exception         #Abort!!
     2803            raise UserAbort('User abort')         #Abort!!
    28032804    pDict,pVals,pWt,pWsum = penaltyFxn(HistoPhases,calcControls,parmDict,varylist)
    28042805    if len(pVals):
  • trunk/imports/G2phase_GPX.py

    r1168 r1812  
    1717import sys
    1818import cPickle
     19import random as ran
    1920import GSASIIIO as G2IO
    2021import GSASIIstrIO as G2stIO
     
    6768            self.Phase['Histograms'] = {}       #remove any histograms
    6869            self.Phase['Pawley ref'] = []       # & any Pawley refl.
     70            self.Phase['ranId'] = ran.randint(0,sys.maxint)
    6971            return True
    7072        except Exception as detail:
Note: See TracChangeset for help on using the changeset viewer.