From b805b453f1a60fd1d0554e61333aeeec870c2c94 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Wed, 21 Jul 2021 21:04:30 +0100 Subject: [PATCH 1/4] deprecate rand_tangent --- src/FiniteDifferences.jl | 2 +- src/{rand_tangent.jl => deprecated.jl} | 36 +++++++++++++++++-------- test/{rand_tangent.jl => deprecated.jl} | 9 ++++--- test/runtests.jl | 2 +- 4 files changed, 33 insertions(+), 16 deletions(-) rename src/{rand_tangent.jl => deprecated.jl} (64%) rename test/{rand_tangent.jl => deprecated.jl} (93%) diff --git a/src/FiniteDifferences.jl b/src/FiniteDifferences.jl index b943030..0a44f84 100644 --- a/src/FiniteDifferences.jl +++ b/src/FiniteDifferences.jl @@ -9,7 +9,7 @@ using StaticArrays export to_vec, grad, jacobian, jvp, j′vp -include("rand_tangent.jl") +include("deprecated.jl") include("methods.jl") include("numerics.jl") include("to_vec.jl") diff --git a/src/rand_tangent.jl b/src/deprecated.jl similarity index 64% rename from src/rand_tangent.jl rename to src/deprecated.jl index adb0ac8..cf10a91 100644 --- a/src/rand_tangent.jl +++ b/src/deprecated.jl @@ -1,3 +1,10 @@ +function depwarn_rt() + Base.depwarn( + "FiniteDifferences.rand_tangent is deprecated, it has moved to ChainRulesTestUtils", + :rand_tangent + ) +end + """ rand_tangent([rng::AbstractRNG,] x) @@ -7,27 +14,33 @@ Rather it is an arbitary value, that is generated using the `rng`. """ rand_tangent(x) = rand_tangent(Random.GLOBAL_RNG, x) -rand_tangent(rng::AbstractRNG, x::Symbol) = NoTangent() -rand_tangent(rng::AbstractRNG, x::AbstractChar) = NoTangent() -rand_tangent(rng::AbstractRNG, x::AbstractString) = NoTangent() +rand_tangent(rng::AbstractRNG, x::Symbol) = (depwarn_rt(); NoTangent()) +rand_tangent(rng::AbstractRNG, x::AbstractChar) = (depwarn_rt(); NoTangent()) +rand_tangent(rng::AbstractRNG, x::AbstractString) = (depwarn_rt(); NoTangent()) -rand_tangent(rng::AbstractRNG, x::Integer) = NoTangent() +rand_tangent(rng::AbstractRNG, x::Integer) = (depwarn_rt(); NoTangent()) # Try and make nice numbers with short decimal representations for good error messages # while also not biasing the sample space too much function rand_tangent(rng::AbstractRNG, x::T) where {T<:Number} - # multiply by 9 to give a bigger range of values tested: no so tightly clustered around 0. + depwarn_rt() + # multiply by 9 to give a bigger range of values tested: + # not so tightly clustered around 0. return round(9 * randn(rng, T), sigdigits=5, base=2) end -rand_tangent(rng::AbstractRNG, x::Float64) = rand(rng, -9:0.01:9) +rand_tangent(rng::AbstractRNG, x::Float64) = (depwarn_rt(); rand(rng, -9:0.01:9)) function rand_tangent(rng::AbstractRNG, x::ComplexF64) + depwarn_rt() return ComplexF64(rand(rng, -9:0.1:9), rand(rng, -9:0.1:9)) end #BigFloat/MPFR is finicky about short numbers, this doesn't always work as well as it should - -# multiply by 9 to give a bigger range of values tested: no so tightly clustered around 0. -rand_tangent(rng::AbstractRNG, ::BigFloat) = round(big(9 * randn(rng)), sigdigits=5, base=2) +function rand_tangent(rng::AbstractRNG, ::BigFloat) + depwarn_rt() + # multiply by 9 to give a bigger range of values tested: + # not so tightly clustered around 0. + return round(big(9 * randn(rng)), sigdigits=5, base=2) +end rand_tangent(rng::AbstractRNG, x::StridedArray{T, 0}) where {T} = fill(rand_tangent(x[1])) rand_tangent(rng::AbstractRNG, x::StridedArray) = rand_tangent.(Ref(rng), x) @@ -53,11 +66,12 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T} end if all(tangent isa NoTangent for tangent in tangents) # if none of my fields can be perturbed then I can't be perturbed + depwarn_rt() return NoTangent() else Tangent{T}(; NamedTuple{field_names}(tangents)...) end end -rand_tangent(rng::AbstractRNG, ::Type) = NoTangent() -rand_tangent(rng::AbstractRNG, ::Module) = NoTangent() +rand_tangent(rng::AbstractRNG, ::Type) = (depwarn_rt(); NoTangent()) +rand_tangent(rng::AbstractRNG, ::Module) = (depwarn_rt(); NoTangent()) diff --git a/test/rand_tangent.jl b/test/deprecated.jl similarity index 93% rename from test/rand_tangent.jl rename to test/deprecated.jl index 6020286..767c52e 100644 --- a/test/rand_tangent.jl +++ b/test/deprecated.jl @@ -1,6 +1,7 @@ -using FiniteDifferences: rand_tangent -@testset "generate_tangent" begin +rand_tangent(args...) = @test_deprecated FiniteDifferences.rand_tangent(args...) + +@testset "rand_tangent" begin rng = MersenneTwister(123456) @testset "Primal: $(typeof(x)), Tangent: $T_tangent" for (x, T_tangent) in [ @@ -89,7 +90,9 @@ using FiniteDifferences: rand_tangent @testset "erroring cases" begin # Ensure struct fallback errors for non-struct types. - @test_throws ArgumentError invoke(rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0) + @test_throws ArgumentError invoke( + FiniteDifferences.rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0 + ) end @testset "compsition of addition" begin diff --git a/test/runtests.jl b/test/runtests.jl index eaec87c..03cd765 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -17,7 +17,7 @@ end Random.seed!(1) @testset "FiniteDifferences" begin - include("rand_tangent.jl") + include("deprecated.jl") include("methods.jl") include("numerics.jl") include("to_vec.jl") From be88aa8bdc00d3c26e47727ce64af9d408d1b899 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Wed, 21 Jul 2021 21:16:55 +0100 Subject: [PATCH 2/4] :wqemove unused test struct --- test/deprecated.jl | 7 ++++++- test/runtests.jl | 6 ------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/deprecated.jl b/test/deprecated.jl index 767c52e..13c49f2 100644 --- a/test/deprecated.jl +++ b/test/deprecated.jl @@ -1,4 +1,9 @@ - +# Test struct for `rand_tangent` and `difference`. +struct Foo + a::Float64 + b::Int + c::Any + end rand_tangent(args...) = @test_deprecated FiniteDifferences.rand_tangent(args...) @testset "rand_tangent" begin diff --git a/test/runtests.jl b/test/runtests.jl index 03cd765..e0b776a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,12 +7,6 @@ using Random using StaticArrays using Test -# Test struct for `rand_tangent` and `difference`. -struct Foo - a::Float64 - b::Int - c::Any -end Random.seed!(1) From b435be47d071fb4fc36c7d2f690cb7bd03df56ec Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Thu, 22 Jul 2021 15:47:48 +0100 Subject: [PATCH 3/4] Comment wrapper function --- test/deprecated.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/deprecated.jl b/test/deprecated.jl index 13c49f2..7ca97e2 100644 --- a/test/deprecated.jl +++ b/test/deprecated.jl @@ -4,6 +4,8 @@ struct Foo b::Int c::Any end + +# to avoid deprecation spam (and actually test deprecations) we will define a wrapper `rand_tangent` function for testing rand_tangent(args...) = @test_deprecated FiniteDifferences.rand_tangent(args...) @testset "rand_tangent" begin From 9882fd16e19fae22733becca54f5df0fbf0081ff Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Thu, 22 Jul 2021 15:48:25 +0100 Subject: [PATCH 4/4] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5d55036..2410f48 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FiniteDifferences" uuid = "26cc04aa-876d-5657-8c51-4c34ba976000" -version = "0.12.15" +version = "0.12.16" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"