Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples of various kinds #62

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
3 changes: 1 addition & 2 deletions daft.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ def render(self, ctx):
l["ha"] = _pop_multiple(l, "center", "ha", "horizontalalignment")

# Deal with ``fixed`` nodes.
scale = self.scale
if self.fixed:
# MAGIC: These magic numbers should depend on the grid/node units.
self.offset[1] += 6
Expand All @@ -249,7 +248,7 @@ def render(self, ctx):
if p["fc"] == "none":
p["fc"] = "k"

diameter = ctx.node_unit * scale
diameter = ctx.node_unit * self.scale
if self.aspect is not None:
aspect = self.aspect
else:
Expand Down
30 changes: 30 additions & 0 deletions examples/brewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Brewer's project, sort of
=========================

This uses a HACK of `Plate` to put a label on the plot.
"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

pgm = daft.PGM([5., 2.5], origin=[2.0, 0.0], aspect=2.1)
pgm.add_node(daft.Node("counts", r"$dN/dm$", 6.0, 2.0))
pgm.add_node(daft.Node("stars", r"stars", 4.5, 2.0,))
pgm.add_node(daft.Node("pixels", r"pixels", 3.0, 2.0, observed=True))
pgm.add_node(daft.Node("psf", r"psf", 4.5, 1.0))
pgm.add_node(daft.Node("noise", r"noise", 3.0, 1.0))
pgm.add_edge("stars", "pixels")
pgm.add_edge("psf", "pixels")
pgm.add_edge("noise", "pixels")
pgm.add_edge("counts", "stars")
pgm.add_plate(daft.Plate([2.0, 0.0, 7.0, 2.5],
label=r"$p(\mbox{pixels}\,|\,\mbox{stars})\,p(\mbox{stars}\,|\,dN/dm)\,p(dN/dm)$",
label_offset=[30,10],
rect_params={"ec": "none",}))
pgm.render()
pgm.figure.savefig("brewer.pdf")
pgm.figure.savefig("brewer.png", dpi=300)
48 changes: 48 additions & 0 deletions examples/hayabusa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Hayabusa
========

No comment.

"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)
import daft

pgm = daft.PGM([4.8, 3.2], origin=[-0.4, -0.7])

# Hierarchical parameters.
pgm.add_node(daft.Node("theta", r"$\theta$", 0, 1))
pgm.add_node(daft.Node("Omega", r"$\Omega$", 4, 1))
pgm.add_node(daft.Node("Sigma", r"$\Sigma$", 0, 0))

# spectral measurements
pgm.add_node(daft.Node("a", r"$a_n$", 1, 2, observed=True))
pgm.add_node(daft.Node("z", r"$z_n$", 2, 2, observed=True))

# Latent variables.
pgm.add_node(daft.Node("M", r"$M_n$", 1, 1))
pgm.add_edge("a", "M")
pgm.add_edge("theta", "M")
pgm.add_node(daft.Node("K", r"$K_n$", 2, 1))
pgm.add_edge("z", "K")
pgm.add_node(daft.Node("DM", r"$DM_n$", 3, 1))
pgm.add_edge("z", "DM")
pgm.add_edge("Omega", "DM")

# Data.
pgm.add_node(daft.Node("m", r"$m_n$", 2, 0, observed=True))
pgm.add_edge("M", "m")
pgm.add_edge("K", "m")
pgm.add_edge("DM", "m")
pgm.add_edge("Sigma", "m")

# And a plate.
pgm.add_plate(daft.Plate([0.5, -0.6, 3, 3], label=r"quasars $n$"))

# Render and save.
pgm.render()
pgm.figure.savefig("hayabusa.pdf")
pgm.figure.savefig("hayabusa.png", dpi=150)
42 changes: 42 additions & 0 deletions examples/stochastic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
The Quintessential PGM
======================

This is a demonstration of a very common structure found in graphical models.
It has been rendered using Daft's default settings for all the parameters
and it shows off how much beauty is baked in by default.

"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

# Instantiate the PGM.
pgm = daft.PGM([5., 3.1], origin=[-0.5, 0.3])

# Hierarchical parameters.
pgm.add_node(daft.Node("alpha", r"$\alpha$", 0.5, 3., fixed=True))
pgm.add_node(daft.Node("weather", r"stochastic", 0.5, 2., aspect=2.5))
pgm.add_node(daft.Node("pop", r"population", 2., 3., aspect=2.5))
pgm.add_node(daft.Node("science", r"target", 2., 2., aspect=2.5))
pgm.add_node(daft.Node("data", r"data set", 1.5, 1., aspect=2.5))
pgm.add_node(daft.Node(r"noise", r"\noindent noise\\ model", 3.7, 1., aspect=2.5))

# Add in the edges.
pgm.add_edge("alpha", "weather")
pgm.add_edge("weather", "data")
pgm.add_edge("pop", "science")
pgm.add_edge("science", "data")
pgm.add_edge("noise", "data")

# And a plate.
pgm.add_plate(daft.Plate([-0.3, 0.5, 3.1, 2.], label=r"data sets",
shift=-0.1))

# Render and save.
pgm.render()
pgm.figure.savefig("stochastic.pdf")
pgm.figure.savefig("stochastic.png", dpi=150)
54 changes: 54 additions & 0 deletions examples/xdqso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Extreme deconvolution of stars
==============================

The (very simple) model that transformed SDSS-III.

"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

# Instantiate the PGM.
pgm = daft.PGM([4.00, 3.55], origin=[-0.6, 0.3])

# Hierarchical parameters.
pgm.add_node(daft.Node("alpha", r"$\alpha$", 1, 3.5, fixed=True))
pgm.add_node(daft.Node("theta", r"$\theta$", 1, 2.5))
pgm.add_node(daft.Node("Sigma", r"$\Sigma$", 1, 1))
pgm.add_node(daft.Node("sigma", r"$\sigma$", 3, 1))

# Latent variables.
pgm.add_node(daft.Node("q", r"$q_m$", 2, 3))
pgm.add_node(daft.Node("X", r"$X_m$", 2, 2))
pgm.add_node(daft.Node("Xt", r"$X_n$", 0, 2))

# Data.
pgm.add_node(daft.Node("qt", r"$q_n$", 0, 3, observed=True))
pgm.add_node(daft.Node("x", r"$x_m$", 2, 1, observed=True))
pgm.add_node(daft.Node("xt", r"$x_n$", 0, 1, observed=True))

# Add in the edges.
pgm.add_edge("alpha", "theta")
pgm.add_edge("theta", "q")
pgm.add_edge("theta", "qt")
pgm.add_edge("theta", "X")
pgm.add_edge("theta", "Xt")
pgm.add_edge("sigma", "x")
pgm.add_edge("Sigma", "xt")
pgm.add_edge("X", "x")
pgm.add_edge("Xt", "xt")
pgm.add_edge("q", "X")
pgm.add_edge("qt", "Xt")

# And plates.
pgm.add_plate(daft.Plate([-0.5, 0.5, 1, 3], label=r"train", shift=-0.1))
pgm.add_plate(daft.Plate([1.5, 0.5, 1, 3], label=r"test", shift=-0.1))

# Render and save.
pgm.render()
pgm.figure.savefig("xdqso.pdf")
pgm.figure.savefig("xdqso.png", dpi=150)
41 changes: 41 additions & 0 deletions examples/xdstars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Extreme deconvolution of stars
==============================

The (very simple) model that transformed SDSS-III.

"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

# Instantiate the PGM.
pgm = daft.PGM([2.2, 3.55], origin=[-0.6, 0.3])

# Hierarchical parameters.
pgm.add_node(daft.Node("alpha", r"$\alpha$", 1, 3.5, fixed=True))
pgm.add_node(daft.Node("theta", r"$\theta$", 1, 2.5))
pgm.add_node(daft.Node("sigma", r"$\Sigma$", 1, 1))

# Latent variable.
pgm.add_node(daft.Node("X", r"$X_n$", 0, 2))

# Data.
pgm.add_node(daft.Node("x", r"$x_n$", 0, 1, observed=True))

# Add in the edges.
pgm.add_edge("alpha", "theta")
pgm.add_edge("theta", "X")
pgm.add_edge("sigma", "x")
pgm.add_edge("X", "x")

# And a plate.
pgm.add_plate(daft.Plate([-0.5, 0.5, 1, 2], label=r"stars $n$", shift=-0.1))

# Render and save.
pgm.render()
pgm.figure.savefig("xdstars.pdf")
pgm.figure.savefig("xdstars.png", dpi=150)