Skip to content

Commit 5a902b0

Browse files
committed
Document
1 parent a736e70 commit 5a902b0

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[deps]
2+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
23
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using Documenter
22
using AbstractPPL
3+
using Distributions
34

45
# Doctest setup
56
DocMeta.setdocmeta!(AbstractPPL, :DocTestSetup, :(using AbstractPPL); recursive=true)
67

78
makedocs(;
89
sitename="AbstractPPL",
9-
modules=[AbstractPPL],
10+
modules=[AbstractPPL, Base.get_extension(AbstractPPL, :AbstractPPLDistributionsExt)],
1011
pages=["Home" => "index.md", "API" => "api.md"],
1112
checkdocs=:exports,
1213
doctest=false,

docs/src/api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ prefix
2121
unprefix
2222
```
2323

24+
## Extracting values corresponding to a VarName
25+
26+
```@docs
27+
hasvalue
28+
getvalue
29+
```
30+
2431
## VarName serialisation
2532

2633
```@docs

ext/AbstractPPLDistributionsExt.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ function make_empty_value(dist::Distributions.LKJCholesky)
7878
end
7979
end
8080

81+
"""
82+
hasvalue(
83+
vals::AbstractDict,
84+
vn::VarName,
85+
dist::Distribution;
86+
error_on_incomplete::Bool=false
87+
)
88+
89+
Check if `vals` contains values for `vn` that is compatible with the
90+
distribution `dist`.
91+
92+
This is a more general version of `hasvalue(vals, vn)`, in that even if
93+
`vn` itself is not inside `vals`, it further checks if `vals` contains
94+
sub-values of `vn` that can be used to reconstruct `vn` given `dist`.
95+
96+
The `error_on_incomplete` flag can be used to detect cases where _some_ of
97+
the values needed for `vn` are present, but others are not. This may help
98+
to detect invalid cases where the user has provided e.g. data of the wrong
99+
shape.
100+
101+
For example:
102+
103+
```jldoctest; setup=:(using Distributions, LinearAlgebra))
104+
julia> d = Dict(@varname(x[1]) => 1.0, @varname(x[2]) => 2.0);
105+
106+
julia> hasvalue(d, @varname(x), MvNormal(zeros(2), I))
107+
true
108+
109+
julia> hasvalue(d, @varname(x), MvNormal(zeros(3), I))
110+
false
111+
112+
julia> hasvalue(d, @varname(x), MvNormal(zeros(3), I); error_on_incomplete=true)
113+
ERROR: hasvalue: only partial values for `x` found in the values provided
114+
[...]
115+
```
116+
"""
81117
# TODO(penelopeysm): Figure out tuple / namedtuple distributions, and LKJCholesky (grr)
82118
function AbstractPPL.hasvalue(
83119
vals::AbstractDict,
@@ -133,6 +169,32 @@ function AbstractPPL.hasvalue(
133169
end
134170
end
135171

172+
"""
173+
getvalue(vals::AbstractDict, vn::VarName, dist::Distribution)
174+
175+
Retrieve the value of `vn` from `vals`, using the distribution `dist` to
176+
reconstruct the value if necessary.
177+
178+
This is a more general version of `getvalue(vals, vn)`, in that even if `vn`
179+
itself is not inside `vals`, it can still reconstruct the value of `vn`
180+
from sub-values of `vn` that are present in `vals`.
181+
182+
For example:
183+
184+
```jldoctest; setup=:(using Distributions, LinearAlgebra))
185+
julia> d = Dict(@varname(x[1]) => 1.0, @varname(x[2]) => 2.0);
186+
187+
julia> getvalue(d, @varname(x), MvNormal(zeros(2), I))
188+
2-element Vector{Float64}:
189+
1.0
190+
2.0
191+
192+
julia> # Use `hasvalue` to check for this case before calling `getvalue`.
193+
getvalue(d, @varname(x), MvNormal(zeros(3), I))
194+
ERROR: getvalue: `x` was not found in the values provided
195+
[...]
196+
```
197+
"""
136198
function AbstractPPL.getvalue(
137199
vals::AbstractDict, vn::VarName, dist::Distributions.Distribution;
138200
)

0 commit comments

Comments
 (0)