"""
Recursively generated graph
===========================

**Daft** is Python, so you can do anything Python can do.  This graph is
generated by recursive code.

"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

def recurse(pgm, nodename, level, c):
    if level > 4:
        return nodename
    r = c / 2
    r1nodename = "r%02d%04d" % (level, r)
    if 2 * r == c:
        print "adding ", r1nodename
        pgm.add_node(daft.Node(r1nodename, r"reduce", 2**level * (r + 0.5) - .5, 3 - 0.7 * level, aspect=1.9))
    pgm.add_edge(nodename, r1nodename)
    if 2 * r == c:
        return recurse(pgm, r1nodename, level + 1, r)
    return

pgm = daft.PGM([16, 8], origin=[-0.5, -1.5])

pgm.add_node(daft.Node("query", r'\texttt{"kittens?"}', 3, 6., aspect=3., plot_params={"ec":"none"}))
pgm.add_node(daft.Node("input", r'input', 7.5, 6., aspect=3.))
pgm.add_edge("query", "input")

for c in range(16):
    nodename = "map %02d" % c
    pgm.add_node(daft.Node(nodename, r"%s" % nodename, c, 3., aspect=1.9))
    pgm.add_edge("input", nodename)
    level = 1
    recurse(pgm, nodename, level, c)

pgm.add_node(daft.Node("output", r"output", 7.5, -1., aspect=3.))
pgm.add_edge("r040000", "output")
pgm.add_node(daft.Node("answer", r"\texttt{http://dwh.gg/}", 12., -1., aspect=3., plot_params={"ec":"none"}))
pgm.add_edge("output", "answer")

pgm.render()
pgm.figure.savefig("recursive.pdf")
pgm.figure.savefig("recursive.png", dpi=200)

@@ -1282,17 +1282,17 @@
 %22, r
-%22
+'
 %5Ctexttt%7B
 http
@@ -1287,16 +1287,17 @@
 %5Ctexttt%7B
+%22
 http://d
@@ -1306,10 +1306,11 @@
 .gg/
-%7D
 %22
+%7D'
 , 12
@@ -1318,32 +1318,33 @@
 , -1., aspect=3.
+5
 , plot_params=%7B%22

adjust input output