Skip to content

Ensure accepted input types are general where possible #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 10, 2022
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "KernelFunctions"
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
version = "0.10.46"
version = "0.10.47"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
22 changes: 22 additions & 0 deletions src/TestUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ function test_interface(
)
end

function test_interface(rng::AbstractRNG, k::Kernel, ::Type{Vector{String}}; kwargs...)
return test_interface(
k,
[randstring(rng) for _ in 1:3],
[randstring(rng) for _ in 1:3],
[randstring(rng) for _ in 1:4];
kwargs...,
)
end

function test_interface(
rng::AbstractRNG, k::Kernel, ::Type{ColVecs{String}}; dim_in=2, kwargs...
)
return test_interface(
k,
ColVecs([randstring(rng) for _ in 1:dim_in, _ in 1:3]),
ColVecs([randstring(rng) for _ in 1:dim_in, _ in 1:3]),
ColVecs([randstring(rng) for _ in 1:dim_in, _ in 1:4]);
kwargs...,
)
end

function test_interface(k::Kernel, T::Type{<:AbstractVector}; kwargs...)
return test_interface(Random.GLOBAL_RNG, k, T; kwargs...)
end
Expand Down
7 changes: 3 additions & 4 deletions src/distances/delta.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Delta is not following the PreMetric rules since d(x, x) == 1
struct Delta <: Distances.UnionPreMetric end

(dist::Delta)(a::Number, b::Number) = a == b
Base.@propagate_inbounds function (dist::Delta)(
a::AbstractArray{<:Number}, b::AbstractArray{<:Number}
)
(dist::Delta)(a, b) = a == b

Base.@propagate_inbounds function (dist::Delta)(a::AbstractArray, b::AbstractArray)
@boundscheck if length(a) != length(b)
throw(
DimensionMismatch(
Expand Down
4 changes: 2 additions & 2 deletions src/transform/selecttransform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _maybe_unwrap(x::AbstractArray{<:Any,0}) = x[]
_map(t::SelectTransform, x::ColVecs) = _wrap(view(x.X, t.select, :), ColVecs)
_map(t::SelectTransform, x::RowVecs) = _wrap(view(x.X, :, t.select), RowVecs)

_wrap(x::AbstractVector{<:Real}, ::Any) = x
_wrap(X::AbstractMatrix{<:Real}, ::Type{T}) where {T} = T(X)
_wrap(x::AbstractVector, ::Any) = x
_wrap(X::AbstractMatrix, ::Type{T}) where {T} = T(X)

Base.show(io::IO, t::SelectTransform) = print(io, "Select Transform (dims: ", t.select, ")")
3 changes: 3 additions & 0 deletions test/basekernels/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# Standardised tests.
TestUtils.test_interface(k, Float64)
TestUtils.test_interface(k, Vector{String})
test_ADs(ZeroKernel)
test_interface_ad_perf(_ -> k, nothing, StableRNG(123456))
end
Expand All @@ -22,6 +23,7 @@

# Standardised tests.
TestUtils.test_interface(k, Float64)
TestUtils.test_interface(k, Vector{String})
test_ADs(WhiteKernel)
test_interface_ad_perf(_ -> k, nothing, StableRNG(123456))
end
Expand All @@ -38,6 +40,7 @@

# Standardised tests.
TestUtils.test_interface(k, Float64)
TestUtils.test_interface(k, Vector{String})
test_ADs(c -> ConstantKernel(; c=only(c)), [c])
test_interface_ad_perf(c -> ConstantKernel(; c=c), c, StableRNG(123456))
end
Expand Down
1 change: 1 addition & 0 deletions test/kernels/kernelproduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Standardised tests.
TestUtils.test_interface(k, Float64)
TestUtils.test_interface(ConstantKernel(; c=1.0) * WhiteKernel(), Vector{String})
test_ADs(
x -> KernelProduct(SqExponentialKernel(), LinearKernel(; c=exp(x[1]))), rand(1)
)
Expand Down
1 change: 1 addition & 0 deletions test/kernels/kernelsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Standardised tests.
TestUtils.test_interface(k, Float64)
TestUtils.test_interface(ConstantKernel(; c=1.5) * WhiteKernel(), Vector{String})
test_ADs(x -> KernelSum(SqExponentialKernel(), LinearKernel(; c=exp(x[1]))), rand(1))
test_interface_ad_perf(2.4, StableRNG(123456)) do c
KernelSum(SqExponentialKernel(), LinearKernel(; c=c))
Expand Down
3 changes: 3 additions & 0 deletions test/kernels/kerneltensorproduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
# Standardised tests.
TestUtils.test_interface(kernel1, ColVecs{Float64})
TestUtils.test_interface(kernel1, RowVecs{Float64})
TestUtils.test_interface(
KernelTensorProduct(WhiteKernel(), ConstantKernel(; c=1.1)), ColVecs{String}
)
test_ADs(
x -> KernelTensorProduct(SqExponentialKernel(), LinearKernel(; c=exp(x[1]))),
rand(1);
Expand Down
4 changes: 4 additions & 0 deletions test/kernels/transformedkernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
@test repr(kt) == repr(k) * "\n\t- " * repr(ScaleTransform(s))

TestUtils.test_interface(k, Float64)
TestUtils.test_interface(
TransformedKernel(ConstantKernel(; c=1.5), FunctionTransform(x -> x * "hi")),
Vector{String},
)
test_ADs(x -> SqExponentialKernel() ∘ ScaleTransform(x[1]), rand(1))
test_interface_ad_perf(0.35, StableRNG(123456)) do λ
SqExponentialKernel() ∘ ScaleTransform(λ)
Expand Down
9 changes: 9 additions & 0 deletions test/transform/functiontransform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
end
end

@testset "String input" begin
f = x -> x * "hello"
t = FunctionTransform(f)
x = [randstring(rng) for _ in 1:3]
y = map(t, x)
@test all([t(x[n]) == y[n] for n in eachindex(x)])
@test all([f(x[n]) == y[n] for n in eachindex(x)])
end

@test repr(FunctionTransform(sin)) == "Function Transform: $(sin)"
f(a, x) = sin.(a .* x)
test_ADs(x -> SEKernel() ∘ FunctionTransform(y -> f(x, y)), randn(rng, 3))
Expand Down
9 changes: 6 additions & 3 deletions test/transform/selecttransform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
x_vecs = [randn(rng, maximum(select)) for _ in 1:3]
x_cols = ColVecs(randn(rng, maximum(select), 6))
x_rows = RowVecs(randn(rng, 4, maximum(select)))
x_string = ColVecs([randstring(rng) for _ in 1:maximum(select), _ in 1:5])

Xs = [x_vecs, x_cols, x_rows]
Xs = [x_vecs, x_cols, x_rows, x_string]

@testset "$(typeof(x))" for x in Xs
x′ = map(t, x)
Expand All @@ -24,8 +25,9 @@
a_vecs = map(x -> AxisArray(x; col=symbols), x_vecs)
a_cols = ColVecs(AxisArray(x_cols.X; col=symbols, index=(1:6)))
a_rows = RowVecs(AxisArray(x_rows.X; index=(1:4), col=symbols))
a_string = ColVecs(AxisArray(x_string.X; col=symbols, index=1:5))

As = [a_vecs, a_cols, a_rows]
As = [a_vecs, a_cols, a_rows, a_string]

@testset "$(typeof(a))" for (a, x) in zip(As, Xs)
a′ = map(ts, a)
Expand Down Expand Up @@ -122,8 +124,9 @@
("Vector{<:Vector}", [randn(6) for _ in 1:3]),
("ColVecs", ColVecs(randn(5, 10))),
("RowVecs", RowVecs(randn(11, 4))),
("ColVecs{String}", ColVecs([randstring() for _ in 1:6, _ in 1:5])),
]
@test KernelFunctions._map(t, x) isa AbstractVector{Float64}
@test KernelFunctions._map(t, x) isa AbstractVector
end
end
end