Ignore:
Timestamp:
Dec 26, 2020 12:25:59 AM (2 years ago)
Author:
toby
Message:

improve refinement messages; help info

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIstrMain.py

    r4666 r4682  
    4848DEBUG = True
    4949
     50def ReportProblems(result,Rvals,varyList):
     51    '''Create a message based results from the refinement
     52    '''
     53    #report on SVD 0's and highly correlated variables
     54    msg = ''
     55    SVD0 = result[2].get('SVD0')
     56    if SVD0 > 0:
     57        msg += 'Warning: There were {} singularities in the Hessian'.format(SVD0)
     58    # process singular variables
     59    psing = result[2].get('psing',[])
     60    if psing:
     61        if msg: msg += '\n'
     62        msg += 'Parameters dropped due to singularities:'
     63    for i,val in enumerate(psing):
     64        if i == 0:
     65            msg += '\n\t{}'.format(varyList[val])
     66        else:
     67            msg += ', {}'.format(varyList[val])
     68    #report on highly correlated variables
     69    Hcorr = result[2].get('Hcorr',[])
     70    for i,(v1,v2,corr) in enumerate(Hcorr):
     71        if msg: msg += '\n'
     72        if i == 0:
     73            msg += 'Note highly correlated parameters:\n'
     74        if corr > .95:
     75            stars = '**'
     76        else:
     77            stars = '   '
     78        msg += ' {} {} and {} (@{:.2f}%)'.format(
     79            stars,varyList[v1],varyList[v2],100.*corr)
     80    if msg:
     81        if 'msg' not in Rvals: Rvals['msg'] = ''
     82        Rvals['msg'] += msg
     83
    5084def RefineCore(Controls,Histograms,Phases,restraintDict,rigidbodyDict,parmDict,varyList,
    5185    calcControls,pawleyLookup,ifSeq,printFile,dlg,refPlotUpdate=None):
     
    6094#    print 'current',varyList
    6195#    for item in parmDict: print item,parmDict[item] ######### show dict just before refinement
    62     G2mv.Map2Dict(parmDict,varyList)
    6396    ifPrint = True
    6497    if ifSeq:
     
    67100    chisq0 = None
    68101    while True:
     102        G2mv.Map2Dict(parmDict,varyList)
    69103        begin = time.time()
    70104        values =  np.array(G2stMth.Dict2Values(parmDict, varyList))
     
    95129            ncyc = result[2]['num cyc']+1
    96130            Rvals['lamMax'] = result[2]['lamMax']
     131            if 'msg' in result[2]:
     132                Rvals['msg'] = result[2]['msg']
    97133            Controls['Marquardt'] = -3  #reset to default
    98134            if 'chisq0' in result[2] and chisq0 is None:
     
    164200#            table = dict(zip(varyList,zip(values,result[0],(result[0]-values)/sig)))
    165201#            for item in table: print item,table[item]               #useful debug - are things shifting?
     202            # report on refinement issues. Result in Rvals['msg']
     203            ReportProblems(result,Rvals,varyList)
    166204            break                   #refinement succeeded - finish up!
    167         except TypeError:          #result[1] is None on singular matrix or LinAlgError
     205        except TypeError:
     206            # if we get here, no result[1] (covar matrix) was returned or other calc error: refinement failed
    168207            IfOK = False
    169208            if not len(varyList):
    170209                covMatrix = []
    171210                break
    172             G2fil.G2Print ('**** Refinement failed - singular matrix ****',mode='error')
    173211            if 'Hessian' in Controls['deriv type']:
     212                SVD0 = result[2].get('SVD0')
     213                if SVD0 == -1:
     214                    G2fil.G2Print ('**** Refinement failed - singular matrix ****',mode='error')
     215                elif SVD0 == -2:
     216                    G2fil.G2Print ('**** Refinement failed - other problem ****',mode='error')
     217                elif SVD0 > 0:
     218                    G2fil.G2Print ('**** Refinement failed with {} SVD singularities ****'.format(SVD0),mode='error')
     219                else:
     220                    G2fil.G2Print ('**** Refinement failed ****',mode='error')
    174221                if result[1] is None:
    175222                    IfOK = False
    176223                    covMatrix = []
    177224                    sig = len(varyList)*[None,]
    178                     break
    179                 num = len(varyList)-1
    180                 for i,val in enumerate(np.flipud(result[2]['psing'])):
    181                     if val:
    182                         G2fil.G2Print ('Removing parameter: '+varyList[num-i])
    183                         del(varyList[num-i])
     225                # report on highly correlated variables
     226                ReportProblems(result,Rvals,varyList)
     227                # process singular variables
     228                psing = result[2].get('psing',[])
     229                if dlg: break # refining interactively
     230                # non-interactive refinement
     231                for val in sorted(psing,reverse=True):
     232                    G2fil.G2Print ('Removing parameter: '+varyList[val])
     233                    del(varyList[val])
     234                if not psing: break    # removed variable(s), try again
    184235            else:
     236                G2fil.G2Print ('**** Refinement failed - singular matrix ****',mode='error')
    185237                Ipvt = result[2]['ipvt']
    186238                for i,ipvt in enumerate(Ipvt):
     
    196248
    197249def Refine(GPXfile,dlg=None,makeBack=True,refPlotUpdate=None):
    198     'Global refinement -- refines to minimize against all histograms'
     250    '''Global refinement -- refines to minimize against all histograms.
     251    This can be called in one of three ways, from :meth:`GSASIIdataGUI.GSASII.OnRefine` in an
     252    interactive refinement, where dlg will be a wx.ProgressDialog, or non-interactively from
     253    :meth:`GSASIIscriptable.G2Project.refine` or from :func:`main`, where dlg will be None.
     254    '''
    199255    import GSASIImpsubs as G2mp
    200256    G2mp.InitMP()
     
    217273        G2fil.G2Print (' *** ERROR - you have no phases to refine! ***')
    218274        G2fil.G2Print (' *** Refine aborted ***')
    219         return False,'No phases'
     275        return False,{'msg':'No phases'}
    220276    if not Histograms:
    221277        G2fil.G2Print (' *** ERROR - you have no data to refine with! ***')
    222278        G2fil.G2Print (' *** Refine aborted ***')
    223         return False,'No data'
     279        return False,{'msg':'No data'}
    224280    rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
    225281    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
     
    250306    msg = G2mv.EvaluateMultipliers(constrDict,parmDict)
    251307    if msg:
    252         return False,'Unable to interpret multiplier(s): '+msg
     308        return False,{'msg':'Unable to interpret multiplier(s): '+msg}
    253309    try:
    254310        G2mv.GenerateConstraints(varyList,constrDict,fixedList,parmDict)
     
    261317        #print 'Errors',errmsg
    262318        #if warnmsg: print 'Warnings',warnmsg
    263         return False,' Constraint error'
     319        return False,{'msg':' Constraint error'}
    264320#    print G2mv.VarRemapShow(varyList)
    265321
     
    280336    printFile.write('\n Refinement results:\n')
    281337    printFile.write(135*'-'+'\n')
     338    Rvals = {}
    282339    try:
    283340        covData = {}
     
    305362            G2stIO.SetHistogramData(parmDict,sigDict,Histograms,calcControls,pFile=printFile)
    306363            if len(frozen):
     364                if 'msg' in Rvals:
     365                    Rvals['msg'] += '\n'
     366                else:
     367                    Rvals['msg'] = ''
    307368                msg = ('Warning: {} variable(s) refined outside limits and were frozen ({} total frozen)'
    308369                    .format(len(frozen),len(parmFrozenList))
    309370                    )
    310371                G2fil.G2Print(msg)
    311                 Rvals['msg'] = msg
     372                Rvals['msg'] += msg
    312373            elif len(parmFrozenList):
     374                if 'msg' in Rvals:
     375                    Rvals['msg'] += '\n'
     376                else:
     377                    Rvals['msg'] = ''
    313378                msg = ('Note: a total of {} variable(s) are frozen due to refining outside limits'
    314379                    .format(len(parmFrozenList))
    315380                    )
    316381                G2fil.G2Print('Note: ',msg)
    317                 Rvals['msg'] = msg
     382                Rvals['msg'] += msg
    318383            G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,rigidbodyDict,covData,parmFrozenList,makeBack)
    319384            printFile.close()
     
    321386            G2fil.G2Print (' ***** Refinement successful *****')
    322387        else:
    323             G2fil.G2Print ('****ERROR - Refinement failed')
    324             raise G2obj.G2Exception('****ERROR - Refinement failed')
     388            G2fil.G2Print ('****ERROR - Refinement failed',mode='error')
     389            if 'msg' in Rvals:
     390                G2fil.G2Print ('Note refinement problem:',mode='warn')
     391                G2fil.G2Print (Rvals['msg'],mode='warn')
     392            raise G2obj.G2Exception('**** ERROR: Refinement failed ****')
    325393    except G2obj.G2RefineCancel as Msg:
    326394        printFile.close()
    327395        G2fil.G2Print (' ***** Refinement stopped *****')
    328         return False,Msg.msg
    329     except G2obj.G2Exception as Msg:  # cell metric error, others?
     396        if not hasattr(Msg,'msg'): Msg.msg = str(Msg)
     397        if 'msg' in Rvals:
     398            Rvals['msg'] += '\n'
     399            Rvals['msg'] += Msg.msg
     400            if not dlg:
     401                G2fil.G2Print ('Note refinement problem:',mode='warn')
     402                G2fil.G2Print (Rvals['msg'],mode='warn')
     403        else:
     404            Rvals['msg'] = Msg.msg
     405        return False,Rvals
     406#    except G2obj.G2Exception as Msg:  # cell metric error, others?
     407    except Exception as Msg:  # cell metric error, others?
     408        if GSASIIpath.GetConfigValue('debug'):
     409            import traceback
     410            print(traceback.format_exc())       
     411        if not hasattr(Msg,'msg'): Msg.msg = str(Msg)
    330412        printFile.close()
    331413        G2fil.G2Print (' ***** Refinement error *****')
    332         return False,Msg.msg
     414        if 'msg' in Rvals:
     415            Rvals['msg'] += '\n\n'
     416            Rvals['msg'] += Msg.msg
     417            if not dlg:
     418                G2fil.G2Print ('Note refinement problem:',mode='warn')
     419                G2fil.G2Print (Rvals['msg'],mode='warn')
     420        else:
     421            Rvals['msg'] = Msg.msg
     422        return False,Rvals
    333423
    334424#for testing purposes, create a file for testderiv
     
    346436    if dlg:
    347437        return True,Rvals
     438    elif 'msg' in Rvals:
     439        G2fil.G2Print ('Reported from refinement:',mode='warn')
     440        G2fil.G2Print (Rvals['msg'],mode='warn')
    348441
    349442def phaseCheck(phaseVary,Phases,histogram):
     
    642735                   
    643736        except G2obj.G2RefineCancel as Msg:
     737            if not hasattr(Msg,'msg'): Msg.msg = str(Msg)
    644738            printFile.close()
    645739            G2fil.G2Print (' ***** Refinement stopped *****')
    646740            return False,Msg.msg
    647741        except G2obj.G2Exception as Msg:  # cell metric error, others?
     742            if not hasattr(Msg,'msg'): Msg.msg = str(Msg)
    648743            printFile.close()
    649744            G2fil.G2Print (' ***** Refinement error *****')
     
    657752        G2stIO.SetSeqResult(GPXfile,Histograms,SeqResult)
    658753    except Exception as msg:
    659         print('Error reading Sequential results')
     754        print('Error reading Sequential results\n',str(msg))
    660755        if GSASIIpath.GetConfigValue('debug'):
    661756            import traceback
Note: See TracChangeset for help on using the changeset viewer.