Implementation of varname_and_value_leaves
that can handle containers
#674
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We often find ourselves in situations where we need to flatten both varnames and values together to produce a "simple" map
varname => value
wherevalue isa Real
rather than aVector
orNamedTuple
, e.g. in construction ofMCMCChains.Chains
.For this we have the function
varname_and_value_leaves(vn, x)
which takes in aVarName
and some valuex
, e.g. aVector
, and returns an iterator over pairs ofVarName
andReal
as desired:DynamicPPL.jl/src/utils.jl
Lines 970 to 985 in 0e40bd0
But very often we actually want to do this for a container of multiple
VarName
s, e.g.OrderedDict
orNamedTuple
. Currently we just do this "by hand" every time we need this, but that seems unnecessary.Sooo this PR just adds an implementation for
OrderedDict
andNamedTuple
(which are the most common containers we work with, though the impl is very trivial) so we don't have to do this by hand everywhere.Ref: #663 where we would benefit from this in the tests.