From fce0c6443f5601872093860a4e0ca0b1377feeaf Mon Sep 17 00:00:00 2001 From: David Widmann Date: Mon, 11 Jan 2021 10:53:26 +0100 Subject: [PATCH] Update tests --- src/deprecated.jl | 2 +- test/basekernels/maha.jl | 38 ++----------------------- test/basekernels/piecewisepolynomial.jl | 19 ++++++++----- 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/deprecated.jl b/src/deprecated.jl index a0805c316..cef98ca37 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -1,5 +1,5 @@ # TODO: remove tests when removed -@deprecate MahalanobisKernel(; P::AbstractMatrix{<:Real}) transform(SqExponentialKernel(), LinearTransform(cholesky(P).U)) +@deprecate MahalanobisKernel(; P::AbstractMatrix{<:Real}) transform(SqExponentialKernel(), LinearTransform(sqrt(2) .* cholesky(P).U)) # TODO: remove keyword argument `maha` when removed @deprecate PiecewisePolynomialKernel{V}(A::AbstractMatrix{<:Real}) where V transform(PiecewisePolynomialKernel{V}(size(A, 1)), LinearTransform(cholesky(A).U)) diff --git a/test/basekernels/maha.jl b/test/basekernels/maha.jl index 44236e7c4..90c9b7f59 100644 --- a/test/basekernels/maha.jl +++ b/test/basekernels/maha.jl @@ -1,6 +1,5 @@ @testset "maha" begin rng = MersenneTwister(123456) - x = 2 * rand(rng) D_in = 3 v1 = rand(rng, D_in) v2 = rand(rng, D_in) @@ -9,40 +8,10 @@ P = Matrix(Cholesky(U, 'U', 0)) @assert isposdef(P) - k = MahalanobisKernel(; P=P) - - @test kappa(k, x) == exp(-x) + k = @test_deprecated MahalanobisKernel(; P=P) + @test k isa TransformedKernel{SqExponentialKernel,<:LinearTransform} + @test k.transform.A ≈ sqrt(2) .* U @test k(v1, v2) ≈ exp(-sqmahalanobis(v1, v2, P)) - @test kappa(ExponentialKernel(), x) == kappa(k, x) - @test repr(k) == "Mahalanobis Kernel (size(P) = $(size(P)))" - - M1, M2 = rand(rng, 3, 2), rand(rng, 3, 2) - - function FiniteDifferences.to_vec(dist::SqMahalanobis) - return vec(dist.qmat), x -> SqMahalanobis(reshape(x, size(dist.qmat)...)) - end - a = rand() - - function test_mahakernel(U::UpperTriangular, v1::AbstractVector, v2::AbstractVector) - return MahalanobisKernel(; P=Array(U' * U))(v1, v2) - end - - @test all( - FiniteDifferences.j′vp(FDM, test_mahakernel, a, U, v1, v2)[1] .≈ - UpperTriangular(Zygote.pullback(test_mahakernel, U, v1, v2)[2](a)[1]), - ) - - function test_sqmaha(U::UpperTriangular, v1::AbstractVector, v2::AbstractVector) - return SqMahalanobis(Array(U' * U))(v1, v2) - end - - @test all( - FiniteDifferences.j′vp(FDM, test_sqmaha, a, U, v1, v2)[1] .≈ - UpperTriangular(Zygote.pullback(test_sqmaha, U, v1, v2)[2](a)[1]), - ) - - # test_ADs(U -> MahalanobisKernel(P=Array(U' * U)), U, ADs=[:Zygote]) - @test_broken "Nothing passes (problem with Mahalanobis distance in Distances)" # Standardised tests. @testset "ColVecs" begin @@ -57,5 +26,4 @@ x2 = RowVecs(randn(2, D_in)) TestUtils.test_interface(k, x0, x1, x2) end - test_params(k, (P,)) end diff --git a/test/basekernels/piecewisepolynomial.jl b/test/basekernels/piecewisepolynomial.jl index a6401419c..97d69f44b 100644 --- a/test/basekernels/piecewisepolynomial.jl +++ b/test/basekernels/piecewisepolynomial.jl @@ -4,21 +4,26 @@ v2 = rand(D) maha = Matrix{Float64}(I, D, D) v = 3 - k = PiecewisePolynomialKernel{v}(maha) - k2 = PiecewisePolynomialKernel(; v=v, maha=maha) + k = PiecewisePolynomialKernel(; v=v, d=D) + k2 = PiecewisePolynomialKernel{v}(D) + k3 = @test_deprecated PiecewisePolynomialKernel{v}(maha) + k4 = @test_deprecated PiecewisePolynomialKernel(; v=v, maha=maha) - @test k2(v1, v2) ≈ k(v1, v2) atol = 1e-5 + @test k2(v1, v2) == k(v1, v2) + @test k3(v1, v2) ≈ k(v1, v2) + @test k4(v1, v2) ≈ k(v1, v2) @test_throws ErrorException PiecewisePolynomialKernel{4}(maha) + @test_throws ErrorException PiecewisePolynomialKernel{4}(D) + @test_throws ErrorException PiecewisePolynomialKernel{v}(-1) - @test repr(k) == "Piecewise Polynomial Kernel (v = $(v), size(maha) = $(size(maha)))" + @test repr(k) == "Piecewise Polynomial Kernel (v = $(v), ⌊d/2⌋ = $(div(D, 2)))" # Standardised tests. TestUtils.test_interface(k, ColVecs{Float64}; dim_in=2) TestUtils.test_interface(k, RowVecs{Float64}; dim_in=2) - # test_ADs(maha-> PiecewisePolynomialKernel(v=2, maha = maha), maha) - @test_broken "Nothing passes (problem with Mahalanobis distance in Distances)" + test_ADs(() -> PiecewisePolynomialKernel{v}(D)) - test_params(k, (maha,)) + test_params(k, ()) end