Skip to content
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

Add _sum for KernelSum{<:AbstractVector} and associated tests #547

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/kernels/kernelsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

_sum(f, ks::Tuple, args...) = f(first(ks), args...) + _sum(f, Base.tail(ks), args...)
_sum(f, ks::Tuple{Tx}, args...) where {Tx} = f(only(ks), args...)
_sum(f, ks::AbstractVector, args...) = sum(k -> f(k, args...), ks)

Check warning on line 48 in src/kernels/kernelsum.jl

View check run for this annotation

Codecov / codecov/patch

src/kernels/kernelsum.jl#L48

Added line #L48 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Codecov says the line is not covered by tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Strange, the Github annotation was complaining about this line.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

But I trust the codecov webpage more, maybe the annotation is just outdated.


(κ::KernelSum)(x, y) = _sum((k, x, y) -> k(x, y), κ.kernels, x, y)

Expand Down
13 changes: 8 additions & 5 deletions test/kernels/kernelsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
k1 = LinearKernel()
k2 = SqExponentialKernel()
k = KernelSum(k1, k2)
@test k == KernelSum([k1, k2]) == KernelSum((k1, k2))
kvec = KernelSum([k1, k2])
@test k == kvec == KernelSum((k1, k2))
for (_k1, _k2) in Iterators.product(
(k1, KernelSum((k1,)), KernelSum([k1])), (k2, KernelSum((k2,)), KernelSum([k2]))
)
@test k == _k1 + _k2
@test kvec == _k1 + _k2
end
@test length(k) == 2
@test repr(k) == (
@test length(k) == length(kvec) == 2
@test repr(k) ==
repr(kvec) ==
"Sum of 2 kernels:\n" *
"\tLinear Kernel (c = 0.0)\n" *
"\tSquared Exponential Kernel (metric = Euclidean(0.0))"
)
"\tSquared Exponential Kernel (metric = Distances.Euclidean(0.0))"
theogf marked this conversation as resolved.
Show resolved Hide resolved

# Standardised tests.
test_interface(k, Float64)
test_interface(kvec, Float64)
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
Expand Down
Loading