make DAG plot and save to file_path as .png
(genotype, file_path, caption=None)
| 5 | |
| 6 | |
| 7 | def plot(genotype, file_path, caption=None): |
| 8 | """ make DAG plot and save to file_path as .png """ |
| 9 | edge_attr = { |
| 10 | 'fontsize': '20', |
| 11 | 'fontname': 'times' |
| 12 | } |
| 13 | node_attr = { |
| 14 | 'style': 'filled', |
| 15 | 'shape': 'rect', |
| 16 | 'align': 'center', |
| 17 | 'fontsize': '20', |
| 18 | 'height': '0.5', |
| 19 | 'width': '0.5', |
| 20 | 'penwidth': '2', |
| 21 | 'fontname': 'times' |
| 22 | } |
| 23 | g = Digraph( |
| 24 | format='png', |
| 25 | edge_attr=edge_attr, |
| 26 | node_attr=node_attr, |
| 27 | engine='dot') |
| 28 | g.body.extend(['rankdir=LR']) |
| 29 | |
| 30 | # input nodes |
| 31 | g.node("c_{k-2}", fillcolor='darkseagreen2') |
| 32 | g.node("c_{k-1}", fillcolor='darkseagreen2') |
| 33 | |
| 34 | # intermediate nodes |
| 35 | n_nodes = len(genotype) |
| 36 | for i in range(n_nodes): |
| 37 | g.node(str(i), fillcolor='lightblue') |
| 38 | |
| 39 | for i, edges in enumerate(genotype): |
| 40 | for op, j in edges: |
| 41 | if j == 0: |
| 42 | u = "c_{k-2}" |
| 43 | elif j == 1: |
| 44 | u = "c_{k-1}" |
| 45 | else: |
| 46 | u = str(j-2) |
| 47 | |
| 48 | v = str(i) |
| 49 | g.edge(u, v, label=op, fillcolor="gray") |
| 50 | |
| 51 | # output node |
| 52 | g.node("c_{k}", fillcolor='palegoldenrod') |
| 53 | for i in range(n_nodes): |
| 54 | g.edge(str(i), "c_{k}", fillcolor="gray") |
| 55 | |
| 56 | # add image caption |
| 57 | if caption: |
| 58 | g.attr(label=caption, overlap='false', fontsize='20', fontname='times') |
| 59 | |
| 60 | g.render(file_path, view=False) |
| 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
no test coverage detected