Changeset 4021 for trunk/GSASIIpwd.py


Ignore:
Timestamp:
Jun 11, 2019 10:05:07 PM (2 years ago)
Author:
toby
Message:

implement filter for screen messages; start to replace print() with G2Print(); scripting changes for PDF

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpwd.py

    r4002 r4021  
    4444    import pydiffax as pyx
    4545except ImportError:
    46     print ('pydiffax is not available for this platform - under develpment')
     46    print ('pydiffax is not available for this platform')
     47import GSASIIfiles as G2fil
    4748
    4849   
     
    440441    parmDict,varyList = MakeParms(peaks)
    441442    if not len(varyList):
    442         print (' Nothing varied')
     443        G2fil.G2Print (' Nothing varied')
    443444        return newpeaks,None,None,None,None,None
    444445   
     
    505506    if showFit:
    506507        rms = Min(xstart)
    507         print('  Optimizing corrections to improve G(r) at low r')
     508        G2fil.G2Print('  Optimizing corrections to improve G(r) at low r')
    508509        if data['Sample Bkg.'].get('Refine',False):
    509510#            data['Flat Bkg'] = 0.
    510             print('  start: Ruland={:.3f}, Sample Bkg mult={:.3f} (RMS:{:.4f})'.format(
     511            G2fil.G2Print('  start: Ruland={:.3f}, Sample Bkg mult={:.3f} (RMS:{:.4f})'.format(
    511512                data['Ruland'],data['Sample Bkg.']['Mult'],rms))
    512513        else:
    513             print('  start: Flat Bkg={:.1f}, BackRatio={:.3f}, Ruland={:.3f} (RMS:{:.4f})'.format(
     514            G2fil.G2Print('  start: Flat Bkg={:.1f}, BackRatio={:.3f}, Ruland={:.3f} (RMS:{:.4f})'.format(
    514515                data['Flat Bkg'],data['BackRatio'],data['Ruland'],rms))
    515516    if data['Sample Bkg.'].get('Refine',False):
     
    526527            msg = 'Not Converged'
    527528        if data['Sample Bkg.'].get('Refine',False):
    528             print('  end:   Ruland={:.3f}, Sample Bkg mult={:.3f} (RMS:{:.4f}) *** {} ***\n'.format(
     529            G2fil.G2Print('  end:   Ruland={:.3f}, Sample Bkg mult={:.3f} (RMS:{:.4f}) *** {} ***\n'.format(
    529530                data['Ruland'],data['Sample Bkg.']['Mult'],res['fun'],msg))
    530531        else:
    531             print('  end:   Flat Bkg={:.1f}, BackRatio={:.3f}, Ruland={:.3f}) *** {} ***\n'.format(
     532            G2fil.G2Print('  end:   Flat Bkg={:.1f}, BackRatio={:.3f}, Ruland={:.3f}) *** {} ***\n'.format(
    532533                data['Flat Bkg'],data['BackRatio'],data['Ruland'],res['fun'],msg))
    533534    return res
     
    925926            break
    926927        except ValueError:
    927             print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
     928            G2fil.G2Print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
    928929            break
    929930    # fixed background from file
     
    931932        mult = fixedBkg.get('_fixedMult',0.0)
    932933        if len(fixedBkg.get('_fixedValues',[])) != len(yb):
    933             print('Lengths of backgrounds do not agree: yb={}, fixed={}'.format(
     934            G2fil.G2Print('Lengths of backgrounds do not agree: yb={}, fixed={}'.format(
    934935                len(yb),len(fixedBkg.get('_fixedValues',[]))))
    935936        elif mult:
     
    10631064            break
    10641065        except ValueError:
    1065             print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
     1066            G2fil.G2Print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
    10661067            break       
    10671068    return dydb,dyddb,dydpk
     
    16051606    varyList = insVary
    16061607    if not len(varyList):
    1607         print ('**** ERROR - nothing to refine! ****')
     1608        G2fil.G2Print ('**** ERROR - nothing to refine! ****')
    16081609        return False
    16091610    while True:
     
    16171618        Values2Dict(parmDict, varyList, result[0])
    16181619        GOF = chisq/(len(peakPos)-len(varyList))       #reduced chi^2
    1619         print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],len(peakPos),len(varyList)))
    1620         print ('calib time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
    1621         print ('chi**2 = %12.6g, reduced chi**2 = %6.2f'%(chisq,GOF))
     1620        G2fil.G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],len(peakPos),len(varyList)))
     1621        G2fil.G2Print ('calib time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
     1622        G2fil.G2Print ('chi**2 = %12.6g, reduced chi**2 = %6.2f'%(chisq,GOF))
    16221623        try:
    16231624            sig = np.sqrt(np.diag(result[1])*GOF)
    16241625            if np.any(np.isnan(sig)):
    1625                 print ('*** Least squares aborted - some invalid esds possible ***')
     1626                G2fil.G2Print ('*** Least squares aborted - some invalid esds possible ***')
    16261627            break                   #refinement succeeded - finish up!
    16271628        except ValueError:          #result[1] is None on singular matrix
    1628             print ('**** Refinement failed - singular matrix ****')
     1629            G2fil.G2Print ('**** Refinement failed - singular matrix ****')
    16291630       
    16301631    sigDict = dict(zip(varyList,sig))
     
    19431944        Rvals['Rwp'] = np.sqrt(chisq/np.sum(w[xBeg:xFin]*(y+fixback)[xBeg:xFin]**2))*100.      #to %
    19441945        Rvals['GOF'] = chisq/(xFin-xBeg-len(varyList))       #reduced chi^2
    1945         print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],xFin-xBeg,len(varyList)))
     1946        G2fil.G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],xFin-xBeg,len(varyList)))
    19461947        if ncyc:
    1947             print ('fitpeak time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
    1948         print ('Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF']))
     1948            G2fil.G2Print ('fitpeak time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
     1949        G2fil.G2Print ('Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF']))
    19491950        sig = [0]*len(varyList)
    19501951        if len(varyList) == 0: break  # if nothing was refined
     
    19521953            sig = np.sqrt(np.diag(result[1])*Rvals['GOF'])
    19531954            if np.any(np.isnan(sig)):
    1954                 print ('*** Least squares aborted - some invalid esds possible ***')
     1955                G2fil.G2Print ('*** Least squares aborted - some invalid esds possible ***')
    19551956            break                   #refinement succeeded - finish up!
    19561957        except ValueError:          #result[1] is None on singular matrix
    1957             print ('**** Refinement failed - singular matrix ****')
     1958            G2fil.G2Print ('**** Refinement failed - singular matrix ****')
    19581959            Ipvt = result[2]['ipvt']
    19591960            for i,ipvt in enumerate(Ipvt):
    19601961                if not np.sum(result[2]['fjac'],axis=1)[i]:
    1961                     print ('Removing parameter: '+varyList[ipvt-1])
     1962                    G2fil.G2Print ('Removing parameter: '+varyList[ipvt-1])
    19621963                    badVary.append(varyList[ipvt-1])
    19631964                    del(varyList[ipvt-1])
     
    19851986    if len(binsperFWHM):
    19861987        if min(binsperFWHM) < 1.:
    1987             print ('*** Warning: calculated peak widths are too narrow to refine profile coefficients ***')
     1988            G2fil.G2Print ('*** Warning: calculated peak widths are too narrow to refine profile coefficients ***')
    19881989            if 'T' in Inst['Type'][0]:
    1989                 print (' Manually increase sig-0, 1, or 2 in Instrument Parameters')
     1990                G2fil.G2Print (' Manually increase sig-0, 1, or 2 in Instrument Parameters')
    19901991            else:
    1991                 print (' Manually increase W in Instrument Parameters')
     1992                G2fil.G2Print (' Manually increase W in Instrument Parameters')
    19921993        elif min(binsperFWHM) < 4.:
    1993             print ('*** Warning: data binning yields too few data points across peak FWHM for reliable Rietveld refinement ***')
    1994             print ('*** recommended is 6-10; you have %.2f ***'%(min(binsperFWHM)))
     1994            G2fil.G2Print ('*** Warning: data binning yields too few data points across peak FWHM for reliable Rietveld refinement ***')
     1995            G2fil.G2Print ('*** recommended is 6-10; you have %.2f ***'%(min(binsperFWHM)))
    19951996    return sigDict,result,sig,Rvals,varyList,parmDict,fullvaryList,badVary
    19961997   
     
    20552056
    20562057def REFDRefine(Profile,ProfDict,Inst,Limits,Substances,data):
    2057     print ('fit REFD data by '+data['Minimizer']+' using %.2f%% data resolution'%(data['Resolution'][0]))
     2058    G2fil.G2Print ('fit REFD data by '+data['Minimizer']+' using %.2f%% data resolution'%(data['Resolution'][0]))
    20582059   
    20592060    class RandomDisplacementBounds(object):
     
    21142115            data['Scale'][0] = parmDict['Scale']
    21152116            line += ' esd: %.4g'%(sigDict['Scale'])                                                             
    2116         print (line)
     2117        G2fil.G2Print (line)
    21172118        line = ' Flat background: %15.4g'%(parmDict['FltBack'])
    21182119        if 'FltBack' in varyList:
    21192120            data['FltBack'][0] = parmDict['FltBack']
    21202121            line += ' esd: %15.3g'%(sigDict['FltBack'])
    2121         print (line)
     2122        G2fil.G2Print (line)
    21222123        for ilay,layer in enumerate(data['Layers']):
    21232124            name = layer['Name']
    2124             print (' Parameters for layer: %d %s'%(ilay,name))
     2125            G2fil.G2Print (' Parameters for layer: %d %s'%(ilay,name))
    21252126            cid = str(ilay)+';'
    21262127            line = ' '
     
    21362137                    if cid+parm in varyList:
    21372138                        line += ' esd: %.3g'%(sigDict[cid+parm])
    2138             print (line)
    2139             print (line2)
     2139            G2fil.G2Print (line)
     2140            G2fil.G2Print (line2)
    21402141   
    21412142    def calcREFD(values,Q,Io,wt,Qsig,parmDict,varyList):
     
    22162217            take_step = RandomDisplacementBounds(xyrng[0], xyrng[1])
    22172218            T0 = estimateT0(take_step)
    2218             print (' Estimated temperature: %.3g'%(T0))
     2219            G2fil.G2Print (' Estimated temperature: %.3g'%(T0))
    22192220            result = so.basinhopping(sumREFD,values,take_step=take_step,disp=True,T=T0,stepsize=Bfac,
    22202221                interval=20,niter=200,minimizer_kwargs={'method':'L-BFGS-B','bounds':bounds,
     
    22362237            ncalc = result[3]
    22372238            covM = []
    2238             print (' MC/SA final temperature: %.4g'%(result[2]))
     2239            G2fil.G2Print (' MC/SA final temperature: %.4g'%(result[2]))
    22392240        elif data['Minimizer'] == 'L-BFGS-B':
    22402241            result = so.minimize(sumREFD,values,method='L-BFGS-B',bounds=bounds,   #ftol=Ftol,
     
    22812282            covMatrix = []
    22822283        sigDict = dict(zip(varyList,sig))
    2283         print (' Results of reflectometry data modelling fit:')
    2284         print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(ncalc,Ifin-Ibeg,len(varyList)))
    2285         print ('Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF']))
     2284        G2fil.G2Print (' Results of reflectometry data modelling fit:')
     2285        G2fil.G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(ncalc,Ifin-Ibeg,len(varyList)))
     2286        G2fil.G2Print ('Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF']))
    22862287        SetModelParms()
    22872288        return True,result,varyList,sig,Rvals,covMatrix,parmDict,''
    22882289    except (ValueError,TypeError):      #when bad LS refinement; covM missing or with nans
    2289         print (Msg)
     2290        G2fil.G2Print (Msg)
    22902291        return False,0,0,0,0,0,0,Msg
    22912292       
     
    24702471       
    24712472def makeRefdFFT(Limits,Profile):
    2472     print ('make fft')
     2473    G2fil.G2Print ('make fft')
    24732474    Q,Io = Profile[:2]
    24742475    Qmin = Limits[1][0]
     
    25352536        if 'bin' in name:
    25362537            DIFFaX = name+'/DIFFaX.exe'
    2537             print (' Execute '+DIFFaX)
     2538            G2fil.G2Print (' Execute '+DIFFaX)
    25382539            break
    25392540    # make form factor file that DIFFaX wants - atom types are GSASII style
     
    26542655            sumPx += p
    26552656        if sumPx != 1.0:    #this has to be picky since DIFFaX is.
    2656             print ('ERROR - Layer probabilities sum to %.3f DIFFaX will insist it = 1.0'%sumPx)
     2657            G2fil.G2Print ('ERROR - Layer probabilities sum to %.3f DIFFaX will insist it = 1.0'%sumPx)
    26572658            df.close()
    26582659            os.remove('data.sfc')
     
    26652666        subp.call(DIFFaX)
    26662667    except OSError:
    2667         print (' DIFFax.exe is not available for this platform - under development')
    2668     print (' DIFFaX time = %.2fs'%(time.time()-time0))
     2668        G2fil.G2Print('DIFFax.exe is not available for this platform',mode='warn')
     2669    G2fil.G2Print (' DIFFaX time = %.2fs'%(time.time()-time0))
    26692670    if os.path.exists('GSASII-DIFFaX.spc'):
    26702671        Xpat = np.loadtxt('GSASII-DIFFaX.spc').T
     
    28312832    time0 = time.time()
    28322833    pyx.pygetspc(controls,Nspec,spec)
    2833     print (' GETSPC time = %.2fs'%(time.time()-time0))
     2834    G2fil.G2Print (' GETSPC time = %.2fs'%(time.time()-time0))
    28342835    time0 = time.time()
    28352836    U = ateln2*inst['U'][1]/10000.
     
    28622863        profile[2][iBeg:iFin] = np.where(profile[1][iBeg:iFin]>0.,1./profile[1][iBeg:iFin],1.0)
    28632864    profile[5][iBeg:iFin] = profile[1][iBeg:iFin]-profile[3][iBeg:iFin]
    2864     print (' Broadening time = %.2fs'%(time.time()-time0))
     2865    G2fil.G2Print (' Broadening time = %.2fs'%(time.time()-time0))
    28652866   
    28662867def CalcStackingSADP(Layers,debug):
     
    28972898        iB += Nblk
    28982899    Layers['Sadp']['Img'] = Sapd
    2899     print (' GETSAD time = %.2fs'%(time.time()-time0))
     2900    G2fil.G2Print (' GETSAD time = %.2fs'%(time.time()-time0))
    29002901   
    29012902###############################################################################
     
    31873188    for i in range(100):
    31883189        getPeakProfile(parmDict1,xdata,varyList,bakType)
    3189     print ('100+6*Ka1-2 peaks=1200 peaks %.2f'%time.time()-time0)
     3190    G2fil.G2Print ('100+6*Ka1-2 peaks=1200 peaks %.2f'%time.time()-time0)
    31903191   
    31913192def test2(name,delt):
     
    32253226    for name,shft in [['pos',0.0001],['sig',0.01],['gam',0.0001],['shl',0.00005]]:
    32263227        test3(name,shft)
    3227     print ("OK")
     3228    G2fil.G2Print ("OK")
    32283229    plotter.StartEventLoop()
Note: See TracChangeset for help on using the changeset viewer.