Skip to content

Commit 32d73f5

Browse files
authored
Merge pull request #105 from sisl/moss/GraphPlot
TeX-free rendering, fallback to GraphPlot if LuaLaTeX fails #53
2 parents 94f8304 + fa087cf commit 32d73f5

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Project.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
99
Discretizers = "6e83dbb3-75ca-525b-8ae2-3751f0dd50b4"
1010
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
11+
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
1112
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
1213
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
1314
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
@@ -16,27 +17,28 @@ Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1617
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1718
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1819
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
20+
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1921
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2022
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2123
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2224
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
23-
TikzGraphs = "b4f28e30-c73f-5eaf-a395-8a9db949a742"
2425

2526
[compat]
2627
DataFrames = "0.21"
2728
DataStructures = "0.11,0.12,0.13,0.14,0.15,0.16,0.17"
2829
Discretizers = "3.0"
2930
Distributions = "0.17,0.18,0.19,0.20,0.21,0.22,0.23"
31+
GraphPlot = "0.4,0.4.1"
3032
IterTools = "1.3"
3133
LightGraphs = "1.0"
3234
LightXML = "0.8,0.9"
3335
NBInclude = "2.0"
3436
Parameters = "0.10,0.11,0.12"
3537
RDatasets = "0.5,0.6"
3638
Reexport = "0.2"
37-
SpecialFunctions = "0.8,0.9,0.10"
39+
Requires = "1.0.1"
40+
SpecialFunctions = "0.8,0.10"
3841
StatsBase = "0.25,0.26,0.27,0.28,0.29,0.30,0.31,0.32,0.33"
39-
TikzGraphs = "1.1"
4042
julia = "1"
4143

4244
[extras]
@@ -46,6 +48,7 @@ RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
4648
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
4749
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
4850
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
51+
TikzGraphs = "b4f28e30-c73f-5eaf-a395-8a9db949a742"
4952

5053
[targets]
51-
test = ["Statistics", "NBInclude", "LinearAlgebra", "Test", "RDatasets", "Random"]
54+
test = ["Statistics", "NBInclude", "LinearAlgebra", "Test", "RDatasets", "Random", "TikzGraphs"]

doc/BayesNets.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"cell_type": "markdown",
3131
"metadata": {},
3232
"source": [
33-
"Visualization of network structure is provided by the TikzGraphs package. Installation requirements (e.g., PGF/Tikz and pdf2svg) are provided [here](http://nbviewer.ipython.org/github/sisl/TikzGraphs.jl/blob/master/doc/TikzGraphs.ipynb)."
33+
"Default visualization of the network structure is provided by the GraphPlot package. However, we recommend using tex-formatted graphs provided by the TikzGraphs package. Installation requirements for TikzGraphs (e.g., PGF/Tikz and pdf2svg) are provided [here](http://nbviewer.ipython.org/github/sisl/TikzGraphs.jl/blob/master/doc/TikzGraphs.ipynb). Simply run `using TikzGraphs` in your Julia session to automatically switch to tex-formatted graphs (thanks to the Requires.jl package)."
3434
]
3535
},
3636
{
@@ -48,7 +48,8 @@
4848
"source": [
4949
"using Random\n",
5050
"Random.seed!(0) # seed the random number generator to 0, for a reproducible demonstration\n",
51-
"using BayesNets"
51+
"using BayesNets\n",
52+
"using TikzGraphs # required to plot tex-formatted graphs (recommended), otherwise GraphPlot.jl is used"
5253
]
5354
},
5455
{

src/BayesNets.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using IterTools
99
using Dates
1010
using SpecialFunctions
1111
using SparseArrays
12+
using Requires
1213

1314
include(joinpath("CPDs", "cpds.jl"))
1415
@reexport using BayesNets.CPDs
@@ -18,7 +19,7 @@ import Base: *, /, +, -
1819
import DataStructures: PriorityQueue, peek
1920
import BayesNets.CPDs.ProbabilisticGraphicalModels: markov_blanket, is_independent, infer
2021
import StatsBase: sample, Weights
21-
import TikzGraphs: plot
22+
import GraphPlot: gplot
2223
import LightGraphs: DiGraph, add_edge!, rem_edge!,
2324
add_vertex!, rem_vertex!, has_edge,
2425
edges, topological_sort_by_dfs, inneighbors,

src/io.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
#
22
# Bayes Net
33
#
4-
Base.showable(::MIME"image/svg+xml", bn::BayesNet) = success(`lualatex -v`)
4+
lualatex_available() = try success(`lualatex -v`) catch; false end
5+
Base.showable(::MIME"image/svg+xml", bn::BayesNet) = true
6+
7+
8+
plot(dag::DAG, nodelabel) = gplot(dag, nodelabel=nodelabel) # GraphPlot (default)
9+
10+
# called at runtime (replaces plot with TikzGraphs, if loaded)
11+
function __init__()
12+
@require TikzGraphs="b4f28e30-c73f-5eaf-a395-8a9db949a742" begin
13+
if lualatex_available()
14+
plot(dag::DAG, nodelabel) = TikzGraphs.plot(dag, nodelabel)
15+
end
16+
end
17+
end
18+
519

620
function plot(bn::BayesNet)
721
if !isempty(names(bn))
8-
plot(bn.dag, AbstractString[string(s) for s in names(bn)]) # NOTE: sometimes the same var shows up twice
22+
dag = bn.dag
23+
nodelabel = AbstractString[string(s) for s in names(bn)] # NOTE: sometimes the same var shows up twice
924
else
10-
plot(DiGraph(1), ["Empty Graph"])
25+
dag = DiGraph(1)
26+
nodelabel = ["Empty Graph"]
1127
end
28+
29+
plot(dag, nodelabel)
1230
end
1331

32+
1433
function Base.show(f::IO, a::MIME"image/svg+xml", bn::BayesNet)
1534
show(f, a, plot(bn))
1635
end

0 commit comments

Comments
 (0)