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

Expand logging test cases for sample_prior_predictive and add return type overloads #7707

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 27 additions & 1 deletion pymc/sampling/forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
from collections.abc import Callable, Iterable, Sequence
from typing import (
Any,
Literal,
TypeAlias,
cast,
overload,
)

import numpy as np
Expand Down Expand Up @@ -360,6 +362,28 @@ def observed_dependent_deterministics(model: Model, extra_observeds=None):
]


@overload
def sample_prior_predictive(
draws: int = 500,
model: Model | None = None,
var_names: Iterable[str] | None = None,
random_seed: RandomState = None,
return_inferencedata: Literal[True] = True,
idata_kwargs: dict | None = None,
compile_kwargs: dict | None = None,
samples: int | None = None,
) -> InferenceData: ...
@overload
def sample_prior_predictive(
draws: int = 500,
model: Model | None = None,
var_names: Iterable[str] | None = None,
random_seed: RandomState = None,
return_inferencedata: Literal[False] = False,
idata_kwargs: dict | None = None,
compile_kwargs: dict | None = None,
samples: int | None = None,
) -> dict[str, np.ndarray]: ...
def sample_prior_predictive(
draws: int = 500,
model: Model | None = None,
Expand Down Expand Up @@ -449,7 +473,9 @@ def sample_prior_predictive(
)

# All model variables have a name, but mypy does not know this
_log.info(f"Sampling: {sorted(volatile_basic_rvs, key=lambda var: var.name)}") # type: ignore[arg-type, return-value]
_log.info(
f"Sampling: {sorted(volatile_basic_rvs.intersection(vars_to_sample), key=lambda var: var.name)}" # type: ignore[arg-type, return-value]
)
values = zip(*(sampler_fn() for i in range(draws)))

data = {k: np.stack(v) for k, v in zip(names, values)}
Expand Down
7 changes: 7 additions & 0 deletions tests/sampling/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,11 +862,18 @@ def test_logging_sampled_basic_rvs_prior(self, caplog):
assert caplog.record_tuples == [("pymc.sampling.forward", logging.INFO, "Sampling: [x, z]")]
caplog.clear()

# RV only
with m:
pm.sample_prior_predictive(draws=1, var_names=["x"])
assert caplog.record_tuples == [("pymc.sampling.forward", logging.INFO, "Sampling: [x]")]
caplog.clear()

# observed only
with m:
pm.sample_prior_predictive(draws=1, var_names=["z"])
assert caplog.record_tuples == [("pymc.sampling.forward", logging.INFO, "Sampling: [z]")]
caplog.clear()

def test_logging_sampled_basic_rvs_posterior(self, caplog):
with pm.Model() as m:
x = pm.Normal("x")
Expand Down
Loading