import QUB import qubtree import Buttons from Dialogs import * from math import * from random import random, expovariate def IdlToChannel(seg): ndwt = seg['DwellCount'].data[0] idwt = seg['Classes'].data tdwt = seg['Durations'].data amp = seg['amp'].data sd = seg['sd'].data dt = seg.parent['sampling'].data[0] # in ms like tdwt chan = [] for i in range(ndwt): #chan.extend( int(tdwt[i] / dt + 0.5) * [ amp[ idwt[i] ] ] ) chan.extend( int(tdwt[i] / dt + 0.5) * [ idwt[i] ] ) seg['Channel'].data = chan def CompareIdlByPoint(seg1, seg2): IdlToChannel(seg1) IdlToChannel(seg2) ch1 = seg1['Channel'].data ch2 = seg2['Channel'].data n = min( ch1.count, ch2.count ) different = 0 for i in range(n): if ch1[i] != ch2[i]: different = different + 1 seg1.remove( seg1['Channel'] ) seg2.remove( seg2['Channel'] ) return [different, n] typestrs = ["QTR_TYPE_EMPTY", "QTR_TYPE_UNKNOWN", "QTR_TYPE_POINTER", "QTR_TYPE_STRING", "QTR_TYPE_UCHAR", "QTR_TYPE_CHAR", "QTR_TYPE_USHORT", "QTR_TYPE_SHORT", "QTR_TYPE_UINT", "QTR_TYPE_INT", "QTR_TYPE_ULONG", "QTR_TYPE_LONG", "QTR_TYPE_FLOAT", "QTR_TYPE_DOUBLE", "QTR_TYPE_LDOUBLE"] def ReportNodeStat(node, indent="", showTypes=1): """print node but skip the unloaded data""" line = node.name if node.data.count: if showTypes: line = line + " (%s)" % typestrs[node.data.type] if (node.data.loaded[0] == 0) and (node.data.loaded[1] == node.data.rows-1) and ((node.data.type == qubtree.QTR_TYPE_STRING) or (node.data.count <= 10)): line = line + " = " + str(node.data) else: line = line + (" : %d elements / %d x %d" % (node.data.count, node.data.rows, node.data.cols)) QUB.Report( indent + line ) if not node.child.isNull: QUB.Report( indent + "{" ) ch = node.child while not ch.isNull: ReportNodeStat(ch, indent + "\t", showTypes) ch = ch.sibling QUB.Report( indent + "}" ) def MakeTreeOutline(node, exampleStyle=1): # 1: full print; 2: stat; """generate html documentation for an example tree""" rep = QUB.Report rep('' % node.name) rep('
%s' % node.name) rep('
Overview') rep('
') rep( node.lineComment ) rep('
Data') rep('
') if node.data.count: rep('%s:\t%d x %d' % (typestrs[node.data.type], node.data.rows, node.data.cols)) else: rep('none') rep('') rep('
Children') rep('
') rep('') rep('
') if exampleStyle == 1: rep('
Example
') rep('
')
        rep( node )
        rep('
') elif exampleStyle == 2: rep('
Example
') rep('
')
        ReportNodeStat(node, "", 0)
        rep('
') rep('') for name in uniqueNames: MakeTreeOutline( node[name], 0 ) def PathOfNode(node): base = '' if not node.parent.isNull: base = PathOfNode(node.parent) + ':' if node.name == '': return base + '<->' else: return base + node.name def CompareTrees(a, b, basein=''): rep = QUB.Report base = basein + a.name + ':' if a.isNull and not b.isNull: rep( '%s:%s: missing in left-hand tree' % (basein, b.name) ) return elif b.isNull and not a.isNull: rep( '%s:%s: missing in right-hand tree' % (basein, a.name) ) return elif a.isNull and b.isNull: return if a.name != b.name: rep( '%s name (%s)' % (base, b.name) ) ad = a.data bd = b.data if ad.type != bd.type: rep( '%s data type:\t%s / %s' % (base, typestrs[ad.type], typestrs[bd.type]) ) if ad.rows != bd.rows: rep( '%s data rows:\t%d / %d' % (base, ad.rows, bd.rows) ) if ad.cols != bd.cols: rep( '%s data cols:\t%d / %d' % (base, ad.cols, bd.cols) ) try: for i in range(min(ad.rows, bd.rows)): ar = ad.row(i) br = bd.row(i) for j in range(min(ad.cols, bd.cols)): if ar[j] != br[j]: rep( '%s data[%d,%d]:\t%s / %s' % (base, i, j, str(ar[j]), str(br[j])) ) except Exception, e: rep( '%s (data not accessible): %s' % (base, str(e)) ) # ok rough for now -- don't account for extra/missing children CompareTrees(a.sibling, b.sibling, basein) CompareTrees(a.child, b.child, base) # button to test qubtree: # open or create orig # open copy, text # close copy # compare copy, orig # compare copy, text # edit orig # prompt to (save, saveas copy, saveas text) or discard def TestTrees(props): orig = qubtree.Open('qtr-test.qtr') if orig.isNull: orig = qubtree.Node('qtr-test') orig.saveAs('qtr-test.qtr') copy = qubtree.Open('qtr-test-copy.qtr',1) copy.close() text = qubtree.ReadText('qtr-test.qtt') QUB.Report('Comparing original with copy: ----------------------') CompareTrees(orig, copy) QUB.Report('') QUB.Report('Comparing copy with text: ----------------------') CompareTrees(copy, text) QUB.editTree(orig) if 'Save' == QUB.Prompt('Save changes to qtr-test?', ['Save', 'Discard'], [])['button']: orig.save() orig.saveAs('qtr-test-copy.qtr') orig.saveAsText('qtr-test.qtt') QUB.Report('Done ----------------------') Buttons.AddTaskButton( 'TestTrees', TestTrees, 'TrTest', 0, 0, "Test qubtree library", "Test qubtree library", "Customize the message:", [] ) Buttons.AddTaskButton( 'EditResults', lambda x: QUB.editTree(QUB.ResultsView.currResults.node), 'EditRes', 0, 0, '', '', '', [] ) def IdlCond(props): ds = QUB.getChosenData()[0] ds['DTol'].data = props['DTol'] QUB.Report( QUB.IdlCond(ds) ) Buttons.AddTaskButton('IdlCond', IdlCond, 'IdlC', 0, 1, "Test ExpCond Idealization", "Test ExpCond Idealization", "", [Buttons.TextField("DTol", "tolerance:", "0.5", acceptFloatGreaterThan(0.0))], 'Scripts') def DwellGen(Tau, Amp): while True: yield expovariate(1.0/Tau) # Tau * log(random()/(Tau*Amp)) def EvtGen(nextDwell): now = 0.0 while True: dwell = nextDwell() yield now, dwell now += dwell def SampledEvtGen(nextEvent, sampling): while True: evt = nextEvent() first, last = evt[0]/sampling, (evt[0]+evt[1])/sampling crossings = floor(last) - floor(first) if first == floor(first): crossings += 1 if last == floor(last): crossings -= 1 yield evt[0], evt[1], crossings, (evt[1]/sampling) def ChildGen(root, name): child = root.find(name) last = None while True: if child.isNull: if last == None: child = root.append(name) else: child = root.insert(name, last) yield child last, child = child, child.nextSameName() class EvtSamplerSim: def __init__(self, Tau, Amp, sampling): self.nextEvent = SampledEvtGen(EvtGen(DwellGen(Tau,Amp).next).next, sampling).next self.results = {} def run(self, n): for i in xrange(n): evt = self.nextEvent() if not self.results.has_key(evt[2]): self.results[evt[2]] = [] self.results[evt[2]].append(evt) def makeResult(self): res = qubtree.Node('Results') for key in self.results.keys(): name = str(key) segger = ChildGen(res['DataSet'], 'Segment') for evt in self.results[key]: seg = segger.next() seg[name].data = evt[3] seg = res['DataSet'].find('Segment') while not seg.isNull: seg.data = (0,0) seg = seg.nextSameName() res['ResultType'].data = 'SimSam' return res def postResult(self): QUB.ResultsView.addResults(self.makeResult()) def RunEvtSampler(Tau, Amp, sampling, n): e = EvtSamplerSim(Tau, Amp, sampling) e.run(n) e.postResult() def tc(n, cat='Mac'): pr = QUB.Profiles[cat].active.clone() pr['ThreadCount'].data = n QUB.Profiles[cat].setProperties(pr) def prop(cat): QUB.Profiles[cat].setProperties( QUB.editTree( QUB.Profiles[cat].active.clone() ) )