diff --git a/daft.py b/daft.py index dd27d07..bf71692 100644 --- a/daft.py +++ b/daft.py @@ -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 @@ -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: diff --git a/examples/brewer.py b/examples/brewer.py new file mode 100644 index 0000000..9e3a0d3 --- /dev/null +++ b/examples/brewer.py @@ -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) diff --git a/examples/hayabusa.py b/examples/hayabusa.py new file mode 100644 index 0000000..b0c8261 --- /dev/null +++ b/examples/hayabusa.py @@ -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) diff --git a/examples/stochastic.py b/examples/stochastic.py new file mode 100644 index 0000000..4306f61 --- /dev/null +++ b/examples/stochastic.py @@ -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) diff --git a/examples/xdqso.py b/examples/xdqso.py new file mode 100644 index 0000000..7fd6b94 --- /dev/null +++ b/examples/xdqso.py @@ -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) diff --git a/examples/xdstars.py b/examples/xdstars.py new file mode 100644 index 0000000..108aadd --- /dev/null +++ b/examples/xdstars.py @@ -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)