model_table#

pymc_extras.printing.model_table(model: Model, *, split_groups: bool = True, truncate_deterministic: int | None = None, parameter_count: bool = True) Table[source]#

Create a rich table with a summary of the model’s variables and their expressions.

Parameters:
  • model (Model) – The PyMC model to summarize.

  • split_groups (bool) – If True, each group of variables (data, free_RVs, deterministics, potentials, observed_RVs) will be separated by a section.

  • truncate_deterministic (int | None) – If not None, truncate the expression of deterministic variables that go beyond this length.

  • empty_dims (bool) – If True, show the dimensions of scalar variables as an empty list.

  • parameter_count (bool) – If True, add a row with the total number of parameters in the model.

Returns:

A rich table with the model’s variables, their expressions and dims.

Return type:

Table

Examples

import numpy as np
import pymc as pm

from pymc_extras.printing import model_table

coords = {"subject": range(20), "param": ["a", "b"]}
with pm.Model(coords=coords) as m:
    x = pm.Data("x", np.random.normal(size=(20, 2)), dims=("subject", "param"))
    y = pm.Data("y", np.random.normal(size=(20,)), dims="subject")

    beta = pm.Normal("beta", mu=0, sigma=1, dims="param")
    mu = pm.Deterministic("mu", pm.math.dot(x, beta), dims="subject")
    sigma = pm.HalfNormal("sigma", sigma=1)

    y_obs = pm.Normal("y_obs", mu=mu, sigma=sigma, observed=y, dims="subject")

table = model_table(m)
table  # Displays the following table in an interactive environment
'''
 Variable  Expression         Dimensions
─────────────────────────────────────────────────────
      x =  Data               subject[20] × param[2]
      y =  Data               subject[20]

   beta ~  Normal(0, 1)       param[2]
  sigma ~  HalfNormal(0, 1)
                              Parameter count = 3

     mu =  f(beta)            subject[20]

  y_obs ~  Normal(mu, sigma)  subject[20]
'''

Output can be explicitly rendered in a rich console or exported to text, html or svg.

from rich.console import Console

console = Console(record=True)
console.print(table)
text_export = console.export_text()
html_export = console.export_html()
svg_export = console.export_svg()