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

Make Test a weak dependency #30

Merged
merged 3 commits into from
Aug 20, 2024
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
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChangesOfVariables"
uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
version = "0.1.8"
version = "0.1.9"

[deps]
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
Expand All @@ -9,19 +9,23 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[weakdeps]
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[extensions]
ChangesOfVariablesInverseFunctionsExt = "InverseFunctions"
ChangesOfVariablesTestExt = "Test"

[compat]
InverseFunctions = "0.1"
LinearAlgebra = "<0.0.1, 1"
Test = "<0.0.1, 1"
julia = "1"

[extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Documenter", "InverseFunctions", "ForwardDiff"]
test = ["Documenter", "InverseFunctions", "ForwardDiff", "Test"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ changes for functions that perform a change of variables (like coordinate
transformations).

`ChangesOfVariables` is a very lightweight package and has no dependencies
beyond `Base`, `LinearAlgebra` and `Test` (plus a weak depdendency on
`InverseFunctions`).
beyond `Base` and `LinearAlgebra` (plus a weak dependency on `InverseFunctions`
and `Test`).

## Documentation

Expand Down
31 changes: 31 additions & 0 deletions ext/ChangesOfVariablesTestExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module ChangesOfVariablesTestExt

using Test: @inferred, @test, @testset
using ChangesOfVariables: ChangesOfVariables, logabsdet, with_logabsdet_jacobian

function ChangesOfVariables.test_with_logabsdet_jacobian(f, x, getjacobian; compare = isapprox, kwargs...)
@testset "test_with_logabsdet_jacobian: $f with input $x" begin
ref_y, test_type_inference = try
@inferred(f(x)), true
catch err
f(x), false
end

y, ladj = if test_type_inference
@inferred with_logabsdet_jacobian(f, x)
else
with_logabsdet_jacobian(f, x)
end

ref_ladj = _generalized_logabsdet(getjacobian(f, x))[1]

@test compare(y, ref_y; kwargs...)
@test compare(ladj, ref_ladj; kwargs...)
end
return nothing
end

_generalized_logabsdet(A) = logabsdet(A)
_generalized_logabsdet(x::Real) = log(abs(x))

end # module
41 changes: 39 additions & 2 deletions src/ChangesOfVariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,51 @@ transformations).
module ChangesOfVariables

using LinearAlgebra
using Test

include("with_ladj.jl")
include("setladj.jl")
include("test.jl")

"""
ChangesOfVariables.test_with_logabsdet_jacobian(f, x, getjacobian; compare = isapprox, kwargs...)

Test if [`with_logabsdet_jacobian(f, x)`](@ref) is implemented correctly.

Checks if the result of `with_logabsdet_jacobian(f, x)` is approximately
equal to `(f(x), logabsdet(getjacobian(f, x)))`

So the test uses `getjacobian(f, x)` to calculate a reference Jacobian for
`f` at `x`. Passing `ForwardDiff.jabobian`, `Zygote.jacobian` or similar as
the `getjacobian` function will do fine in most cases. If input and output
of `f` are real scalar values, use `ForwardDiff.derivative`.

Note that the result of `getjacobian(f, x)` must be a real-valued matrix
or a real scalar, so you may need to use a custom `getjacobian` function
that transforms the shape of `x` and `f(x)` internally, in conjunction
with automatic differentiation.

`kwargs...` are forwarded to `compare`.

!!! Note
On Julia >= 1.9, you have to load the `Test` standard library to be able to use
this function.
"""
function test_with_logabsdet_jacobian end

@static if !isdefined(Base, :get_extension)
include("../ext/ChangesOfVariablesInverseFunctionsExt.jl")
include("../ext/ChangesOfVariablesTestExt.jl")
end

# Better error message if users forget to load Test
if isdefined(Base, :get_extension) && isdefined(Base.Experimental, :register_error_hint)
function __init__()
Base.Experimental.register_error_hint(MethodError) do io, exc, _, _
if exc.f === test_with_logabsdet_jacobian &&
(Base.get_extension(ChangesOfVariables, :ChangesOfVariablesTest) === nothing)
print(io, "\nDid you forget to load Test?")
end
end
end
end

end # module
48 changes: 0 additions & 48 deletions src/test.jl

This file was deleted.

Loading