Ignore:
Timestamp:
Oct 23, 2017 11:39:16 AM (4 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
  • trunk/GSASIIstrMain.py

    r3100 r3136  
    1212# $Id$
    1313########### SVN repository information ###################
     14from __future__ import division, print_function
     15import platform
    1416import sys
    1517import os.path as ospath
     
    1719import math
    1820import copy
     21if '2' in platform.python_version_tuple()[0]:
     22    import cPickle
     23else:
     24    import pickle as cPickle
    1925import numpy as np
    2026import numpy.linalg as nl
     
    6066        #print '*** before fit chi**2',np.sum(G2stMth.errRefine(values,*args)**2)
    6167        #fl = open('beforeFit.cpickle','wb')
    62         #import cPickle
    6368        #cPickle.dump(values,fl,1)
    6469        #cPickle.dump(args[:-1],fl,1)
     
    103108        Rvals['Rwp'] = np.sqrt(Rvals['chisq']/Histograms['sumwYo'])*100.      #to %
    104109        Rvals['GOF'] = np.sqrt(Rvals['chisq']/(Histograms['Nobs']-len(varyList)))
    105         print >>printFile,' Number of function calls:',result[2]['nfev'],   \
    106             ' No. of observations: ',Histograms['Nobs'],' No. of parameters: ',len(varyList),   \
    107             ' User rejected: ',Histograms['Nrej'],' Sp. gp. extinct: ',Histograms['Next']
    108         print >>printFile,' Refinement time = %8.3fs, %8.3fs/cycle, for %d cycles'%(runtime,runtime/ncyc,ncyc)
    109         print >>printFile,' wR = %7.2f%%, chi**2 = %12.6g, GOF = %6.2f'%(Rvals['Rwp'],Rvals['chisq'],Rvals['GOF'])
     110        printFile.write(' Number of function calls: %d No. of observations: %d No. of parameters: %d User rejected: %d Sp. gp. extinct: %d\n'%  \
     111            (result[2]['nfev'],Histograms['Nobs'],len(varyList),Histograms['Nrej'],Histograms['Next']))
     112        printFile.write(' Refinement time = %8.3fs, %8.3fs/cycle, for %d cycles\n'%(runtime,runtime/ncyc,ncyc))
     113        printFile.write(' wR = %7.2f%%, chi**2 = %12.6g, GOF = %6.2f\n'%(Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']))
    110114        IfOK = True
    111115        try:
     
    113117            sig = np.sqrt(np.diag(covMatrix))
    114118            if np.any(np.isnan(sig)) or not sig.shape:
    115                 print '*** Least squares aborted - some invalid esds possible ***'
     119                print ('*** Least squares aborted - some invalid esds possible ***')
    116120#            table = dict(zip(varyList,zip(values,result[0],(result[0]-values)/sig)))
    117121#            for item in table: print item,table[item]               #useful debug - are things shifting?
     
    123127                sig = []
    124128                break
    125             print '**** Refinement failed - singular matrix ****'
     129            print ('**** Refinement failed - singular matrix ****')
    126130            if 'Hessian' in Controls['deriv type']:
    127131                num = len(varyList)-1
    128132                for i,val in enumerate(np.flipud(result[2]['psing'])):
    129133                    if val:
    130                         print 'Removing parameter: ',varyList[num-i]
     134                        print ('Removing parameter: '+varyList[num-i])
    131135                        del(varyList[num-i])
    132136            else:
     
    134138                for i,ipvt in enumerate(Ipvt):
    135139                    if not np.sum(result[2]['fjac'],axis=1)[i]:
    136                         print 'Removing parameter: ',varyList[ipvt-1]
     140                        print ('Removing parameter: '+varyList[ipvt-1])
    137141                        del(varyList[ipvt-1])
    138142                        break
     
    161165    Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile)
    162166    if not Phases:
    163         print ' *** ERROR - you have no phases to refine! ***'
    164         print ' *** Refine aborted ***'
     167        print (' *** ERROR - you have no phases to refine! ***')
     168        print (' *** Refine aborted ***')
    165169        return False,'No phases'
    166170    if not Histograms:
    167         print ' *** ERROR - you have no data to refine with! ***'
    168         print ' *** Refine aborted ***'
     171        print (' *** ERROR - you have no data to refine with! ***')
     172        print (' *** Refine aborted ***')
    169173        return False,'No data'
    170174    rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
     
    201205        #print 'IndependentVars',G2mv.GetIndependentVars()
    202206    except:
    203         print ' *** ERROR - your constraints are internally inconsistent ***'
     207        print (' *** ERROR - your constraints are internally inconsistent ***')
    204208        #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
    205209        #print 'Errors',errmsg
     
    209213
    210214    ifPrint = True
    211     print >>printFile,'\n Refinement results:'
    212     print >>printFile,135*'-'
    213     try:
     215    printFile.write('\n Refinement results:\n')
     216    printFile.write(135*'-'+'\n')
     217    if True:
     218#    try:
    214219        covData = {}
    215220        IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histograms,Phases,restraintDict,
     
    233238            G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,rigidbodyDict,covData,makeBack)
    234239            printFile.close()
    235             print ' Refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst'
    236             print ' ***** Refinement successful *****'
     240            print (' Refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst')
     241            print (' ***** Refinement successful *****')
    237242        else:
    238             print '****ERROR - Refinement failed'
     243            print ('****ERROR - Refinement failed')
    239244            raise G2obj.G2Exception('****ERROR - Refinement failed')
    240     except G2obj.G2Exception,Msg:
    241         printFile.close()
    242         return False,Msg.msg
     245#    except G2obj.G2Exception(Msg):
     246#        printFile.close()
     247#        return False,Msg.msg
    243248
    244249#for testing purposes!!!
    245250    if DEBUG and IfOK:
    246251#needs: values,HistoPhases,parmDict,varylist,calcControls,pawleyLookup
    247         import cPickle
    248252        fl = open('testDeriv.dat','wb')
    249253        cPickle.dump(result[0],fl,1)
     
    291295
    292296    printFile = open(ospath.splitext(GPXfile)[0]+'.lst','w')
    293     print 'Starting Sequential Refinement'
     297    print ('Starting Sequential Refinement')
    294298    G2stIO.ShowBanner(printFile)
    295299    Controls = G2stIO.GetControls(GPXfile)
     
    298302    Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile)
    299303    if not Phases:
    300         print ' *** ERROR - you have no phases to refine! ***'
    301         print ' *** Refine aborted ***'
     304        print (' *** ERROR - you have no phases to refine! ***')
     305        print (' *** Refine aborted ***')
    302306        return False,'No phases'
    303307    if not Histograms:
    304         print ' *** ERROR - you have no data to refine with! ***'
    305         print ' *** Refine aborted ***'
     308        print (' *** ERROR - you have no data to refine with! ***')
     309        print (' *** Refine aborted ***')
    306310        return False,'No data'
    307311    rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
     
    312316    for item in phaseVary:
    313317        if '::A0' in item:
    314             print '**** WARNING - lattice parameters should not be refined in a sequential refinement ****'
    315             print '****           instead use the Dij parameters for each powder histogram            ****'
     318            print ('**** WARNING - lattice parameters should not be refined in a sequential refinement ****')
     319            print ('****           instead use the Dij parameters for each powder histogram            ****')
    316320            return False,'Lattice parameter refinement error - see console message'
    317321        if '::C(' in item:
    318             print '**** WARNING - phase texture parameters should not be refined in a sequential refinement ****'
    319             print '****           instead use the C(L,N) parameters for each powder histogram               ****'
     322            print ('**** WARNING - phase texture parameters should not be refined in a sequential refinement ****')
     323            print ('****           instead use the C(L,N) parameters for each powder histogram               ****')
    320324            return False,'Phase texture refinement error - see console message'
    321325    if 'Seq Data' in Controls:
     
    386390            constraintInfo = (groups,parmlist,constrDict,fixedList,ihst)
    387391        except:
    388             print ' *** ERROR - your constraints are internally inconsistent ***'
     392            print (' *** ERROR - your constraints are internally inconsistent ***')
    389393            #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
    390394            #print 'Errors',errmsg
     
    433437
    434438        ifPrint = False
    435         print >>printFile,'\n Refinement results for histogram: v'+histogram
    436         print >>printFile,135*'-'
    437         try:
     439        printFile.write('\n Refinement results for histogram: %s\n'%histogram)
     440        printFile.write(135*'-'+'\n')
     441        if True:
     442#        try:
    438443            IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histo,Phases,restraintDict,
    439444                rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifPrint,printFile,dlg)
     
    441446                PlotFunction(G2frame,Histo[histogram]['Data'],histogram)
    442447
    443             print '  wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%(
    444                 Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2'])
     448            print ('  wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%(
     449                Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2']))
    445450            # add the uncertainties into the esd dictionary (sigDict)
    446451            sigDict = dict(zip(varyList,sig))
     
    479484                        newparm = ':'.join(items)
    480485                        NewparmDict[newparm] = parmDict[parm]
    481         except G2obj.G2Exception,Msg:
    482             printFile.close()
    483             print ' ***** Refinement aborted *****'
    484             return False,Msg.msg
     486#        except G2obj.G2Exception(Msg):
     487#            printFile.close()
     488#            print (' ***** Refinement aborted *****')
     489#            return False,Msg.msg
    485490    if Controls.get('Reverse Seq'):
    486491        histNames.reverse()
     
    488493    G2stIO.SetSeqResult(GPXfile,Histograms,SeqResult)
    489494    printFile.close()
    490     print ' Sequential refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst'
    491     print ' ***** Sequential refinement successful *****'
     495    print (' Sequential refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst')
     496    print (' ***** Sequential refinement successful *****')
    492497    return True,'Success'
    493498
     
    730735    if DATData['Natoms'] == 4:  #torsion
    731736        Tors,sig = G2mth.GetDATSig(Oatoms,Datoms,Amat,SGData,covData)
    732         print ' Torsion angle for '+DATData['Name']+' atom sequence: ',atmSeq,'=',G2mth.ValEsd(Tors,sig)
    733         print ' NB: Atom sequence determined by selection order'
     737        print (' Torsion angle for %s atom sequence: %s = %s'%(DATData['Name'],str(atmSeq).replace("'","")[1:-1],G2mth.ValEsd(Tors,sig)))
     738        print (' NB: Atom sequence determined by selection order')
    734739        return      # done with torsion
    735740    elif DATData['Natoms'] == 3:  #angle
    736741        Ang,sig = G2mth.GetDATSig(Oatoms,Datoms,Amat,SGData,covData)
    737         print ' Angle in '+DATData['Name']+' for atom sequence: ',atmSeq,'=',G2mth.ValEsd(Ang,sig)
    738         print ' NB: Atom sequence determined by selection order'
     742        print (' Angle in %s for atom sequence: %s = %s'%(DATData['Name'],str(atmSeq).replace("'","")[1:-1],G2mth.ValEsd(Ang,sig)))
     743        print (' NB: Atom sequence determined by selection order')
    739744    else:   #2 atoms - distance
    740745        Dist,sig = G2mth.GetDATSig(Oatoms,Datoms,Amat,SGData,covData)
    741         print ' Distance in '+DATData['Name']+' for atom sequence: ',atmSeq,'=',G2mth.ValEsd(Dist,sig)
     746        print (' Distance in %s for atom sequence: %s = %s'%(DATData['Name'],str(atmSeq).replace("'","")[1:-1],G2mth.ValEsd(Dist,sig)))
    742747
    743748def BestPlane(PlaneData):
     
    745750
    746751    def ShowBanner(name):
    747         print 80*'*'
    748         print '   Best plane result for phase '+name
    749         print 80*'*','\n'
     752        print (80*'*')
     753        print ('   Best plane result for phase '+name)
     754        print (80*'*','\n')
    750755
    751756    ShowBanner(PlaneData['Name'])
     
    767772    for i,xyz in enumerate(XYZ):
    768773        Zmat += np.outer(xyz.T,xyz)
    769     print ' Selected atoms centered at %10.5f %10.5f %10.5f'%(sumXYZ[0],sumXYZ[1],sumXYZ[2])
     774    print (' Selected atoms centered at %10.5f %10.5f %10.5f'%(sumXYZ[0],sumXYZ[1],sumXYZ[2]))
    770775    Evec,Emat = nl.eig(Zmat)
    771776    Evec = np.sqrt(Evec)/(Natoms-3)
     
    773778    XYZ = np.inner(XYZ,Emat.T).T
    774779    XYZ = np.array([XYZ[Order[2]],XYZ[Order[1]],XYZ[Order[0]]]).T
    775     print ' Atoms in Cartesian best plane coordinates:'
    776     print ' Name         X         Y         Z'
     780    print (' Atoms in Cartesian best plane coordinates:')
     781    print (' Name         X         Y         Z')
    777782    for i,xyz in enumerate(XYZ):
    778         print ' %6s%10.3f%10.3f%10.3f'%(Atoms[i][1].ljust(6),xyz[0],xyz[1],xyz[2])
    779     print '\n Best plane RMS X =%8.3f, Y =%8.3f, Z =%8.3f'%(Evec[Order[2]],Evec[Order[1]],Evec[Order[0]])
     783        print (' %6s%10.3f%10.3f%10.3f'%(Atoms[i][1].ljust(6),xyz[0],xyz[1],xyz[2]))
     784    print ('\n Best plane RMS X =%8.3f, Y =%8.3f, Z =%8.3f'%(Evec[Order[2]],Evec[Order[1]],Evec[Order[0]]))
    780785
    781786def main():
     
    786791        GPXfile = arg[1]
    787792        if not ospath.exists(GPXfile):
    788             print 'ERROR - ',GPXfile," doesn't exist!"
     793            print ('ERROR - '+GPXfile+" doesn't exist!")
    789794            exit()
    790795    else:
    791         print 'ERROR - missing filename'
     796        print ('ERROR - missing filename')
    792797        exit()
    793798    # TODO: figure out if this is a sequential refinement and call SeqRefine(GPXfile,None)
Note: See TracChangeset for help on using the changeset viewer.