LightGraphs vs. SimpleGraphs

@anon94023334 Thank you for your help with this! I think I kind of have it working. I need to find dependencies in a model like this:

julia> lda
@model (α, N, K, V, η) begin
    M = length(N)
    β ~ Dirichlet(repeat([η], V)) |> iid(K)
    θ ~ Dirichlet(repeat([α], K)) |> iid(M)
    z ~ For(1:M) do m
            Categorical(θ[m]) |> iid(N[m])
        end
    w ⩪ For(1:M) do m
            For(1:N[m]) do n
                Categorical(β[(z[m])[n]])
            end
        end
end

Using your example as a template, I can define

using LightGraphs
using MetaGraphs
function graph(m::Model)
    vars = variables(m)
    g = MetaDiGraph(length(vars))
    for (n,v) in enumerate(vars)
        set_prop!(g, n, :name, v)
    end
    set_indexing_prop!(g, :name)
    postwalk(m.body) do x 
        if @capture(x,v_~d_) || @capture(x,v_⩪d_) || @capture(x,v_=d_)
            for rhs in findsubexprs(d,vars)
                add_edge!(g,(g[v,:name],g[rhs,:name]))
            end
        else x
        end
    end
    g
end

Then to find dependencies…

julia> g = graph(lda)
{10, 14} directed Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)

julia> [(g[e.src,:name] => g[e.dst,:name]) for e in edges(g)]
14-element Array{Pair{Symbol,Symbol},1}:
 :w => :N
 :w => :M
 :w => :z
 :w => :β
 :M => :N
 :z => :N
 :z => :M
 :z => :θ
 :β => :K
 :β => :V
 :β => :η
 :θ => :α
 :θ => :M
 :θ => :K
2 Likes