|
20 | 20 | from aesara.tensor.basic import TensorVariable, Variable
|
21 | 21 | from aesara.tensor.elemwise import DimShuffle
|
22 | 22 | from aesara.tensor.random.basic import RandomVariable
|
| 23 | +from aesara.tensor.random.var import ( |
| 24 | + RandomGeneratorSharedVariable, |
| 25 | + RandomStateSharedVariable, |
| 26 | +) |
23 | 27 | from aesara.tensor.var import TensorConstant
|
24 | 28 |
|
25 | 29 | from pymc.model import Model
|
|
31 | 35 | ]
|
32 | 36 |
|
33 | 37 |
|
34 |
| -def str_for_dist(rv: TensorVariable, formatting: str = "plain", include_params: bool = True) -> str: |
35 |
| - """Make a human-readable string representation of a RandomVariable in a model, either |
| 38 | +def str_for_dist( |
| 39 | + dist: TensorVariable, formatting: str = "plain", include_params: bool = True |
| 40 | +) -> str: |
| 41 | + """Make a human-readable string representation of a Distribution in a model, either |
36 | 42 | LaTeX or plain, optionally with distribution parameter values included."""
|
37 | 43 |
|
38 | 44 | if include_params:
|
39 | 45 | # first 3 args are always (rng, size, dtype), rest is relevant for distribution
|
40 |
| - dist_args = [_str_for_input_var(x, formatting=formatting) for x in rv.owner.inputs[3:]] |
| 46 | + if isinstance(dist.owner.op, RandomVariable): |
| 47 | + dist_args = [ |
| 48 | + _str_for_input_var(x, formatting=formatting) for x in dist.owner.inputs[3:] |
| 49 | + ] |
| 50 | + else: |
| 51 | + dist_args = [ |
| 52 | + _str_for_input_var(x, formatting=formatting) |
| 53 | + for x in dist.owner.inputs |
| 54 | + if not isinstance(x, (RandomStateSharedVariable, RandomGeneratorSharedVariable)) |
| 55 | + ] |
| 56 | + |
| 57 | + print_name = dist.name |
41 | 58 |
|
42 |
| - print_name = rv.name if rv.name is not None else "<unnamed>" |
43 | 59 | if "latex" in formatting:
|
44 |
| - print_name = r"\text{" + _latex_escape(print_name) + "}" |
45 |
| - dist_name = rv.owner.op._print_name[1] |
| 60 | + if print_name is not None: |
| 61 | + print_name = r"\text{" + _latex_escape(dist.name) + "}" |
| 62 | + |
| 63 | + op_name = ( |
| 64 | + dist.owner.op._print_name[1] |
| 65 | + if hasattr(dist.owner.op, "_print_name") |
| 66 | + else r"\\operatorname{Unknown}" |
| 67 | + ) |
46 | 68 | if include_params:
|
47 |
| - return r"${} \sim {}({})$".format(print_name, dist_name, ",~".join(dist_args)) |
| 69 | + if print_name: |
| 70 | + return r"${} \sim {}({})$".format(print_name, op_name, ",~".join(dist_args)) |
| 71 | + else: |
| 72 | + return r"${}({})$".format(op_name, ",~".join(dist_args)) |
| 73 | + |
48 | 74 | else:
|
49 |
| - return rf"${print_name} \sim {dist_name}$" |
| 75 | + if print_name: |
| 76 | + return rf"${print_name} \sim {op_name}$" |
| 77 | + else: |
| 78 | + return rf"${op_name}$" |
| 79 | + |
50 | 80 | else: # plain
|
51 |
| - dist_name = rv.owner.op._print_name[0] |
| 81 | + dist_name = ( |
| 82 | + dist.owner.op._print_name[0] if hasattr(dist.owner.op, "_print_name") else "Unknown" |
| 83 | + ) |
52 | 84 | if include_params:
|
53 |
| - return r"{} ~ {}({})".format(print_name, dist_name, ", ".join(dist_args)) |
| 85 | + if print_name: |
| 86 | + return r"{} ~ {}({})".format(print_name, dist_name, ", ".join(dist_args)) |
| 87 | + else: |
| 88 | + return r"{}({})".format(dist_name, ", ".join(dist_args)) |
54 | 89 | else:
|
55 |
| - return rf"{print_name} ~ {dist_name}" |
56 |
| - |
57 |
| - |
58 |
| -def str_for_symbolic_dist( |
59 |
| - rv: TensorVariable, formatting: str = "plain", include_params: bool = True |
60 |
| -) -> str: |
61 |
| - """Make a human-readable string representation of a SymbolicDistribution in a model, |
62 |
| - either LaTeX or plain, optionally with distribution parameter values included.""" |
63 |
| - |
64 |
| - if "latex" in formatting: |
65 |
| - return rf"$\text{{{rv.name}}} \sim \text{{{rv.owner.op}}}$" |
66 |
| - else: |
67 |
| - return rf"{rv.name} ~ {rv.owner.op}" |
| 90 | + if print_name: |
| 91 | + return rf"{print_name} ~ {dist_name}" |
| 92 | + else: |
| 93 | + return dist_name |
68 | 94 |
|
69 | 95 |
|
70 | 96 | def str_for_model(model: Model, formatting: str = "plain", include_params: bool = True) -> str:
|
@@ -145,7 +171,11 @@ def _is_potential_or_determinstic(var: Variable) -> bool:
|
145 | 171 |
|
146 | 172 |
|
147 | 173 | def _str_for_input_rv(var: Variable, formatting: str) -> str:
|
148 |
| - _str = var.name if var.name is not None else "<unnamed>" |
| 174 | + _str = ( |
| 175 | + var.name |
| 176 | + if var.name is not None |
| 177 | + else str_for_dist(var, formatting=formatting, include_params=True) |
| 178 | + ) |
149 | 179 | if "latex" in formatting:
|
150 | 180 | return r"\text{" + _latex_escape(_str) + "}"
|
151 | 181 | else:
|
|
0 commit comments