Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print OP name for unnamed RVs instead of raising AssertionErrors #7428

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading