Changeset 3999 for trunk/GSASIIpwdGUI.py


Ignore:
Timestamp:
May 27, 2019 4:07:46 PM (3 years ago)
Author:
toby
Message:

reorg PDF computation/save as non-GUI code, add PDF to scriptable, doc build fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpwdGUI.py

    r3995 r3999  
    63326332        if name.strip():
    63336333            pId = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,name)
    6334             if pId == 0:
     6334            if not pId:
    63356335                print(key,'Entry',name,'Not found.')
    63366336                problem = True
     
    63546354    low r (-4 pi r #density).
    63556355    '''
    6356     import scipy.optimize as opt
    6357     Min,Init,Done = SetupPDFEval(G2frame,data)
    6358     xstart = Init()
    6359     bakMul = data['Sample Bkg.']['Mult']
    6360     if showFit:
    6361         rms = Min(xstart)
    6362         print('  Optimizing corrections to improve G(r) at low r')
    6363         if data['Sample Bkg.'].get('Refine',False):
    6364 #            data['Flat Bkg'] = 0.
    6365             print('  start: Ruland={:.3f}, Sample Bkg mult={:.3f} (RMS:{:.4f})'.format(
    6366                 data['Ruland'],data['Sample Bkg.']['Mult'],rms))
    6367         else:
    6368             print('  start: Flat Bkg={:.1f}, BackRatio={:.3f}, Ruland={:.3f} (RMS:{:.4f})'.format(
    6369                 data['Flat Bkg'],data['BackRatio'],data['Ruland'],rms))
    6370     if data['Sample Bkg.'].get('Refine',False):
    6371         res = opt.minimize(Min,xstart,bounds=([0.01,1],[1.2*bakMul,0.8*bakMul]),
    6372                     method='L-BFGS-B',options={'maxiter':maxCycles},tol=0.001)
    6373     else:
    6374         res = opt.minimize(Min,xstart,bounds=([0,None],[0,1],[0.01,1]),
    6375                     method='L-BFGS-B',options={'maxiter':maxCycles},tol=0.001)
    6376     Done(res['x'])
    6377     if showFit:
    6378         if res['success']:
    6379             msg = 'Converged'
    6380         else:
    6381             msg = 'Not Converged'
    6382         if data['Sample Bkg.'].get('Refine',False):
    6383             print('  end:   Ruland={:.3f}, Sample Bkg mult={:.3f} (RMS:{:.4f}) *** {} ***\n'.format(
    6384                 data['Ruland'],data['Sample Bkg.']['Mult'],res['fun'],msg))
    6385         else:
    6386             print('  end:   Flat Bkg={:.1f}, BackRatio={:.3f}, Ruland={:.3f}) *** {} ***\n'.format(
    6387                 data['Flat Bkg'],data['BackRatio'],data['Ruland'],res['fun'],msg))
    6388     return res['success']
    6389 
    6390 def SetupPDFEval(G2frame,data):
    6391     '''Create functions needed to optimize the PDF at low r
    6392     '''
    6393     Data = copy.deepcopy(data)
    63946356    xydata = {}
    63956357    for key in ['Sample','Sample Bkg.','Container','Container Bkg.']:
    6396         name = Data[key]['Name']
     6358        name = data[key]['Name']
    63976359        if name:
    63986360            xydata[key] = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,G2frame.root,name))
    6399     powName = Data['Sample']['Name']
     6361    powName = data['Sample']['Name']
    64006362    powId = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,powName)
    64016363    limits = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,powId,'Limits'))[1]
    64026364    inst = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame,powId,'Instrument Parameters'))[0]
    6403     numbDen = G2pwd.GetNumDensity(Data['ElList'],Data['Form Vol'])
    6404     BkgMax = 1.
    6405     def EvalLowPDF(arg):
    6406         '''Objective routine -- evaluates the RMS deviations in G(r)
    6407         from -4(pi)*#density*r for for r<Rmin
    6408         arguments are ['Flat Bkg','BackRatio','Ruland'] scaled so that
    6409         the min & max values are between 0 and 1.
    6410         '''
    6411         if Data['Sample Bkg.'].get('Refine',False):
    6412             R,S = arg
    6413             Data['Sample Bkg.']['Mult'] = S
    6414         else:
    6415             F,B,R = arg
    6416             Data['Flat Bkg'] = F*BkgMax
    6417             Data['BackRatio'] = B
    6418         Data['Ruland'] = R/10.
    6419         G2pwd.CalcPDF(Data,inst,limits,xydata)
    6420         # test low r computation
    6421         g = xydata['GofR'][1][1]
    6422         r = xydata['GofR'][1][0]
    6423         g0 = g[r < Data['Rmin']] + 4*np.pi*r[r < Data['Rmin']]*numbDen
    6424         M = sum(g0**2)/len(g0)
    6425         return M
    6426     def GetCurrentVals():
    6427         '''Get the current ['Flat Bkg','BackRatio','Ruland'] with scaling
    6428         '''
    6429         if data['Sample Bkg.'].get('Refine',False):
    6430                 return [max(10*data['Ruland'],.05),data['Sample']['Mult']]
    6431         try:
    6432             F = data['Flat Bkg']/BkgMax
    6433         except:
    6434             F = 0
    6435         return [F,data['BackRatio'],max(10*data['Ruland'],.05)]
    6436     def SetFinalVals(arg):
    6437         '''Set the 'Flat Bkg', 'BackRatio' & 'Ruland' values from the
    6438         scaled, refined values and plot corrected region of G(r)
    6439         '''
    6440         if data['Sample Bkg.'].get('Refine',False):
    6441             R,S = arg
    6442             data['Sample Bkg.']['Mult'] = S
    6443         else:
    6444             F,B,R = arg
    6445             data['Flat Bkg'] = F*BkgMax
    6446             data['BackRatio'] = B
    6447         data['Ruland'] = R/10.
    6448         G2pwd.CalcPDF(data,inst,limits,xydata)
    6449     EvalLowPDF(GetCurrentVals())
    6450     BkgMax = max(xydata['IofQ'][1][1])/50.
    6451     return EvalLowPDF,GetCurrentVals,SetFinalVals
     6365    res = G2pwd.OptimizePDF(data,xydata,limits,inst,showFit,maxCycles)
     6366    return res['success']
    64526367   
    64536368def UpdatePDFGrid(G2frame,data):
     
    65336448            mulBox.Add(multSpin,0,WACV)
    65346449            fileSizer.Add(mulBox,0,WACV)
    6535             if 'Refine' in item and item['Name']:
     6450            if 'Refine' in item and item['Name'] and 'Sample' in key:
    65366451                refMult = wx.CheckBox(parent=G2frame.dataWindow,label='Refine?')
    65376452                refMult.SetValue(item['Refine'])
     
    66686583            finally:
    66696584                wx.EndBusyCursor()
     6585            OnComputePDF(event)
    66706586            wx.CallAfter(UpdatePDFGrid,G2frame,data)
    6671             OnComputePDF(event)       
    66726587                       
    66736588        def AfterChangeNoRefresh(invalid,value,tc):
     
    71177032        mainSizer.Add(SFGctrlSizer(),0,WACV)
    71187033        G2G.HorizontalLine(mainSizer,G2frame.dataWindow)
    7119     mainSizer.Add(DiffSizer(),0,WACV)
     7034        mainSizer.Add(DiffSizer(),0,WACV)
     7035    else:
     7036        mainSizer.Add(wx.StaticText(G2frame.dataWindow,wx.ID_ANY,
     7037                                     powName+' not in Tree'))
    71207038    G2frame.dataWindow.SetDataSize()
    71217039
Note: See TracChangeset for help on using the changeset viewer.