Date
import graphviz as gv
import tempfile, os
from IPython.display import Image

def view_dot(dot_text):
    with tempfile.TemporaryDirectory() as dname:
        filename = os.path.join(dname, "test")
        src = gv.Source(dot_text,format='png').render(filename)
        return Image(open(src,'rb').read())

例:

view_dot('''
digraph {
layout=neato;

X [label="X(exposure)"; pos="1,0!"];
Y [label="Y(outcome)"; pos="4,0!"];
U [label="C(confounding)"; pos="2.5,1.5!"];
X -> Y;
U ->X;
U ->Y;
}
''')
view_dot('''
digraph {
layout=neato;

X [label="X(exposure)"; pos="1,0!"];
Y [label="Y(outcome)"; pos="4,0!"];
U [label="C(confounding)"; pos="2.5,1.5!"];
#C [label="C(measured confounding)"; pos="1,2!"];
Z [lable="Z(instrumental variable)"; pos="0,1!"]
Z -> X;
X -> Y;
U ->X;
U ->Y;
}
''')

Comments

comments powered by Disqus