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