emcee icon indicating copy to clipboard operation
emcee copied to clipboard

ValueError: set_state can only be used with legacy MT19937state instances.

Open ogoann opened this issue 6 years ago • 1 comments

General information:

  • emcee version: 3.0.1
  • platform: Scientific Linux 7.7, python 3.7.3
  • installation method (pip/conda/source/other?): conda forge

Problem description: When opening old chains saved as h5 files with python 2.7, I get this error: ValueError: set_state can only be used with legacy MT19937state instances.

What have you tried so far?:

I think this is python 2 vs 3 issue, since the variable state[0] in EnsembleSampler is a string. Saved with python 2 and read with python 3, it's a binary string, and thus needs to be converted to a proper string. This could be fixed by e.g. adding this line:

state[0]=state[0].decode()

before line 147 in ensemble.py.

Minimal example:

import emcee

filename = 'chain.h5' # already existing chain file, saved with python 2.7
backend = emcee.backends.HDFBackend(filename)
nwalkers = backend.shape[0]
ndim = backend.shape[1]
#following line results in error
sampler = emcee.EnsembleSampler(nwalkers, ndim, None, backend=backend)

Traceback

Traceback (most recent call last):
  File "example.py", line 8, in <module>
    sampler = emcee.EnsembleSampler(nwalkers, ndim, None, backend=backend)
  File "/anaconda3/lib/python3.7/site-packages/emcee/ensemble.py", line 147, in __init__
    self._random.set_state(state)
  File "mtrand.pyx", line 276, in numpy.random.mtrand.RandomState.set_state
ValueError: set_state can only be used with legacy MT19937state instances.

ogoann avatar Nov 25 '19 18:11 ogoann

Excellent. Thanks for this update! This gives me enough to track this down and test it appropriately. I'll let you know if I need more info. Thanks!

dfm avatar Nov 25 '19 18:11 dfm