Skip to content

Commit 6511882

Browse files
penelopeysmmhauru
andauthored
Move hasvalue and getvalue from DynamicPPL; implement extra Distributions-based methods (#125)
* Move hasvalue and getvalue to AbstractPPL; reimplement * Add hasvalue for (some) distributions * Bump min Julia to 1.10 * Make hasvalue and getvalue use the most specific value * Specify getvalue semantics in docstring * Simplify logic (can rely on normalisation) * Add tests for composition of head/tail and init/last * Finish implementing distributions methods * Document * Fix LinearAlgebra version bound * Try to fix documentation for extension (why is this so complicated...) * Fix extension documentation * Implement fallback {has,get}value methods for NamedTuple + Distribution * Fix wrong way round composition, add more tests * Update src/hasvalue.jl Co-authored-by: Markus Hauru <[email protected]> * Update HISTORY.md Co-authored-by: Markus Hauru <[email protected]> * Minor bump * Fix test (forgot to push this...) * Add extra example for getvalue * Tweak error message when value not found * Format (?!) * Fix doctests * Add a docstring to the extension * Add tests for colons --------- Co-authored-by: Markus Hauru <[email protected]>
1 parent 7be9556 commit 6511882

File tree

13 files changed

+1021
-45
lines changed

13 files changed

+1021
-45
lines changed

HISTORY.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
## 0.13.0
2+
3+
Minimum compatibility has been bumped to Julia 1.10.
4+
5+
Added the new functions `hasvalue(container::T, ::VarName[, ::Distribution])` and `getvalue(container::T, ::VarName[, ::Distribution])`, where `T` is either `NamedTuple` or `AbstractDict{<:VarName}`.
6+
7+
These functions check whether a given `VarName` has a value in the given `NamedTuple` or `AbstractDict`, and return the value if it exists.
8+
9+
The optional `Distribution` argument allows one to reconstruct a full value from its component indices.
10+
For example, if `container` has `x[1]` and `x[2]`, then `hasvalue(container, @varname(x), dist)` will return true if `size(dist) == (2,)` (for example, `MvNormal(zeros(2), I)`).
11+
In this case plain `hasvalue(container, @varname(x))` would return `false`, since we can not know whether the vector-valued variable `x` has all of its elements specified in `container` (there might be an `x[3]` missing).
12+
13+
These functions (without the `Distribution` argument) were previously in DynamicPPL.jl (albeit unexported).
14+
115
## 0.12.0
216

317
### VarName constructors

Project.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
33
keywords = ["probablistic programming"]
44
license = "MIT"
55
desc = "Common interfaces for probabilistic programming"
6-
version = "0.12.0"
6+
version = "0.13.0"
77

88
[deps]
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
@@ -13,11 +13,20 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1313
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1414
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1515

16+
[weakdeps]
17+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
18+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
19+
20+
[extensions]
21+
AbstractPPLDistributionsExt = ["Distributions", "LinearAlgebra"]
22+
1623
[compat]
1724
AbstractMCMC = "2, 3, 4, 5"
1825
Accessors = "0.1"
1926
DensityInterface = "0.4"
27+
Distributions = "0.25"
28+
LinearAlgebra = "<0.0.1, 1.10"
2029
JSON = "0.19 - 0.21"
2130
Random = "1.6"
2231
StatsBase = "0.32, 0.33, 0.34"
23-
julia = "~1.6.6, 1.7.3"
32+
julia = "1.10"

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[deps]
2+
AbstractPPL = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
3+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
24
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
5+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

docs/make.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using Documenter
22
using AbstractPPL
3+
# trigger DistributionsExt loading
4+
using Distributions, LinearAlgebra
35

46
# Doctest setup
57
DocMeta.setdocmeta!(AbstractPPL, :DocTestSetup, :(using AbstractPPL); recursive=true)
68

79
makedocs(;
810
sitename="AbstractPPL",
9-
modules=[AbstractPPL],
11+
modules=[AbstractPPL, Base.get_extension(AbstractPPL, :AbstractPPLDistributionsExt)],
1012
pages=["Home" => "index.md", "API" => "api.md"],
1113
checkdocs=:exports,
1214
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

0 commit comments

Comments
 (0)