Changeset 3136 for trunk/GSASIIpwd.py


Ignore:
Timestamp:
Oct 23, 2017 11:39:16 AM (8 years ago)
Author:
vondreele
Message:

make GSAS-II python 3.6 compliant & preserve python 2.7 use;changes:
do from future import division, print_function for all GSAS-II py sources
all menu items revised to be py 2.7/3.6 compliant
all wx.OPEN --> wx.FD_OPEN in file dialogs
all integer divides (typically for image pixel math) made explicit with ; ambiguous ones made floats as appropriate
all print "stuff" --> print (stuff)
all print >> pFile,'stuff' --> pFile.writeCIFtemplate('stuff')
all read file opens made explicit 'r' or 'rb'
all cPickle imports made for py2.7 or 3.6 as cPickle or _pickle; test for '2' platform.version_tuple[0] for py 2.7
define cPickleload to select load(fp) or load(fp,encoding='latin-1') for loading gpx files; provides cross compatibility between py 2.7/3.6 gpx files
make dict.keys() as explicit list(dict.keys()) as needed (NB: possible source of remaining py3.6 bugs)
make zip(a,b) as explicit list(zip(a,b)) as needed (NB: possible source of remaining py3.6 bugs)
select unichr/chr according test for '2' platform.version_tuple[0] for py 2.7 (G2pwdGUI * G2plot) for special characters
select wg.EVT_GRID_CELL_CHANGE (classic) or wg.EVT_GRID_CELL_CHANGED (phoenix) in grid Bind
maxint --> maxsize; used in random number stuff
raise Exception,"stuff" --> raise Exception("stuff")
wx 'classic' sizer.DeleteWindows?() or 'phoenix' sizer.Clear(True)
wx 'classic' SetToolTipString?(text) or 'phoenix' SetToolTip?(wx.ToolTip?(text)); define SetToolTipString?(self,text) to handle the choice in plots
status.SetFields? --> status.SetStatusText?
'classic' AddSimpleTool? or 'phoenix' self.AddTool? for plot toolbar; Bind different as well
define GetItemPydata? as it doesn't exist in wx 'phoenix'
allow python versions 2.7 & 3.6 to run GSAS-II
Bind override commented out - no logging capability (NB: remove all logging code?)
all import ContentsValidator? open filename & test if valid then close; filepointer removed from Reader
binary importers (mostly images) test for 'byte' type & convert as needed to satisfy py 3.6 str/byte rules

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/GSASIIpwd.py

    r3070 r3136  
    1313# $Id$
    1414########### SVN repository information ###################
     15from __future__ import division, print_function
    1516import sys
    1617import math
     
    4041    import pydiffax as pyx
    4142except ImportError:
    42     print 'pydiffax is not available for this platform - under develpment'
     43    print ('pydiffax is not available for this platform - under develpment')
    4344
    4445   
     
    434435    parmDict,varyList = MakeParms(peaks)
    435436    if not len(varyList):
    436         print ' Nothing varied'
     437        print (' Nothing varied')
    437438        return newpeaks,None,None,None,None,None
    438439   
     
    529530    nmax = max(nmin+1,nmax)
    530531    for p in range(nmin,nmax):
    531         if max(factorize(p).keys()) < thresh:
     532        if max(list(factorize(p).keys())) < thresh:
    532533            plist.append(p)
    533534    return plist
     
    822823            break
    823824        except ValueError:
    824             print '**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****'
     825            print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
    825826            break       
    826827    return yb,sumBk
     
    949950            break
    950951        except ValueError:
    951             print '**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****'
     952            print ('**** WARNING - backround peak '+str(iD)+' sigma is negative; fix & try again ****')
    952953            break       
    953954    return dydb,dyddb,dydpk
     
    14321433       
    14331434    def InstPrint(Inst,sigDict):
    1434         print 'Instrument Parameters:'
     1435        print ('Instrument Parameters:')
    14351436        if 'C' in Inst['Type'][0]:
    14361437            ptfmt = "%12.6f"
     
    14481449                else:
    14491450                    sigstr += 12*' '
    1450         print ptlbls
    1451         print ptstr
    1452         print sigstr
     1451        print (ptlbls)
     1452        print (ptstr)
     1453        print (sigstr)
    14531454       
    14541455    def errPeakPos(values,peakDsp,peakPos,peakWt,dataType,parmDict,varyList):
     
    14731474    varyList = insVary
    14741475    if not len(varyList):
    1475         print '**** ERROR - nothing to refine! ****'
     1476        print ('**** ERROR - nothing to refine! ****')
    14761477        return False
    14771478    while True:
     
    14851486        Values2Dict(parmDict, varyList, result[0])
    14861487        GOF = chisq/(len(peakPos)-len(varyList))       #reduced chi^2
    1487         print 'Number of function calls:',result[2]['nfev'],' Number of observations: ',len(peakPos),' Number of parameters: ',len(varyList)
    1488         print 'calib time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc)
    1489         print 'chi**2 = %12.6g, reduced chi**2 = %6.2f'%(chisq,GOF)
     1488        print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],len(peakPos),len(varyList)))
     1489        print ('calib time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
     1490        print ('chi**2 = %12.6g, reduced chi**2 = %6.2f'%(chisq,GOF))
    14901491        try:
    14911492            sig = np.sqrt(np.diag(result[1])*GOF)
    14921493            if np.any(np.isnan(sig)):
    1493                 print '*** Least squares aborted - some invalid esds possible ***'
     1494                print ('*** Least squares aborted - some invalid esds possible ***')
    14941495            break                   #refinement succeeded - finish up!
    14951496        except ValueError:          #result[1] is None on singular matrix
    1496             print '**** Refinement failed - singular matrix ****'
     1497            print ('**** Refinement failed - singular matrix ****')
    14971498       
    14981499    sigDict = dict(zip(varyList,sig))
     
    15591560               
    15601561    def BackgroundPrint(Background,sigDict):
    1561         print 'Background coefficients for',Background[0][0],'function'
     1562        print ('Background coefficients for '+Background[0][0]+' function')
    15621563        ptfmt = "%12.5f"
    15631564        ptstr =  'value: '
     
    15721573                    sigstr += " "*12
    15731574            if len(ptstr) > 75:
    1574                 print ptstr
    1575                 if Background[0][1]: print sigstr
     1575                print (ptstr)
     1576                if Background[0][1]: print (sigstr)
    15761577                ptstr =  'value: '
    15771578                sigstr = 'esd  : '
    15781579        if len(ptstr) > 8:
    1579             print ptstr
    1580             if Background[0][1]: print sigstr
     1580            print (ptstr)
     1581            if Background[0][1]: print (sigstr)
    15811582
    15821583        if Background[1]['nDebye']:
    15831584            parms = ['DebyeA;','DebyeR;','DebyeU;']
    1584             print 'Debye diffuse scattering coefficients'
     1585            print ('Debye diffuse scattering coefficients')
    15851586            ptfmt = "%12.5f"
    1586             print ' term       DebyeA       esd        DebyeR       esd        DebyeU        esd'
     1587            print (' term       DebyeA       esd        DebyeR       esd        DebyeU        esd')
    15871588            for term in range(Background[1]['nDebye']):
    15881589                line = ' term %d'%(term)
     
    15931594                    else:
    15941595                        line += " "*12
    1595                 print line
     1596                print (line)
    15961597        if Background[1]['nPeaks']:
    1597             print 'Coefficients for Background Peaks'
     1598            print ('Coefficients for Background Peaks')
    15981599            ptfmt = "%15.3f"
    15991600            for j,pl in enumerate(Background[1]['peaksList']):
     
    16101611                    else:
    16111612                        sigstr += " "*15
    1612                 print names
    1613                 print ptstr
    1614                 print sigstr
     1613                print (names)
     1614                print (ptstr)
     1615                print (sigstr)
    16151616                           
    16161617    def SetInstParms(Inst):
     
    16581659       
    16591660    def InstPrint(Inst,sigDict):
    1660         print 'Instrument Parameters:'
     1661        print ('Instrument Parameters:')
    16611662        ptfmt = "%12.6f"
    16621663        ptlbls = 'names :'
     
    16721673                else:
    16731674                    sigstr += 12*' '
    1674         print ptlbls
    1675         print ptstr
    1676         print sigstr
     1675        print (ptlbls)
     1676        print (ptstr)
     1677        print (sigstr)
    16771678
    16781679    def SetPeaksParms(dataType,Peaks):
     
    17221723                       
    17231724    def PeaksPrint(dataType,parmDict,sigDict,varyList,ptsperFW):
    1724         print 'Peak coefficients:'
     1725        print ('Peak coefficients:')
    17251726        if 'C' in dataType:
    17261727            names = ['pos','int','sig','gam']
     
    17341735                head += name.center(10)+'esd'.center(10)
    17351736        head += 'bins'.center(8)
    1736         print head
     1737        print (head)
    17371738        if 'C' in dataType:
    17381739            ptfmt = {'pos':"%10.5f",'int':"%10.1f",'sig':"%10.3f",'gam':"%10.3f"}
     
    17531754                        ptstr += 10*' '
    17541755            ptstr += '%9.2f'%(ptsperFW[i])
    1755             print '%s'%(('Peak'+str(i+1)).center(8)),ptstr
     1756            print ('%s'%(('Peak'+str(i+1)).center(8)),ptstr)
    17561757               
    17571758    def devPeakProfile(values,xdata,ydata, weights,dataType,parmdict,varylist,bakType,dlg):
     
    18091810        Rvals['Rwp'] = np.sqrt(chisq/np.sum(w[xBeg:xFin]*(y+fixback)[xBeg:xFin]**2))*100.      #to %
    18101811        Rvals['GOF'] = chisq/(xFin-xBeg-len(varyList))       #reduced chi^2
    1811         print 'Number of function calls:',result[2]['nfev'],' Number of observations: ',xFin-xBeg,' Number of parameters: ',len(varyList)
     1812        print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],xFin-xBeg,len(varyList)))
    18121813        if ncyc:
    1813             print 'fitpeak time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc)
    1814         print 'Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF'])
     1814            print ('fitpeak time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
     1815        print ('Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF']))
    18151816        sig = [0]*len(varyList)
    18161817        if len(varyList) == 0: break  # if nothing was refined
     
    18181819            sig = np.sqrt(np.diag(result[1])*Rvals['GOF'])
    18191820            if np.any(np.isnan(sig)):
    1820                 print '*** Least squares aborted - some invalid esds possible ***'
     1821                print ('*** Least squares aborted - some invalid esds possible ***')
    18211822            break                   #refinement succeeded - finish up!
    18221823        except ValueError:          #result[1] is None on singular matrix
    1823             print '**** Refinement failed - singular matrix ****'
     1824            print ('**** Refinement failed - singular matrix ****')
    18241825            Ipvt = result[2]['ipvt']
    18251826            for i,ipvt in enumerate(Ipvt):
    18261827                if not np.sum(result[2]['fjac'],axis=1)[i]:
    1827                     print 'Removing parameter: ',varyList[ipvt-1]
     1828                    print ('Removing parameter: '+varyList[ipvt-1])
    18281829                    badVary.append(varyList[ipvt-1])
    18291830                    del(varyList[ipvt-1])
     
    18511852    if len(binsperFWHM):
    18521853        if min(binsperFWHM) < 1.:
    1853             print '*** Warning: calculated peak widths are too narrow to refine profile coefficients ***'
     1854            print ('*** Warning: calculated peak widths are too narrow to refine profile coefficients ***')
    18541855            if 'T' in Inst['Type'][0]:
    1855                 print ' Manually increase sig-0, 1, or 2 in Instrument Parameters'
     1856                print (' Manually increase sig-0, 1, or 2 in Instrument Parameters')
    18561857            else:
    1857                 print ' Manually increase W in Instrument Parameters'
     1858                print (' Manually increase W in Instrument Parameters')
    18581859        elif min(binsperFWHM) < 4.:
    1859             print '*** Warning: data binning yields too few data points across peak FWHM for reliable Rietveld refinement ***'
    1860             print '*** recommended is 6-10; you have %.2f ***'%(min(binsperFWHM))
     1860            print ('*** Warning: data binning yields too few data points across peak FWHM for reliable Rietveld refinement ***')
     1861            print ('*** recommended is 6-10; you have %.2f ***'%(min(binsperFWHM)))
    18611862    return sigDict,result,sig,Rvals,varyList,parmDict,fullvaryList,badVary
    18621863   
     
    19211922
    19221923def REFDRefine(Profile,ProfDict,Inst,Limits,Substances,data):
    1923     print 'fit REFD data by '+data['Minimizer']+' using %.2f%% data resolution'%(data['Resolution'][0])
     1924    print ('fit REFD data by '+data['Minimizer']+' using %.2f%% data resolution'%(data['Resolution'][0]))
    19241925   
    19251926    class RandomDisplacementBounds(object):
     
    19801981            data['Scale'][0] = parmDict['Scale']
    19811982            line += ' esd: %.4g'%(sigDict['Scale'])                                                             
    1982         print line
     1983        print (line)
    19831984        line = ' Flat background: %15.4g'%(parmDict['FltBack'])
    19841985        if 'FltBack' in varyList:
    19851986            data['FltBack'][0] = parmDict['FltBack']
    19861987            line += ' esd: %15.3g'%(sigDict['FltBack'])
    1987         print line
     1988        print (line)
    19881989        for ilay,layer in enumerate(data['Layers']):
    19891990            name = layer['Name']
    1990             print ' Parameters for layer: %d %s'%(ilay,name)
     1991            print (' Parameters for layer: %d %s'%(ilay,name))
    19911992            cid = str(ilay)+';'
    19921993            line = ' '
     
    20022003                    if cid+parm in varyList:
    20032004                        line += ' esd: %.3g'%(sigDict[cid+parm])
    2004             print line
    2005             print line2
     2005            print (line)
     2006            print (line2)
    20062007   
    20072008    def calcREFD(values,Q,Io,wt,Qsig,parmDict,varyList):
     
    20822083            take_step = RandomDisplacementBounds(xyrng[0], xyrng[1])
    20832084            T0 = estimateT0(take_step)
    2084             print ' Estimated temperature: %.3g'%(T0)
     2085            print (' Estimated temperature: %.3g'%(T0))
    20852086            result = so.basinhopping(sumREFD,values,take_step=take_step,disp=True,T=T0,stepsize=Bfac,
    20862087                interval=20,niter=200,minimizer_kwargs={'method':'L-BFGS-B','bounds':bounds,
     
    21022103            ncalc = result[3]
    21032104            covM = []
    2104             print ' MC/SA final temperature: %.4g'%(result[2])
     2105            print (' MC/SA final temperature: %.4g'%(result[2]))
    21052106        elif data['Minimizer'] == 'L-BFGS-B':
    21062107            result = so.minimize(sumREFD,values,method='L-BFGS-B',bounds=bounds,   #ftol=Ftol,
     
    21472148            covMatrix = []
    21482149        sigDict = dict(zip(varyList,sig))
    2149         print ' Results of reflectometry data modelling fit:'
    2150         print 'Number of function calls:',ncalc,' Number of observations: ',Ifin-Ibeg,' Number of parameters: ',len(varyList)
    2151         print 'Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF'])
     2150        print (' Results of reflectometry data modelling fit:')
     2151        print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(ncalc,Ifin-Ibeg,len(varyList)))
     2152        print ('Rwp = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f'%(Rvals['Rwp'],chisq,Rvals['GOF']))
    21522153        SetModelParms()
    21532154        return True,result,varyList,sig,Rvals,covMatrix,parmDict,''
    21542155    except (ValueError,TypeError):      #when bad LS refinement; covM missing or with nans
    2155         print Msg
     2156        print (Msg)
    21562157        return False,0,0,0,0,0,0,Msg
    21572158       
     
    23362337       
    23372338def makeRefdFFT(Limits,Profile):
    2338     print 'make fft'
     2339    print ('make fft')
    23392340    Q,Io = Profile[:2]
    23402341    Qmin = Limits[1][0]
     
    24012402        if 'bin' in name:
    24022403            DIFFaX = name+'/DIFFaX.exe'
    2403             print ' Execute ',DIFFaX
     2404            print (' Execute '+DIFFaX)
    24042405            break
    24052406    # make form factor file that DIFFaX wants - atom types are GSASII style
    24062407    sf = open('data.sfc','w')
    24072408    sf.write('GSASII special form factor file for DIFFaX\n\n')
    2408     atTypes = Layers['AtInfo'].keys()
     2409    atTypes = list(Layers['AtInfo'].keys())
    24092410    if 'H' not in atTypes:
    24102411        atTypes.insert(0,'H')
     
    25202521            sumPx += p
    25212522        if sumPx != 1.0:    #this has to be picky since DIFFaX is.
    2522             print 'ERROR - Layer probabilities sum to ',sumPx,' DIFFaX will insist it = 1.0'
     2523            print ('ERROR - Layer probabilities sum to %.3f DIFFaX will insist it = 1.0'%sumPx)
    25232524            df.close()
    25242525            os.remove('data.sfc')
     
    25312532        subp.call(DIFFaX)
    25322533    except OSError:
    2533         print ' DIFFax.exe is not available for this platform - under development'
    2534     print ' DIFFaX time = %.2fs'%(time.time()-time0)
     2534        print (' DIFFax.exe is not available for this platform - under development')
     2535    print (' DIFFaX time = %.2fs'%(time.time()-time0))
    25352536    if os.path.exists('GSASII-DIFFaX.spc'):
    25362537        Xpat = np.loadtxt('GSASII-DIFFaX.spc').T
     
    26282629    Cell = Layers['Cell'][1:4]+Layers['Cell'][6:7]
    26292630# atoms in layers
    2630     atTypes = Layers['AtInfo'].keys()
     2631    atTypes = list(Layers['AtInfo'].keys())
    26312632    AtomXOU = []
    26322633    AtomTp = []
     
    26972698    time0 = time.time()
    26982699    pyx.pygetspc(controls,Nspec,spec)
    2699     print ' GETSPC time = %.2fs'%(time.time()-time0)
     2700    print (' GETSPC time = %.2fs'%(time.time()-time0))
    27002701    time0 = time.time()
    27012702    U = ateln2*inst['U'][1]/10000.
     
    27282729        profile[2][iBeg:iFin] = np.where(profile[1][iBeg:iFin]>0.,1./profile[1][iBeg:iFin],1.0)
    27292730    profile[5][iBeg:iFin] = profile[1][iBeg:iFin]-profile[3][iBeg:iFin]
    2730     print ' Broadening time = %.2fs'%(time.time()-time0)
     2731    print (' Broadening time = %.2fs'%(time.time()-time0))
    27312732   
    27322733def CalcStackingSADP(Layers,debug):
     
    27632764        iB += Nblk
    27642765    Layers['Sadp']['Img'] = Sapd
    2765     print ' GETSAD time = %.2fs'%(time.time()-time0)
     2766    print (' GETSAD time = %.2fs'%(time.time()-time0))
    27662767#    GSASIIpath.IPyBreak()
    27672768   
     
    28202821    for i in range(100):
    28212822        getPeakProfile(parmDict1,xdata,varyList,bakType)
    2822     print '100+6*Ka1-2 peaks=1200 peaks',time.time()-time0
     2823    print ('100+6*Ka1-2 peaks=1200 peaks %.2f'%time.time()-time0)
    28232824   
    28242825def test2(name,delt):
     
    28582859    for name,shft in [['pos',0.0001],['sig',0.01],['gam',0.0001],['shl',0.00005]]:
    28592860        test3(name,shft)
    2860     print "OK"
     2861    print ("OK")
    28612862    plotter.StartEventLoop()
Note: See TracChangeset for help on using the changeset viewer.