|
| 1 | +import itertools |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from pymc import Model |
| 6 | +from pymc.printing import str_for_dist, str_for_potential_or_deterministic |
| 7 | +from pytensor.compile.sharedvalue import SharedVariable |
| 8 | +from pytensor.graph.type import Constant |
| 9 | +from rich.box import SIMPLE_HEAD |
| 10 | +from rich.table import Table |
| 11 | + |
| 12 | + |
| 13 | +def _extract_value(var: SharedVariable | Constant) -> np.ndarray: |
| 14 | + if isinstance(var, SharedVariable): |
| 15 | + return var.get_value(borrow=True) |
| 16 | + else: |
| 17 | + return var.data |
| 18 | + |
| 19 | + |
| 20 | +def model_table( |
| 21 | + model: Model, |
| 22 | + split_groups: bool = True, |
| 23 | + truncate_deterministic: int | None = None, |
| 24 | + parameter_count: bool = True, |
| 25 | +) -> Table: |
| 26 | + """Create a rich table with a summary of the model's variables and their expressions. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + model : Model |
| 31 | + The PyMC model to summarize. |
| 32 | + split_groups : bool |
| 33 | + If True, each group of variables (data, free_RVs, deterministics, potentials, observed_RVs) |
| 34 | + will be separated by a section. |
| 35 | + truncate_deterministic : int | None |
| 36 | + If not None, truncate the expression of deterministic variables that go beyond this length. |
| 37 | + parameter_count : bool |
| 38 | + If True, add a row with the total number of parameters in the model. |
| 39 | +
|
| 40 | + Returns |
| 41 | + ------- |
| 42 | + Table |
| 43 | + A rich table with the model's variables, their expressions and dims. |
| 44 | +
|
| 45 | + Examples |
| 46 | + -------- |
| 47 | + .. code-block:: python |
| 48 | +
|
| 49 | + import numpy as np |
| 50 | + import pymc as pm |
| 51 | +
|
| 52 | + from pymc_experimental.printing import model_table |
| 53 | +
|
| 54 | + coords = {"subject": range(20), "param": ["a", "b"]} |
| 55 | + with pm.Model(coords=coords) as m: |
| 56 | + x = pm.Data("x", np.random.normal(size=(20, 2)), dims=("subject", "param")) |
| 57 | + y = pm.Data("y", np.random.normal(size=(20,)), dims="subject") |
| 58 | +
|
| 59 | + beta = pm.Normal("beta", mu=0, sigma=1, dims="param") |
| 60 | + mu = pm.Deterministic("mu", pm.math.dot(x, beta), dims="subject") |
| 61 | + sigma = pm.HalfNormal("sigma", sigma=1) |
| 62 | +
|
| 63 | + y_obs = pm.Normal("y_obs", mu=mu, sigma=sigma, observed=y, dims="subject") |
| 64 | +
|
| 65 | + table = model_table(m) |
| 66 | + table # Displays the following table in an interactive environment |
| 67 | + ''' |
| 68 | + Variable Expression Dimensions |
| 69 | + ───────────────────────────────────────────────────── |
| 70 | + x = Data subject[20] × param[2] |
| 71 | + y = Data subject[20] |
| 72 | +
|
| 73 | + beta ~ Normal(0, 1) param[2] |
| 74 | + sigma ~ HalfNormal(0, 1) |
| 75 | + Parameter count = 3 |
| 76 | +
|
| 77 | + mu = f(beta) subject[20] |
| 78 | +
|
| 79 | + y_obs ~ Normal(mu, sigma) subject[20] |
| 80 | + ''' |
| 81 | +
|
| 82 | + Output can be explicitly rendered in a rich console or exported to text, html or svg. |
| 83 | +
|
| 84 | + .. code-block:: python |
| 85 | +
|
| 86 | + from rich.console import Console |
| 87 | +
|
| 88 | + console = Console(record=True) |
| 89 | + console.print(table) |
| 90 | + text_export = console.export_text() |
| 91 | + html_export = console.export_html() |
| 92 | + svg_export = console.export_svg() |
| 93 | +
|
| 94 | + """ |
| 95 | + table = Table( |
| 96 | + show_header=True, |
| 97 | + show_edge=False, |
| 98 | + box=SIMPLE_HEAD, |
| 99 | + highlight=False, |
| 100 | + collapse_padding=True, |
| 101 | + ) |
| 102 | + table.add_column("Variable", justify="right") |
| 103 | + table.add_column("Expression", justify="left") |
| 104 | + table.add_column("Dimensions") |
| 105 | + |
| 106 | + dim_sizes = {k: _extract_value(v) for k, v in model.dim_lengths.items()} |
| 107 | + |
| 108 | + groups = ( |
| 109 | + model.data_vars, |
| 110 | + model.free_RVs, |
| 111 | + model.deterministics, |
| 112 | + model.potentials, |
| 113 | + model.observed_RVs, |
| 114 | + ) |
| 115 | + if not split_groups: |
| 116 | + groups = (itertools.chain.from_iterable(groups),) |
| 117 | + |
| 118 | + for group in groups: |
| 119 | + if not group: |
| 120 | + continue |
| 121 | + |
| 122 | + for var in group: |
| 123 | + var_name = var.name |
| 124 | + dims = model.named_vars_to_dims.get(var_name, ()) |
| 125 | + |
| 126 | + is_data = var in model.data_vars |
| 127 | + is_deterministic = var in model.deterministics |
| 128 | + is_potential = var in model.potentials |
| 129 | + |
| 130 | + if is_data: |
| 131 | + var_expr = "Data" |
| 132 | + elif is_deterministic: |
| 133 | + str_repr = str_for_potential_or_deterministic(var, dist_name="") |
| 134 | + _, var_expr = str_repr.split(" ~ ") |
| 135 | + var_expr = var_expr[1:-1] # Remove outer parentheses (f(...)) |
| 136 | + if truncate_deterministic is not None and len(var_expr) > truncate_deterministic: |
| 137 | + contents = var_expr[2:-1].split(", ") |
| 138 | + str_len = 0 |
| 139 | + for show_n, content in enumerate(contents): |
| 140 | + str_len += len(content) + 2 |
| 141 | + if str_len > truncate_deterministic: |
| 142 | + break |
| 143 | + var_expr = f"f({', '.join(contents[:show_n])}, ...)" |
| 144 | + elif is_potential: |
| 145 | + var_expr = str_for_potential_or_deterministic(var, dist_name="Potential").split( |
| 146 | + " ~ " |
| 147 | + )[1] |
| 148 | + else: |
| 149 | + var_expr = str_for_dist(var).split(" ~ ")[1] |
| 150 | + |
| 151 | + dims_and_sizes = " × ".join(f"{dim}[{dim_sizes[dim]}]" for dim in dims) |
| 152 | + sep = f'[b]{" =" if (is_data or is_deterministic or is_potential) else " ~"}[/b]' |
| 153 | + table.add_row(var_name + sep, var_expr, dims_and_sizes) |
| 154 | + |
| 155 | + if parameter_count and (not split_groups or group == model.free_RVs): |
| 156 | + rv_shapes = model.eval_rv_shapes() |
| 157 | + n_parameters = np.sum( |
| 158 | + [np.prod(rv_shapes[free_rv.name]).astype(int) for free_rv in model.free_RVs] |
| 159 | + ) |
| 160 | + table.add_row("", "", f"[i]Parameter count = {n_parameters}[/i]") |
| 161 | + |
| 162 | + table.add_section() |
| 163 | + |
| 164 | + return table |
0 commit comments