@@ -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 )
@@ -1972,113 +1936,6 @@ function _setval_kernel!(vi::VarInfoOrThreadSafeVarInfo, vn::VarName, values, ke
1972
1936
return indices
1973
1937
end
1974
1938
1975
- """
1976
- setval_and_resample!(vi::VarInfo, x)
1977
- setval_and_resample!(vi::VarInfo, values, keys)
1978
- setval_and_resample!(vi::VarInfo, chains::AbstractChains, sample_idx, chain_idx)
1979
-
1980
- Set the values in `vi` to the provided values and those which are not present
1981
- in `x` or `chains` to *be* resampled.
1982
-
1983
- Note that this does *not* resample the values not provided! It will call
1984
- `setflag!(vi, vn, "del")` for variables `vn` for which no values are provided, which means
1985
- that the next time we call `model(vi)` these variables will be resampled.
1986
-
1987
- ## Note
1988
- - This suffers from the same limitations as [`setval!`](@ref). See `setval!` for more info.
1989
-
1990
- ## Example
1991
- ```jldoctest
1992
- julia> using DynamicPPL, Distributions, StableRNGs
1993
-
1994
- julia> @model function demo(x)
1995
- m ~ Normal()
1996
- for i in eachindex(x)
1997
- x[i] ~ Normal(m, 1)
1998
- end
1999
- end;
2000
-
2001
- julia> rng = StableRNG(42);
2002
-
2003
- julia> m = demo([missing]);
2004
-
2005
- julia> var_info = DynamicPPL.VarInfo(rng, m);
2006
- # Checking the setting of "del" flags only makes sense for VarInfo{<:Metadata}. For VarInfo{<:VarNamedVector} the flag is effectively always set.
2007
-
2008
- julia> var_info[@varname(m)]
2009
- -0.6702516921145671
2010
-
2011
- julia> var_info[@varname(x[1])]
2012
- -0.22312984965118443
2013
-
2014
- julia> DynamicPPL.setval_and_resample!(var_info, (m = 100.0, )); # set `m` and ready `x[1]` for resampling
2015
-
2016
- julia> var_info[@varname(m)] # [✓] changed
2017
- 100.0
2018
-
2019
- julia> var_info[@varname(x[1])] # [✓] unchanged
2020
- -0.22312984965118443
2021
-
2022
- julia> m(rng, var_info); # sample `x[1]` conditioned on `m = 100.0`
2023
-
2024
- julia> var_info[@varname(m)] # [✓] unchanged
2025
- 100.0
2026
-
2027
- julia> var_info[@varname(x[1])] # [✓] changed
2028
- 101.37363069798343
2029
- ```
2030
-
2031
- ## See also
2032
- - [`setval!`](@ref)
2033
- """
2034
- function setval_and_resample! (vi:: VarInfoOrThreadSafeVarInfo , x)
2035
- return setval_and_resample! (vi, values (x), keys (x))
2036
- end
2037
- function setval_and_resample! (vi:: VarInfoOrThreadSafeVarInfo , values, keys)
2038
- return _apply! (_setval_and_resample_kernel!, vi, values, keys)
2039
- end
2040
- function setval_and_resample! (
2041
- vi:: VarInfoOrThreadSafeVarInfo , chains:: AbstractChains , sample_idx:: Int , chain_idx:: Int
2042
- )
2043
- if supports_varname_indexing (chains)
2044
- # First we need to set every variable to be resampled.
2045
- for vn in keys (vi)
2046
- set_flag! (vi, vn, " del" )
2047
- end
2048
- # Then we set the variables in `varinfo` from `chain`.
2049
- for vn in varnames (chains)
2050
- vn_updated = nested_setindex_maybe! (
2051
- vi, getindex_varname (chains, sample_idx, vn, chain_idx), vn
2052
- )
2053
-
2054
- # Unset the `del` flag if we found something.
2055
- if vn_updated != = nothing
2056
- # NOTE: This will be triggered even if only a subset of a variable has been set!
2057
- unset_flag! (vi, vn_updated, " del" )
2058
- end
2059
- end
2060
- else
2061
- setval_and_resample! (vi, chains. value[sample_idx, :, chain_idx], keys (chains))
2062
- end
2063
- end
2064
-
2065
- function _setval_and_resample_kernel! (
2066
- vi:: VarInfoOrThreadSafeVarInfo , vn:: VarName , values, keys
2067
- )
2068
- indices = findall (Base. Fix1 (subsumes_string, string (vn)), keys)
2069
- if ! isempty (indices)
2070
- val = reduce (vcat, values[indices])
2071
- setval! (vi, val, vn)
2072
- settrans!! (vi, false , vn)
2073
- else
2074
- # Ensures that we'll resample the variable corresponding to `vn` if we run
2075
- # the model on `vi` again.
2076
- set_flag! (vi, vn, " del" )
2077
- end
2078
-
2079
- return indices
2080
- end
2081
-
2082
1939
values_as (vi:: VarInfo ) = vi. metadata
2083
1940
values_as (vi:: VarInfo , :: Type{Vector} ) = copy (getindex_internal (vi, Colon ()))
2084
1941
function values_as (vi:: UntypedVarInfo , :: Type{NamedTuple} )
0 commit comments