-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathdrawtree.py
57 lines (40 loc) · 1.45 KB
/
drawtree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Image, ImageDraw
def getwidth(tree):
if tree.tb == None and tree.fb == None: return 1 # leaf
return getwidth(tree.tb) + getwidth(tree.fb)
# XXX: why does `tree` have a value in this function?
#def getdepth(clust):
#print tree
#return 0
def getdepth(tree):
if tree.tb == None and tree.fb == None: return 0 # leaf
return max(getdepth(tree.tb), getdepth(tree.fb)) + 1
def drawtree(tree, png='tree.png'):
w = getwidth(tree)*100
h = getdepth(tree)*100 + 120
img = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(img)
drawnode(draw, tree, w/2, 20)
img.save(png, 'PNG')
def drawnode(draw, tree, x, y):
if tree.results == None: # internal node
# Get width of each branch
w1 = getwidth(tree.fb) * 100
w2 = getwidth(tree.tb) * 100
left = x - (w1 + w2)/2
right = x + (w1 + w2)/2
# Draw condition
draw.text((x - 20, y - 10), '%s:%s' % (tree.col, tree.value), (0, 0, 0))
# Draw links to branches
draw.line((x, y, left + w1/2, y+100), fill=(255, 0, 0))
draw.line((x, y, right - w2/2, y+100), fill=(255, 0, 0))
drawnode(draw, tree.fb, left + w1/2, y + 100)
drawnode(draw, tree.tb, right - w2/2, y + 100)
else:
txt = ' \n'.join(['%s:%d' % v for v in tree.results.items()])
draw.text((x - 20, y), txt, (0, 0, 0))
if __name__ == '__main__':
import treepredict
tree = treepredict.buildtree(treepredict.testdata())
drawtree(tree, 'tree.png')
print 'Wrote tree.png'