def RefineCore(Controls,Histograms,Phases,restraintDict,rigidbodyDict,parmDict,varyList, calcControls,pawleyLookup,ifSeq,printFile,dlg,refPlotUpdate=None): '''Core optimization routines, shared between SeqRefine and Refine :returns: 5-tuple of ifOk (bool), Rvals (dict), result, covMatrix, sig ''' # print 'current',varyList # for item in parmDict: print item,parmDict[item] ######### show dict just before refinement G2mv.Map2Dict(parmDict,varyList) ifPrint = True if ifSeq: ifPrint = False Rvals = {} while True: begin = time.time() values = np.array(G2stMth.Dict2Values(parmDict, varyList)) if np.any(np.isnan(values)): raise G2obj.G2Exception('ERROR - nan found in LS parameters - use Calculate/View LS parms to locate') # test code to compute GOF and save for external repeat #args = ([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg) #print '*** before fit chi**2',np.sum(G2stMth.errRefine(values,*args)**2) #fl = open('beforeFit.cpickle','wb') #cPickle.dump(values,fl,1) #cPickle.dump(args[:-1],fl,1) #fl.close() Ftol = Controls['min dM/M'] Xtol = Controls['SVDtol'] Factor = Controls['shift factor'] if 'Jacobian' in Controls['deriv type']: result = so.leastsq(G2stMth.errRefine,values,Dfun=G2stMth.dervRefine,full_output=True, ftol=Ftol,col_deriv=True,factor=Factor, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) ncyc = int(result[2]['nfev']/2) if refPlotUpdate is not None: refPlotUpdate(Histograms) # update plot after completion elif 'analytic Hessian' in Controls['deriv type']: Lamda = Controls.get('Marquardt',-3) maxCyc = Controls['max cyc'] result = G2mth.HessianLSQ(G2stMth.errRefine,values,Hess=G2stMth.HessRefine,ftol=Ftol,xtol=Xtol,maxcyc=maxCyc,Print=ifPrint,lamda=Lamda, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg), refPlotUpdate=refPlotUpdate) ncyc = result[2]['num cyc']+1 Rvals['lamMax'] = result[2]['lamMax'] Controls['Marquardt'] = -3 #reset to default elif 'Hessian SVD' in Controls['deriv type']: maxCyc = Controls['max cyc'] result = G2mth.HessianSVD(G2stMth.errRefine,values,Hess=G2stMth.HessRefine,ftol=Ftol,xtol=Xtol,maxcyc=maxCyc,Print=ifPrint, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg), refPlotUpdate=refPlotUpdate) if result[1] is None: IfOK = False covMatrix = [] sig = len(varyList)*[None,] break ncyc = result[2]['num cyc']+1 else: #'numeric' result = so.leastsq(G2stMth.errRefine,values,full_output=True,ftol=Ftol,epsfcn=1.e-8,factor=Factor, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) ncyc = 1 if len(varyList): ncyc = int(result[2]['nfev']/len(varyList)) if refPlotUpdate is not None: refPlotUpdate(Histograms) # update plot # table = dict(zip(varyList,zip(values,result[0],(result[0]-values)))) # for item in table: print item,table[item] #useful debug - are things shifting? runtime = time.time()-begin Rvals['SVD0'] = result[2].get('SVD0',0) Rvals['converged'] = result[2].get('Converged') Rvals['DelChi2'] = result[2].get('DelChi2',-1.) Rvals['chisq'] = np.sum(result[2]['fvec']**2) G2stMth.Values2Dict(parmDict, varyList, result[0]) G2mv.Dict2Map(parmDict,varyList) Rvals['Nobs'] = Histograms['Nobs'] Rvals['Rwp'] = np.sqrt(Rvals['chisq']/Histograms['sumwYo'])*100. #to % Rvals['GOF'] = np.sqrt(Rvals['chisq']/(Histograms['Nobs']-len(varyList))) printFile.write(' Number of function calls: %d No. of observations: %d No. of parameters: %d User rejected: %d Sp. gp. extinct: %d\n'% \ (result[2]['nfev'],Histograms['Nobs'],len(varyList),Histograms['Nrej'],Histograms['Next'])) if ncyc: printFile.write(' Refinement time = %8.3fs, %8.3fs/cycle, for %d cycles\n'%(runtime,runtime/ncyc,ncyc)) printFile.write(' wR = %7.2f%%, chi**2 = %12.6g, GOF = %6.2f\n'%(Rvals['Rwp'],Rvals['chisq'],Rvals['GOF'])) sig = len(varyList)*[None,] if 'None' in str(type(result[1])) and ifSeq: #this bails out of a sequential refinement on singular matrix IfOK = False covMatrix = [] G2fil.G2Print ('Warning: **** Refinement failed - singular matrix ****') if 'Hessian' in Controls['deriv type']: num = len(varyList)-1 for i,val in enumerate(np.flipud(result[2]['psing'])): if val: G2fil.G2Print('Bad parameter: '+varyList[num-i],mode='warn') else: Ipvt = result[2]['ipvt'] for i,ipvt in enumerate(Ipvt): if not np.sum(result[2]['fjac'],axis=1)[i]: G2fil.G2Print('Bad parameter: '+varyList[ipvt-1],mode='warn') break IfOK = True try: covMatrix = result[1]*Rvals['GOF']**2 sig = np.sqrt(np.diag(covMatrix)) if np.any(np.isnan(sig)) or not sig.shape: G2fil.G2Print ('*** Least squares aborted - some invalid esds possible ***',mode='error') # table = dict(zip(varyList,zip(values,result[0],(result[0]-values)/sig))) # for item in table: print item,table[item] #useful debug - are things shifting? break #refinement succeeded - finish up! except TypeError: #result[1] is None on singular matrix or LinAlgError IfOK = False if not len(varyList): covMatrix = [] break G2fil.G2Print ('**** Refinement failed - singular matrix ****',mode='error') if 'Hessian' in Controls['deriv type']: if result[1] is None: IfOK = False covMatrix = [] sig = len(varyList)*[None,] break num = len(varyList)-1 for i,val in enumerate(np.flipud(result[2]['psing'])): if val: G2fil.G2Print ('Removing parameter: '+varyList[num-i]) del(varyList[num-i]) else: Ipvt = result[2]['ipvt'] for i,ipvt in enumerate(Ipvt): if not np.sum(result[2]['fjac'],axis=1)[i]: G2fil.G2Print ('Removing parameter: '+varyList[ipvt-1]) del(varyList[ipvt-1]) break if IfOK: G2stMth.GetFobsSq(Histograms,Phases,parmDict,calcControls) return IfOK,Rvals,result,covMatrix,sig
def RefineCore(Controls,Histograms,Phases,restraintDict,rigidbodyDict,parmDict,varyList, calcControls,pawleyLookup,ifPrint,printFile,dlg): 'Core optimization routines, shared between SeqRefine and Refine' # print 'current',varyList # for item in parmDict: print item,parmDict[item] ######### show dict just before refinement G2mv.Map2Dict(parmDict,varyList) Rvals = {} while True: begin = time.time() values = np.array(G2stMth.Dict2Values(parmDict, varyList)) # test code to compute GOF and save for external repeat #args = ([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg) #print '*** before fit chi**2',np.sum(G2stMth.errRefine(values,*args)**2) #fl = open('beforeFit.cpickle','wb') #import cPickle #cPickle.dump(values,fl,1) #cPickle.dump(args[:-1],fl,1) #fl.close() Ftol = Controls['min dM/M'] Factor = Controls['shift factor'] if 'Jacobian' in Controls['deriv type']: result = so.leastsq(G2stMth.errRefine,values,Dfun=G2stMth.dervRefine,full_output=True, ftol=Ftol,col_deriv=True,factor=Factor, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) ncyc = int(result[2]['nfev']/2) elif 'Hessian' in Controls['deriv type']: maxCyc = Controls['max cyc'] result = G2mth.HessianLSQ(G2stMth.errRefine,values,Hess=G2stMth.HessRefine,ftol=Ftol,maxcyc=maxCyc,Print=ifPrint, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) ncyc = result[2]['num cyc']+1 Rvals['lamMax'] = result[2]['lamMax'] else: #'numeric' result = so.leastsq(G2stMth.errRefine,values,full_output=True,ftol=Ftol,epsfcn=1.e-8,factor=Factor, args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) ncyc = 1 if len(varyList): ncyc = int(result[2]['nfev']/len(varyList)) # table = dict(zip(varyList,zip(values,result[0],(result[0]-values)))) # for item in table: print item,table[item] #useful debug - are things shifting? runtime = time.time()-begin Rvals['converged'] = result[2].get('Converged') Rvals['DelChi2'] = result[2].get('DelChi2',-1.) Rvals['chisq'] = np.sum(result[2]['fvec']**2) G2stMth.Values2Dict(parmDict, varyList, result[0]) G2mv.Dict2Map(parmDict,varyList) Rvals['Nobs'] = Histograms['Nobs'] Rvals['Rwp'] = np.sqrt(Rvals['chisq']/Histograms['sumwYo'])*100. #to % Rvals['GOF'] = np.sqrt(Rvals['chisq']/(Histograms['Nobs']-len(varyList))) print >>printFile,' Number of function calls:',result[2]['nfev'], \ ' No. of observations: ',Histograms['Nobs'],' No. of parameters: ',len(varyList), \ ' User rejected: ',Histograms['Nrej'],' Sp. gp. extinct: ',Histograms['Next'] print >>printFile,' Refinement time = %8.3fs, %8.3fs/cycle, for %d cycles'%(runtime,runtime/ncyc,ncyc) print >>printFile,' wR = %7.2f%%, chi**2 = %12.6g, GOF = %6.2f'%(Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']) IfOK = True try: covMatrix = result[1]*Rvals['GOF']**2 sig = np.sqrt(np.diag(covMatrix)) if np.any(np.isnan(sig)): print '*** Least squares aborted - some invalid esds possible ***' # table = dict(zip(varyList,zip(values,result[0],(result[0]-values)/sig))) # for item in table: print item,table[item] #useful debug - are things shifting? break #refinement succeeded - finish up! except TypeError,FloatingPointError: #result[1] is None on singular matrix IfOK = False if not len(varyList): covMatrix = [] sig = [] break print '**** Refinement failed - singular matrix ****' if 'Hessian' in Controls['deriv type']: num = len(varyList)-1 for i,val in enumerate(np.flipud(result[2]['psing'])): if val: print 'Removing parameter: ',varyList[num-i] del(varyList[num-i]) else: Ipvt = result[2]['ipvt'] for i,ipvt in enumerate(Ipvt): if not np.sum(result[2]['fjac'],axis=1)[i]: print 'Removing parameter: ',varyList[ipvt-1] del(varyList[ipvt-1]) break