API

The PGM Object

All daft scripts will start with the creation of a PGM object. This object contains a list of Node objects and Edge objects connecting them. You can also specify rendering parameters and other default parameters when you initialize your PGM.

class daft.PGM(shape, origin=[0, 0], grid_unit=2, node_unit=1, observed_style='shaded', line_width=1, node_ec='k', directed=True, aspect=1.0, label_params={})

The base object for building a graphical model representation.

Parameters:
  • shape – The number of rows and columns in the grid.
  • origin – The coordinates of the bottom left corner of the plot.
  • grid_unit – (optional) The size of the grid spacing measured in centimeters.
  • node_unit – (optional) The base unit for the node size. This is a number in centimeters that sets the default diameter of the nodes.
  • observed_style – (optional) How should the “observed” nodes be indicated? This must be one of: "shaded", "inner" or "outer" where inner and outer nodes are shown as double circles with the second circle plotted inside or outside of the standard one, respectively.
  • node_ec – (optional) The default edge color for the nodes.
  • directed – (optional) Should the edges be directed by default?
  • aspect – (optional) The default aspect ratio for the nodes.
  • label_params – (optional) Default node label parameters.
add_edge(name1, name2, directed=None, **kwargs)

Construct an Edge between two named Node objects.

Parameters:
  • name1 – The name identifying the first node.
  • name2 – The name identifying the second node. If the edge is directed, the arrow will point to this node.
  • directed – (optional) Should this be a directed edge?
add_node(node)

Add a Node to the model.

Parameters:node – The Node instance to add.
add_plate(plate)

Add a Plate object to the model.

render()

Render the Plate, Edge and Node objects in the model. This will create a new figure with the correct dimensions and plot the model in this area.

Nodes

class daft.Node(name, content, x, y, scale=1, aspect=None, observed=False, fixed=False, offset=[0, 0], plot_params={}, label_params=None)

The representation of a random variable in a PGM.

Parameters:
  • name – The plain-text identifier for the node.
  • content – The display form of the variable.
  • x – The x-coordinate of the node in model units.
  • y – The y-coordinate of the node.
  • scale – (optional) The diameter (or height) of the node measured in multiples of node_unit as defined by the PGM object.
  • aspect – (optional) The aspect ratio width/height for elliptical nodes; default 1.
  • observed – (optional) Should this be a conditioned variable?
  • fixed – (optional) Should this be a fixed (not permitted to vary) variable? If True, modifies or over-rides diameter, offset, facecolor, and a few other plot_params settings. This setting conflicts with observed.
  • offset – (optional) The (dx, dy) offset of the label (in points) from the default centered position.
  • plot_params – (optional) A dictionary of parameters to pass to the matplotlib.patches.Ellipse constructor.
render(ctx)

Render the node.

Parameters:ctx – The _rendering_context object.

Edges

class daft.Edge(node1, node2, directed=True, plot_params={})

An edge between two Node objects.

Parameters:
  • node1 – The first Node.
  • node2 – The second Node. The arrow will point towards this node.
  • directed – (optional) Should the edge be directed from node1 to node2? In other words: should it have an arrow?
  • plot_params – (optional) A dictionary of parameters to pass to the plotting command when rendering.
render(ctx)

Render the edge in the given axes.

Parameters:ctx – The _rendering_context object.

Plates

class daft.Plate(rect, label=None, label_offset=[5, 5], shift=0, rect_params={})

A plate to encapsulate repeated independent processes in the model.

Parameters:
  • rect – The rectangle describing the plate bounds in model coordinates.
  • label – (optional) A string to annotate the plate.
  • label_offset – (optional) The x and y offsets of the label text measured in points.
  • shift – (optional) The vertical “shift” of the plate measured in model units. This will move the bottom of the panel by shift units.
  • rect_params – (optional) A dictionary of parameters to pass to the matplotlib.patches.Rectangle constructor.
render(ctx)

Render the plate in the given axes.

Parameters:ctx – The _rendering_context object.

The Rendering Context

class daft._rendering_context(**kwargs)
Parameters:
  • shape – The number of rows and columns in the grid.
  • origin – The coordinates of the bottom left corner of the plot.
  • grid_unit – The size of the grid spacing measured in centimeters.
  • node_unit – The base unit for the node size. This is a number in centimeters that sets the default diameter of the nodes.
  • observed_style – How should the “observed” nodes be indicated? This must be one of: "shaded", "inner" or "outer" where inner and outer nodes are shown as double circles with the second circle plotted inside or outside of the standard one, respectively.
  • node_ec – The default edge color for the nodes.
  • directed – Should the edges be directed by default?
  • aspect – The default aspect ratio for the nodes.
  • label_params – Default node label parameters.
convert(*xy)

Convert from model coordinates to plot coordinates.

Fork me on GitHub