Skip to content

Commit

Permalink
Print OP name for unnamed RVs instead of raising AssertionErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianopaz authored and ricardoV94 committed Jul 24, 2024
1 parent e988bc5 commit 8eaa9be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pymc/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ def _expand(x):
if x.owner and isinstance(x.owner.op, RandomVariable | SymbolicRandomVariable):
parents.append(x)
xname = x.name
if xname is None:
# If the variable is unnamed, we show the op's name as we do
# with constants
opname = x.owner.op.name
if opname is not None:
xname = rf"<{opname}>"
assert xname is not None
names.append(xname)

Expand Down
9 changes: 8 additions & 1 deletion tests/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ def setup_class(self):
# add a potential as well
pot = Potential("pot", mu**2)

# add a deterministic that depends on an unnamed random variable
pred = Deterministic("pred", Normal.dist(0, 1))

self.distributions = [alpha, sigma, mu, b, Z, nb2, zip, w, nested_mix, Y_obs, pot]
self.deterministics_or_potentials = [mu, pot]
self.deterministics_or_potentials = [mu, pot, pred]
# tuples of (formatting, include_params)
self.formats = [("plain", True), ("plain", False), ("latex", True), ("latex", False)]
self.expected = {
Expand All @@ -146,6 +149,7 @@ def setup_class(self):
),
r"Y_obs ~ Normal(mu, sigma)",
r"pot ~ Potential(f(beta, alpha))",
r"pred ~ Deterministic(f(<normal>))",
],
("plain", False): [
r"alpha ~ Normal",
Expand All @@ -159,6 +163,7 @@ def setup_class(self):
r"nested_mix ~ MarginalMixture",
r"Y_obs ~ Normal",
r"pot ~ Potential",
r"pred ~ Deterministic",
],
("latex", True): [
r"$\text{alpha} \sim \operatorname{Normal}(0,~10)$",
Expand All @@ -176,6 +181,7 @@ def setup_class(self):
),
r"$\text{Y_obs} \sim \operatorname{Normal}(\text{mu},~\text{sigma})$",
r"$\text{pot} \sim \operatorname{Potential}(f(\text{beta},~\text{alpha}))$",
r"$\text{pred} \sim \operatorname{Deterministic}(f(\text{<normal>}))",
],
("latex", False): [
r"$\text{alpha} \sim \operatorname{Normal}$",
Expand All @@ -189,6 +195,7 @@ def setup_class(self):
r"$\text{nested_mix} \sim \operatorname{MarginalMixture}$",
r"$\text{Y_obs} \sim \operatorname{Normal}$",
r"$\text{pot} \sim \operatorname{Potential}$",
r"$\text{pred} \sim \operatorname{Deterministic}",
],
}

Expand Down

0 comments on commit 8eaa9be

Please sign in to comment.