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 4 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.trace(chns::T) where {T<:MCMCChains.Chains}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think trace is an appropriate name, it should be something like traceplot or plot_chains to make it clear this is outputting a figure.

If you decide to go the method dispatch way, it would be good to deconstruct it a bit better:

  • Make a mutable function that takes an Axis
  • Create separate functions for each subplot.

This way the code is beneficial for anyone who would just like some part of the code.

Copy link
Contributor Author

@alecloudenback alecloudenback Apr 7, 2024

Choose a reason for hiding this comment

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

I don't think trace is an appropriate name, it should be something like traceplot or plot_chains to make it clear this is outputting a figure.

trace follows the convention that plot names in the Julia ecosystem don't have the plot postfix, e.g. scatter or lines instead of scatterplot or linesplot

If convert_arguments is not sufficient, you can use the @recipe macro no?

Recipes are for creating a single sub-plot, not defining a figure with multiple sub-plots. I'm not sure there's a way to use a recipe unless all chains and parameters were plotted into a single sub-plot and not faceted the way MCMC plots tend to be

Make a mutable function that takes an Axis

Edit: I think I understand the desire for this, but I think this goes back to the fact that MCMC-type plots are facets of multiple plots/axes instead. MCMC-type plots are inherently figures not just axes/plots So, e.g. trace!(axis,...) isn't feasible with the facets.

Create separate functions for each subplot.

i.e. where you could get the left side and right sight separately?

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

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
17 changes: 17 additions & 0 deletions src/MCMCChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export rafterydiag
export rstar

export hpd
export trace

"""
Chains
Expand Down Expand Up @@ -85,4 +86,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