-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplotting.html
211 lines (163 loc) · 12.8 KB
/
plotting.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Plotting Models — pgmpy 0.1.23 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/alabaster.css?v=7b53859b" />
<script src="_static/documentation_options.js?v=20522496"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Example Notebooks" href="examples.html" />
<link rel="prev" title="XMLBeliefNetwork" href="readwrite/xmlbelief.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="plotting-models">
<h1>Plotting Models<a class="headerlink" href="#plotting-models" title="Link to this heading">¶</a></h1>
<p>pgmpy offers a few different ways to plot the model structure.</p>
<ol class="arabic simple">
<li><p>Using <cite>pygraphviz</cite> (<a class="reference external" href="https://pygraphviz.github.io/">https://pygraphviz.github.io/</a>)</p></li>
<li><p>Using <cite>networkx.drawing</cite> module (<a class="reference external" href="https://networkx.org/documentation/stable/reference/drawing.html">https://networkx.org/documentation/stable/reference/drawing.html</a>)</p></li>
<li><p>Using <cite>daft</cite> (<a class="reference external" href="https://docs.daft-pgm.org/">https://docs.daft-pgm.org/</a>)</p></li>
</ol>
<section id="using-pygraphviz">
<h2>1. Using <cite>pygraphviz</cite><a class="headerlink" href="#using-pygraphviz" title="Link to this heading">¶</a></h2>
<p><cite>pygraphviz</cite> is a Python wrapper to Graphviz that has a lot for functionality
for graph visualization. pgmpy provides a method to create a pygraphviz object from
Bayesian Networks and DAGs that can then be plotted using graphviz.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Get an example model</span>
<span class="kn">from</span> <span class="nn">pgmpy.utils</span> <span class="kn">import</span> <span class="n">get_example_model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">get_example_model</span><span class="p">(</span><span class="s2">"sachs"</span><span class="p">)</span>
<span class="c1"># Convert model into pygraphviz object</span>
<span class="n">model_graphviz</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">to_graphviz</span><span class="p">()</span>
<span class="c1"># Plot the model.</span>
<span class="n">model_graphviz</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="s2">"sachs.png"</span><span class="p">,</span> <span class="n">prog</span><span class="o">=</span><span class="s2">"dot"</span><span class="p">)</span>
<span class="c1"># Other file formats can also be specified.</span>
<span class="n">model_graphviz</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="s2">"sachs.pdf"</span><span class="p">,</span> <span class="n">prog</span><span class="o">=</span><span class="s2">"dot"</span><span class="p">)</span>
<span class="n">model_graphviz</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="s2">"sachs.svg"</span><span class="p">,</span> <span class="n">prog</span><span class="o">=</span><span class="s2">"dot"</span><span class="p">)</span>
</pre></div>
</div>
<p>The output <cite>sachs.png</cite> is shown below. Users can also tryout other layout methods supported by pygraphviz such as: <cite>neato</cite>, <cite>dot</cite>, <cite>twopi</cite>, <cite>circo</cite>, <cite>fdp</cite>, <cite>nop</cite>.</p>
<a class="reference internal image-reference" href="_images/sachs.png"><img alt="_images/sachs.png" src="_images/sachs.png" style="width: 434.25px; height: 404.25px;" />
</a>
</section>
<section id="using-daft">
<h2>2. Using <cite>daft</cite><a class="headerlink" href="#using-daft" title="Link to this heading">¶</a></h2>
<p>Daft is a python package that uses matplotlib to render high quality plots suitable for publications.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Get an example model</span>
<span class="kn">from</span> <span class="nn">pgmpy.utils</span> <span class="kn">import</span> <span class="n">get_example_model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">get_example_model</span><span class="p">(</span><span class="s2">"sachs"</span><span class="p">)</span>
<span class="c1"># Get a daft object.</span>
<span class="n">model_daft</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">to_daft</span><span class="p">()</span>
<span class="c1"># To open the plot</span>
<span class="n">model_daft</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="c1"># Save the plot</span>
<span class="n">model_daft</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="s1">'sachs.png'</span><span class="p">)</span>
<span class="c1"># Daft provides plenty of options for customization. Please refer DAG.to_daft documentation and daft's documentation.</span>
<span class="n">model_daft_custom</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">to_daft</span><span class="p">(</span><span class="n">node_pos</span><span class="o">=</span><span class="s1">'shell'</span><span class="p">,</span>
<span class="n">pgm_params</span><span class="o">=</span><span class="p">{</span><span class="s1">'observed_style'</span><span class="p">:</span> <span class="s1">'shade'</span><span class="p">,</span> <span class="s1">'grid_unit'</span><span class="p">:</span> <span class="mi">3</span><span class="p">},</span>
<span class="n">edge_params</span><span class="o">=</span><span class="p">{(</span><span class="s1">'PKA'</span><span class="p">,</span> <span class="s1">'P38'</span><span class="p">):</span> <span class="p">{</span><span class="s1">'label'</span><span class="p">:</span> <span class="mi">2</span><span class="p">}},</span>
<span class="n">node_params</span><span class="o">=</span><span class="p">{</span><span class="s1">'Mek'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'shape'</span><span class="p">:</span> <span class="s1">'rectangle'</span><span class="p">}})</span>
</pre></div>
</div>
<p>The output of the two plots above.</p>
<img alt="_images/sachs_daft_plain.png" src="_images/sachs_daft_plain.png" />
<img alt="_images/sachs_daft_shell.png" src="_images/sachs_daft_shell.png" />
</section>
<section id="using-networkx-drawing">
<h2>3. Using <cite>networkx.drawing</cite><a class="headerlink" href="#using-networkx-drawing" title="Link to this heading">¶</a></h2>
<p>Lastly, as both <cite>pgmpy.models.BayesianNetwork</cite> and <cite>pgmpy.base.DAG</cite> inherit <cite>networkx.DiGraph</cite>, all of networkx’s drawing functionality can be directly used on both DAGs and Bayesian Networks.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="c1"># Get an example model</span>
<span class="kn">from</span> <span class="nn">pgmpy.utils</span> <span class="kn">import</span> <span class="n">get_example_model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">get_example_model</span><span class="p">(</span><span class="s2">"sachs"</span><span class="p">)</span>
<span class="c1"># Plot the model</span>
<span class="n">nx</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo">
<a href="index.html">
<img class="logo" src="_static/logo.png" alt="Logo" />
</a>
</p>
<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="started/base.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="base/base.html">Base Model Structures</a></li>
<li class="toctree-l1"><a class="reference internal" href="models/base.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="factors/base.html">Parameterization</a></li>
<li class="toctree-l1"><a class="reference internal" href="exact_infer/base.html">Exact Inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="exact_infer/model_testing.html">Model Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="approx_infer/base.html">Approximate Inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="param_estimator/base.html">Parameter Estimation</a></li>
<li class="toctree-l1"><a class="reference internal" href="structure_estimator/base.html">Structure Learning</a></li>
<li class="toctree-l1"><a class="reference internal" href="metrics/metrics.html">Metrics for testing models</a></li>
<li class="toctree-l1"><a class="reference internal" href="readwrite/base.html">Reading/Writing to File</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Plotting Models</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#using-pygraphviz">1. Using <cite>pygraphviz</cite></a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-daft">2. Using <cite>daft</cite></a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-networkx-drawing">3. Using <cite>networkx.drawing</cite></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="examples.html">Example Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorial Notebooks</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="readwrite/xmlbelief.html" title="previous chapter">XMLBeliefNetwork</a></li>
<li>Next: <a href="examples.html" title="next chapter">Example Notebooks</a></li>
</ul></li>
</ul>
</div>
<script async src="https://media.ethicalads.io/media/client/ethicalads.min.js"></script>
<div data-ea-publisher="pgmpyorg" data-ea-type="image" data-ea-style="horizontal"></div><script async src="https://www.googletagmanager.com/gtag/js?id=G-HCFR07M31W"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-HCFR07M31W');
</script>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2023, Ankur Ankan.
|
Powered by <a href="https://www.sphinx-doc.org/">Sphinx 7.3.7</a>
& <a href="https://alabaster.readthedocs.io">Alabaster 0.7.16</a>
|
<a href="_sources/plotting.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>