Skip to content

Commit

Permalink
Alow plotting kernel without nodeset (#31)
Browse files Browse the repository at this point in the history
* allow plotting kernel without nodeset

* format
  • Loading branch information
JoshuaLampert authored Apr 10, 2024
1 parent ab160df commit fe8472e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/visualization.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
@recipe function f(kernel::AbstractKernel{Dim}; x_min = -1.0, x_max = 1.0,
N = 50) where {Dim}
if Dim == 1
x = LinRange(x_min, x_max, N)
title --> get_name(kernel)
x, kernel.(Ref(0.0), x)
elseif Dim == 2
nodeset = homogeneous_hypercube(N, x_min, x_max; dim = 2)
x = unique(values_along_dim(nodeset, 1))
y = unique(values_along_dim(nodeset, 2))
z = reshape(kernel.(Ref([0.0, 0.0]), nodeset), (N, N))
seriestype --> :heatmap # :contourf
title --> get_name(kernel)
x, y, z
else
@error("Plotting a kernel is only supported for dimension up to 2, but the kernel has dimension $Dim")
end
end

@recipe function f(x::AbstractVector, kernel::AbstractKernel)
xguide --> "r"
title --> get_name(kernel)
Expand All @@ -14,6 +33,7 @@ end
y = values_along_dim(nodeset, 2)
seriestype --> :scatter
label --> "nodes"
title --> get_name(kernel)
x, y, kernel.(Ref([0.0, 0.0]), nodeset)
else
@error("Plotting a kernel is only supported for dimension up to 2, but the set has dimension $(dim(nodeset))")
Expand Down
5 changes: 4 additions & 1 deletion test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,17 @@ using Plots
@testset "Visualization" begin
f = sum
kernel = GaussKernel{3}(shape_parameter = 0.5)
kernel_1d = Matern12Kernel{1}()
trafo_kernel = TransformationKernel{2}(kernel, x -> [x[1] + x[2]^2, x[1]])
@test_nowarn plot(-1.0:0.1:1.0, kernel)
for dim in 1:3
nodes = homogeneous_hypercube(5; dim = dim)
@test_nowarn plot(nodes)
if dim < 3
@test_nowarn plot(nodes, kernel)
# Transformtion kernel can only be plotted in the dimension of the input of the trafo
@test_nowarn plot(kernel_1d)
@test_nowarn plot(trafo_kernel, x_min = -2, x_max = 2, N = 100)
# Transformation kernel can only be plotted in the dimension of the input of the trafo
if dim == 2
@test_nowarn plot(nodes, trafo_kernel)
end
Expand Down

0 comments on commit fe8472e

Please sign in to comment.