Skip to content

Testing (and fixing) handling of AbstractVector{AbstractVector{T}} inputs #370

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 17 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.22"
version = "0.10.23"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 2 additions & 0 deletions src/basekernels/fbm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ end
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h) / 2

_mod(x::AbstractVector{<:Real}) = abs2.(x)
_mod(x::AbstractVector{<:AbstractVector{<:Real}}) = sum.(abs2, x)
# two lines above could be combined into the second (dispatching on general AbstractVectors), but this (somewhat) more performant
_mod(x::ColVecs) = vec(sum(abs2, x.X; dims=1))
_mod(x::RowVecs) = vec(sum(abs2, x.X; dims=2))

Expand Down
19 changes: 5 additions & 14 deletions src/distances/delta.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
# Delta is not following the PreMetric rules since d(x, x) == 1
struct Delta <: Distances.UnionPreMetric end

@inline function Distances._evaluate(::Delta, a::AbstractVector, b::AbstractVector)
@boundscheck if length(a) != length(b)
throw(
DimensionMismatch(
"first array has length $(length(a)) which does not match the length of the " *
"second, $(length(b)).",
),
)
end
return a == b
end

Distances.result_type(::Delta, Ta::Type, Tb::Type) = Bool

@inline Distances.eval_op(::Delta, a::Real, b::Real) = a == b
@inline Distances.eval_reduce(::Delta, a, b) = a && b
@inline Distances.eval_start(::Delta, a, b) = true
Comment on lines +4 to +6
Copy link
Member

Choose a reason for hiding this comment

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

This will result in much more complicated code, I don't think we should use the default dispatches in Distances and implement these internal functions.

Copy link
Member

Choose a reason for hiding this comment

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

We should just have to check x == y for the inputs x and y.

Copy link
Member

Choose a reason for hiding this comment

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

Is there even a reason to implement Distances._evaluate here? Isn't (::Delta)(x, y) sufficient?

@inline (dist::Delta)(a::AbstractArray, b::AbstractArray) = Distances._evaluate(dist, a, b)
@inline (dist::Delta)(a::Number, b::Number) = a == b

Distances.result_type(::Delta, Ta::Type, Tb::Type) = Bool
15 changes: 15 additions & 0 deletions src/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ function test_interface(
)
end

function test_interface(
rng::AbstractRNG, k::Kernel, ::Type{<:Vector{Vector{T}}}; dim_in=2, kwargs...
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rng::AbstractRNG, k::Kernel, ::Type{<:Vector{Vector{T}}}; dim_in=2, kwargs...
rng::AbstractRNG, k::Kernel, ::Type{<:AbstractVector{<:AbstractVector{T}}}; dim_in=2, kwargs...

Sorry what's the reasoning for not using this?

Copy link
Member

Choose a reason for hiding this comment

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

The test itself utilises a Vector{Vector{T}}, so to my mind this type constraint better reflects what's going on 🤷

) where {T<:Real}
return test_interface(
k,
[randn(rng, T, dim_in) for _ in 1:1001],
[randn(rng, T, dim_in) for _ in 1:1001],
[randn(rng, T, dim_in) for _ in 1:1000];
kwargs...,
)
end

function test_interface(k::Kernel, T::Type{<:AbstractVector}; kwargs...)
return test_interface(Random.GLOBAL_RNG, k, T; kwargs...)
end
Expand All @@ -147,6 +159,9 @@ function test_interface(rng::AbstractRNG, k::Kernel, T::Type{<:Real}; kwargs...)
@testset "RowVecs{$T}" begin
test_interface(rng, k, RowVecs{T}; kwargs...)
end
@testset "Vector{Vector{T}}" begin
test_interface(rng, k, Vector{Vector{T}}; kwargs...)
end
end

function test_interface(k::Kernel, T::Type{<:Real}=Float64; kwargs...)
Expand Down