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