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

enabled type checking for io_pymc3 input #1629

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Fix `ess/rhat` plots in `plot_forest` ([1606](https://github.com/arviz-devs/arviz/pull/1606))
* Fix `from_numpyro` crash when importing model with `thinning=x` for `x > 1` ([1619](https://github.com/arviz-devs/arviz/pull/1619))
* Upload updated mypy.ini in ci if mypy copilot fails ([1624](https://github.com/arviz-devs/arviz/pull/1624))
* Enforced using `pymc3.backends.base.MultiTrace` as the `trace` value for `io_pymc3` ([1629](https://github.com/arviz-devs/arviz/pull/1629))
Copy link
Member

Choose a reason for hiding this comment

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

I would say something along the lines of raising an informative error if inferencedata is passed


### Deprecation
* Deprecated `index_origin` and `order` arguments in `az.summary` ([1201](https://github.com/arviz-devs/arviz/pull/1201))
Expand Down
6 changes: 6 additions & 0 deletions arviz/data/io_pymc3.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def __init__(
# way to access the model from the trace.
self.attrs = None
if trace is not None:
if isinstance(self.trace, InferenceData):
raise ValueError(
"Using the `InferenceData` as a `trace` argument won't work. "
"Please use the `arviz.InferenceData.extend` method to extend the "
"`InferenceData` with groups from another `InferenceData`."
)
if self.model is None:
self.model = list(self.trace._straces.values())[ # pylint: disable=protected-access
0
Expand Down
10 changes: 10 additions & 0 deletions arviz/tests/external_tests/test_data_pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def test_from_pymc_predictions(self, data, eight_schools_params):
assert ivalues.shape[0] == 1 # one chain in predictions
assert np.all(np.isclose(ivalues[0], values))

def test_from_pymc_trace_inference_data(self):
# check if error is raised successfully after passing InferenceData as trace
Copy link
Member

Choose a reason for hiding this comment

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

This should be a docstring using """ instead of a comment

with pm.Model():
p = pm.Uniform("p", 0, 1)
pm.Binomial("w", p=p, n=2, observed=1)
trace = pm.sample(100, chains=2, return_inferencedata=True)
assert isinstance(trace, InferenceData)
Copy link
Member

Choose a reason for hiding this comment

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

I think we don't need to sample from a model, we can use an example inferencedata or create it with from_dict and then do az.from_pymc3(idata, model=pm.Model()) directly

with pytest.raises(ValueError):
from_pymc3(trace=trace)

def test_from_pymc_predictions_new(self, data, eight_schools_params):
# check creating new
inference_data, posterior_predictive = self.make_predictions_inference_data(
Expand Down