Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plotting via Makie as Extension #454

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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 @@ -26,6 +26,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
74 changes: 74 additions & 0 deletions ext/MCMCChainsMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module MCMCChainsMakieExt

import MCMCChains
import Makie


function MCMCChains.myplot(chns::T) where {T<:MCMCChains.Chains}
params = 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

colors = Makie.to_colormap(:tol_vibrant)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @devmotion this is a bit too opiniated, what if I want to use a different set of colors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will explore ways to make more flexible.

width = 600
height = max(400, 80 * n_params)

Check warning on line 16 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L14-L16

Added lines #L14 - L16 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this, it is too restrictive...


fig = Makie.Figure(; size=(width, height))

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

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 24 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L20-L24

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

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

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

Check warning on line 36 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L34-L36

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

Check warning on line 38 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L38

Added line #L38 was not covered by tests
end
end

Check warning on line 40 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L40

Added line #L40 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 46 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L42-L46

Added lines #L42 - L46 were not covered by tests
ax,
values;
label=string(chain),
color=(colors[chain], 0.1),
strokewidth=1,
strokecolor=(colors[chain], 0.7)
)
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

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

Check warning on line 58 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L56-L58

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

Check warning on line 60 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L60

Added line #L60 was not covered by tests
end
end

Check warning on line 62 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L62

Added line #L62 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 65 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L64-L65

Added lines #L64 - L65 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 67 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L67

Added line #L67 was not covered by tests

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

Check warning on line 70 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L69-L70

Added lines #L69 - L70 were not covered by tests

return fig

Check warning on line 72 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L72

Added line #L72 was not covered by tests
end
end
5 changes: 5 additions & 0 deletions src/MCMCChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ include("plot.jl")
include("tables.jl")
include("rstar.jl")


export myplot

# functions with no methods
function myplot end
end # module
Loading