Skip to content

Commit 58ebb25

Browse files
committed
Add tests for deprecated constructor
1 parent 09c739d commit 58ebb25

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/mcmc/gibbs.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ function Gibbs(algs::Pair...)
279279
return Gibbs(map(first, algs), map(wrap_algorithm_maybe, map(last, algs)))
280280
end
281281

282+
# The below constructor only serves to provide backwards compatibility with the constructor
283+
# of the old Gibbs sampler. It is deprecated and will be removed in the future.
284+
function Gibbs(algs::InferenceAlgorithm...)
285+
alg_dict = Dict{Any,InferenceAlgorithm}()
286+
for alg in algs
287+
space = getspace(alg)
288+
space_vns = if (space isa Symbol || space isa VarName)
289+
space
290+
else
291+
tuple((s isa Symbol ? VarName{s}() : s for s in space)...)
292+
end
293+
alg_dict[space_vns] = alg
294+
end
295+
Base.depwarn(
296+
"Specifying which sampler to use with which variable using syntax like `Gibbs(NUTS(:x), MH(:y))` is deprecated and will be removed in the future. Please use `Gibbs(; x=NUTS(), y=MH())` instead.",
297+
:Gibbs,
298+
)
299+
return Gibbs(alg_dict)
300+
end
301+
282302
# TODO: Remove when no longer needed.
283303
DynamicPPL.getspace(::Gibbs) = ()
284304

test/mcmc/gibbs.jl

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using DynamicPPL: DynamicPPL
1414
using ForwardDiff: ForwardDiff
1515
using Random: Random
1616
using ReverseDiff: ReverseDiff
17-
using Test: @test, @testset
17+
using Test: @test, @test_deprecated, @testset
1818
using Turing
1919
using Turing: Inference
2020
using Turing.Inference: AdvancedHMC, AdvancedMH
@@ -44,16 +44,34 @@ has_dot_assume(::DEMO_MODELS_WITHOUT_DOT_ASSUME) = false
4444
has_dot_assume(::DynamicPPL.Model) = true
4545

4646
@testset "Testing gibbs.jl with $adbackend" for adbackend in ADUtils.adbackends
47-
@testset "gibbs constructor" begin
48-
N = 500
49-
s1 = begin
50-
alg = HMC(0.1, 5, :s, :m; adtype=adbackend)
51-
Gibbs(; s=alg, m=alg)
52-
end
53-
s2 = begin
54-
alg = PG(10)
55-
Gibbs(@varname(s) => alg, @varname(m) => alg)
47+
@testset "Deprecated Gibbs constructors" begin
48+
N = 10
49+
@test_deprecated s1 = Gibbs(HMC(0.1, 5, :s, :m; adtype=adbackend))
50+
@test_deprecated s2 = Gibbs(PG(10, :s, :m))
51+
@test_deprecated s3 = Gibbs(PG(3, :s), HMC(0.4, 8, :m; adtype=adbackend))
52+
@test_deprecated s4 = Gibbs(PG(3, :s), HMC(0.4, 8, :m; adtype=adbackend))
53+
@test_deprecated s5 = Gibbs(CSMC(3, :s), HMC(0.4, 8, :m; adtype=adbackend))
54+
@test_deprecated s6 = Gibbs(HMC(0.1, 5, :s; adtype=adbackend), ESS(:m))
55+
for s in (s1, s2, s3, s4, s5, s6)
56+
@test DynamicPPL.alg_str(Turing.Sampler(s, gdemo_default)) == "Gibbs"
5657
end
58+
59+
# Check that the samplers work despite using the deprecated constructor.
60+
sample(gdemo_default, s1, N)
61+
sample(gdemo_default, s2, N)
62+
sample(gdemo_default, s3, N)
63+
sample(gdemo_default, s4, N)
64+
sample(gdemo_default, s5, N)
65+
sample(gdemo_default, s6, N)
66+
67+
g = Turing.Sampler(s3, gdemo_default)
68+
@test sample(gdemo_default, g, N) isa MCMCChains.Chains
69+
end
70+
71+
@testset "Gibbs constructors" begin
72+
N = 10
73+
s1 = Gibbs((@varname(s), @varname(m)) => HMC(0.1, 5, :s, :m; adtype=adbackend))
74+
s2 = Gibbs((@varname(s), @varname(m)) => PG(10))
5775
s3 = Gibbs((; s=PG(3), m=HMC(0.4, 8; adtype=adbackend)))
5876
s4 = Gibbs(Dict(@varname(s) => PG(3), @varname(m) => HMC(0.4, 8; adtype=adbackend)))
5977
s5 = Gibbs(; s=CSMC(3), m=HMC(0.4, 8; adtype=adbackend))

0 commit comments

Comments
 (0)