"Created by" plugin

The @model macro is capable of saving arbitrary extra metadata information for individual factor nodes upon creation. This feature is used by various plugins, one of which is GraphPPL.NodeCreatedByPlugin.

GraphPPL.NodeCreatedByPluginType
NodeCreatedByPlugin

A plugin that adds a created_by property to the factor node. This field is used to track the expression that created the node.

source

This plugin saves the expression that has been used to create a particular factor node, which can later be queried, for example, for debugging purposes. Here's how it works:

@model function probabilistic_model()
    x ~ Beta(1, 1)
    for i in 1:10
        y[i] ~ Normal(x, 1)
    end
end

model = GraphPPL.create_model(
    GraphPPL.with_plugins(
        probabilistic_model(),
        GraphPPL.PluginsCollection(GraphPPL.NodeCreatedByPlugin())
    )
)

We can now read the :created_by extra field for each individual node to see the expression that has created it. To do that we need to call the GraphPPL.getextra on GraphPPL.NodeData object with the :created_by as the key.

GraphPPL.factor_nodes(model) do label, nodedata
    println("Node ", label, " has been created by ", GraphPPL.getextra(nodedata, :created_by))
end
Node Distributions.Beta_6 has been created by x ~ Beta(1, 1)
Node Distributions.Normal_10 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_14 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_18 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_22 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_26 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_30 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_34 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_38 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_42 has been created by y[i] ~ Normal(x, 1)
Node Distributions.Normal_46 has been created by y[i] ~ Normal(x, 1)

The nodes correctly identify the expressions, which have created them.