nodedata

A module for creating and managing node data. Node data in this library can have many types, dependent on whether the conditional probability distributions are discrete, linear Gaussian, or hybrid, and on whether the Bayesian network is static or dynamic. For example input files, see discrete bayesian network, hybrid bayesian network, linear gaussian bayesian network, and dynamic discrete bayesian network.

class libpgm.nodedata.NodeData[source]

This class represents the node data for each node in a graph. If the Bayesian network is static, it contains the attribute Vdata. If the Bayesian network is dynamic, it contains two attributes, initial_Vdata and twotbn_Vdata. If the Bayesian network has hybrid CPDs, it contains the additional attribute nodes.

Vdata = None

A dictionary of node data.

initial_Vdata = None

In dynamic graphs, a dictionary containing node data for the initial time interval.

twotbn_Vdata = None

In dynamic graphs, a dictionary containing node data for every time step interval after the first one.

nodes = None

In hybrid graphs, a dictionary of {key:value} pairs linking the name of each node (the key) to a clas instance (the value) which represents the node, its data, and its sampling function.

load(path)[source]

Load node data from an input file located at path. Input file must be a plaintext .txt file with a JSON-style representation of a dict. The dict must have the top-level key Vdata or two top-level keys, initial_Vdata and twotbn_Vdata. For example:

{
    "Vdata": {
        "<vertex 1>": <dict containing vertex 1 data>,
        ...
        "<vertex n>": <dict containing vertex n data>
    }
}

or:

{
    "initial_Vdata": {
        "<vertex 1>": <dict containing vertex 1 data>,
        ...
        "<vertex n>": <dict containing vertex n data>
    }
    "twotbn_Vdata": {
        "<vertex 1>": <dict containing vertex 1 data>,
        ...
        "<vertex n>": <dict containing vertex n data>
    }
}
The function takes the following arguments:
  1. path – The path to the text file that contains input data (e.g., “mydictionary.txt”)

In the static case, it modifies Vdata to hold the dictionary found at path. In the dynamic case, it modifies the initial_Vdata and twotbn_Vdata attributes to hold the dictionaries found at path.

entriestoinstances()[source]

For each node, convert dictionary entry to class instance.

This method is used only when dealing with Hybrid Bayesian networks as found in the hybayesiannetwork module.

The type of the node must be located in the ‘type’ attribute of the node’s dictionary entry. To see an example of such a dictionary, see hybrid bayesian network. This type is used to instantiate a corresponding class from libpgm/CPDtypes/, and store the node’s dictionary info in that class. Thus we lose none of the dictionary data, yet we gain the ability to use the instantiated class’s built-in function to choose its own outcome based on the outcomes of its parents.

In order for this method to be called, the self.Vdata attribute must have dictionary entries of the following form:

<vertex name>: {
    'type': <type of node -- must correspond to module in /CPDtypes>,
    'parents': <array of parents of node>,
    'children': <array of children of node>,
    <whatever further entries are required by the type*> 
}

For instance, type “discrete” requires a “cprob” entry, while type “lg” requires “mean_base”, “mean_scal”, and “variance” entries.

The function draws on the data in the Vdata attribute, and instantiates the attribute nodes, which is a dictionary of {name: instance} pairs where ‘name’ is the name of the node and ‘instance’ is a class instance containing the node data and the proper sampling function.

Previous topic

orderedskeleton

Next topic

discretebayesiannetwork

This Page