def cdbnprint(G, mtype="obs", bend=5, curve=5, R=1): """ Prints out a compressed dbn repesentation of the graph in TikZ/Latex format """ output = StringIO.StringIO() BE = set() n = len(G) nodes = G.keys() idx = np.argsort([int(v) for v in nodes]) nodes = [nodes[i] for i in idx] g = dict2graph(ecj.cloneBfree(G)) paintSCC(g, colors) for i in range(0, n): node = g.vs[i]['label'] rc = g.vs[i]["color"] print >>output, "{ \\definecolor{mycolor}{RGB}{"\ + str(rc[0]) + "," + str(rc[1]) + "," + str(rc[2]) + "}" mcolor = "fill = {rgb: red," + str(rc[0]) + "; green," + str(rc[1]) +\ "; blue," + str(rc[2]) + "}" print >>output, "\\node[" + mtype + ", fill=mycolor] (" + node + ") at (" +\ str(-i * 360 / n + 180) + ":" + str(R) + ") {" + node + "};}" # print >>output,"\\foreach \\name/\\angle in {"+",".join( # [nodes[i]+"/"+str(-i*360/n+180) for i in range(0,n)])+"}" # print >>output,"\\node["+mtype+"] (\\name) at (\\angle:"\ # +str(R)+") {\\name};" for i in range(0, n): v = nodes[i] ll = [v + '/' + u for u in G[v]] for l in ll: a, b = l.split('/') if G[a][b].intersection([(edge_type['bidirected'], 0)]): if not(BE.intersection([(a, b)]) or BE.intersection([(b, a)])): ang_a = -nodes.index(a) * 360 / n + 180 ang_b = -nodes.index(b) * 360 / n + 180 print >>output, ' \\draw[pilip, on layer=back] (' + a + ') -- (' + b + ');' if G[a][b].intersection([(edge_type['directed'], 1)]): ang_a = -nodes.index(a) * 360 / n + 180 ang_b = -nodes.index(b) * 360 / n + 180 if a == b: print >>output, "\\path[overlay,draw,pil] (" + a + ")" +\ " .. controls +(" + "%.5f" % (bend + ang_a) +\ ":" + fstr % (2 * curve) + "mm) and +(" +\ "%.5f" % (ang_a - bend) +\ ":" + "%.5f" % (2 * curve) + "mm) .. (" + b + ");" else: print >>output, "\\path[overlay,draw,pil] (" + a + ")" +\ " .. controls +(" + "%.5f" % (bend + getangle(ang_a, ang_b)) +\ ":" + fstr % (curve) + "mm) and +(" +\ fstr % (getangle(ang_b, ang_a) - bend) +\ ":" + fstr % (curve) + "mm) .. (" + b + ");" return output
def gprint(G, mtype="obs", bend=5, curve=5, R=1, layout=None, scale=5): """ Prints out an automatically layout compressed dbn repesentation of the graph in TikZ/Latex format """ output = StringIO.StringIO() BE = set() n = len(G) if not layout: g = dict2graph(ecj.cloneBfree(G)) layout = g.layout_fruchterman_reingold(maxiter=50000, coolexp=1.1) # layout = g.layout_graphopt(niter=50000, node_charge=0.08) layout.center([0, 0]) layout.scale(float(1 / scipy.absolute(layout.coords).max())) layout.scale(R) cc = scipy.round_(array(layout.coords), decimals=4) else: g = dict2graph(ecj.cloneBfree(G)) cc = array(layout.coords) paintSCC(g, colors) for i in range(0, n): node = g.vs[i]['label'] rc = g.vs[i]["color"] print >>output, "{ \\definecolor{mycolor}{RGB}{"\ + str(rc[0]) + "," + str(rc[1]) + "," + str(rc[2]) + "}" mcolor = "fill = {rgb: red," + str(rc[0]) + "; green," + str(rc[1]) +\ "; blue," + str(rc[2]) + "}" print >>output, "\\node[" + mtype + ", fill=mycolor] (" + node + ") at (" +\ str(cc[i][0]) + "," + str(cc[i][1]) + ") {" + node + "};}" for i in range(0, n): v = g.vs[i]['label'] ll = [v + '/' + u for u in G[v]] for l in ll: a, b = l.split('/') if G[a][b].intersection([(edge_type['bidirected'], 0)]): if not(BE.intersection([(a, b)]) or BE.intersection([(b, a)])): print >>output, ' \\draw[pilip, on layer=back] (' +\ a + ') -- (' + b + ');' if G[a][b].intersection([(edge_type['directed'], 1)]): if a == b: dff = cc[g.vs['label'].index(a)] - scipy.mean(cc, 0) ang = scipy.arctan2(dff[1], dff[0]) ang_a = scipy.rad2deg(ang) print >>output, "\\path[overlay,draw,pil] (" + a + ")" +\ " .. controls +(" + "%.5f" % (bend + ang_a) +\ ":" + fstr % (2 * curve) + "mm) and +(" +\ "%.5f" % (ang_a - bend) +\ ":" + "%.5f" % (2 * curve) + "mm) .. (" + b + ");" else: dff = cc[g.vs['label'].index(b)] \ - cc[g.vs['label'].index(a)] ang = scipy.arctan2(dff[1], dff[0]) ang_a = scipy.rad2deg(ang) ang_b = ang_a + 180 print >>output, "\\path[overlay,draw,pil] (" + a + ")" +\ " .. controls +(" +\ "%.5f" % (bend + ang_a) +\ ":" + fstr % (curve) + "mm) and +(" +\ fstr % (ang_b - bend) +\ ":" + fstr % (curve) + "mm) .. (" + b + ");" return output