Skip to content

Dict interface for varinfo is weird #1055

@penelopeysm

Description

@penelopeysm
julia> using DynamicPPL, Distributions

julia> @model function f()
           x ~ Normal()
           y ~ Normal()
           z = Vector{Float64}(undef, 2)
           z .~ Normal()
       end
f (generic function with 2 methods)

julia> v = VarInfo(f());

julia> keys(v)
4-element Vector{VarName}:
 x
 y
 z[1]
 z[2]

julia> values(v) isa VarInfo
true

julia> collect(pairs(v))
ERROR: MethodError: no method matching length(::VarInfo{@NamedTuple{…}, DynamicPPL.AccumulatorTuple{…}})
The function `length` exists, but no method is defined for this combination of argument types.

keys(v) makes sense, but I would expect values(v) to give the corresponding values not just return the VarInfo, and pairs(v) to give the (key, value) pairs (instead I don't even know what it returns because I can't get it into a vector).

This means that you have to do odd things like this, when it should probably just be Dict(pairs(v)).

julia> Dict(k => v[k] for k in keys(v))
Dict{VarName, Float64} with 4 entries:
  y    => -1.03936
  x    => -0.114171
  z[2] => 0.417849
  z[1] => -0.997633

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions