Changeset 3913
- Timestamp:
- Apr 21, 2019 10:05:23 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIdataGUI.py
r3909 r3913 3880 3880 self.GPXtree.Expand(Id) 3881 3881 SelectDataTreeItem(self,Id) 3882 self.GPXtree.SelectItem(Id) # needed on OSX or item is not selected in tree; perhaps not needed elsewhere 3882 3883 elif phaseId: 3883 3884 SelectDataTreeItem(self,phaseId) 3885 self.GPXtree.SelectItem(phaseId) # as before for OSX 3884 3886 self.CheckNotebook() 3885 3887 if self.dirname: os.chdir(self.dirname) # to get Mac/Linux to change directory! … … 4555 4557 return 4556 4558 if warnmsg: 4557 print('Conflict between refin ment flag settings and constraints:\n'+4559 print('Conflict between refinement flag settings and constraints:\n'+ 4558 4560 warnmsg+'\nRefinement not possible') 4559 4561 self.ErrorDialog('Refinement Flag Error', … … 4571 4573 self.SaveTreeSetting() # save the current tree selection 4572 4574 self.GPXtree.SaveExposedItems() # save the exposed/hidden tree items 4575 refPlotUpdate = G2plt.PlotPatterns(self,refineMode=True) # prepare for plot updating 4573 4576 try: 4574 OK,Msg = G2stMn.Refine(self.GSASprojectfile,dlg ) #Msg is Rvals dict if Ok=True4577 OK,Msg = G2stMn.Refine(self.GSASprojectfile,dlg,refPlotUpdate=refPlotUpdate) #Msg is Rvals dict if Ok=True 4575 4578 finally: 4576 4579 dlg.Update(101.) # forces the Auto_Hide; needed after move w/Win & wx3.0 … … 4589 4592 try: 4590 4593 if dlg2.ShowModal() == wx.ID_OK: 4594 if refPlotUpdate: refPlotUpdate({},restore=True) 4591 4595 wx.CallAfter(self.reloadFromGPX) 4596 else: 4597 if refPlotUpdate: refPlotUpdate({},restore=True) 4592 4598 finally: 4593 4599 dlg2.Destroy() 4594 4600 else: 4595 4601 self.ErrorDialog('Refinement error',Msg) 4596 4602 4597 4603 def reloadFromGPX(self): 4598 4604 '''Deletes current data tree & reloads it from GPX file (after a … … 4644 4650 try: 4645 4651 win.replotFunction(*win.replotArgs,**win.replotKWargs) 4646 except :4652 except Exception as msg: 4647 4653 if GSASIIpath.GetConfigValue('debug'): 4648 print('Error with args',win.replotArgs,win.replotKWargs) 4654 print('Error calling',win.replotFunction,'with args', 4655 win.replotArgs,win.replotKWargs) 4656 if GSASIIpath.GetConfigValue('debug'): 4657 print(msg) 4658 GSASIIpath.IPyBreak() 4649 4659 # delete any remaining plots that have not been updated and need a refresh (win.plotRequiresRedraw) 4650 4660 for lbl,win in zip(self.G2plotNB.plotList,self.G2plotNB.panelList): … … 4665 4675 self.OnRefine(event) 4666 4676 return 4677 plotHist = self.GPXtree.GetItemText(self.PatternId) 4667 4678 Id = GetGPXtreeItemId(self,self.root,'Sequential results') 4668 4679 if not Id: … … 4694 4705 dlg.SetSize((int(Size[0]*1.2),Size[1])) # increase size a bit along x 4695 4706 dlg.CenterOnParent() 4707 # find 1st histogram to be refined 4708 if 'Seq Data' in Controls: 4709 histNames = Controls['Seq Data'] 4710 else: # patch from before Controls['Seq Data'] was implemented 4711 histNames = G2stIO.GetHistogramNames(GPXfile,['PWDR',]) 4712 if Controls.get('Reverse Seq'): 4713 histNames.reverse() 4714 # select it 4715 self.PatternId = GetGPXtreeItemId(self,self.root,histNames[0]) 4716 refPlotUpdate = G2plt.PlotPatterns(self,refineMode=True) # prepare for plot updating 4696 4717 try: 4697 OK,Msg = G2stMn.SeqRefine(self.GSASprojectfile,dlg, G2plt.SequentialPlotPattern,self)#Msg is Rvals dict if Ok=True4718 OK,Msg = G2stMn.SeqRefine(self.GSASprojectfile,dlg,refPlotUpdate) #Msg is Rvals dict if Ok=True 4698 4719 finally: 4699 4720 dlg.Update(101.) # forces the Auto_Hide; needed after move w/Win & wx3.0 … … 4704 4725 try: 4705 4726 if dlg.ShowModal() == wx.ID_OK: 4727 if refPlotUpdate: refPlotUpdate({},restore=True) 4706 4728 self.PickIdText = None #force reload of PickId contents 4707 4729 self.GPXtree.DeleteChildren(self.root) … … 4712 4734 self.GPXtree.RestoreExposedItems() 4713 4735 self.ResetPlots() 4736 self.PatternId = GetGPXtreeItemId(self,self.root,plotHist) 4737 SelectDataTreeItem(self,self.PatternId) 4714 4738 sId = GetGPXtreeItemId(self,self.root,'Sequential results') 4715 4739 SelectDataTreeItem(self,sId) 4740 self.GPXtree.SelectItem(sId) 4741 else: 4742 if refPlotUpdate: refPlotUpdate({},restore=True) 4716 4743 finally: 4717 4744 dlg.Destroy() -
trunk/GSASIImath.py
r3900 r3913 99 99 return res,nzero 100 100 101 def HessianLSQ(func,x0,Hess,args=(),ftol=1.49012e-8,xtol=1.e-6, maxcyc=0,lamda=-3,Print=False): 102 101 def HessianLSQ(func,x0,Hess,args=(),ftol=1.49012e-8,xtol=1.e-6, maxcyc=0,lamda=-3,Print=False,refPlotUpdate=None): 103 102 """ 104 103 Minimize the sum of squares of a function (:math:`f`) evaluated on a series of … … 209 208 print (' Cycle: %d, Time: %.2fs, Chi**2: %.5g for %d obs., Lambda: %.3g, Delta: %.3g'%( 210 209 icycle,time.time()-time0,chisq1,Nobs,lamMax,deltaChi2)) 210 Histograms = args[0][0] 211 if refPlotUpdate is not None: refPlotUpdate(Histograms,icycle) # update plot 211 212 if deltaChi2 < ftol: 212 213 ifConverged = True … … 233 234 return [x0,None,{'num cyc':icycle,'fvec':M,'nfev':nfev,'lamMax':lamMax,'psing':psing,'SVD0':-1}] 234 235 235 def HessianSVD(func,x0,Hess,args=(),ftol=1.49012e-8,xtol=1.e-6, maxcyc=0,lamda=-3,Print=False ):236 def HessianSVD(func,x0,Hess,args=(),ftol=1.49012e-8,xtol=1.e-6, maxcyc=0,lamda=-3,Print=False,refPlotUpdate=None): 236 237 237 238 """ … … 319 320 print (' Cycle: %d, Time: %.2fs, Chi**2: %.5g, Delta: %.3g'%( 320 321 icycle,time.time()-time0,chisq1,deltaChi2)) 322 Histograms = args[0][0] 323 if refPlotUpdate is not None: refPlotUpdate(Histograms,icycle) # update plot 321 324 if deltaChi2 < ftol: 322 325 ifConverged = True -
trunk/GSASIIplot.py
r3909 r3913 1744 1744 ##### PlotPatterns 1745 1745 ################################################################################ 1746 def SequentialPlotPattern(G2frame,refdata,histogram):1747 '''This is passed into :func:`GSASIIstrMain.SeqRefine` where it is used to1748 provide a plot of the current powder histogram just after a refinement. It1749 takes the old refinement information (Rfactors, curve locations, etc.) and1750 combines it with the refinement results in refdata and passes that to1751 :func:`PlotPatterns`1752 '''1753 if not histogram.startswith('PWDR'): return1754 pickId = G2frame.PickId1755 G2frame.PickId = G2frame.PatternId = G2gd.GetGPXtreeItemId(G2frame, G2frame.root, histogram)1756 treedata = G2frame.GPXtree.GetItemPyData(G2frame.PatternId)1757 PlotPatterns(G2frame,newPlot=True,plotType='PWDR',data=[treedata[0],refdata])1758 wx.Yield() # force a plot update (needed on Windows?)1759 G2frame.PickId = pickId1760 1761 1746 def ReplotPattern(G2frame,newPlot,plotType,PatternName=None,PickName=None): 1762 1747 '''This does the same as PlotPatterns except that it expects the information … … 1777 1762 1778 1763 def PlotPatterns(G2frame,newPlot=False,plotType='PWDR',data=None, 1779 extraKeys=[] ):1764 extraKeys=[],refineMode=False): 1780 1765 '''Powder pattern plotting package - displays single or multiple powder patterns as intensity vs 1781 1766 2-theta, q or TOF. Can display multiple patterns as "waterfall plots" or contour plots. Log I … … 2594 2579 else: 2595 2580 Plot.set_ylim(CurLims['ylims']) 2581 Page.toolbar.push_current() 2596 2582 Plot.figure.canvas.draw() 2597 2583 #GSASIIpath.IPyBreak() 2598 2584 2599 2585 def onPlotFormat(event): 2586 '''Change the appearance of the current plot''' 2600 2587 changePlotSettings(G2frame,Plot) 2588 2589 def refPlotUpdate(Histograms,cycle=None,restore=False): 2590 '''called to update an existing plot during a Rietveld fit 2591 ''' 2592 if restore: 2593 (G2frame.SinglePlot,G2frame.Contour,G2frame.Weight, 2594 G2frame.plusPlot,G2frame.SubBack,Page.plotStyle['logPlot']) = savedSettings 2595 return 2596 2597 if plottingItem not in Histograms: 2598 histoList = [i for i in Histograms.keys() if i.startswith('PWDR ')] 2599 if len(histoList) == 0: 2600 print('Skipping plot, no PWDR item found!') 2601 return 2602 plotItem = histoList[0] 2603 else: 2604 plotItem = plottingItem 2605 xye = np.array(ma.getdata(Histograms[plotItem]['Data'])) # strips mask 2606 xye0 = Histograms[plotItem]['Data'][0] 2607 if Page.plotStyle['qPlot']: 2608 X = 2.*np.pi/G2lat.Pos2dsp(Parms,xye0) 2609 Ibeg = np.searchsorted(X,2.*np.pi/G2lat.Pos2dsp(Parms,limits[1][0])) 2610 Ifin = np.searchsorted(X,2.*np.pi/G2lat.Pos2dsp(Parms,limits[1][1])) 2611 elif Page.plotStyle['dPlot']: 2612 X = G2lat.Pos2dsp(Parms,xye0) 2613 Ibeg = np.searchsorted(X,G2lat.Pos2dsp(Parms,limits[1][1])) 2614 Ifin = np.searchsorted(X,G2lat.Pos2dsp(Parms,limits[1][0])) 2615 else: 2616 X = copy.deepcopy(xye0) 2617 Ibeg = np.searchsorted(X,limits[1][0]) 2618 Ifin = np.searchsorted(X,limits[1][1]) 2619 if Page.plotStyle['sqrtPlot']: 2620 olderr = np.seterr(invalid='ignore') #get around sqrt(-ve) error 2621 Y = np.where(xye[1]>=0.,np.sqrt(xye[1]),-np.sqrt(-xye[1])) 2622 Z = np.where(xye[3]>=0.,np.sqrt(xye[3]),-np.sqrt(-xye[3])) 2623 W = np.where(xye[4]>=0.,np.sqrt(xye[4]),-np.sqrt(-xye[4])) 2624 #D = np.where(xye[5],(Y-Z),0.)-Page.plotStyle['delOffset'] 2625 np.seterr(invalid=olderr['invalid']) 2626 else: 2627 Y = copy.copy(xye[1]) 2628 Z = copy.copy(xye[3]) 2629 W = copy.copy(xye[4]) 2630 #D = xye[5]-Page.plotStyle['delOffset'] #powder background 2631 DZ = (xye[1]-xye[3])*np.sqrt(xye[2]) 2632 DifLine[0].set_xdata(X[Ibeg:Ifin]) 2633 DifLine[0].set_ydata(DZ[Ibeg:Ifin]) 2634 Plot1.set_ylim((min(DZ[Ibeg:Ifin]),max(DZ[Ibeg:Ifin]))) 2635 CalcLine[0].set_xdata(X) 2636 ObsLine[0].set_xdata(X) 2637 BackLine[0].set_xdata(X) 2638 CalcLine[0].set_ydata(Z) 2639 ObsLine[0].set_ydata(Y) 2640 BackLine[0].set_ydata(W) 2641 if cycle: 2642 Title = '{} cycle #{}'.format(plotItem,cycle) 2643 else: 2644 Title = plotItem 2645 if Page.plotStyle['sqrtPlot']: 2646 Plot.set_title(r'$\sqrt{I}$ for '+Title) 2647 else: 2648 Plot.set_title(Title) 2649 Page.canvas.draw() 2650 2601 2651 #===================================================================================== 2602 2652 # beginning PlotPatterns execution … … 2635 2685 G2frame.UseLimits = {'xlims':[False,False],'ylims':[False,False], 2636 2686 'dylims':[False,False]} 2687 #===================================================================================== 2688 # code to setup for plotting Rietveld results. Turns off multiplot, 2689 # sqrtplot, turn on + and weight plot, but sqrtPlot qPlot and dPlot are not changed. 2690 # Magnification regions are ignored. 2691 # the last-plotted histogram (from G2frame.PatternId) is used for this plotting 2692 # (except in seq. fitting) 2693 # Returns a pointer to refPlotUpdate, which is used to update the plot when this 2694 # returns 2695 if refineMode: 2696 plottingItem = G2frame.GPXtree.GetItemText(G2frame.PatternId) 2697 # save settings to be restored after refinement with repPlotUpdate({},restore=True) 2698 savedSettings = (G2frame.SinglePlot,G2frame.Contour,G2frame.Weight, 2699 G2frame.plusPlot,G2frame.SubBack,Page.plotStyle['logPlot']) 2700 G2frame.SinglePlot = True 2701 G2frame.Contour = False 2702 G2frame.Weight = True 2703 G2frame.plusPlot = True 2704 G2frame.SubBack = False 2705 Page.plotStyle['logPlot'] = False 2706 #===================================================================================== 2637 2707 if not new: 2638 2708 G2frame.xylim = limits … … 2907 2977 if 'PWDR' in plottype and G2frame.SinglePlot and not ( 2908 2978 Page.plotStyle['logPlot'] or Page.plotStyle['sqrtPlot'] or G2frame.Contour): 2909 magLineList = data[0].get('Magnification',[]) 2979 if not refineMode: 2980 magLineList = data[0].get('Magnification',[]) 2910 2981 if ('C' in ParmList[0]['Type'][0] and Page.plotStyle['dPlot']) or \ 2911 2982 ('T' in ParmList[0]['Type'][0] and Page.plotStyle['qPlot']): # reversed regions relative to data order … … 3054 3125 else: 3055 3126 Plot.set_ylim(bottom=np.min(np.trim_zeros(YB))/2.,top=np.max(Y)*2.) 3127 # Matplotlib artist lists used for refPlotUpdate 3128 ObsLine = None 3129 CalcLine = None 3130 BackLine = None 3131 DifLine = None 3056 3132 if G2frame.Weight: 3057 3133 Plot1.set_yscale("linear") … … 3095 3171 if G2frame.SubBack: 3096 3172 if 'PWDR' in plottype: 3097 Plot.plot(Xum,Y-W,colors[0]+pP,picker=False,clip_on=Clip_on,label='_obs') #Io-Ib3098 Plot.plot(X,Z-W,colors[1],picker=False,label='_calc') #Ic-Ib3173 ObsLine = Plot.plot(Xum,Y-W,colors[0]+pP,picker=False,clip_on=Clip_on,label='_obs') #Io-Ib 3174 CalcLine = Plot.plot(X,Z-W,colors[1],picker=False,label='_calc') #Ic-Ib 3099 3175 else: 3100 3176 Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs') … … 3103 3179 if 'PWDR' in plottype: 3104 3180 ObsLine = Plot.plot(Xum,Y,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs') #Io 3105 Plot.plot(X,Z,colors[1],picker=False,label='_calc') #Ic3181 CalcLine = Plot.plot(X,Z,colors[1],picker=False,label='_calc') #Ic 3106 3182 else: 3107 3183 Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs') 3108 3184 Plot.plot(X,ZB,colors[1],picker=False,label='_calc') 3109 3185 if 'PWDR' in plottype and (G2frame.SinglePlot or G2frame.plusPlot): 3110 Plot.plot(X,W,colors[2],picker=False,label='_bkg') #Ib3186 BackLine = Plot.plot(X,W,colors[2],picker=False,label='_bkg') #Ib 3111 3187 if not G2frame.Weight: DifLine = Plot.plot(X,D,colors[3],picker=1.,label='_diff') #Io-Ic 3112 3188 Plot.axhline(0.,color='k',label='_zero') … … 3187 3263 Plot.axvline(hkl[-2],color=clr,dashes=(5,5)) 3188 3264 elif G2frame.GPXtree.GetItemText(PickId) in ['Reflection Lists'] or \ 3189 'PWDR' in G2frame.GPXtree.GetItemText(PickId) :3265 'PWDR' in G2frame.GPXtree.GetItemText(PickId) or refineMode: 3190 3266 Phases = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,PatternId,'Reflection Lists')) 3191 3267 l = GSASIIpath.GetConfigValue('Tick_length',8.0) … … 3281 3357 if DifLine[0]: 3282 3358 G2frame.dataWindow.moveDiffCurve.Enable(True) 3359 if refineMode: return refPlotUpdate 3283 3360 3284 3361 def PublishRietveldPlot(G2frame,Pattern,Plot,Page): -
trunk/GSASIIstrMain.py
r3855 r3913 48 48 49 49 def RefineCore(Controls,Histograms,Phases,restraintDict,rigidbodyDict,parmDict,varyList, 50 calcControls,pawleyLookup,ifSeq,printFile,dlg ):50 calcControls,pawleyLookup,ifSeq,printFile,dlg,refPlotUpdate=None): 51 51 '''Core optimization routines, shared between SeqRefine and Refine 52 52 … … 80 80 args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) 81 81 ncyc = int(result[2]['nfev']/2) 82 if refPlotUpdate is not None: refPlotUpdate(Histograms) # update plot after completion 82 83 elif 'analytic Hessian' in Controls['deriv type']: 83 84 Lamda = Controls.get('Marquardt',-3) 84 85 maxCyc = Controls['max cyc'] 85 86 result = G2mth.HessianLSQ(G2stMth.errRefine,values,Hess=G2stMth.HessRefine,ftol=Ftol,xtol=Xtol,maxcyc=maxCyc,Print=ifPrint,lamda=Lamda, 86 args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) 87 args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg), 88 refPlotUpdate=refPlotUpdate) 87 89 ncyc = result[2]['num cyc']+1 88 90 Rvals['lamMax'] = result[2]['lamMax'] … … 91 93 maxCyc = Controls['max cyc'] 92 94 result = G2mth.HessianSVD(G2stMth.errRefine,values,Hess=G2stMth.HessRefine,ftol=Ftol,xtol=Xtol,maxcyc=maxCyc,Print=ifPrint, 93 args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg)) 95 args=([Histograms,Phases,restraintDict,rigidbodyDict],parmDict,varyList,calcControls,pawleyLookup,dlg), 96 refPlotUpdate=refPlotUpdate) 94 97 if result[1] is None: 95 98 IfOK = False … … 104 107 if len(varyList): 105 108 ncyc = int(result[2]['nfev']/len(varyList)) 109 if refPlotUpdate is not None: refPlotUpdate(Histograms) # update plot 106 110 # table = dict(zip(varyList,zip(values,result[0],(result[0]-values)))) 107 111 # for item in table: print item,table[item] #useful debug - are things shifting? … … 173 177 return IfOK,Rvals,result,covMatrix,sig 174 178 175 def Refine(GPXfile,dlg=None,makeBack=True ):179 def Refine(GPXfile,dlg=None,makeBack=True,refPlotUpdate=None): 176 180 'Global refinement -- refines to minimize against all histograms' 177 181 import GSASIImpsubs as G2mp … … 249 253 covData = {} 250 254 IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histograms,Phases,restraintDict, 251 rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg) 255 rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg, 256 refPlotUpdate=refPlotUpdate) 252 257 if IfOK: 253 258 sigDict = dict(zip(varyList,sig)) … … 315 320 return NewVary 316 321 317 def SeqRefine(GPXfile,dlg, PlotFunction=None,G2frame=None):322 def SeqRefine(GPXfile,dlg,refPlotUpdate=None): 318 323 '''Perform a sequential refinement -- cycles through all selected histgrams, 319 324 one at a time … … 480 485 # try: 481 486 IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histo,Phases,restraintDict, 482 rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg) 483 if PlotFunction: 484 PlotFunction(G2frame,Histo[histogram]['Data'],histogram) 487 rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg, 488 refPlotUpdate=refPlotUpdate) 485 489 print (' wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%( 486 490 Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2'])) -
trunk/makeBat.py
r3680 r3913 13 13 14 14 ''' 15 version = "$Id$" 15 16 # creates Windows files to aid in running GSAS-II 16 17 # creates RunGSASII.bat and a desktop shortcut to that file
Note: See TracChangeset
for help on using the changeset viewer.