Skip to content

Commit 281641b

Browse files
committed
better handling of empty chains
1 parent e255608 commit 281641b

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/chains.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function load_chains(
5656
ks = keys(io)
5757
chunk_ks = sort([k for k in ks if startswith(k,"chunks_")], by=k->parse(Int,k[8:end]))
5858
chunk_ks = chunk_ks[burnin_chunks>=0 ? (burnin_chunks+1:end) : (end+burnin_chunks+1:end)]
59+
chains = [[]]
5960
@showprogress (progress ? 1 : Inf) for (isfirst,k) in flagfirst(chunk_ks)
6061
if isfirst
6162
chains = read(io,k)
@@ -119,10 +120,12 @@ lastindex(c::Chain) = lastindex(c.chain)
119120
lastindex(c::Chain, d) = d==1 ? lastindex(c.chain) : error("`end` only valid in first dim of Chain")
120121
size(c::Chain) = size(c.chain)
121122
function Base.print_array(io::IO, c::Chain; indent=" ")
122-
_,cols = displaysize(io)
123-
for k in keys(c[end])
124-
str = string("$(indent)$(k) => ", repr(c[k]; context=(:limit => true)))
125-
println(io, Base._truncate_at_width_or_chars(str, cols))
123+
if !isempty(c)
124+
_,cols = displaysize(io)
125+
for k in keys(c[end])
126+
str = string("$(indent)$(k) => ", repr(c[k]; context=(:limit => true)))
127+
println(io, Base._truncate_at_width_or_chars(str, cols))
128+
end
126129
end
127130
end
128131

@@ -146,6 +149,7 @@ wrap_chains(chain::Vector) = Chain(chain)
146149
Convert a chain of batch-length-`D` fields to `D` chains of unbatched fields.
147150
"""
148151
function unbatch(chain::Chain)
152+
isempty(chain) && return [chain]
149153
D = batch_length(chain[end][:logpdf])
150154
(D==1) && return [chain]
151155
Chains(map(1:D) do I

src/sampling.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function sample_joint(
247247

248248
# initialize chains
249249
states = map(copy, repeated(rundat, nchains))
250-
if (filename != nothing) && isfile(filename) && resume
250+
if resume && (filename != nothing) && isfile(filename) && jldopen(io->haskey(io,"rundat"),filename,"r")
251251
clobber_chain = false
252252
@info "Resuming chain at $filename"
253253
chunks_index = jldopen(filename,"r") do io

0 commit comments

Comments
 (0)