Skip to content

Commit

Permalink
Make Test a weak dependency (#30)
Browse files Browse the repository at this point in the history
* Make Test a weak dependency

* Fix typo

* Import `logabsdet`
  • Loading branch information
devmotion authored Aug 20, 2024
1 parent 9101502 commit 7442802
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 54 deletions.
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.

2 comments on commit 7442802

@devmotion
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/113535

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.1.9 -m "<description of version>" 7442802dce509a59cc33214966c7c7e25e6c36c0
git push origin v0.1.9

Please sign in to comment.