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

fix io_pymc3 bug when model not passed explicitly #1240

Merged
merged 2 commits into from
Jun 17, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* loo-pit plot. The kde is computed over the data interval (this could be shorter than [0, 1]). The hdi is computed analitically (#1215)
* Added `html_repr` of InferenceData objects for jupyter notebooks. (#1217)
* Added support for PyJAGS via the function `from_pyjags` in the module arviz.data.io_pyjags. (#1219)
* `from_pymc3` can now retrieve `coords` and `dims` from model context (#1228
and #1240)

### Maintenance and fixes
* Include data from `MultiObservedRV` to `observed_data` when using
Expand Down
8 changes: 4 additions & 4 deletions arviz/data/io_pymc3.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ def arbitrary_element(dct: Dict[Any, np.ndarray]) -> np.ndarray:
self.ndraws = aelem.shape[0]

self.coords = coords
if coords is None and hasattr(model, "coords"):
self.coords = model.coords
if coords is None and hasattr(self.model, "coords"):
self.coords = self.model.coords

self.dims = dims
if dims is None and hasattr(model, "RV_dims"):
self.dims = {k: list(v) for k, v in model.RV_dims.items()}
if dims is None and hasattr(self.model, "RV_dims"):
self.dims = {k: list(v) for k, v in self.model.RV_dims.items()}

self.observations, self.multi_observations = self.find_observations()

Expand Down
11 changes: 9 additions & 2 deletions arviz/tests/external_tests/test_data_pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ def test_posterior_predictive_warning(self, data, eight_schools_params, caplog):
packaging.version.Version(pm.__version__) < packaging.version.Version("3.9.0"),
reason="Requires PyMC3 >= 3.9.0",
)
def test_autodetect_coords_from_model(self):
@pytest.mark.parametrize("use_context", [True, False])
def test_autodetect_coords_from_model(self, use_context):
df_data = pd.DataFrame(columns=["date"]).set_index("date")
dates = pd.date_range(start="2020-05-01", end="2020-05-20")
for city, mu in {"Berlin": 15, "San Marino": 18, "Paris": 16}.items():
Expand Down Expand Up @@ -231,8 +232,14 @@ def test_autodetect_coords_from_model(self):
draws=30,
step=pm.Metropolis(),
)
idata = from_pymc3(trace=trace, model=model)
if use_context:
idata = from_pymc3(trace=trace)
if not use_context:
idata = from_pymc3(trace=trace, model=model)

assert "city" in list(idata.posterior.dims)
assert "city" in list(idata.observed_data.dims)
assert "date" in list(idata.observed_data.dims)
np.testing.assert_array_equal(idata.posterior.coords["city"], coords["city"])
np.testing.assert_array_equal(idata.observed_data.coords["date"], coords["date"])
np.testing.assert_array_equal(idata.observed_data.coords["city"], coords["city"])
Expand Down