From 11ebab929e78cd9e4282c4c6fc6bffb177c2d816 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 20 Aug 2026 01:22:50 +0100 Subject: [PATCH 1/4] Add `Draw` struct --- HISTORY.md | 11 +++++++++++ Project.toml | 2 +- src/VarNames.jl | 5 +++++ src/draw.jl | 32 ++++++++++++++++++++++++++++++++ test/draw.jl | 16 ++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 HISTORY.md create mode 100644 src/draw.jl create mode 100644 test/draw.jl diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 0000000..6d1b32a --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,11 @@ +# 0.1.1 + +Added the `Draw` struct, which bundles a `VarNamedTuple` of parameter values with a `NamedTuple` of extra information, as well as the accessor functions `parameters` and `extras`. +This is intended to represent a single MCMC sample. + +`Draw` is structurally the same as what used to be called `ParamsWithStats` in DynamicPPL. + +# 0.1.0 + +Initial release. +This is a direct port of code from AbstractPPL and DynamicPPL, with one major change: `@vn` is now an alias for `@varname`, which is hopefully helpful since it gets used quite a lot. diff --git a/Project.toml b/Project.toml index 3a706a8..4072f2e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "VarNames" uuid = "09a315b2-63fc-4165-8d7f-d59368e770cc" -version = "0.1.0" +version = "0.1.1" authors = ["Penelope Yong ", "Markus Hauru "] [deps] diff --git a/src/VarNames.jl b/src/VarNames.jl index 7c5ab57..0d1cfe2 100644 --- a/src/VarNames.jl +++ b/src/VarNames.jl @@ -70,6 +70,11 @@ export VarNamedTuple, SkipTemplate, @vnt +include("draw.jl") +export Draw +export parameters +export extras + using BangBang: push!!, empty!!, setindex!! export push!!, empty!!, setindex!! diff --git a/src/draw.jl b/src/draw.jl new file mode 100644 index 0000000..55f95e2 --- /dev/null +++ b/src/draw.jl @@ -0,0 +1,32 @@ +""" + Draw{P<:VarNamedTuple,S<:NamedTuple} + +A struct that bundles a set of parameter values stored as a `VarNamedTuple`, along with a +`NamedTuple` of extra information associated with this set of values (for example, this can +be used to store log-probability values, etc.). + +In VarNames.jl, this struct is not used at all; however, it is defined here so that other +packages can dispatch on it easily. +""" +struct Draw{P<:VarNamedTuple,S<:NamedTuple} + parameters::P + extras::S +end + +""" + VarNames.parameters(draw::Draw)::VarNamedTuple + +Returns the parameters of the draw as a `VarNamedTuple`. +""" +function parameters(draw::Draw) + return draw.parameters +end + +""" + VarNames.extras(draw::Draw)::NamedTuple + +Returns the extra information of the draw as a `NamedTuple`. +""" +function extras(draw::Draw) + return draw.extras +end diff --git a/test/draw.jl b/test/draw.jl new file mode 100644 index 0000000..5f3f257 --- /dev/null +++ b/test/draw.jl @@ -0,0 +1,16 @@ +module DrawTests + +using VarNames +using Test + +@testset "draw" begin + # Since it's just a bare struct, just test its API. + vnt = VarNamedTuple(x = 0.5, y = randn(3)) + nt = (a = :hello, logjoint = randn()) + d = Draw(vnt, nt) + + @test parameters(d) == vnt + @test extras(d) == nt +end + +end # module DrawTests From 8e6939b3ffd15685ff4373df4626a518042c98a2 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 20 Aug 2026 01:28:49 +0100 Subject: [PATCH 2/4] Actually run the tests --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 05fbd3e..da996e2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,4 +9,5 @@ using Test include("varname/leaves.jl") include("varname/serialize.jl") include("varnamedtuple.jl") + include("draw.jl") end From ed70a434f1afc0fa0ae78128cba74b0bccc05fcf Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 20 Aug 2026 01:29:43 +0100 Subject: [PATCH 3/4] Document --- docs/src/vnt/api.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/src/vnt/api.md b/docs/src/vnt/api.md index a9b477d..73470f3 100644 --- a/docs/src/vnt/api.md +++ b/docs/src/vnt/api.md @@ -16,3 +16,9 @@ templated_setindex!! densify!! skeleton ``` + +```@docs +Draw +parameters +extras +``` From 57d1425214d7b83afa50653f709f5f720831c927 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 20 Aug 2026 01:30:05 +0100 Subject: [PATCH 4/4] Format --- test/draw.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/draw.jl b/test/draw.jl index 5f3f257..4f9da41 100644 --- a/test/draw.jl +++ b/test/draw.jl @@ -5,8 +5,8 @@ using Test @testset "draw" begin # Since it's just a bare struct, just test its API. - vnt = VarNamedTuple(x = 0.5, y = randn(3)) - nt = (a = :hello, logjoint = randn()) + vnt = VarNamedTuple(x=0.5, y=randn(3)) + nt = (a=:hello, logjoint=randn()) d = Draw(vnt, nt) @test parameters(d) == vnt