Skip to content

Commit e9a4f1f

Browse files
committed
Improve loadstate implementation
1 parent 39e4d28 commit e9a4f1f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/mcmc/abstractmcmc.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ function AbstractMCMC.sample(
136136
)
137137
end
138138

139-
loadstate(c::MCMCChains.Chains) = c.info.samplerstate
139+
function loadstate(chain::MCMCChains.Chains)
140+
if !haskey(chain.info, :samplerstate)
141+
throw(
142+
ArgumentError(
143+
"the chain object does not contain the final state of the sampler; to save the final state you must sample with `save_state=true`",
144+
),
145+
)
146+
end
147+
return chain.info[:samplerstate]
148+
end
140149

141150
# TODO(penelopeysm): Remove initialstep and generalise MCMC sampling procedures
142151
function initialstep end

test/mcmc/hmc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ using Turing
236236
10;
237237
nadapts=0,
238238
discard_adapt=false,
239-
initial_state=chn1.info.samplerstate,
239+
initial_state=loadstate(chn1),
240240
)
241241
# if chn2 uses initial_state, its first sample should be somewhere around 5. if
242242
# initial_state isn't used, it will be sampled from [-2, 2] so this test should fail

0 commit comments

Comments
 (0)