@@ -1506,42 +1506,6 @@ function islinked(vi::VarInfo)
1506
1506
return any (istrans (vi, vn) for vn in keys (vi))
1507
1507
end
1508
1508
1509
- function nested_setindex_maybe! (vi:: UntypedVarInfo , val, vn:: VarName )
1510
- return _nested_setindex_maybe! (vi, getmetadata (vi, vn), val, vn)
1511
- end
1512
- function nested_setindex_maybe! (
1513
- vi:: VarInfo{<:NamedTuple{names}} , val, vn:: VarName{sym}
1514
- ) where {names,sym}
1515
- return if sym in names
1516
- _nested_setindex_maybe! (vi, getmetadata (vi, vn), val, vn)
1517
- else
1518
- nothing
1519
- end
1520
- end
1521
- function _nested_setindex_maybe! (
1522
- vi:: VarInfo , md:: Union{Metadata,VarNamedVector} , val, vn:: VarName
1523
- )
1524
- # If `vn` is in `vns`, then we can just use the standard `setindex!`.
1525
- vns = Base. keys (md)
1526
- if vn in vns
1527
- setindex! (vi, val, vn)
1528
- return vn
1529
- end
1530
-
1531
- # Otherwise, we need to check if either of the `vns` subsumes `vn`.
1532
- i = findfirst (Base. Fix2 (subsumes, vn), vns)
1533
- i === nothing && return nothing
1534
-
1535
- vn_parent = vns[i]
1536
- val_parent = getindex (vi, vn_parent) # TODO : Ensure that we're working with a view here.
1537
- # Split the varname into its tail optic.
1538
- optic = remove_parent_optic (vn_parent, vn)
1539
- # Update the value for the parent.
1540
- val_parent_updated = set!! (val_parent, optic, val)
1541
- setindex! (vi, val_parent_updated, vn_parent)
1542
- return vn_parent
1543
- end
1544
-
1545
1509
# The default getindex & setindex!() for get & set values
1546
1510
# NOTE: vi[vn] will always transform the variable to its original space and Julia type
1547
1511
function getindex (vi:: VarInfo , vn:: VarName )
@@ -2045,113 +2009,6 @@ function _setval_kernel!(vi::VarInfoOrThreadSafeVarInfo, vn::VarName, values, ke
2045
2009
return indices
2046
2010
end
2047
2011
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
-
2155
2012
values_as (vi:: VarInfo ) = vi. metadata
2156
2013
values_as (vi:: VarInfo , :: Type{Vector} ) = copy (getindex_internal (vi, Colon ()))
2157
2014
function values_as (vi:: UntypedVarInfo , :: Type{NamedTuple} )
0 commit comments