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

supressing ArviZ warning and allowing step selection #213

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
23 changes: 18 additions & 5 deletions pymc_experimental/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import hashlib
import json
import warnings
from abc import abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -290,12 +291,18 @@ def sample_model(self, **kwargs):
raise RuntimeError(
"The model hasn't been built yet, call .build_model() first or call .fit() instead."
)

with self.model:
sampler_args = {**self.sampler_config, **kwargs}
idata = pm.sample(**sampler_args)
idata.extend(pm.sample_prior_predictive())
idata.extend(pm.sample_posterior_predictive(idata))
if "step" in sampler_args:
step_function_name = sampler_args["step"]
step_function = getattr(pm, step_function_name)
sampler_args["step"] = step_function()
Copy link
Member

@ricardoV94 ricardoV94 Jul 11, 2023

Choose a reason for hiding this comment

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

step_function may need arguments as well

idata = pm.sample(**sampler_args)
idata.extend(pm.sample_prior_predictive())
idata.extend(pm.sample_posterior_predictive(idata))
sampler_args["step"] = step_function_name
else:
idata = pm.sample(**sampler_args)

idata = self.set_idata_attrs(idata)
return idata
Expand Down Expand Up @@ -496,7 +503,13 @@ def fit(
X_df = pd.DataFrame(X, columns=X.columns)
combined_data = pd.concat([X_df, y], axis=1)
assert all(combined_data.columns), "All columns must have non-empty names"
self.idata.add_groups(fit_data=combined_data.to_xarray()) # type: ignore
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=UserWarning,
message="The group fit_data is not defined in the InferenceData scheme",
)
self.idata.add_groups(fit_data=combined_data.to_xarray()) # type: ignore

return self.idata # type: ignore

Expand Down
9 changes: 9 additions & 0 deletions pymc_experimental/tests/test_model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,12 @@ def test_id():
).hexdigest()[:16]

assert model_builder.id == expected_id


def test_step_selection_in_sample_config(toy_X, toy_y):
sampler_config = {
"step": "Slice",
}
model = test_ModelBuilder(sampler_config=sampler_config)
model.fit(toy_X, toy_y)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe monkeypatch pm.sample to confirm it was called with step and that's instance(step, Slice)?

assert model.idata is not None