Mercurial > repos > shellac > sam_consensus_v3
comparison env/share/doc/networkx-2.5/examples/algorithms/plot_decomposition.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 ============= | |
3 Decomposition | |
4 ============= | |
5 | |
6 Example of creating a junction tree from a directed graph. | |
7 """ | |
8 | |
9 import networkx as nx | |
10 from networkx.algorithms import moral | |
11 from networkx.algorithms.tree.decomposition import junction_tree | |
12 from networkx.drawing.nx_agraph import graphviz_layout as layout | |
13 import matplotlib.pyplot as plt | |
14 | |
15 B = nx.DiGraph() | |
16 B.add_nodes_from(["A", "B", "C", "D", "E", "F"]) | |
17 B.add_edges_from( | |
18 [("A", "B"), ("A", "C"), ("B", "D"), ("B", "F"), ("C", "E"), ("E", "F")] | |
19 ) | |
20 | |
21 options = {"with_labels": True, "node_color": "white", "edgecolors": "blue"} | |
22 | |
23 bayes_pos = layout(B, prog="neato") | |
24 ax1 = plt.subplot(1, 3, 1) | |
25 plt.title("Bayesian Network") | |
26 nx.draw_networkx(B, pos=bayes_pos, **options) | |
27 | |
28 mg = moral.moral_graph(B) | |
29 plt.subplot(1, 3, 2, sharex=ax1, sharey=ax1) | |
30 plt.title("Moralized Graph") | |
31 nx.draw_networkx(mg, pos=bayes_pos, **options) | |
32 | |
33 jt = junction_tree(B) | |
34 plt.subplot(1, 3, 3) | |
35 plt.title("Junction Tree") | |
36 nsize = [2000 * len(n) for n in list(jt.nodes())] | |
37 nx.draw_networkx(jt, pos=layout(jt, prog="neato"), node_size=nsize, **options) | |
38 | |
39 plt.tight_layout() | |
40 plt.show() |