pymc-devs / pymc-examples

Examples of PyMC models, including a library of Jupyter notebooks.
https://www.pymc.io/projects/examples/en/latest/
MIT License
269 stars 234 forks source link

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

Open tommy3001 opened 1 year ago

tommy3001 commented 1 year ago

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?

OriolAbril commented 1 year ago

Therefore, the promised API compatibly to PyMC3 is not completely given.

The APIs are similar instead of not having anything to do (like pymc 2 and 3) but it is still a major version with many breaking changes.

The notebook has been updated to one of the beta releases: https://www.pymc.io/projects/examples/en/latest/gaussian_processes/log-gaussian-cox-process.html. Now the code should work but you will need to remove keep_size argument in the latest versions.