Changeset 4025


Ignore:
Timestamp:
Jun 13, 2019 3:56:48 PM (2 years ago)
Author:
toby
Message:

more print filtering

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIImath.py

    r4016 r4025  
    2929import GSASIIspc as G2spc
    3030import GSASIIpwd as G2pwd
     31import GSASIIfiles as G2fil
    3132import numpy.fft as fft
    3233import scipy.optimize as so
     
    157158    nfev = 0
    158159    if Print:
    159         print (' Hessian Levenberg-Marquardt SVD refinement on %d variables:'%(n))
     160        G2fil.G2Print(' Hessian Levenberg-Marquardt SVD refinement on %d variables:'%(n))
    160161    Lam = np.zeros((n,n))
    161162    while icycle < maxcyc:
     
    174175        Amat /= Anorm
    175176        if Print:
    176             print ('initial chi^2 %.5g on %d obs.'%(chisq0,Nobs))
     177            G2fil.G2Print('initial chi^2 %.5g on %d obs.'%(chisq0,Nobs))
    177178        chitol = ftol
    178179        while True:
     
    183184            except nl.LinAlgError:
    184185                psing = list(np.where(np.diag(nl.qr(Amatlam)[1]) < 1.e-14)[0])
    185                 print ('ouch #1 bad SVD inversion; change parameterization for ',psing)
     186                G2fil.G2Print('ouch #1 bad SVD inversion; change parameterization for ',psing, mode='error')
    186187                return [x0,None,{'num cyc':icycle,'fvec':M,'nfev':nfev,'lamMax':lamMax,'psing':psing,'SVD0':-1}]
    187188            Xvec = np.inner(Ainv,Yvec)      #solve
     
    193194                lam *= 10.
    194195                if Print:
    195                     print ('new chi^2 %.5g on %d obs., %d SVD zeros ; matrix modification needed; lambda now %.1e'  \
     196                    G2fil.G2Print('new chi^2 %.5g on %d obs., %d SVD zeros ; matrix modification needed; lambda now %.1e'  \
    196197                           %(chisq1,Nobs,Nzeros,lam))
    197198            else:
     
    200201                break
    201202            if lam > 10.:
    202                 print ('ouch #3 chisq1 %g.4 stuck > chisq0 %g.4'%(chisq1,chisq0))
     203                G2fil.G2Print('ouch #3 chisq1 %g.4 stuck > chisq0 %g.4'%(chisq1,chisq0), mode='warn')
    203204                break
    204205            chitol *= 2
     
    206207        deltaChi2 = (chisq0-chisq1)/chisq0
    207208        if Print:
    208             print (' Cycle: %d, Time: %.2fs, Chi**2: %.5g for %d obs., Lambda: %.3g,  Delta: %.3g'%(
     209            G2fil.G2Print(' Cycle: %d, Time: %.2fs, Chi**2: %.5g for %d obs., Lambda: %.3g,  Delta: %.3g'%(
    209210                icycle,time.time()-time0,chisq1,Nobs,lamMax,deltaChi2))
    210211        Histograms = args[0][0]
     
    212213        if deltaChi2 < ftol:
    213214            ifConverged = True
    214             if Print: print ("converged")
     215            if Print: G2fil.G2Print("converged")
    215216            break
    216217        icycle += 1
     
    224225    try:
    225226        Bmat,Nzero = pinv(Amatlam,xtol)    #Moore-Penrose inversion (via SVD) & count of zeros
    226         if Print: print ('Found %d SVD zeros'%(Nzero))
     227        if Print: G2fil.G2Print('Found %d SVD zeros'%(Nzero), mode='warn')
    227228        Bmat = Bmat/Anorm
    228229        return [x0,Bmat,{'num cyc':icycle,'fvec':M,'nfev':nfev,'lamMax':lamMax,'psing':[],'SVD0':Nzero,'Converged': ifConverged, 'DelChi2':deltaChi2}]
    229230    except nl.LinAlgError:
    230         print ('ouch #2 linear algebra error in making v-cov matrix')
     231        G2fil.G2Print('ouch #2 linear algebra error in making v-cov matrix', mode='error')
    231232        psing = []
    232233        if maxcyc:
     
    289290    nfev = 0
    290291    if Print:
    291         print (' Hessian SVD refinement on %d variables:'%(n))
     292        G2fil.G2Print(' Hessian SVD refinement on %d variables:'%(n))
    292293    while icycle < maxcyc:
    293294        time0 = time.time()
     
    304305        Amat /= Anorm
    305306        if Print:
    306             print ('initial chi^2 %.5g'%(chisq0))
     307            G2fil.G2Print('initial chi^2 %.5g'%(chisq0))
    307308        try:
    308309            Ainv,Nzeros = pinv(Amat,xtol)    #do Moore-Penrose inversion (via SVD)
    309310        except nl.LinAlgError:
    310             print ('ouch #1 bad SVD inversion; change parameterization')
     311            G2fil.G2Print('ouch #1 bad SVD inversion; change parameterization', mode='warn')
    311312            psing = list(np.where(np.diag(nl.qr(Amat)[1]) < 1.e-14)[0])
    312313            return [x0,None,{'num cyc':icycle,'fvec':M,'nfev':nfev,'lamMax':0.,'psing':psing,'SVD0':-1}]
     
    318319        deltaChi2 = (chisq0-chisq1)/chisq0
    319320        if Print:
    320             print (' Cycle: %d, Time: %.2fs, Chi**2: %.5g, Delta: %.3g'%(
     321            G2fil.G2Print(' Cycle: %d, Time: %.2fs, Chi**2: %.5g, Delta: %.3g'%(
    321322                icycle,time.time()-time0,chisq1,deltaChi2))
    322323        Histograms = args[0][0]
     
    324325        if deltaChi2 < ftol:
    325326            ifConverged = True
    326             if Print: print ("converged")
     327            if Print: G2fil.G2Print("converged")
    327328            break
    328329        icycle += 1
     
    335336    try:
    336337        Bmat,Nzero = pinv(Amat,xtol)    #Moore-Penrose inversion (via SVD) & count of zeros
    337         print ('Found %d SVD zeros'%(Nzero))
     338        G2fil.G2Print('Found %d SVD zeros'%(Nzero), mode='warn')
    338339#        Bmat = nl.inv(Amatlam); Nzeros = 0
    339340        Bmat = Bmat/Anorm
     
    341342            'SVD0':Nzero,'Converged': ifConverged, 'DelChi2':deltaChi2}]
    342343    except nl.LinAlgError:
    343         print ('ouch #2 linear algebra error in making v-cov matrix')
     344        G2fil.G2Print('ouch #2 linear algebra error in making v-cov matrix', mode='error')
    344345        psing = []
    345346        if maxcyc:
     
    27412742            Boxes[box[0],box[1],box[2],Boxes[box[0],box[1],box[2],0]] = ib
    27422743        except IndexError:
    2743             print('too many atoms in box' )
     2744            G2fil.G2Print('Error: too many atoms in box' )
    27442745            continue
    27452746    #Box content checks with errat.f $ erratv2.cpp ibox1 arrays
     
    30103011            Values2Dict(parmDict, varyList, result[0])
    30113012            GOF = chisq/(len(result[2]['fvec'])-len(varyList))       #reduced chi^2
    3012             print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],len(result[2]['fvec']),len(varyList)))
    3013             print ('refinement time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
     3013            G2fil.G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],len(result[2]['fvec']),len(varyList)))
     3014            G2fil.G2Print ('refinement time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
    30143015            try:
    30153016                sig = np.sqrt(np.diag(result[1])*GOF)
    30163017                if np.any(np.isnan(sig)):
    3017                     print ('*** Least squares aborted - some invalid esds possible ***')
     3018                    G2fil.G2Print ('*** Least squares aborted - some invalid esds possible ***', mode='error')
    30183019                break                   #refinement succeeded - finish up!
    30193020            except ValueError:          #result[1] is None on singular matrix
    3020                 print ('**** Refinement failed - singular matrix ****')
     3021                G2fil.G2Print ('**** Refinement failed - singular matrix ****', mode='error')
    30213022                return None
    30223023        else:
     
    30653066    generalData = data['General']
    30663067    if not generalData['Map']['MapType']:
    3067         print ('**** ERROR - Fourier map not defined')
     3068        G2fil.G2Print ('**** ERROR - Fourier map not defined')
    30683069        return
    30693070    mapData = generalData['Map']
     
    31193120    mapData['rhoMax'] = max(np.max(mapData['rho']),-np.min(mapData['rho']))
    31203121    mapData['minmax'] = [np.max(mapData['rho']),np.min(mapData['rho'])]
    3121     print ('Omit map time: %.4f no. elements: %d dimensions: %s'%(time.time()-time0,Fhkl.size,str(Fhkl.shape)))
     3122    G2fil.G2Print ('Omit map time: %.4f no. elements: %d dimensions: %s'%(time.time()-time0,Fhkl.size,str(Fhkl.shape)))
    31223123    return mapData
    31233124   
     
    31853186                    Fhkl[h,k,l] = complex(Fosq,0.)
    31863187    rho = fft.fftn(fft.fftshift(Fhkl))/cell[6]
    3187     print ('Fourier map time: %.4f no. elements: %d dimensions: %s'%(time.time()-time0,Fhkl.size,str(Fhkl.shape)))
     3188    G2fil.G2Print ('Fourier map time: %.4f no. elements: %d dimensions: %s'%(time.time()-time0,Fhkl.size,str(Fhkl.shape)))
    31883189    mapData['Type'] = reflDict['Type']
    31893190    mapData['rho'] = np.real(rho)
     
    32563257    mapData['rhoMax'] = max(np.max(mapData['rho']),-np.min(mapData['rho']))
    32573258    mapData['minmax'] = [np.max(mapData['rho']),np.min(mapData['rho'])]
    3258     print ('Fourier map time: %.4f no. elements: %d dimensions: %s'%(time.time()-time0,Fhkl.size,str(Fhkl.shape)))
     3259    G2fil.G2Print ('Fourier map time: %.4f no. elements: %d dimensions: %s'%(time.time()-time0,Fhkl.size,str(Fhkl.shape)))
    32593260
    32603261# map printing for testing purposes
     
    33393340        i += 1
    33403341    DH = np.array(DH)
    3341     print (' map offset no.of terms: %d from %d reflections'%(len(DH),len(Flist)))
     3342    G2fil.G2Print (' map offset no.of terms: %d from %d reflections'%(len(DH),len(Flist)))
    33423343    Dphi = np.array(Dphi)
    33433344    steps = np.array(hklShape)
     
    33513352    chisq = np.min(Mmap)
    33523353    DX = -np.array(np.unravel_index(np.argmin(Mmap),Mmap.shape))
    3353     print (' map offset chi**2: %.3f, map offset: %d %d %d'%(chisq,DX[0],DX[1],DX[2]))
     3354    G2fil.G2Print (' map offset chi**2: %.3f, map offset: %d %d %d'%(chisq,DX[0],DX[1],DX[2]))
    33543355#    print (np.dot(DX,DH.T)+.5)%1.-Dphi
    33553356    return DX
     
    34443445            break
    34453446    np.seterr(**old)
    3446     print (' Charge flip time: %.4f'%(time.time()-time0),'no. elements: %d'%(Ehkl.size))
     3447    G2fil.G2Print (' Charge flip time: %.4f'%(time.time()-time0),'no. elements: %d'%(Ehkl.size))
    34473448    CErho = np.real(fft.fftn(fft.fftshift(CEhkl)))/10.  #? to get on same scale as e-map
    3448     print (' No.cycles = %d Residual Rcf =%8.3f%s Map size: %s'%(Ncyc,Rcf,'%',str(CErho.shape)))
     3449    G2fil.G2Print (' No.cycles = %d Residual Rcf =%8.3f%s Map size: %s'%(Ncyc,Rcf,'%',str(CErho.shape)))
    34493450    roll = findOffset(SGData,A,CEhkl)               #CEhkl needs to be just the observed set, not the full set!
    34503451       
     
    35053506        i += 1
    35063507    DH = np.array(DH)
    3507     print (' map offset no.of terms: %d from %d reflections'%(len(DH),len(Flist)))
     3508    G2fil.G2Print (' map offset no.of terms: %d from %d reflections'%(len(DH),len(Flist)))
    35083509    Dphi = np.array(Dphi)
    35093510    steps = np.array(hklmShape)
     
    35173518    chisq = np.min(Mmap)
    35183519    DX = -np.array(np.unravel_index(np.argmin(Mmap),Mmap.shape))
    3519     print (' map offset chi**2: %.3f, map offset: %d %d %d %d'%(chisq,DX[0],DX[1],DX[2],DX[3]))
     3520    G2fil.G2Print (' map offset chi**2: %.3f, map offset: %d %d %d %d'%(chisq,DX[0],DX[1],DX[2],DX[3]))
    35203521#    print (np.dot(DX,DH.T)+.5)%1.-Dphi
    35213522    return DX
     
    36053606            break
    36063607    np.seterr(**old)
    3607     print (' Charge flip time: %.4f no. elements: %d'%(time.time()-time0,Ehkl.size))
     3608    G2fil.G2Print (' Charge flip time: %.4f no. elements: %d'%(time.time()-time0,Ehkl.size))
    36083609    CErho = np.real(fft.fftn(fft.fftshift(CEhkl[:,:,:,maxM+1])))/10.    #? to get on same scale as e-map
    36093610    SSrho = np.real(fft.fftn(fft.fftshift(CEhkl)))/10.                  #? ditto
    3610     print (' No.cycles = %d Residual Rcf =%8.3f%s Map size: %s'%(Ncyc,Rcf,'%',str(CErho.shape)))
     3611    G2fil.G2Print (' No.cycles = %d Residual Rcf =%8.3f%s Map size: %s'%(Ncyc,Rcf,'%',str(CErho.shape)))
    36113612    roll = findSSOffset(SGData,SSGData,A,CEhkl)               #CEhkl needs to be just the observed set, not the full set!
    36123613
     
    37783779        steps = np.array((3*[step,]),dtype='int32')
    37793780    except KeyError:
    3780         print ('**** ERROR - Fourier map not defined')
     3781        G2fil.G2Print ('**** ERROR - Fourier map not defined')
    37813782        return peaks,mags
    37823783    rhoMask = ma.array(rho,mask=(rho<contLevel))
     
    44364437        af = asarray(fqueue)*1.0
    44374438        if retval == 5:
    4438             print (' User terminated run; incomplete MC/SA')
     4439            G2fil.G2Print ('Error: User terminated run; incomplete MC/SA')
    44394440            keepGoing = False
    44404441            break
     
    44434444            if abs(af[-1]-best_state.cost) > feps*10:
    44444445                retval = 5
    4445                 print (" Warning: Cooled to %.4f > selected Tmin %.4f in %d steps"%(squeeze(last_state.cost),Tf,iters-1))
     4446                G2fil.G2Print (" Warning: Cooled to %.4f > selected Tmin %.4f in %d steps"%(squeeze(last_state.cost),Tf,iters-1))
    44464447            break
    44474448        if (Tf is not None) and (schedule.T < Tf):
     
    44534454            break
    44544455        if (iters > maxiter):
    4455             print  (" Warning: Maximum number of iterations exceeded.")
     4456            G2fil.G2Print  (" Warning: Maximum number of iterations exceeded.")
    44564457            retval = 3
    44574458            break
     
    44734474        timelist.append(result[1])
    44744475        nsflist.append(result[2])
    4475         print (' MC/SA final fit: %.3f%% structure factor time: %.3f'%(100*result[0][2],result[1]))
     4476        G2fil.G2Print (' MC/SA final fit: %.3f%% structure factor time: %.3f'%(100*result[0][2],result[1]))
    44764477    out_q.put(outlist)
    44774478    out_t.put(timelist)
     
    48794880    refs = np.array(refs).T
    48804881    if start:
    4881         print (' Minimum d-spacing used: %.2f No. reflections used: %d'%(MCSA['dmin'],nRef))
    4882         print (' Number of parameters varied: %d'%(len(varyList)))
     4882        G2fil.G2Print (' Minimum d-spacing used: %.2f No. reflections used: %d'%(MCSA['dmin'],nRef))
     4883        G2fil.G2Print (' Number of parameters varied: %d'%(len(varyList)))
    48834884        start = False
    48844885    parmDict['sumFosq'] = sumFosq
     
    49024903            lower=lower, upper=upper, slope=MCSA['log slope'],ranStart=MCSA.get('ranStart',False),
    49034904            ranRange=MCSA.get('ranRange',10.)/100.,autoRan=MCSA.get('autoRan',False),dlg=pgbar)
    4904         print (' Acceptance rate: %.2f%% MCSA residual: %.2f%%'%(100.*results[5]/results[3],100.*results[1]))
     4905        G2fil.G2Print (' Acceptance rate: %.2f%% MCSA residual: %.2f%%'%(100.*results[5]/results[3],100.*results[1]))
    49054906        results = so.minimize(mcsaCalc,results[0],method='L-BFGS-B',args=(refs,rcov,cosTable,ifInv,allFF,RBdata,varyList,parmDict),
    49064907            bounds=bounds,)
Note: See TracChangeset for help on using the changeset viewer.