pymc-devs / pymc-devs/pymc-examples

Error in log_gaussian_cox_process Notebook: TypeError: DictConverter.posterior_predictive is not a dictionary

Open
#453 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
398
Forks
325
Avg merge
9d 15m
Merged PRs (30d)
1

Description

Error in log_gaussian_cox_process Notebook:
pymc-examples/examples/case_studies:

Issue description

When switching to PyMC v4.2.2 in log_gaussian_cox_process Notebook, I get in cell 19
with the call

spp_trace = pm.sample_posterior_predictive(
trace, var_names=["log_intensity_new"], keep_size=True
)

the following issue :


TypeError Traceback (most recent call last)
Cell In [19], line 13
7 intensity_new = gp.conditional("log_intensity_new", Xnew=xy_new)
9 spp_trace = pm.sample_posterior_predictive(
10 trace, var_names=["log_intensity_new"], keep_size=True
11 )
12 trace.extend(
---> 13 az.from_dict(posterior_predictive=spp_trace, dims={"log_intensity_new": ["sample"]})
14 )
15 intensity_samples = np.exp(trace.posterior_predictive["log_intensity_new"])

File ~/mambaforge/envs/pymc_env/lib/python3.10/site-packages/arviz/data/io_dict.py:460, in from_dict(posterior, posterior_predictive, predictions, sample_stats, log_likelihood, prior, prior_predictive, sample_stats_prior, observed_data, constant_data, predictions_constant_data, warmup_posterior, warmup_posterior_predictive, warmup_predictions, warmup_log_likelihood, warmup_sample_stats, save_warmup, index_origin, coords, dims, pred_dims, pred_coords, attrs, **kwargs)
353 def from_dict(
354 posterior=None,
355 *,
(...)
378 **kwargs,
379 ):
380 """Convert Dictionary data into an InferenceData object.
381
382 For a usage example read the
(...)
433 InferenceData
434 """
435 return DictConverter(
436 posterior=posterior,
437 posterior_predictive=posterior_predictive,
438 predictions=predictions,
439 sample_stats=sample_stats,
440 log_likelihood=log_likelihood,
441 prior=prior,
442 prior_predictive=prior_predictive,
443 sample_stats_prior=sample_stats_prior,
444 observed_data=observed_data,
445 constant_data=constant_data,
446 predictions_constant_data=predictions_constant_data,
447 warmup_posterior=warmup_posterior,
448 warmup_posterior_predictive=warmup_posterior_predictive,
449 warmup_predictions=warmup_predictions,
450 warmup_log_likelihood=warmup_log_likelihood,
451 warmup_sample_stats=warmup_sample_stats,
452 save_warmup=save_warmup,
453 index_origin=index_origin,
454 coords=coords,
455 dims=dims,
456 pred_dims=pred_dims,
457 pred_coords=pred_coords,
458 attrs=attrs,
459 **kwargs,
--> 460 ).to_inference_data()

File ~/mambaforge/envs/pymc_env/lib/python3.10/site-packages/arviz/data/io_dict.py:338, in DictConverter.to_inference_data(self)
327 def to_inference_data(self):
328 """Convert all available data to an InferenceData object.
329
330 Note that if groups can not be created, then the InferenceData
331 will not have those groups.
332 """
333 return InferenceData(
334 **{
335 "posterior": self.posterior_to_xarray(),
336 "sample_stats": self.sample_stats_to_xarray(),
337 "log_likelihood": self.log_likelihood_to_xarray(),
--> 338 "posterior_predictive": self.posterior_predictive_to_xarray(),
339 "predictions": self.predictions_to_xarray(),
340 "prior": self.prior_to_xarray(),
341 "sample_stats_prior": self.sample_stats_prior_to_xarray(),
342 "prior_predictive": self.prior_predictive_to_xarray(),
343 "observed_data": self.observed_data_to_xarray(),
344 "constant_data": self.constant_data_to_xarray(),
345 "predictions_constant_data": self.predictions_constant_data_to_xarray(),
346 "save_warmup": self.save_warmup,
347 "attrs": self.attrs,
348 }
349 )

File ~/mambaforge/envs/pymc_env/lib/python3.10/site-packages/arviz/data/base.py:65, in requires.call..wrapped(cls)
63 if all((getattr(cls, prop_i) is None for prop_i in prop)):
64 return None
---> 65 return func(cls)

File ~/mambaforge/envs/pymc_env/lib/python3.10/site-packages/arviz/data/io_dict.py:192, in DictConverter.posterior_predictive_to_xarray(self)
190 data_warmup = self._init_dict(f"{WARMUP_TAG}posterior_predictive")
191 if not isinstance(data, dict):
--> 192 raise TypeError("DictConverter.posterior_predictive is not a dictionary")
193 if not isinstance(data_warmup, dict):
194 raise TypeError("DictConverter.warmup_posterior_predictive is not a dictionary")

TypeError: DictConverter.posterior_predictive is not a dictionary

Therefore, the promised API compatibly to PyMC3 is not completely given.
Which kind of code changes are necessary in the Notebook to get it run?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the log_gaussian_cox_process notebook and start at cell 19, where pm.sample_posterior_predictive is passed to az.from_dict. Check the returned value and the surrounding trace handling. Done means the notebook runs with PyMC v4.2.2 and produces intensity_samples without the DictConverter type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.