diff --git a/Project.toml b/Project.toml index 9d6d39596..1bbb5294d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "JuliaBUGS" uuid = "ba9fb4c0-828e-4473-b6a1-cd2560fee5bf" -version = "0.6" +version = "0.6.1" [deps] AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" @@ -32,7 +32,6 @@ GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2" GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231" MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" -TikzGraphs = "b4f28e30-c73f-5eaf-a395-8a9db949a742" [extensions] JuliaBUGSAdvancedHMCExt = ["AdvancedHMC", "MCMCChains"] @@ -40,7 +39,6 @@ JuliaBUGSAdvancedMHExt = ["AdvancedMH", "MCMCChains"] JuliaBUGSGraphMakieExt = ["GraphMakie", "GLMakie"] JuliaBUGSGraphPlotExt = ["GraphPlot"] JuliaBUGSMCMCChainsExt = ["MCMCChains"] -JuliaBUGSTikzGraphsExt = ["TikzGraphs"] [compat] AbstractMCMC = "5" @@ -69,7 +67,6 @@ PDMats = "0.10, 0.11" SpecialFunctions = "2" StaticArrays = "1.9" Statistics = "1.9" -TikzGraphs = "1" julia = "1.9" [extras] diff --git a/docs/assets/graphplot.svg b/docs/assets/graphplot.svg index ea888c6ea..33dfafd27 100644 --- a/docs/assets/graphplot.svg +++ b/docs/assets/graphplot.svg @@ -1,151 +1,167 @@ - + - - - + + + - - + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - d - - - + + - g + a - + - l + f - + - f + b - + - b + c - + - a + g - + - c + d - + h - + e - + i + + + l + + + + + + + + + \ No newline at end of file diff --git a/docs/assets/makie.jpg b/docs/assets/makie.jpg deleted file mode 100644 index e7d55282b..000000000 Binary files a/docs/assets/makie.jpg and /dev/null differ diff --git a/docs/assets/makie.png b/docs/assets/makie.png new file mode 100644 index 000000000..dae26b61f Binary files /dev/null and b/docs/assets/makie.png differ diff --git a/docs/assets/tikz.svg b/docs/assets/tikz.svg deleted file mode 100644 index f47ad6530..000000000 --- a/docs/assets/tikz.svg +++ /dev/null @@ -1,144 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/docs/src/graph_plotting.md b/docs/src/graph_plotting.md index bf5aafafe..977597a91 100644 --- a/docs/src/graph_plotting.md +++ b/docs/src/graph_plotting.md @@ -1,10 +1,13 @@ # Plotting graphs -Plotting the graph can be very beneficial for debugging the model. +Plotting the graphical model can be very beneficial for debugging the model. -> **Note** Plate notation is not yet supported. Therefore, it's advisable for users to begin with a more streamlined model that contains fewer nodes, allowing for clearer visualization. +!!! note "Plate notation is not yet supported" + Users are advised to begin with a model that contains fewer nodes, so that the graph is easier to visualize. -In Julia, we've set up standard plotting routines using various graphing libraries. You can visualize graphs with three different libraries by employing a common model, as detailed below: +We have set up standard plotting routines with [`GraphMakie.jl`](https://github.com/MakieOrg/GraphMakie.jl) and [`GraphPlot.jl`](https://github.com/JuliaGraphs/GraphPlot.jl), via package extensions. + +Observed nodes are colored in gray, unobserved nodes are colored in white, and deterministic nodes are colored in light blue. ```julia model_def = @bugs begin @@ -20,41 +23,36 @@ model_def = @bugs begin l ~ dnorm(0, 1) end -inits = Dict( - :a => 1.0, - :b => 2.0, - :c => 3.0, - :d => 4.0, - :e => 5.0, - - # :f => 1.0, - # :g => 2.0, - # :h => 4.0, - - :i => 4.0, - :l => -2.0, +data = ( + e = 5.0, ) -model = compile(model_def, NamedTuple(), inits) -``` +inits = ( + a = 1.0, + b = 2.0, + c = 3.0, + d = 4.0, + i = 4.0, + l = -2.0, +) -## [`TikzGraphs.jl`](https://github.com/JuliaTeX/TikzGraphs.jl). -```julia -using TikzGraphs -TikzGraphs.plot(model) +model = compile(model_def, data, inits) ``` -![TikzGraphs](https://raw.githubusercontent.com/TuringLang/JuliaBUGS.jl/master/docs/assets/tikz.svg) ## [`GraphPlot.jl`](https://github.com/JuliaGraphs/GraphPlot.jl) + ```julia using GraphPlot gplot(model) ``` + ![GraphPlot](https://raw.githubusercontent.com/TuringLang/JuliaBUGS.jl/master/docs/assets/graphplot.svg) ## [`GraphMakie.jl`](https://github.com/MakieOrg/GraphMakie.jl) + ```julia using GLMakie, GraphMakie graphplot(model) ``` -![GraphMakie](https://raw.githubusercontent.com/TuringLang/JuliaBUGS.jl/master/docs/assets/makie.jpg) + +![GraphMakie](https://raw.githubusercontent.com/TuringLang/JuliaBUGS.jl/master/docs/assets/makie.png) diff --git a/ext/JuliaBUGSGraphMakieExt.jl b/ext/JuliaBUGSGraphMakieExt.jl index 87813dce5..7920c19c9 100644 --- a/ext/JuliaBUGSGraphMakieExt.jl +++ b/ext/JuliaBUGSGraphMakieExt.jl @@ -1,6 +1,7 @@ module JuliaBUGSGraphMakieExt using GLMakie +using GLMakie.ColorTypes: RGBA using GraphMakie using JuliaBUGS using JuliaBUGS.MetaGraphsNext @@ -11,19 +12,22 @@ end function GraphMakie.graphplot(g::JuliaBUGS.BUGSGraph, parameters; kwargs...) colors = [] for node in labels(g) - if g[node].node_type == JuliaBUGS.Stochastic - if node in parameters - push!(colors, :green) + if g[node].is_stochastic + if g[node].is_observed + push!(colors, RGBA(0.5, 0.5, 0.5, 1.0)) else - push!(colors, :yellow) + push!(colors, RGBA(1, 1, 1, 1)) end else - push!(colors, :red) + push!(colors, RGBA(0.8, 0.9, 1.0, 1.0)) end end ilabels = get(kwargs, :ilabels, map(x -> String(Symbol(x)), labels(g))) node_color = get(kwargs, :node_color, colors) - return graphplot(g.graph; ilabels=ilabels, node_color=node_color, kwargs...) + + return graphplot( + g.graph; ilabels=ilabels, node_color=node_color, arrow_shift=:end, kwargs... + ) end end diff --git a/ext/JuliaBUGSGraphPlotExt.jl b/ext/JuliaBUGSGraphPlotExt.jl index ae4bbfa60..42d7ca68b 100644 --- a/ext/JuliaBUGSGraphPlotExt.jl +++ b/ext/JuliaBUGSGraphPlotExt.jl @@ -9,21 +9,21 @@ function GraphPlot.gplot(m::JuliaBUGS.BUGSModel; kwargs...) end function GraphPlot.gplot(g::JuliaBUGS.BUGSGraph, parameters; kwargs...) - colors = [] + colors = String[] for node in labels(g) - if g[node].node_type == JuliaBUGS.Stochastic - if node in parameters - push!(colors, :green) + if g[node].is_stochastic + if g[node].is_observed + push!(colors, "gray") else - push!(colors, :yellow) + push!(colors, "white") end else - push!(colors, :red) + push!(colors, "lightblue") end end nodelabel = get(kwargs, :nodelabel, map(x -> String(Symbol(x)), labels(g))) - nodefillc = get(kwargs, :nodefillc, String.(colors)) + nodefillc = get(kwargs, :nodefillc, colors) return gplot(g.graph; nodelabel=nodelabel, nodefillc=nodefillc, kwargs...) end diff --git a/ext/JuliaBUGSTikzGraphsExt.jl b/ext/JuliaBUGSTikzGraphsExt.jl deleted file mode 100644 index 190a5fe5c..000000000 --- a/ext/JuliaBUGSTikzGraphsExt.jl +++ /dev/null @@ -1,35 +0,0 @@ -module JuliaBUGSTikzGraphsExt - -using TikzGraphs -using JuliaBUGS -using JuliaBUGS.MetaGraphsNext - -function TikzGraphs.plot(m::JuliaBUGS.BUGSModel; kwargs...) - return TikzGraphs.plot(m.g, m.parameters; kwargs...) -end - -function TikzGraphs.plot(g::JuliaBUGS.BUGSGraph, parameters; kwargs...) - color_dict = Dict{Int,String}() - for (i, node) in enumerate(labels(g)) - if node in parameters - color_dict[i] = "fill=green!10" - else - color_dict[i] = "fill=yellow!10" - end - end - - node_style = get(kwargs, :node_style, "draw, rounded corners, fill=blue!10") - node_styles = get(kwargs, :node_styles, color_dict) - edge_style = get(kwargs, :edge_style, "black") - - return TikzGraphs.plot( - g.graph, - map(x -> String(Symbol(x)), labels(g)); - node_style=node_style, - node_styles=node_styles, - edge_style=edge_style, - kwargs..., - ) -end - -end