-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module PlotRecipes | ||
|
||
using RecipesBase | ||
using Random | ||
using ..ParameterDistributions | ||
|
||
export plot_marginal_hist | ||
|
||
@recipe function plot(pd::ParameterDistribution; constrained = true, n_sample = 1e4, rng = Random.GLOBAL_RNG) | ||
samples = sample(rng, pd, Int(n_sample)) | ||
if constrained | ||
samples = transform_unconstrained_to_constrained(pd, samples) | ||
end | ||
|
||
# First attempt, make it into a samples dist and plot histograms instead | ||
n_plots = ndims(pd) | ||
batches = batch(pd) | ||
|
||
rows = Int(ceil(sqrt(n_plots))) | ||
cols = Int(floor(sqrt(n_plots))) | ||
tfs = 16 | ||
fs = 14 | ||
|
||
# subplot attr | ||
legend := false | ||
framestyle := repeat([:axes], n_plots) | ||
grid := false | ||
|
||
layout := n_plots | ||
size --> (rows * 400, cols * 400) | ||
titlefontsize --> tfs | ||
xtickfontsize --> fs | ||
ytickfontsize --> fs | ||
xguidefontsize --> fs | ||
yguidefontsize --> fs | ||
|
||
for i in 1:n_plots | ||
batch_id = [j for j = 1:length(batches) if i ∈ batches[j]][1] | ||
dim_in_batch = i - minimum(batches[batch_id]) + 1 # i.e. if i=5 in batch 3:6, this would be "3" | ||
@series begin | ||
seriestype := :histogram | ||
color := batch_id | ||
subplot := i | ||
title := pd.name[batch_id] * " (dim " * string(dim_in_batch) * ")" | ||
samples[i, :] | ||
end | ||
end | ||
|
||
end | ||
|
||
@recipe function plot(d::PDT; n_sample = 1e4, rng = Random.GLOBAL_RNG) where {PDT <: ParameterDistributionType} | ||
samples = sample(rng, d, Int(n_sample)) | ||
|
||
# First attempt, make it into a samples dist and plot histograms instead | ||
n_plots = ndims(d) | ||
|
||
size_l = Int(ceil(sqrt(n_plots))) | ||
tfs = 16 | ||
fs = 14 | ||
|
||
# subplot attr | ||
legend := false | ||
framestyle := repeat([:axes], n_plots) | ||
grid := false | ||
layout := n_plots | ||
size --> (size_l * 400, size_l * 400) | ||
titlefontsize --> tfs | ||
xtickfontsize --> fs | ||
ytickfontsize --> fs | ||
xguidefontsize --> fs | ||
yguidefontsize --> fs | ||
|
||
for i in 1:n_plots | ||
@series begin | ||
seriestype := :histogram | ||
subplot := i | ||
title := "dim " * string(i) | ||
samples[i, :] | ||
end | ||
end | ||
|
||
end | ||
|
||
|
||
|
||
end #module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Test | ||
using Distributions | ||
using LinearAlgebra | ||
using Random | ||
using StatsBase | ||
|
||
using EnsembleKalmanProcesses.ParameterDistributions | ||
using EnsembleKalmanProcesses.PlotRecipes | ||
|
||
if TEST_PLOT_OUTPUT | ||
@testset "PlotRecipes" begin | ||
@testset "Plot ParameterDistribution" begin | ||
d1 = Parameterized(MvNormal(zeros(4), 0.1 * I)) | ||
c1 = [no_constraint(), bounded_below(-1.0), bounded_above(0.4), bounded(-0.1, 0.2)] | ||
name1 = "constrained_mvnormal" | ||
u1 = ParameterDistribution(d1, c1, name1) | ||
|
||
d2 = Samples([1 2 3 4]) | ||
c2 = [bounded(10, 15)] | ||
name2 = "constrained_sampled" | ||
u2 = ParameterDistribution(d2, c2, name2) | ||
|
||
d3 = VectorOfParameterized(repeat([Beta(2, 2)], 3)) | ||
c3 = repeat([no_constraint()], 3) | ||
name3 = "vector_beta" | ||
u3 = ParameterDistribution(d3, c3, name3) | ||
|
||
u = combine_distributions([u1, u2, u3]) | ||
|
||
# PDTs | ||
plt1 = plot(d1) | ||
savefig(plt1, joinpath(@__DIR__, name1 * ".png")) | ||
plt2 = plot(d2) | ||
savefig(plt2, joinpath(@__DIR__, name2 * ".png")) | ||
plt3 = plot(d3) | ||
savefig(plt3, joinpath(@__DIR__, name3 * ".png")) | ||
|
||
# full param dist | ||
plt_constrained = plot(u) | ||
savefig(plt_constrained, joinpath(@__DIR__, "full_dist_constrained.png")) | ||
plt_unconstrained = plot(u, constrained = false) | ||
savefig(plt_unconstrained, joinpath(@__DIR__, "full_dist_unconstrained.png")) | ||
# just a redundant test to show that the plots were created | ||
@test 1 == 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters