Skip to content
Open
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
9 changes: 7 additions & 2 deletions pymc_extras/model/marginal/marginal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pymc.distributions.discrete import Bernoulli, Categorical, DiscreteUniform
from pymc.distributions.transforms import Chain
from pymc.logprob.transforms import IntervalTransform
from pymc.model import Model
from pymc.model import Model, modelcontext
from pymc.model.fgraph import (
ModelFreeRV,
ModelValuedVar,
Expand Down Expand Up @@ -337,8 +337,8 @@ def transform_posterior_pts(model, posterior_pts):


def recover_marginals(
model: Model,
idata: InferenceData,
model: Model | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make all but idata kwarg only?

var_names: Sequence[str] | None = None,
return_samples: bool = True,
extend_inferencedata: bool = True,
Expand Down Expand Up @@ -389,6 +389,11 @@ def recover_marginals(


"""
if isinstance(idata, Model):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment that this is temporary check due to API change and should be removed eventually

raise TypeError("The first argument of `recover_marginals` must be an idata")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise TypeError("The first argument of `recover_marginals` must be an idata")
raise TypeError("The order of arguments of `recover_marginals` changed. The first input must be an idata")


model = modelcontext(model)

unmarginal_model = unmarginalize(model)

# Find the names of the marginalized variables
Expand Down
11 changes: 7 additions & 4 deletions tests/model/marginal/test_marginal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,9 @@ def test_basic(self):
)
idata = InferenceData(posterior=dict_to_dataset(prior))

idata = recover_marginals(marginal_m, idata, return_samples=True)
with marginal_m:
idata = recover_marginals(idata, return_samples=True)

post = idata.posterior
assert "k" in post
assert "lp_k" in post
Expand Down Expand Up @@ -881,7 +883,8 @@ def test_coords(self):
posterior=dict_to_dataset({k: np.expand_dims(prior[k], axis=0) for k in prior})
)

idata = recover_marginals(marginal_m, idata, return_samples=True)
with marginal_m:
idata = recover_marginals(idata, return_samples=True)
post = idata.posterior
assert post.idx.dims == ("chain", "draw", "year")
assert post.lp_idx.dims == ("chain", "draw", "year", "lp_idx_dim")
Expand All @@ -907,7 +910,7 @@ def test_batched(self):
posterior=dict_to_dataset({k: np.expand_dims(prior[k], axis=0) for k in prior})
)

idata = recover_marginals(marginal_m, idata, return_samples=True)
idata = recover_marginals(idata, return_samples=True)
post = idata.posterior
assert post["y"].shape == (1, 20, 2, 3)
assert post["idx"].shape == (1, 20, 3, 2)
Expand All @@ -933,7 +936,7 @@ def test_nested(self):
)
idata = InferenceData(posterior=dict_to_dataset(prior))

idata = recover_marginals(marginal_m, idata, return_samples=True)
idata = recover_marginals(idata, return_samples=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we testing the explicit model kwarg anywhere now?

post = idata.posterior
assert "idx" in post
assert "lp_idx" in post
Expand Down
Loading