|
| 1 | +module TestUtils |
| 2 | + |
| 3 | +const __ATOL = 1e-9 |
| 4 | + |
| 5 | +using LinearAlgebra |
| 6 | +using KernelFunctions |
| 7 | +using Random |
| 8 | +using Test |
| 9 | + |
| 10 | +""" |
| 11 | + test_interface( |
| 12 | + k::Kernel, |
| 13 | + x0::AbstractVector, |
| 14 | + x1::AbstractVector, |
| 15 | + x2::AbstractVector; |
| 16 | + atol=__ATOL, |
| 17 | + ) |
| 18 | +
|
| 19 | +Run various consistency checks on `k` at the inputs `x0`, `x1`, and `x2`. |
| 20 | +`x0` and `x1` should be of the same length with different values, while `x0` and `x2` should |
| 21 | +be of different lengths. |
| 22 | +
|
| 23 | + test_interface([rng::AbstractRNG], k::Kernel, T::Type{<:AbstractVector}; atol=__ATOL) |
| 24 | +
|
| 25 | +`test_interface` offers certain types of test data generation to make running these tests |
| 26 | +require less code for common input types. For example, `Vector{<:Real}`, `ColVecs{<:Real}`, |
| 27 | +and `RowVecs{<:Real}` are supported. For other input vector types, please provide the data |
| 28 | +manually. |
| 29 | +""" |
| 30 | +function test_interface( |
| 31 | + k::Kernel, |
| 32 | + x0::AbstractVector, |
| 33 | + x1::AbstractVector, |
| 34 | + x2::AbstractVector; |
| 35 | + atol=__ATOL, |
| 36 | +) |
| 37 | + # TODO: uncomment the tests of ternary kerneldiagmatrix. |
| 38 | + |
| 39 | + # Ensure that we have the required inputs. |
| 40 | + @assert length(x0) == length(x1) |
| 41 | + @assert length(x0) ≠ length(x2) |
| 42 | + |
| 43 | + # Check that kerneldiagmatrix basically works. |
| 44 | + # @test kerneldiagmatrix(k, x0, x1) isa AbstractVector |
| 45 | + # @test length(kerneldiagmatrix(k, x0, x1)) == length(x0) |
| 46 | + |
| 47 | + # Check that pairwise basically works. |
| 48 | + @test kernelmatrix(k, x0, x2) isa AbstractMatrix |
| 49 | + @test size(kernelmatrix(k, x0, x2)) == (length(x0), length(x2)) |
| 50 | + |
| 51 | + # Check that elementwise is consistent with pairwise. |
| 52 | + # @test kerneldiagmatrix(k, x0, x1) ≈ diag(kernelmatrix(k, x0, x1)) atol=atol |
| 53 | + |
| 54 | + # Check additional binary elementwise properties for kernels. |
| 55 | + # @test kerneldiagmatrix(k, x0, x1) ≈ kerneldiagmatrix(k, x1, x0) |
| 56 | + @test kernelmatrix(k, x0, x2) ≈ kernelmatrix(k, x2, x0)' atol=atol |
| 57 | + |
| 58 | + # Check that unary elementwise basically works. |
| 59 | + @test kerneldiagmatrix(k, x0) isa AbstractVector |
| 60 | + @test length(kerneldiagmatrix(k, x0)) == length(x0) |
| 61 | + |
| 62 | + # Check that unary pairwise basically works. |
| 63 | + @test kernelmatrix(k, x0) isa AbstractMatrix |
| 64 | + @test size(kernelmatrix(k, x0)) == (length(x0), length(x0)) |
| 65 | + @test kernelmatrix(k, x0) ≈ kernelmatrix(k, x0)' atol=atol |
| 66 | + |
| 67 | + # Check that unary elementwise is consistent with unary pairwise. |
| 68 | + @test kerneldiagmatrix(k, x0) ≈ diag(kernelmatrix(k, x0)) atol=atol |
| 69 | + |
| 70 | + # Check that unary pairwise produces a positive definite matrix (approximately). |
| 71 | + @test eigmin(Matrix(kernelmatrix(k, x0))) > -atol |
| 72 | + |
| 73 | + # Check that unary elementwise / pairwise are consistent with the binary versions. |
| 74 | + # @test kerneldiagmatrix(k, x0) ≈ kerneldiagmatrix(k, x0, x0) atol=atol |
| 75 | + @test kernelmatrix(k, x0) ≈ kernelmatrix(k, x0, x0) atol=atol |
| 76 | + |
| 77 | + # Check that basic kernel evaluation succeeds and is consistent with `kernelmatrix`. |
| 78 | + @test k(first(x0), first(x1)) isa Real |
| 79 | + @test kernelmatrix(k, x0, x2) ≈ [k(xl, xr) for xl in x0, xr in x2] |
| 80 | + |
| 81 | + tmp = Matrix{Float64}(undef, length(x0), length(x2)) |
| 82 | + @test kernelmatrix!(tmp, k, x0, x2) ≈ kernelmatrix(k, x0, x2) |
| 83 | + |
| 84 | + tmp_square = Matrix{Float64}(undef, length(x0), length(x0)) |
| 85 | + @test kernelmatrix!(tmp_square, k, x0) ≈ kernelmatrix(k, x0) |
| 86 | + |
| 87 | + tmp_diag = Vector{Float64}(undef, length(x0)) |
| 88 | + @test kerneldiagmatrix!(tmp_diag, k, x0) ≈ kerneldiagmatrix(k, x0) |
| 89 | +end |
| 90 | + |
| 91 | +function test_interface( |
| 92 | + rng::AbstractRNG, k::Kernel, ::Type{Vector{T}}; kwargs... |
| 93 | +) where {T<:Real} |
| 94 | + test_interface(k, randn(rng, T, 3), randn(rng, T, 3), randn(rng, T, 2); kwargs...) |
| 95 | +end |
| 96 | + |
| 97 | +function test_interface( |
| 98 | + rng::AbstractRNG, k::Kernel, ::Type{<:ColVecs{T}}; dim_in=2, kwargs..., |
| 99 | +) where {T<:Real} |
| 100 | + test_interface( |
| 101 | + k, |
| 102 | + ColVecs(randn(rng, T, dim_in, 3)), |
| 103 | + ColVecs(randn(rng, T, dim_in, 3)), |
| 104 | + ColVecs(randn(rng, T, dim_in, 2)); |
| 105 | + kwargs..., |
| 106 | + ) |
| 107 | +end |
| 108 | + |
| 109 | +function test_interface( |
| 110 | + rng::AbstractRNG, k::Kernel, ::Type{<:RowVecs{T}}; dim_in=2, kwargs..., |
| 111 | +) where {T<:Real} |
| 112 | + test_interface( |
| 113 | + k, |
| 114 | + RowVecs(randn(rng, T, 3, dim_in)), |
| 115 | + RowVecs(randn(rng, T, 3, dim_in)), |
| 116 | + RowVecs(randn(rng, T, 2, dim_in)); |
| 117 | + kwargs..., |
| 118 | + ) |
| 119 | +end |
| 120 | + |
| 121 | +function test_interface(k::Kernel, T::Type{<:AbstractVector}; kwargs...) |
| 122 | + test_interface(Random.GLOBAL_RNG, k, T; kwargs...) |
| 123 | +end |
| 124 | + |
| 125 | +function test_interface(rng::AbstractRNG, k::Kernel, T::Type{<:Real}; kwargs...) |
| 126 | + test_interface(rng, k, Vector{T}; kwargs...) |
| 127 | + test_interface(rng, k, ColVecs{T}; kwargs...) |
| 128 | + test_interface(rng, k, RowVecs{T}; kwargs...) |
| 129 | +end |
| 130 | + |
| 131 | +function test_interface(k::Kernel, T::Type{<:Real}=Float64; kwargs...) |
| 132 | + test_interface(Random.GLOBAL_RNG, k, T; kwargs...) |
| 133 | +end |
| 134 | + |
| 135 | +end # module |
0 commit comments