Skip to content

Commit 0b6e364

Browse files
authored
Fix varinfo[:] for empty varinfo (#926)
* Fix varinfo[:] for empty varinfo * Update changelog, bump patch * Function no longer mutates, remove !! * No need to empty!! if it's not mutated * `vi` escapes the scope of the function... * Warn in check_model if model is empty * Fix doctest
1 parent 459e041 commit 0b6e364

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# DynamicPPL Changelog
22

3+
## 0.36.5
4+
5+
`varinfo[:]` now returns an empty vector if `varinfo::DynamicPPL.NTVarInfo` is empty, rather than erroring.
6+
7+
In its place, `check_model` now issues a warning if the model is empty.
8+
39
## 0.36.4
410

511
Added compatibility with DifferentiationInterface.jl 0.7, and also with JET.jl 0.10.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.36.4"
3+
version = "0.36.5"
44

55
[deps]
66
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

src/debug_utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ function conditioned_varnames(context)
338338
end
339339

340340
function check_varnames_seen(varnames_seen::AbstractDict{VarName,Int})
341+
if isempty(varnames_seen)
342+
@warn "The model does not contain any parameters."
343+
return true
344+
end
345+
341346
issuccess = true
342347
for (varname, count) in varnames_seen
343348
if count == 0
@@ -416,6 +421,8 @@ julia> print(trace)
416421
assume: x ~ Normal{Float64}(μ=0.0, σ=1.0) ⟼ -0.670252 (logprob = -1.14356)
417422
418423
julia> issuccess, trace = check_model_and_trace(rng, demo_correct() | (x = 1.0,));
424+
┌ Warning: The model does not contain any parameters.
425+
└ @ DynamicPPL.DebugUtils DynamicPPL.jl/src/debug_utils.jl:342
419426
420427
julia> issuccess
421428
true

src/varinfo.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@ getindex_internal(vi::VarInfo, ::Colon) = getindex_internal(vi.metadata, Colon()
854854
function getindex_internal(vi::NTVarInfo, ::Colon)
855855
return reduce(vcat, map(Base.Fix2(getindex_internal, Colon()), vi.metadata))
856856
end
857+
function getindex_internal(vi::VarInfo{NamedTuple{(),Tuple{}}}, ::Colon)
858+
return float(Real)[]
859+
end
857860
function getindex_internal(md::Metadata, ::Colon)
858861
return mapreduce(
859862
Base.Fix1(getindex_internal, md), vcat, md.vns; init=similar(md.vals, 0)

test/varinfo.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ end
7878
@test vn2 == vn1
7979
@test hash(vn2) == hash(vn1)
8080

81-
function test_base!!(vi_original)
82-
vi = empty!!(vi_original)
81+
function test_base(vi_original)
82+
vi = deepcopy(vi_original)
8383
@test getlogp(vi) == 0
8484
@test isempty(vi[:])
8585

@@ -97,8 +97,10 @@ end
9797

9898
@test length(vi[vn]) == 1
9999
@test vi[vn] == r
100+
@test vi[:] == [r]
100101
vi = DynamicPPL.setindex!!(vi, 2 * r, vn)
101102
@test vi[vn] == 2 * r
103+
@test vi[:] == [2 * r]
102104

103105
# TODO(mhauru) Implement these functions for other VarInfo types too.
104106
if vi isa DynamicPPL.UntypedVectorVarInfo
@@ -113,12 +115,11 @@ end
113115
@test ~isempty(vi)
114116
end
115117

116-
vi = VarInfo()
117-
test_base!!(vi)
118-
test_base!!(DynamicPPL.typed_varinfo(vi))
119-
test_base!!(SimpleVarInfo())
120-
test_base!!(SimpleVarInfo(Dict()))
121-
test_base!!(SimpleVarInfo(DynamicPPL.VarNamedVector()))
118+
test_base(VarInfo())
119+
test_base(DynamicPPL.typed_varinfo(VarInfo()))
120+
test_base(SimpleVarInfo())
121+
test_base(SimpleVarInfo(Dict()))
122+
test_base(SimpleVarInfo(DynamicPPL.VarNamedVector()))
122123
end
123124

124125
@testset "get/set/acc/resetlogp" begin

0 commit comments

Comments
 (0)