joshspeagle / dynesty

Dynamic Nested Sampling package for computing Bayesian posteriors and evidences
https://dynesty.readthedocs.io/
MIT License
347 stars 76 forks source link

Replace `pickle` with `dill` as `pickle_module` in `dynesty/utils.py` #419

Closed dilpath closed 1 year ago

dilpath commented 1 year ago

Dynesty version pip install dynesty==2.0.3

Describe the bug In my use of the dynesty.utils.save_sampler method, it fails with the following error.

  File "dynesty/utils.py", line 2280, in save_sampler
    pickle_module.dump(D, fp)
AttributeError: Can't pickle local object 'DynestySampler.initialize.<locals>.loglikelihood'

As described by @segasai [1], if I simply pickle the sampler with dill, it works without error. It would be great if the default pickle module could be changed to dill.

[1] https://github.com/joshspeagle/dynesty/pull/386#issuecomment-1230885706

segasai commented 1 year ago

I believe it's possible to use dill, if before running your code you change the pickle_module object in dynesty.utils That's a bit of an unofficial interface, but it works and it was written specifically for that. I think we may need some way official API for it, but I'm not sure it's a big deal.

import dill
import dynesty.utils
dynesty.utils.pickle_module=dill
dilpath commented 1 year ago

Thanks! Works well for me :+1: