LoLab-MSM / PyDREAM

MT-DREAM(ZS) algorithm for model optimization, calibration, selection
GNU General Public License v3.0
50 stars 33 forks source link

Invalid filename for chain history #19

Open mbraakhekke opened 4 years ago

mbraakhekke commented 4 years ago

Hi,

I'm getting an error because the filename generated for the chain history file contains a colon (:), as part of the timestamp.

My code and error message (for one thread) are added below.

cheers, Maarten

from pydream.core import run_dream
from pydream.parameters import FlatParam
import numpy as np
from scipy.stats import norm, uniform

mu = 0
var = 1

def Latin_hypercube(minn, maxn, N):
    y = np.random.rand(N, len(minn))
    x = np.zeros((N, len(minn)))

    for j in range(len(minn)):
        idx = np.random.permutation(N)
        P = (idx - y[:,j])/N
        x[:,j] = minn[j] + P * (maxn[j] - minn[j])

    return x

if __name__ == '__main__':
    history_seed = Latin_hypercube([-1],[1],50)
    np.save('history_seed.npy',history_seed)
    param = FlatParam(np.array(0))
    sampled_params, log_ps = run_dream(param, likelihood, 
              start = np.array(0), 
              start_random = False, 
              niterations = 1000,
              verbose = False,
              history_file = 'history_seed.npy')
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\multiprocessing\pool.py", line 44, in mapstar
    return list(map(*args))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pydream\core.py", line 127, in _sample_dream
    raise e
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pydream\core.py", line 114, in _sample_dream
    sampled_params[iteration], log_prior , log_like = step_fxn(q0)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pydream\Dream.py", line 421, in astep
    raise e
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pydream\Dream.py", line 362, in astep
    self.record_history(self.nseedchains, self.total_var_dimension, q_new, self.len_history)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pydream\Dream.py", line 945, in record_history
    self.save_history_to_disc(np.frombuffer(Dream_shared_vars.history.get_obj()), prefix)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pydream\Dream.py", line 959, in save_history_to_disc
    np.save(filename, history)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\numpy\lib\npyio.py", line 517, in save
    fid = open(file, "wb")
OSError: [Errno 22] Invalid argument: '2020_04_26_18:58:29_DREAM_chain_history.npy'
ortega2247 commented 4 years ago

Hi,

Thanks for reporting this issue. The fastest workaround for this problem is to pass a str to themodel_name argument in the run_dream function:

sampled_params, log_ps = run_dream(param, likelihood, 
          start = np.array(0), 
          start_random = False, 
          niterations = 1000,
          verbose = False,
          history_file = 'history_seed.npy',
          model_name='pydream_example')

That way pydream would use pydream_example as the prefix for all output files.

I am going to leave this issue open as I think this should be fixed in pydream anyway.

Please let me know if you have any other questions of issues.

Best,

Oscar

mbraakhekke commented 4 years ago

Thanks. The workaround solves it.