Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extensions]
MCMCChainsMakieExt = "Makie"

[compat]
AbstractMCMC = "0.4, 0.5, 1.0, 2.0, 3.0, 4, 5"
AxisArrays = "0.4.4"
Expand Down
116 changes: 116 additions & 0 deletions ext/MCMCChainsMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module MCMCChainsMakieExt

import MCMCChains
import Makie


function MCMCChains.trace(chns::T; figure=(;), colormap=Makie.to_colormap(:tol_vibrant)) where {T<:MCMCChains.Chains}
params = MCMCChains.names(chns, :parameters)

Check warning on line 8 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L7-L8

Added lines #L7 - L8 were not covered by tests

n_chains = length(MCMCChains.chains(chns))
n_samples = length(chns)
n_params = length(params)

Check warning on line 12 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L10-L12

Added lines #L10 - L12 were not covered by tests


colormap = if colormap isa Symbol
Makie.to_colormap(colormap)

Check warning on line 16 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L15-L16

Added lines #L15 - L16 were not covered by tests
else
colormap

Check warning on line 18 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L18

Added line #L18 was not covered by tests
end
@show length(colormap)
colorindex(i) =

Check warning on line 21 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L20-L21

Added lines #L20 - L21 were not covered by tests
mod(i - 1, length(colormap)) + 1

# set size if not provided
figure = let
width = 600
height = max(400, 80 * n_params)
nt = (size=(width, height),)
merge(nt, figure)

Check warning on line 29 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L25-L29

Added lines #L25 - L29 were not covered by tests
end

fig = Makie.Figure(; figure...)

Check warning on line 32 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L32

Added line #L32 was not covered by tests

for (i, param) in enumerate(params)
ax = Makie.Axis(fig[i+1, 1]; ylabel=string(param))
for chain in 1:n_chains
values = chns[:, param, chain]
Makie.lines!(

Check warning on line 38 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L34-L38

Added lines #L34 - L38 were not covered by tests
ax,
1:n_samples,
values;
label=string(chain),
color=(colormap[colorindex(chain)], 0.7),
linewidth=0.7
)
end

Check warning on line 46 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L46

Added line #L46 was not covered by tests

Makie.hideydecorations!(ax; label=false)
if i < n_params
Makie.hidexdecorations!(ax; grid=false)

Check warning on line 50 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L48-L50

Added lines #L48 - L50 were not covered by tests
else
ax.xlabel = "Iteration"

Check warning on line 52 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L52

Added line #L52 was not covered by tests
end
end

Check warning on line 54 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L54

Added line #L54 was not covered by tests

for (i, param) in enumerate(params)
ax = Makie.Axis(fig[i+1, 2]; ylabel=string(param))
for chain in 1:n_chains
values = chns[:, param, chain]
Makie.density!(

Check warning on line 60 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L56-L60

Added lines #L56 - L60 were not covered by tests
ax,
values;
label=string(chain),
color=(colormap[colorindex(chain)], 0.1),
strokewidth=1,
strokecolor=(colormap[colorindex(chain)], 0.7)
)
end

Check warning on line 68 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L68

Added line #L68 was not covered by tests

Makie.hideydecorations!(ax)
if i < n_params
Makie.hidexdecorations!(ax; grid=false)

Check warning on line 72 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L70-L72

Added lines #L70 - L72 were not covered by tests
else
ax.xlabel = "Parameter estimate"

Check warning on line 74 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L74

Added line #L74 was not covered by tests
end
end

Check warning on line 76 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L76

Added line #L76 was not covered by tests

axes = [only(Makie.contents(fig[i+1, 2])) for i in 1:n_params]
Makie.linkxaxes!(axes...)

Check warning on line 79 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L78-L79

Added lines #L78 - L79 were not covered by tests

Makie.Legend(fig[1, 1:2], first(axes), "Chain", orientation=:horizontal, titlehalign=:left, halign=:left, titleposition=:left)

Check warning on line 81 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L81

Added line #L81 was not covered by tests

Makie.rowgap!(fig.layout, 10)
Makie.colgap!(fig.layout, 10)

Check warning on line 84 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L83-L84

Added lines #L83 - L84 were not covered by tests

return fig

Check warning on line 86 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L86

Added line #L86 was not covered by tests
end

# https://docs.makie.org/stable/explanations/specapi#convert_arguments-for-GridLayoutSpec
import Makie.SpecApi as S

# Our custom type we want to write a conversion method for
struct PlotGrid
nplots::Tuple{Int,Int}
end

# If we want to use the `color` attribute in the conversion, we have to
# mark it via `used_attributes`
Makie.used_attributes(::T) where {T<:MCMCChains.Chains} = (:linewidth, :alpha)

Check warning on line 99 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L99

Added line #L99 was not covered by tests

# The conversion method creates a grid of `Axis` objects with `Lines` plot inside
# We restrict to Plot{plot}, so that only `plot(PlotGrid(...))` works, but not e.g. `scatter(PlotGrid(...))`.
function Makie.convert_arguments(::Type{Makie.Plot{Makie.plot}}, chn::T; linewidth=0.7, alpha=0.6) where {T<:MCMCChains.Chains}
n_iterations, n_params, n_chains = size(chn)
axes_left = [

Check warning on line 105 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L103-L105

Added lines #L103 - L105 were not covered by tests
S.Axis(plots=[S.Lines(chn.value[:, p, i]; linewidth, alpha, label=string(i)) for i in 1:n_chains])
for p in 1:n_params
]
axes_right = [

Check warning on line 109 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L109

Added line #L109 was not covered by tests
S.Axis(plots=[S.Density(chn.value[:, p, i]; alpha, label=string(i)) for i in 1:n_chains])
for p in 1:n_params
]
return S.GridLayout(hcat(axes_left, axes_right))

Check warning on line 113 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L113

Added line #L113 was not covered by tests
end

end
17 changes: 17 additions & 0 deletions src/MCMCChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export rafterydiag
export rstar

export hpd
export trace

"""
Chains
Expand Down Expand Up @@ -108,4 +109,20 @@ include("plot.jl")
include("tables.jl")
include("rstar.jl")


### trace function via Extensions still needs to define a base function
function trace end

function __init__()

Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
if exc.f in [trace]
if isempty(methods(exc.f))
print(io, "\n$(exc.f) has no methods, yet. Makie has to be loaded for the plotting extension to be activated. Run `using Makie`, `using CairoMakie`, `using GLMakie` or any other package that also loads Makie.")
end
end
end
end


end # module
Loading