Skip to content

Commit

Permalink
**BREAKING** change of sampleplot behaviour and defaults (#213)
Browse files Browse the repository at this point in the history
This PR makes two breaking changes to the `sampleplot` plotting utility:

1. **Most default settings have been removed.**
   - `sampleplot` no longer imposes a default seriescolor.
     **How to obtain the previous color:** manually pass `seriescolor = "red"`.
   - The markers have been removed as they seemed more confusing than helpful (overwhelmingly large in the legend, almost invisibly tiny on the plot). The `linealpha` default has been increased to 0.35 to keep visual appearance the same.
     **How to obtain the previous markers:** If you actually liked the tiny markers, [check the diff](https://github.com/JuliaGaussianProcesses/AbstractGPs.jl/pull/213/files#diff-59728b925dcbfab15dd2def5624b3bd8b3a6a609570dc56379f69c8ee0bdef92L124-L127) for the previous default settings that you now need to pass manually.
2. **Multiple samples (`samples` kwarg > 1) are now plotted as a single series.** This means, for example, that passing a string as `label` only adds one legend entry for all samples, not one _per_ sample.
   **How to obtain previous behaviour:** If you actually want each sample to be a different series (for example, to give them different colors), you now need to call `sampleplot` multiple times instead.

Additionally, this PR contains the following non-breaking minor changes:
- minor fix: error message for check of `ribbon_scale` in `plot` recipe
- internal code cleanup (`pop!` for custom-defined kwarg)
  • Loading branch information
st-- authored Aug 26, 2021
1 parent 4423ebc commit 7d42ab2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AbstractGPs"
uuid = "99985d1d-32ba-4be9-9821-2ec096f28918"
authors = ["JuliaGaussianProcesses Team"]
version = "0.4.0"
version = "0.5.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
AbstractGPs = "0.4"
AbstractGPs = "0.4, 0.5"
Documenter = "0.27"
2 changes: 1 addition & 1 deletion examples/regression-1d/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"

[compat]
AbstractGPs = "0.4"
AbstractGPs = "0.4, 0.5"
AdvancedHMC = "0.2"
Distributions = "0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25"
DynamicHMC = "2.2, 3.1"
Expand Down
50 changes: 19 additions & 31 deletions examples/regression-1d/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,19 @@ mean(logpdf(gp_posterior(x_train, y_train, p)(x_test), y_test) for p in samples)
# We sample 5 functions from each posterior GP given by the final 100 samples of kernel
# parameters.

plt = scatter(
x_train,
y_train;
xlim=(0, 1),
xlabel="x",
ylabel="y",
title="posterior (AdvancedHMC)",
label="Train Data",
)
for p in samples[(end - 100):end]
sampleplot!(plt, 0:0.02:1, gp_posterior(x_train, y_train, p); samples=5)
plt = plot(; xlim=(0, 1), xlabel="x", ylabel="y", title="posterior (AdvancedHMC)")
for (i, p) in enumerate(samples[(end - 100):end])
sampleplot!(
plt,
0:0.02:1,
gp_posterior(x_train, y_train, p);
samples=5,
seriescolor="red",
label=(i == 1 ? "samples" : nothing),
)
end
scatter!(plt, x_test, y_test; label="Test Data")
scatter!(plt, x_train, y_train; label="Train Data", markercolor=1)
scatter!(plt, x_test, y_test; label="Test Data", markercolor=2)
plt

# #### DynamicHMC
Expand Down Expand Up @@ -290,18 +290,11 @@ mean(logpdf(gp_posterior(x_train, y_train, p)(x_test), y_test) for p in samples)
# We sample a function from the posterior GP for the final 100 samples of kernel
# parameters.

plt = scatter(
x_train,
y_train;
xlim=(0, 1),
xlabel="x",
ylabel="y",
title="posterior (DynamicHMC)",
label="Train Data",
)
plt = plot(; xlim=(0, 1), xlabel="x", ylabel="y", title="posterior (DynamicHMC)")
scatter!(plt, x_train, y_train; label="Train Data")
scatter!(plt, x_test, y_test; label="Test Data")
for p in samples[(end - 100):end]
sampleplot!(plt, 0:0.02:1, gp_posterior(x_train, y_train, p))
sampleplot!(plt, 0:0.02:1, gp_posterior(x_train, y_train, p); seriescolor="red")
end
plt

Expand Down Expand Up @@ -349,18 +342,13 @@ mean(logpdf(gp_posterior(x_train, y_train, p)(x_test), y_test) for p in samples)
# We sample a function from the posterior GP for the final 100 samples of kernel
# parameters.

plt = scatter(
x_train,
y_train;
xlim=(0, 1),
xlabel="x",
ylabel="y",
title="posterior (EllipticalSliceSampling)",
label="Train Data",
plt = plot(;
xlim=(0, 1), xlabel="x", ylabel="y", title="posterior (EllipticalSliceSampling)"
)
scatter!(plt, x_train, y_train; label="Train Data")
scatter!(plt, x_test, y_test; label="Test Data")
for p in samples[(end - 100):end]
sampleplot!(plt, 0:0.02:1, gp_posterior(x_train, y_train, p))
sampleplot!(plt, 0:0.02:1, gp_posterior(x_train, y_train, p); seriescolor="red")
end
plt

Expand Down
26 changes: 13 additions & 13 deletions src/util/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
length(x) == length(gp.x) ||
throw(DimensionMismatch("length of `x` and `gp.x` has to be equal"))
scale::Float64 = pop!(plotattributes, :ribbon_scale, 1.0)
scale > 0.0 || error("`bandwidth` keyword argument must be non-negative")
scale >= 0.0 || error("`ribbon_scale` keyword argument must be non-negative")

# compute marginals
μ, σ2 = mean_and_var(gp)
Expand Down Expand Up @@ -82,16 +82,19 @@ Plot samples from the projection `f` of a Gaussian process versus `x`.
Make sure to load [Plots.jl](https://github.com/JuliaPlots/Plots.jl) before you use
this function.
When plotting multiple samples, these are treated as a _single_ series (i.e.,
only a single entry will be added to the legend when providing a `label`).
# Example
```julia
using Plots
gp = GP(SqExponentialKernel())
sampleplot(gp(rand(5)); samples=10, markersize=5)
sampleplot(gp(rand(5)); samples=10, linealpha=1.0)
```
The given example plots 10 samples from the projection of the GP `gp`. The `markersize` is modified
from default of 0.5 to 5.
The given example plots 10 samples from the projection of the GP `gp`.
The `linealpha` is modified from default of 0.35 to 1.
---
sampleplot(x::AbstractVector, gp::AbstractGP; samples=1, kwargs...)
Expand All @@ -115,18 +118,15 @@ SamplePlot((f,)::Tuple{<:FiniteGP}) = SamplePlot((f.x, f))
SamplePlot((x, gp)::Tuple{<:AbstractVector,<:AbstractGP}) = SamplePlot((gp(x, 1e-9),))

@recipe function f(sp::SamplePlot)
nsamples::Int = get(plotattributes, :samples, 1)
nsamples::Int = pop!(plotattributes, :samples, 1)
samples = rand(sp.f, nsamples)

flat_x = repeat(vcat(sp.x, NaN), nsamples)
flat_f = vec(vcat(samples, fill(NaN, 1, nsamples)))

# Set default attributes
seriestype --> :line
linealpha --> 0.2
markershape --> :circle
markerstrokewidth --> 0.0
markersize --> 0.5
markeralpha --> 0.3
seriescolor --> "red"
linealpha --> 0.35
label --> ""

return sp.x, samples
return flat_x, flat_f
end
6 changes: 3 additions & 3 deletions test/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
gp = f(x, 0.1)

plt = @test_deprecated sampleplot(gp, 10)
@test plt.n == 10
@test plt.n == 1

@test_deprecated sampleplot!(gp, 4)
@test plt.n == 14
@test plt.n == 2

@test_deprecated sampleplot!(Plots.current(), gp, 3)
@test plt.n == 17
@test plt.n == 3
end
22 changes: 12 additions & 10 deletions test/util/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
z = rand(10)
plt1 = sampleplot(z, gp)
@test plt1.n == 1
@test plt1.series_list[1].plotattributes[:x] == sort(z)
@test isequal(plt1.series_list[1].plotattributes[:x], vcat(z, NaN))

plt2 = sampleplot(gp; samples=10)
@test plt2.n == 10
sort_x = sort(x)
@test all(series.plotattributes[:x] == sort_x for series in plt2.series_list)
plt2 = sampleplot(gp; samples=3)
@test plt2.n == 1
plt2_x = plt2.series_list[1].plotattributes[:x]
plt2_y = plt2.series_list[1].plotattributes[:y]
@test isequal(plt2_x, vcat(x, NaN, x, NaN, x, NaN))
@test length(plt2_y) == length(plt2_x)
@test isnan(plt2_y[length(z) + 1]) && isnan(plt2_y[2length(z) + 2])

z = rand(7)
plt3 = sampleplot(z, f; samples=8)
@test plt3.n == 8
sort_z = sort(z)
@test all(series.plotattributes[:x] == sort_z for series in plt3.series_list)
z3 = rand(7)
plt3 = sampleplot(z3, f; samples=2)
@test plt3.n == 1
@test isequal(plt3.series_list[1].plotattributes[:x], vcat(z3, NaN, z3, NaN))

# Check recipe dispatches for `FiniteGP`s
rec = RecipesBase.apply_recipe(Dict{Symbol,Any}(), gp)
Expand Down

2 comments on commit 7d42ab2

@st--
Copy link
Member Author

@st-- st-- commented on 7d42ab2 Aug 26, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/43566

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" 7d42ab2cca84c6ac35352de69e0f3e579bf14589
git push origin v0.5.0

Please sign in to comment.