@@ -2045,113 +2045,6 @@ function _setval_kernel!(vi::VarInfoOrThreadSafeVarInfo, vn::VarName, values, ke
20452045 return indices
20462046end
20472047
2048- """
2049- setval_and_resample!(vi::VarInfo, x)
2050- setval_and_resample!(vi::VarInfo, values, keys)
2051- setval_and_resample!(vi::VarInfo, chains::AbstractChains, sample_idx, chain_idx)
2052-
2053- Set the values in `vi` to the provided values and those which are not present
2054- in `x` or `chains` to *be* resampled.
2055-
2056- Note that this does *not* resample the values not provided! It will call
2057- `setflag!(vi, vn, "del")` for variables `vn` for which no values are provided, which means
2058- that the next time we call `model(vi)` these variables will be resampled.
2059-
2060- ## Note
2061- - This suffers from the same limitations as [`setval!`](@ref). See `setval!` for more info.
2062-
2063- ## Example
2064- ```jldoctest
2065- julia> using DynamicPPL, Distributions, StableRNGs
2066-
2067- julia> @model function demo(x)
2068- m ~ Normal()
2069- for i in eachindex(x)
2070- x[i] ~ Normal(m, 1)
2071- end
2072- end;
2073-
2074- julia> rng = StableRNG(42);
2075-
2076- julia> m = demo([missing]);
2077-
2078- julia> var_info = DynamicPPL.VarInfo(rng, m);
2079- # Checking the setting of "del" flags only makes sense for VarInfo{<:Metadata}. For VarInfo{<:VarNamedVector} the flag is effectively always set.
2080-
2081- julia> var_info[@varname(m)]
2082- -0.6702516921145671
2083-
2084- julia> var_info[@varname(x[1])]
2085- -0.22312984965118443
2086-
2087- julia> DynamicPPL.setval_and_resample!(var_info, (m = 100.0, )); # set `m` and ready `x[1]` for resampling
2088-
2089- julia> var_info[@varname(m)] # [✓] changed
2090- 100.0
2091-
2092- julia> var_info[@varname(x[1])] # [✓] unchanged
2093- -0.22312984965118443
2094-
2095- julia> m(rng, var_info); # sample `x[1]` conditioned on `m = 100.0`
2096-
2097- julia> var_info[@varname(m)] # [✓] unchanged
2098- 100.0
2099-
2100- julia> var_info[@varname(x[1])] # [✓] changed
2101- 101.37363069798343
2102- ```
2103-
2104- ## See also
2105- - [`setval!`](@ref)
2106- """
2107- function setval_and_resample! (vi:: VarInfoOrThreadSafeVarInfo , x)
2108- return setval_and_resample! (vi, values (x), keys (x))
2109- end
2110- function setval_and_resample! (vi:: VarInfoOrThreadSafeVarInfo , values, keys)
2111- return _apply! (_setval_and_resample_kernel!, vi, values, keys)
2112- end
2113- function setval_and_resample! (
2114- vi:: VarInfoOrThreadSafeVarInfo , chains:: AbstractChains , sample_idx:: Int , chain_idx:: Int
2115- )
2116- if supports_varname_indexing (chains)
2117- # First we need to set every variable to be resampled.
2118- for vn in keys (vi)
2119- set_flag! (vi, vn, " del" )
2120- end
2121- # Then we set the variables in `varinfo` from `chain`.
2122- for vn in varnames (chains)
2123- vn_updated = nested_setindex_maybe! (
2124- vi, getindex_varname (chains, sample_idx, vn, chain_idx), vn
2125- )
2126-
2127- # Unset the `del` flag if we found something.
2128- if vn_updated != = nothing
2129- # NOTE: This will be triggered even if only a subset of a variable has been set!
2130- unset_flag! (vi, vn_updated, " del" )
2131- end
2132- end
2133- else
2134- setval_and_resample! (vi, chains. value[sample_idx, :, chain_idx], keys (chains))
2135- end
2136- end
2137-
2138- function _setval_and_resample_kernel! (
2139- vi:: VarInfoOrThreadSafeVarInfo , vn:: VarName , values, keys
2140- )
2141- indices = findall (Base. Fix1 (subsumes_string, string (vn)), keys)
2142- if ! isempty (indices)
2143- val = reduce (vcat, values[indices])
2144- setval! (vi, val, vn)
2145- settrans!! (vi, false , vn)
2146- else
2147- # Ensures that we'll resample the variable corresponding to `vn` if we run
2148- # the model on `vi` again.
2149- set_flag! (vi, vn, " del" )
2150- end
2151-
2152- return indices
2153- end
2154-
21552048values_as (vi:: VarInfo ) = vi. metadata
21562049values_as (vi:: VarInfo , :: Type{Vector} ) = copy (getindex_internal (vi, Colon ()))
21572050function values_as (vi:: UntypedVarInfo , :: Type{NamedTuple} )
0 commit comments