Skip to content

Commit

Permalink
Testing (and fixing) handling of AbstractVector{AbstractVector{T}} in…
Browse files Browse the repository at this point in the history
…puts (#370)

* Fix input types, improve readability

* Add missing bit

* Add doc string

* Fix mistake

* Add docstring to docs

* Reformulate

* Add test

* Potential fix for Delta distance

* Fix fbm

* Further Delta fixes

* Formatter

* Remove old Delta implementation

* Make interface more specific

* Formatter

* Bump patch
  • Loading branch information
Crown421 authored Oct 6, 2021
1 parent 979a019 commit 9f708d0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
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
@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...
) 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

2 comments on commit 9f708d0

@Crown421
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/46218

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.23 -m "<description of version>" 9f708d0d517984a0da0559abc6631d97f2d024a4
git push origin v0.10.23

Please sign in to comment.