Skip to content

Commit 08e4c08

Browse files
mhaurupenelopeysm
andauthored
Remove selector/space stuff (#2458)
* Remove selector stuff from ESS * Remove selector stuff from MH * Remove selector stuff from HMC * Remove selector stuff from Emcee * Remove selector stuff from IS * Add missing getspace methods * Remove selector stuff for particle methods * Fix an HMC selector bug * Code style * Fix Emcee selector bug * Fix typo in ESS tests * Fix some constructor overwrites * Remove unnecessary tests * Remove selector stuff from SGHMC * Remove drop_space and other non-longer-necessary deprecation measures * Bump minor version 0.37. Add a HISTORY.md entry * Apply suggestions from code review Co-authored-by: Penelope Yong <[email protected]> * Remove unnecessary type parameters Co-authored-by: Penelope Yong <[email protected]> * Simplify constructors in particle_mcmc.jl * Remove calls to setgid and updategid --------- Co-authored-by: Penelope Yong <[email protected]>
1 parent b97334b commit 08e4c08

File tree

19 files changed

+137
-431
lines changed

19 files changed

+137
-431
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Release 0.37.0
2+
3+
## Breaking changes
4+
5+
0.37 removes the old Gibbs constructors deprecated in 0.36.
6+
17
# Release 0.36.0
28

39
## Breaking changes

src/mcmc/Inference.jl

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,6 @@ abstract type Hamiltonian <: InferenceAlgorithm end
9191
abstract type StaticHamiltonian <: Hamiltonian end
9292
abstract type AdaptiveHamiltonian <: Hamiltonian end
9393

94-
# TODO(mhauru) Remove the below function once all the space/Selector stuff has been removed.
95-
"""
96-
drop_space(alg::InferenceAlgorithm)
97-
98-
Return an `InferenceAlgorithm` like `alg`, but with all space information removed.
99-
"""
100-
function drop_space end
101-
102-
function drop_space(sampler::Sampler)
103-
return Sampler(drop_space(sampler.alg), sampler.selector)
104-
end
105-
10694
include("repeat_sampler.jl")
10795

10896
"""
@@ -146,11 +134,6 @@ struct ExternalSampler{S<:AbstractSampler,AD<:ADTypes.AbstractADType,Unconstrain
146134
end
147135
end
148136

149-
# External samplers don't have notion of space to begin with.
150-
drop_space(x::ExternalSampler) = x
151-
152-
DynamicPPL.getspace(::ExternalSampler) = ()
153-
154137
"""
155138
requires_unconstrained_space(sampler::ExternalSampler)
156139
@@ -217,8 +200,6 @@ Algorithm for sampling from the prior.
217200
"""
218201
struct Prior <: InferenceAlgorithm end
219202

220-
drop_space(x::Prior) = x
221-
222203
function AbstractMCMC.step(
223204
rng::Random.AbstractRNG,
224205
model::DynamicPPL.Model,
@@ -592,13 +573,6 @@ include("emcee.jl")
592573
# Typing tools #
593574
################
594575

595-
for alg in (:SMC, :PG, :MH, :IS, :ESS, :Emcee)
596-
@eval DynamicPPL.getspace(::$alg{space}) where {space} = space
597-
end
598-
for alg in (:HMC, :HMCDA, :NUTS, :SGLD, :SGHMC)
599-
@eval DynamicPPL.getspace(::$alg{<:Any,space}) where {space} = space
600-
end
601-
602576
function DynamicPPL.get_matching_type(
603577
spl::Sampler{<:Union{PG,SMC}}, vi, ::Type{TV}
604578
) where {T,N,TV<:Array{T,N}}
@@ -609,6 +583,8 @@ end
609583
# Utilities #
610584
##############
611585

586+
# TODO(mhauru) Remove this once DynamicPPL has removed all its Selector stuff.
587+
DynamicPPL.getspace(::InferenceAlgorithm) = ()
612588
DynamicPPL.getspace(spl::Sampler) = getspace(spl.alg)
613589
DynamicPPL.inspace(vn::VarName, spl::Sampler) = inspace(vn, getspace(spl.alg))
614590

src/mcmc/emcee.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Foreman-Mackey, D., Hogg, D. W., Lang, D., & Goodman, J. (2013).
1313
emcee: The MCMC Hammer. Publications of the Astronomical Society of the
1414
Pacific, 125 (925), 306. https://doi.org/10.1086/670067
1515
"""
16-
struct Emcee{space,E<:AMH.Ensemble} <: InferenceAlgorithm
16+
struct Emcee{E<:AMH.Ensemble} <: InferenceAlgorithm
1717
ensemble::E
1818
end
1919

@@ -23,11 +23,9 @@ function Emcee(n_walkers::Int, stretch_length=2.0)
2323
# ensemble sampling.
2424
prop = AMH.StretchProposal(nothing, stretch_length)
2525
ensemble = AMH.Ensemble(n_walkers, prop)
26-
return Emcee{(),typeof(ensemble)}(ensemble)
26+
return Emcee{typeof(ensemble)}(ensemble)
2727
end
2828

29-
drop_space(alg::Emcee{space,E}) where {space,E} = Emcee{(),E}(alg.ensemble)
30-
3129
struct EmceeState{V<:AbstractVarInfo,S}
3230
vi::V
3331
states::S

src/mcmc/ess.jl

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ Mean
2020
│ 1 │ m │ 0.824853 │
2121
```
2222
"""
23-
struct ESS{space} <: InferenceAlgorithm end
24-
25-
ESS() = ESS{()}()
26-
ESS(space::Symbol) = ESS{(space,)}()
27-
28-
drop_space(alg::ESS) = ESS()
23+
struct ESS <: InferenceAlgorithm end
2924

3025
# always accept in the first step
3126
function DynamicPPL.initialstep(
@@ -35,7 +30,7 @@ function DynamicPPL.initialstep(
3530
vns = _getvns(vi, spl)
3631
length(vns) == 1 ||
3732
error("[ESS] does only support one variable ($(length(vns)) variables specified)")
38-
for vn in vns[1]
33+
for vn in only(vns)
3934
dist = getdist(vi, vn)
4035
EllipticalSliceSampling.isgaussian(typeof(dist)) ||
4136
error("[ESS] only supports Gaussian prior distributions")
@@ -48,7 +43,7 @@ function AbstractMCMC.step(
4843
rng::AbstractRNG, model::Model, spl::Sampler{<:ESS}, vi::AbstractVarInfo; kwargs...
4944
)
5045
# obtain previous sample
51-
f = vi[spl]
46+
f = vi[:]
5247

5348
# define previous sampler state
5449
# (do not use cache to avoid in-place sampling from prior)
@@ -129,13 +124,11 @@ function (ℓ::ESSLogLikelihood)(f::AbstractVector)
129124
end
130125

131126
function DynamicPPL.tilde_assume(
132-
rng::Random.AbstractRNG, ctx::DefaultContext, sampler::Sampler{<:ESS}, right, vn, vi
127+
rng::Random.AbstractRNG, ::DefaultContext, ::Sampler{<:ESS}, right, vn, vi
133128
)
134-
return if inspace(vn, sampler)
135-
DynamicPPL.tilde_assume(rng, LikelihoodContext(), SampleFromPrior(), right, vn, vi)
136-
else
137-
DynamicPPL.tilde_assume(rng, ctx, SampleFromPrior(), right, vn, vi)
138-
end
129+
return DynamicPPL.tilde_assume(
130+
rng, LikelihoodContext(), SampleFromPrior(), right, vn, vi
131+
)
139132
end
140133

141134
function DynamicPPL.tilde_observe(
@@ -145,22 +138,11 @@ function DynamicPPL.tilde_observe(
145138
end
146139

147140
function DynamicPPL.dot_tilde_assume(
148-
rng::Random.AbstractRNG,
149-
ctx::DefaultContext,
150-
sampler::Sampler{<:ESS},
151-
right,
152-
left,
153-
vns,
154-
vi,
141+
rng::Random.AbstractRNG, ::DefaultContext, ::Sampler{<:ESS}, right, left, vns, vi
155142
)
156-
# TODO: Or should we do `all(Base.Fix2(inspace, sampler), vns)`?
157-
return if inspace(first(vns), sampler)
158-
DynamicPPL.dot_tilde_assume(
159-
rng, LikelihoodContext(), SampleFromPrior(), right, left, vns, vi
160-
)
161-
else
162-
DynamicPPL.dot_tilde_assume(rng, ctx, SampleFromPrior(), right, left, vns, vi)
163-
end
143+
return DynamicPPL.dot_tilde_assume(
144+
rng, LikelihoodContext(), SampleFromPrior(), right, left, vns, vi
145+
)
164146
end
165147

166148
function DynamicPPL.dot_tilde_observe(

src/mcmc/gibbs.jl

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ struct Gibbs{N,V<:NTuple{N,AbstractVector{<:VarName}},A<:NTuple{N,Any}} <:
345345

346346
# Ensure that samplers have the same selector, and that varnames are lists of
347347
# VarNames.
348-
samplers = tuple(map(set_selector drop_space, samplers)...)
348+
samplers = tuple(map(set_selector, samplers)...)
349349
varnames = tuple(map(to_varname_list, varnames)...)
350350
return new{length(samplers),typeof(varnames),typeof(samplers)}(varnames, samplers)
351351
end
@@ -355,49 +355,6 @@ function Gibbs(algs::Pair...)
355355
return Gibbs(map(first, algs), map(last, algs))
356356
end
357357

358-
# The below two constructors only provide backwards compatibility with the constructor of
359-
# the old Gibbs sampler. They are deprecated and will be removed in the future.
360-
function Gibbs(alg1::InferenceAlgorithm, other_algs::InferenceAlgorithm...)
361-
algs = [alg1, other_algs...]
362-
varnames = map(algs) do alg
363-
space = getspace(alg)
364-
if (space isa VarName)
365-
space
366-
elseif (space isa Symbol)
367-
VarName{space}()
368-
else
369-
tuple((s isa Symbol ? VarName{s}() : s for s in space)...)
370-
end
371-
end
372-
msg = (
373-
"Specifying which sampler to use with which variable using syntax like " *
374-
"`Gibbs(NUTS(:x), MH(:y))` is deprecated and will be removed in the future. " *
375-
"Please use `Gibbs(; x=NUTS(), y=MH())` instead. If you want different iteration " *
376-
"counts for different subsamplers, use e.g. " *
377-
"`Gibbs(@varname(x) => RepeatSampler(NUTS(), 2), @varname(y) => MH())`"
378-
)
379-
Base.depwarn(msg, :Gibbs)
380-
return Gibbs(varnames, map(set_selector drop_space, algs))
381-
end
382-
383-
function Gibbs(
384-
alg_with_iters1::Tuple{<:InferenceAlgorithm,Int},
385-
other_algs_with_iters::Tuple{<:InferenceAlgorithm,Int}...,
386-
)
387-
algs_with_iters = [alg_with_iters1, other_algs_with_iters...]
388-
algs = Iterators.map(first, algs_with_iters)
389-
iters = Iterators.map(last, algs_with_iters)
390-
algs_duplicated = Iterators.flatten((
391-
Iterators.repeated(alg, iter) for (alg, iter) in zip(algs, iters)
392-
))
393-
# This calls the other deprecated constructor from above, hence no need for a depwarn
394-
# here.
395-
return Gibbs(algs_duplicated...)
396-
end
397-
398-
# TODO: Remove when no longer needed.
399-
DynamicPPL.getspace(::Gibbs) = ()
400-
401358
struct GibbsState{V<:DynamicPPL.AbstractVarInfo,S}
402359
vi::V
403360
states::S

0 commit comments

Comments
 (0)