Skip to content

Commit

Permalink
Merge pull request #79 from jonschumacher/JS/warn-on-filter
Browse files Browse the repository at this point in the history
Issue warning when filtering `kwargs`
  • Loading branch information
nHackel authored Apr 12, 2024
2 parents 94d2bf6 + 41ab643 commit 26646b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/RegularizedLeastSquares.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ See also [`isapplicable`](@ref), [`linearSolverList`](@ref).
"""
applicableSolverList(args...) = filter(solver -> isapplicable(solver, args...), linearSolverListReal())

function filterKwargs(T::Type, kwargs)
table = methods(T)
keywords = union(Base.kwarg_decl.(table)...)
filtered = filter(in(keywords), keys(kwargs))

if length(filtered) < length(kwargs)
filteredout = filter(!in(keywords), keys(kwargs))
@warn "The following arguments were passed but filtered out: $(join(filteredout, ", ")). Please watch closely if this introduces unexpexted behaviour in your code."
end

return [key=>kwargs[key] for key in filtered]
end

"""
createLinearSolver(solver::AbstractLinearSolver, A; kargs...)
Expand All @@ -247,18 +260,12 @@ regularized linear systems. All solvers return an approximate solution to Ax = b
TODO: give a hint what solvers are available
"""
function createLinearSolver(solver::Type{T}, A; kargs...) where {T<:AbstractLinearSolver}
table = methods(T)
keywords = union(Base.kwarg_decl.(table)...)
filtered = filter(in(keywords), keys(kargs))
return solver(A; [key=>kargs[key] for key in filtered]...)
function createLinearSolver(solver::Type{T}, A; kwargs...) where {T<:AbstractLinearSolver}
return solver(A; filterKwargs(T, kwargs)...)
end

function createLinearSolver(solver::Type{T}; AHA, kargs...) where {T<:AbstractLinearSolver}
table = methods(T)
keywords = union(Base.kwarg_decl.(table)...)
filtered = filter(in(keywords), keys(kargs))
return solver(; [key=>kargs[key] for key in filtered]..., AHA = AHA)
function createLinearSolver(solver::Type{T}; AHA, kwargs...) where {T<:AbstractLinearSolver}
return solver(; filterKwargs(T, kwargs)..., AHA = AHA)
end

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using RegularizedLeastSquares, LinearAlgebra, RegularizedLeastSquares.LinearOper
using Random, Test
using FFTW

include("testCreation.jl")
include("testKaczmarz.jl")
include("testProxMaps.jl")
include("testSolvers.jl")
Expand Down
3 changes: 3 additions & 0 deletions test/testCreation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@testset "Creation of solvers" begin
@test_logs (:warn, Regex("The following arguments were passed but filtered out: testKwarg*")) createLinearSolver(Kaczmarz, zeros(42, 42), testKwarg=1337)
end

0 comments on commit 26646b3

Please sign in to comment.