def DrawSpiral(canvas,startColor,endColor,startRadius,endRadius,nLoops,degsPerSlice=70,degsPerStep=1, startAngle=0,centerPos=None,dir=1): if centerPos is None: centerPos = (canvas.size[0]/2,canvas.size[1]/2) nSlices = int(math.ceil(360*nLoops/degsPerSlice)) radPerStep = math.pi*degsPerStep/180. stepsPerSlice = degsPerSlice/degsPerStep radiusStep = float(endRadius-startRadius)/(stepsPerSlice*nSlices) colorStep = (array(endColor,float)-array(startColor,float))/nSlices print('INFO:',nSlices,radPerStep,stepsPerSlice,radiusStep,colorStep) angle = math.pi*startAngle/180. radius = startRadius color = array(startColor,float) for i in range(nSlices): pts = [ (centerPos[0],centerPos[1])] for j in range(stepsPerSlice): xPos = centerPos[0] + radius*math.cos(angle) yPos = centerPos[1] + radius*math.sin(angle) pts.append((xPos,yPos)) angle += dir*radPerStep radius += radiusStep xPos = centerPos[0] + radius*math.cos(angle) yPos = centerPos[1] + radius*math.sin(angle) pts.append((xPos,yPos) ) canvas.drawPolygon(pts,edgeColor=pid.transparent, fillColor=pid.Color(color[0],color[1],color[2]), closed=1) angle -= dir*radPerStep color += colorStep
class VisOpts(object): circRad = 10 minCircRad = 4 maxCircRad = 16 circColor = piddle.Color(0.6,0.6,0.9) terminalEmptyColor = piddle.Color(.8,.8,.2) terminalOnColor = piddle.Color(0.8,0.8,0.8) terminalOffColor = piddle.Color(0.2,0.2,0.2) outlineColor = piddle.transparent lineColor = piddle.Color(0,0,0) lineWidth = 2 horizOffset = 10 vertOffset = 50 labelFont = piddle.Font(face='helvetica',size=10) highlightColor = piddle.Color(1.,1.,.4) highlightWidth = 2
def convertColor(color): color = pid.Color(color[0], color[1], color[2]) return color
def convertColor(color): return pid.Color(color[0], color[1], color[2])
def DrawTreeNode(node,loc,canvas,nRes=2,scaleLeaves=False,showPurity=False): """Recursively displays the given tree node and all its children on the canvas """ try: nChildren = node.totNChildren except AttributeError: nChildren = None if nChildren is None: CalcTreeNodeSizes(node) if not scaleLeaves or not node.GetTerminal(): rad = visOpts.circRad else: scaleLoc = getattr(node, "_scaleLoc", 0.5) rad = visOpts.minCircRad + node._scaleLoc*(visOpts.maxCircRad-visOpts.minCircRad) x1 = loc[0] - rad y1 = loc[1] - rad x2 = loc[0] + rad y2 = loc[1] + rad if showPurity and node.GetTerminal(): examples = node.GetExamples() nEx = len(examples) if nEx: tgtVal = int(node.GetLabel()) purity = 0.0 for ex in examples: if int(ex[-1])==tgtVal: purity += 1./len(examples) else: purity = 1.0 deg = purity*math.pi xFact = rad*math.sin(deg) yFact = rad*math.cos(deg) pureX = loc[0]+xFact pureY = loc[1]+yFact children = node.GetChildren() # just move down one level childY = loc[1] + visOpts.vertOffset # this is the left-hand side of the leftmost span childX = loc[0] - ((visOpts.horizOffset+visOpts.circRad)*node.totNChildren)/2 for i in range(len(children)): # center on this child's space child = children[i] halfWidth = ((visOpts.horizOffset+visOpts.circRad)*child.totNChildren)/2 childX = childX + halfWidth childLoc = [childX,childY] canvas.drawLine(loc[0],loc[1],childLoc[0],childLoc[1], visOpts.lineColor,visOpts.lineWidth) DrawTreeNode(child,childLoc,canvas,nRes=nRes,scaleLeaves=scaleLeaves, showPurity=showPurity) # and move over to the leftmost point of the next child childX = childX + halfWidth if node.GetTerminal(): lab = node.GetLabel() cFac = float(lab)/float(nRes-1) if hasattr(node,'GetExamples') and node.GetExamples(): theColor = (1.-cFac)*visOpts.terminalOffColor + cFac*visOpts.terminalOnColor outlColor = visOpts.outlineColor else: theColor = (1.-cFac)*visOpts.terminalOffColor + cFac*visOpts.terminalOnColor outlColor = visOpts.terminalEmptyColor canvas.drawEllipse(x1,y1,x2,y2, outlColor,visOpts.lineWidth, theColor) if showPurity: canvas.drawLine(loc[0],loc[1],pureX,pureY,piddle.Color(1,1,1),2) else: theColor = visOpts.circColor canvas.drawEllipse(x1,y1,x2,y2, visOpts.outlineColor,visOpts.lineWidth, theColor) # this does not need to be done every time canvas.defaultFont=visOpts.labelFont labelStr = str(node.GetLabel()) strLoc = (loc[0] - canvas.stringWidth(labelStr)/2, loc[1]+canvas.fontHeight()/4) canvas.drawString(labelStr,strLoc[0],strLoc[1]) node._bBox = (x1,y1,x2,y2)