Skip to content

Commit eb3dfa3

Browse files
authored
Fix documentation plotting examples and doctest failures (Issue #157) (#160)
* Refactor usage examples to use Compose for plotting Updated usage examples to use Compose for plotting instead of TikzPictures. Adjusted SVG drawing syntax for consistency across examples. * Fix SVG drawing dimensions in usage documentation * Add Compose package to Project.toml * Update SVG drawing paths in usage documentation * Add new dependencies to Project.toml * Update CI workflow to skip dependencies temporarily Temporarily skip dependency builds and add debug flag. * Refactor CI workflow by removing debug env Removed temporary debug flag and updated job matrix. * Update usage.md
1 parent 153dd4a commit eb3dfa3

2 files changed

Lines changed: 45 additions & 44 deletions

File tree

docs/Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
[deps]
22
BayesNets = "ba4760a4-c768-5bed-964b-cf806dc591cb"
3+
Compose = "a81c6b42-2e10-5240-aca2-a61377ecd94b"
4+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
35
Discretizers = "6e83dbb3-75ca-525b-8ae2-3751f0dd50b4"
6+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
47
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
8+
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
59
RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
610
TikzGraphs = "b4f28e30-c73f-5eaf-a395-8a9db949a742"
711
TikzPictures = "37f6aa50-8035-52d0-81c2-5a1d08754b2d"
812

913
[compat]
10-
Documenter = "1"
14+
Documenter = "1"

docs/src/usage.md

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
```@meta
2+
CurrentModule = BayesNets
3+
```
14
# Usage
25

36
```@setup bayesnet
4-
using BayesNets, TikzGraphs, TikzPictures
5-
```
6-
7-
```julia
8-
using Random
9-
Random.seed!(0) # seed the random number generator to 0, for a reproducible demonstration
107
using BayesNets
11-
using TikzGraphs # required to plot tex-formatted graphs (recommended), otherwise GraphPlot.jl is used
12-
using TikzPictures
8+
using DataFrames
9+
using Distributions
10+
using Random
11+
using Graphs
12+
Random.seed!(0)
1313
```
1414

1515
## Representation
@@ -25,11 +25,11 @@ a = \mathcal{N}(0,1) \qquad b = \mathcal{N}(2a +3,1)
2525
bn = BayesNet()
2626
push!(bn, StaticCPD(:a, Normal(1.0)))
2727
push!(bn, LinearGaussianCPD(:b, [:a], [2.0], 3.0, 1.0))
28-
plot = BayesNets.plot(bn)
29-
TikzPictures.save(SVG("plot1"), plot)
28+
p = BayesNets.plot(bn)
29+
p
3030
```
3131

32-
![](plot1.svg)
32+
3333

3434
## Conditional Probability Distributions
3535

@@ -56,17 +56,17 @@ cpdA = fit(StaticCPD{Normal}, data, :a)
5656
cpdB = fit(LinearGaussianCPD, data, :b, [:a])
5757
5858
bn2 = BayesNet([cpdA, cpdB])
59-
plot = BayesNets.plot(bn2) # hide
60-
TikzPictures.save(SVG("plot2"), plot) # hide
59+
p = BayesNets.plot(bn2)
60+
p
6161
```
6262

63-
![](plot2.svg)
63+
6464

6565
Each `CPD` implements four functions:
6666

6767
* `name(cpd)` - obtain the name of the variable target variable
6868
* `parents(cpd)` - obtain the list of parents
69-
* `nparams(cpd` - obtain the number of free parameters in the CPD
69+
* `nparams(cpd)` - obtain the number of free parameters in the CPD
7070
* `cpd(assignment)` - allows calling `cpd()` to obtain the conditional distribution
7171
* `Distributions.fit(Type{CPD}, data, target, parents)`
7272

@@ -88,22 +88,20 @@ The NamedCategorical distribution allows for String or Symbol return values. The
8888
bn2 = BayesNet()
8989
push!(bn2, StaticCPD(:sighted, NamedCategorical([:bird, :plane, :superman], [0.40, 0.55, 0.05])))
9090
push!(bn2, FunctionalCPD{Bernoulli}(:happy, [:sighted], a->Bernoulli(a == :superman ? 0.95 : 0.2)))
91-
plot = BayesNets.plot(bn2) # hide
92-
TikzPictures.save(SVG("plot3"), plot) # hide
91+
p = BayesNets.plot(bn2)
92+
p
9393
```
9494

95-
![](plot3.svg)
95+
9696

9797
Variables can be removed by name using `delete!`. A warning will be issued when removing a CPD with children.
9898

9999
```@example bayesnet
100100
delete!(bn2, :happy)
101-
plot = BayesNets.plot(bn2) # hide
102-
TikzPictures.save(SVG("plot4"), plot) # hide
101+
p = BayesNets.plot(bn2)
102+
p
103103
```
104104

105-
![](plot4.svg)
106-
107105
## Likelihood
108106

109107
A Bayesian Network represents a joint probability distribution, $P(x_1, x_2, \ldots, x_n)$.
@@ -153,11 +151,10 @@ bn = BayesNet()
153151
push!(bn, StaticCPD(:a, Categorical([0.3,0.7])))
154152
push!(bn, StaticCPD(:b, Categorical([0.6,0.4])))
155153
push!(bn, CategoricalCPD{Bernoulli}(:c, [:a, :b], [2,2], [Bernoulli(0.1), Bernoulli(0.2), Bernoulli(1.0), Bernoulli(0.4)]))
156-
plot = BayesNets.plot(bn) # hide
157-
TikzPictures.save(SVG("plot5"), plot) # hide
154+
p = BayesNets.plot(bn)
155+
p
158156
```
159157

160-
![](plot5.svg)
161158

162159
```julia
163160
rand(bn, RejectionSampler(:c=>1), 5)
@@ -202,11 +199,11 @@ b=[1,1,1,2,2,2,2,1,1,2,1,1],
202199
a=[1,1,1,2,1,1,2,1,1,2,1,1])
203200
204201
bn5 = fit(DiscreteBayesNet, data, (:a=>:b, :a=>:c, :b=>:c))
205-
plot = BayesNets.plot(bn5) # hide
206-
TikzPictures.save(SVG("plot6"), plot) # hide
202+
p = BayesNets.plot(bn5)
203+
p
207204
```
208205

209-
![](plot6.svg)
206+
210207

211208
Fitting a ```DiscreteCPD```, which is a ```CategoricalCPD{Categorical}```, can be done with a specified number of categories. This prevents cases where your test data does not provide an example for every category.
212209

@@ -230,11 +227,11 @@ push!(bn, DiscreteCPD(:c, [:a, :b], [2,2],
230227
Categorical([0.4,0.6]),
231228
]))
232229
233-
plot = BayesNets.plot(bn) # hide
234-
TikzPictures.save(SVG("plot7"), plot) # hide
230+
p = BayesNets.plot(bn)
231+
p
235232
```
236233

237-
![](plot7.svg)
234+
238235

239236
```@example bayesnet
240237
ϕ = infer(bn, :c, evidence=Assignment(:b=>1))
@@ -288,11 +285,11 @@ parameters = K2GraphSearch([:Species, :SepalLength, :SepalWidth, :PetalLength, :
288285
max_n_parents=2)
289286
bn = fit(BayesNet, data, parameters)
290287
291-
plot = BayesNets.plot(bn) # hide
292-
TikzPictures.save(SVG("plot8"), plot) # hide
288+
p = BayesNets.plot(bn)
289+
p
293290
```
294291

295-
![](plot8.svg)
292+
296293

297294
CPD types can also be specified per-node. Note that complete CPD definitions are required - simply using `StaticCPD` is insufficient as you need the target distribution type as well, as in `StaticCPD{Categorical}`.
298295

@@ -317,11 +314,11 @@ data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3],
317314
parameters = GreedyHillClimbing(ScoreComponentCache(data), max_n_parents=3, prior=UniformPrior())
318315
bn = fit(DiscreteBayesNet, data, parameters)
319316
320-
plot = BayesNets.plot(bn) # hide
321-
TikzPictures.save(SVG("plot9"), plot) # hide
317+
p = BayesNets.plot(bn)
318+
p
322319
```
323320

324-
![](plot9.svg)
321+
325322

326323
We can specify the number of categories for each variable in case it cannot be correctly inferred:
327324

@@ -345,10 +342,10 @@ count(bn, :a, data)
345342
statistics(bn.dag, data)
346343
```
347344
```@example bayesnet
348-
table(bn, :b)
345+
BayesNets.table(bn, :b)
349346
```
350347
```@example bayesnet
351-
table(bn, :c, :a=>1)
348+
BayesNets.table(bn, :c, :a=>1)
352349
```
353350

354351
## Reading from XDSL
@@ -358,11 +355,11 @@ Discrete Bayesian Networks can be read from the .XDSL file format.
358355
```@example bayesnet
359356
bn = readxdsl(joinpath(dirname(pathof(BayesNets)), "..", "test", "sample_bn.xdsl"))
360357
361-
plot = BayesNets.plot(bn) # hide
362-
TikzPictures.save(SVG("plot10"), plot) # hide
358+
p = BayesNets.plot(bn)
359+
p
363360
```
364361

365-
![](plot10.svg)
362+
366363

367364
## Bayesian Score for a Network Structure
368365

@@ -374,5 +371,5 @@ data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3],
374371
a=[1,1,1,2,1,1,2,1,1,2,1,1])
375372
g = DAG(3)
376373
add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,1,3)
377-
bayesian_score(g, [:a,:b,:c], data)
374+
BayesNets.bayesian_score(g, [:a, :b, :c], data)
378375
```

0 commit comments

Comments
 (0)