Changeset 1505 for branch/logging/log.py


Ignore:
Timestamp:
Sep 20, 2014 8:27:59 PM (7 years ago)
Author:
toby
Message:

track with relative phase & histogram numbers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branch/logging/log.py

    r1497 r1505  
    55G2logList = [None]
    66'Contains a list of logged actions; first item is ignored'
    7 LogInfo = {'Logging':False, 'Tree':None}
     7LogInfo = {'Logging':False, 'Tree':None, 'G2frame':None}
    88'Contains a dict with values that are needed in the module'
    99
     
    1212# TODO:
    1313### Note: no provinance info for histogram instrument parameters: need to remove insVal etc.
    14 
    15 # track histograms and phases with relative indices
    1614
    1715#===========================================================================
     
    3331        if debug: print 'Logging var change: w/treeRefs',treeRefs,'indexRefs',indexRefs,'new value=',value
    3432    def __str__(self):
     33        treeList = self.treeRefs[:]
     34        if type(treeList[0]) is tuple:
     35            treeList[0] = 'Hist #'+str(treeList[0][1]+1)+' of type '+treeList[0][0]
     36        elif len(treeList) > 1 and type(treeList[1]) is int:
     37            treeList[1] = 'Phase #'+str(treeList[1]+1)
    3538        return 'Variable change: Key(s)= '+_l2s(self.indexRefs)+' to value='+str(self.value)
    3639
     
    6366    def __init__(self,itemlist):
    6467        self.treeItemList = itemlist
    65         if debug: print 'Logging press on tree: "',itemlist
     68        if debug: print 'Logging press on tree: ',itemlist
    6669    def __str__(self):
    67         return 'Tree item pressed: '+_l2s(self.treeItemList)
     70        treeList = self.treeItemList[:]
     71        if type(treeList[0]) is tuple:
     72            treeList[0] = 'Hist #'+str(treeList[0][1]+1)+' of type '+treeList[0][0]
     73        elif len(treeList) > 1 and type(treeList[1]) is int:
     74            treeList[1] = 'Phase #'+str(treeList[1]+1)
     75        return 'Tree item pressed: '+_l2s(treeList)
    6876           
    6977def _wrapper(func):
     
    170178    def __init__(self,parent=None,*args,**kwargs):
    171179        super(self.__class__,self).__init__(parent=parent,*args,**kwargs)
    172         self.G2frame = parent.GetParent()
     180        LogInfo['G2frame'] = self.G2frame = parent.GetParent()
    173181        self.root = self.AddRoot('Loaded Data: ')
    174182        self.SelectionChanged = None
     
    194202        if self.SelectionChanged:
    195203            textlist = self._getTreeItemsList(event.GetItem())
    196             if LogInfo['Logging']:
     204            if LogInfo['Logging'] and event.GetItem() != self.root:
     205                textlist[0] = self.GetRelativeHistNum(textlist[0])
     206                if textlist[0] == "Phases" and len(textlist) > 1:
     207                    textlist[1] = self.GetRelativePhaseNum(textlist[1])
    197208                G2logList.append(TreeLogEntry(textlist))
    198209            self.SelectionChanged(event)
     
    224235        self.repaintAction = None
    225236
     237    def GetRelativeHistNum(self,histname):
     238        '''Returns list with a histogram type and a relative number for that
     239        histogram, or the original string if not a histogram
     240        '''
     241        histtype = histname.split()[0]
     242        if histtype != histtype.upper(): # histograms (only) have a keyword all in caps
     243            return histname
     244        item, cookie = self.GetFirstChild(self.root)
     245        i = 0
     246        while item:
     247            itemtext = self.GetItemText(item)
     248            if itemtext == histname:
     249                return histtype,i
     250            elif itemtext.split()[0] == histtype:
     251                i += 1
     252            item, cookie = self.GetNextChild(self.root, cookie)
     253        else:
     254            raise Exception("Histogram not found: "+histname)
     255
     256    def ConvertRelativeHistNum(self,histtype,histnum):
     257        '''Converts a histogram type and relative histogram number to a
     258        histogram name in the current project
     259        '''
     260        item, cookie = self.GetFirstChild(self.root)
     261        i = 0
     262        while item:
     263            itemtext = self.GetItemText(item)
     264            if itemtext.split()[0] == histtype:
     265                if i == histnum: return itemtext
     266                i += 1
     267            item, cookie = self.GetNextChild(self.root, cookie)
     268        else:
     269            raise Exception("Histogram #'+str(histnum)+' of type "+histtype+' not found')
     270       
     271    def GetRelativePhaseNum(self,phasename):
     272        '''Returns a phase number if the string matches a phase name
     273        or else returns the original string
     274        '''
     275        item, cookie = self.GetFirstChild(self.root)
     276        while item:
     277            itemtext = self.GetItemText(item)
     278            if itemtext == "Phases":
     279                parent = item
     280                item, cookie = self.GetFirstChild(parent)
     281                i = 0
     282                while item:
     283                    itemtext = self.GetItemText(item)
     284                    if itemtext == phasename:
     285                        return i
     286                    item, cookie = self.GetNextChild(parent, cookie)
     287                    i += 1
     288                else:
     289                    return phasename # not a phase name
     290            item, cookie = self.GetNextChild(self.root, cookie)
     291        else:
     292            raise Exception("No phases found ")
     293
     294    def ConvertRelativePhaseNum(self,phasenum):
     295        '''Converts relative phase number to a phase name in
     296        the current project
     297        '''
     298        item, cookie = self.GetFirstChild(self.root)
     299        while item:
     300            itemtext = self.GetItemText(item)
     301            if itemtext == "Phases":
     302                parent = item
     303                item, cookie = self.GetFirstChild(parent)
     304                i = 0
     305                while item:
     306                    if i == phasenum:
     307                        return self.GetItemText(item)
     308                    item, cookie = self.GetNextChild(parent, cookie)
     309                    i += 1
     310                else:
     311                    raise Exception("Phase "+str(phasenum)+" not found")
     312            item, cookie = self.GetNextChild(self.root, cookie)
     313        else:
     314            raise Exception("No phases found ")
     315               
    226316    def RepaintDataWindow(self):
    227317        item = self.repaintAction
     
    251341        'Perform a Tree press action when read from the log'
    252342        parent = self.root
    253         for txt in logitem.treeItemList:
     343        for i,txt in enumerate(logitem.treeItemList):
     344            if i == 0 and type(txt) is tuple:
     345                txt = self.ConvertRelativeHistNum(*txt)
     346            elif i == 1 and type(txt) is int and logitem.treeItemList[0] == "Phases":
     347                txt = self.ConvertRelativePhaseNum(txt)
    254348            item = G2gd.GetPatternTreeItemId(self.G2frame,parent,txt)
    255349            if not item:
     
    280374        'Perform a Variable Change action, when read from the log'
    281375        parentId = self.root
    282         for treeitem in logitem.treeRefs:
     376        for i,treeitem in enumerate(logitem.treeRefs):
     377            if i == 0 and type(treeitem) is tuple:
     378                treeitem = self.ConvertRelativeHistNum(*treeitem)
    283379            item, cookie = self.GetFirstChild(parentId)
    284380            while item:
     
    313409    if not LogInfo['Logging']: return
    314410    if hasattr(result,'treeRefs'):
     411        treevars = result.treeRefs[:]
     412        treevars[0] = LogInfo['Tree'].GetRelativeHistNum(treevars[0])
     413        if treevars[0] == "Phases" and len(treevars) > 1:
     414            treevars[1] = LogInfo['Tree'].GetRelativePhaseNum(treevars[1])
    315415        lastLog = G2logList[-1]
    316416        fullrefs = result.indexRefs+[key]
    317417        if type(lastLog) is VarLogEntry:
    318             if lastLog.treeRefs == result.treeRefs and lastLog.indexRefs == fullrefs:
     418            if lastLog.treeRefs == treevars and lastLog.indexRefs == fullrefs:
    319419                lastLog.value = result[key]
    320420                if debug: print 'update last log to ',result[key]
    321421                return
    322         G2logList.append(VarLogEntry(result.treeRefs,fullrefs,result[key]))
     422        G2logList.append(VarLogEntry(treevars,fullrefs,result[key]))
    323423    else:
    324424        print key,'Error: var change has no provenance info'
     
    424524def LogOn():
    425525    'Turn On logging of actions'
     526    if debug: print 'LogOn'
    426527    LogInfo['Logging'] = True
    427528
    428529def LogOff():
    429530    'Turn Off logging of actions'
     531    if debug: print 'LogOff'
    430532    LogInfo['Logging'] = False
    431 
     533   
    432534def ShowLogStatus():
    433535    'Return the logging status'
     
    436538def ReplayLog(event):
    437539    'replay the logged actions (needs to be a wx.widget)'
    438     LogOff() # TODO: need to update menu item as well
     540   
     541    LogInfo['G2frame'].OnMacroRecordStatus(None,False) # turn off recording
    439542    LogInfo['Tree'].ClearDataRepaint()
    440543    print 70*'='
    441544    print 'Performing logged actions:'
    442545    for item in G2logList:
    443         if item:
    444             print item
     546        if item: # skip over 1st item in list (None)
     547            print 'replaying',item
    445548            LogInfo['Tree'].ReplayLogItem(item)
    446549            wx.Yield()
     
    451554   
    452555   
     556LogOn() # for debug
Note: See TracChangeset for help on using the changeset viewer.