@@ -1498,42 +1498,6 @@ function islinked(vi::VarInfo)
14981498 return any (istrans (vi, vn) for vn in keys (vi))
14991499end
15001500
1501- function nested_setindex_maybe! (vi:: UntypedVarInfo , val, vn:: VarName )
1502- return _nested_setindex_maybe! (vi, getmetadata (vi, vn), val, vn)
1503- end
1504- function nested_setindex_maybe! (
1505- vi:: VarInfo{<:NamedTuple{names}} , val, vn:: VarName{sym}
1506- ) where {names,sym}
1507- return if sym in names
1508- _nested_setindex_maybe! (vi, getmetadata (vi, vn), val, vn)
1509- else
1510- nothing
1511- end
1512- end
1513- function _nested_setindex_maybe! (
1514- vi:: VarInfo , md:: Union{Metadata,VarNamedVector} , val, vn:: VarName
1515- )
1516- # If `vn` is in `vns`, then we can just use the standard `setindex!`.
1517- vns = Base. keys (md)
1518- if vn in vns
1519- setindex! (vi, val, vn)
1520- return vn
1521- end
1522-
1523- # Otherwise, we need to check if either of the `vns` subsumes `vn`.
1524- i = findfirst (Base. Fix2 (subsumes, vn), vns)
1525- i === nothing && return nothing
1526-
1527- vn_parent = vns[i]
1528- val_parent = getindex (vi, vn_parent) # TODO : Ensure that we're working with a view here.
1529- # Split the varname into its tail optic.
1530- optic = remove_parent_optic (vn_parent, vn)
1531- # Update the value for the parent.
1532- val_parent_updated = set!! (val_parent, optic, val)
1533- setindex! (vi, val_parent_updated, vn_parent)
1534- return vn_parent
1535- end
1536-
15371501# The default getindex & setindex!() for get & set values
15381502# NOTE: vi[vn] will always transform the variable to its original space and Julia type
15391503function getindex (vi:: VarInfo , vn:: VarName )
@@ -1978,113 +1942,6 @@ function _setval_kernel!(vi::VarInfoOrThreadSafeVarInfo, vn::VarName, values, ke
19781942 return indices
19791943end
19801944
1981- """
1982- setval_and_resample!(vi::VarInfo, x)
1983- setval_and_resample!(vi::VarInfo, values, keys)
1984- setval_and_resample!(vi::VarInfo, chains::AbstractChains, sample_idx, chain_idx)
1985-
1986- Set the values in `vi` to the provided values and those which are not present
1987- in `x` or `chains` to *be* resampled.
1988-
1989- Note that this does *not* resample the values not provided! It will call
1990- `setflag!(vi, vn, "del")` for variables `vn` for which no values are provided, which means
1991- that the next time we call `model(vi)` these variables will be resampled.
1992-
1993- ## Note
1994- - This suffers from the same limitations as [`setval!`](@ref). See `setval!` for more info.
1995-
1996- ## Example
1997- ```jldoctest
1998- julia> using DynamicPPL, Distributions, StableRNGs
1999-
2000- julia> @model function demo(x)
2001- m ~ Normal()
2002- for i in eachindex(x)
2003- x[i] ~ Normal(m, 1)
2004- end
2005- end;
2006-
2007- julia> rng = StableRNG(42);
2008-
2009- julia> m = demo([missing]);
2010-
2011- julia> var_info = DynamicPPL.VarInfo(rng, m);
2012- # Checking the setting of "del" flags only makes sense for VarInfo{<:Metadata}. For VarInfo{<:VarNamedVector} the flag is effectively always set.
2013-
2014- julia> var_info[@varname(m)]
2015- -0.6702516921145671
2016-
2017- julia> var_info[@varname(x[1])]
2018- -0.22312984965118443
2019-
2020- julia> DynamicPPL.setval_and_resample!(var_info, (m = 100.0, )); # set `m` and ready `x[1]` for resampling
2021-
2022- julia> var_info[@varname(m)] # [✓] changed
2023- 100.0
2024-
2025- julia> var_info[@varname(x[1])] # [✓] unchanged
2026- -0.22312984965118443
2027-
2028- julia> m(rng, var_info); # sample `x[1]` conditioned on `m = 100.0`
2029-
2030- julia> var_info[@varname(m)] # [✓] unchanged
2031- 100.0
2032-
2033- julia> var_info[@varname(x[1])] # [✓] changed
2034- 101.37363069798343
2035- ```
2036-
2037- ## See also
2038- - [`setval!`](@ref)
2039- """
2040- function setval_and_resample! (vi:: VarInfoOrThreadSafeVarInfo , x)
2041- return setval_and_resample! (vi, values (x), keys (x))
2042- end
2043- function setval_and_resample! (vi:: VarInfoOrThreadSafeVarInfo , values, keys)
2044- return _apply! (_setval_and_resample_kernel!, vi, values, keys)
2045- end
2046- function setval_and_resample! (
2047- vi:: VarInfoOrThreadSafeVarInfo , chains:: AbstractChains , sample_idx:: Int , chain_idx:: Int
2048- )
2049- if supports_varname_indexing (chains)
2050- # First we need to set every variable to be resampled.
2051- for vn in keys (vi)
2052- set_flag! (vi, vn, " del" )
2053- end
2054- # Then we set the variables in `varinfo` from `chain`.
2055- for vn in varnames (chains)
2056- vn_updated = nested_setindex_maybe! (
2057- vi, getindex_varname (chains, sample_idx, vn, chain_idx), vn
2058- )
2059-
2060- # Unset the `del` flag if we found something.
2061- if vn_updated != = nothing
2062- # NOTE: This will be triggered even if only a subset of a variable has been set!
2063- unset_flag! (vi, vn_updated, " del" )
2064- end
2065- end
2066- else
2067- setval_and_resample! (vi, chains. value[sample_idx, :, chain_idx], keys (chains))
2068- end
2069- end
2070-
2071- function _setval_and_resample_kernel! (
2072- vi:: VarInfoOrThreadSafeVarInfo , vn:: VarName , values, keys
2073- )
2074- indices = findall (Base. Fix1 (subsumes_string, string (vn)), keys)
2075- if ! isempty (indices)
2076- val = reduce (vcat, values[indices])
2077- setval! (vi, val, vn)
2078- settrans!! (vi, false , vn)
2079- else
2080- # Ensures that we'll resample the variable corresponding to `vn` if we run
2081- # the model on `vi` again.
2082- set_flag! (vi, vn, " del" )
2083- end
2084-
2085- return indices
2086- end
2087-
20881945values_as (vi:: VarInfo ) = vi. metadata
20891946values_as (vi:: VarInfo , :: Type{Vector} ) = copy (getindex_internal (vi, Colon ()))
20901947function values_as (vi:: UntypedVarInfo , :: Type{NamedTuple} )
0 commit comments