joshspeagle / dynesty

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

Renaming pickle file from .tmp to .pickle in save_sampler() fails in Windows. #450

Closed rodleiva closed 1 year ago

rodleiva commented 1 year ago

Dynesty version Specify the exact version, and how you installed it (pip/git) 2.1.2 installed with git

Describe the bug Saving the pickle file fails if the file already exists. This happens only on Windows but not in Linux. I corroborated that the problem is the os.rename() python function in utils.py save_sampler(), line 2326. It works fine when replaced by os.replace()

Setup I am runningthe following dynesty setup: [insert a copy of the dynesty code used during initialization and runtime] Not relevant, the problem is with the underlying os.rename() in python

Dynesty output Show (at least the last few lines of) dynesty output

sampler.run_nested(nlive_init.n_live, dlogz_init.dlogz_init, File "C:\Users\Pedro\Desktop\20221104_470309\myvenv\lib\site-packages\dynesty\dynamicsampler.py", line 2060, in run_nested self. save(checkpoint_file) File "C:\Users\Pedro\Desktop\20221104_470309\myvenv\lib\site-packages\dynesty\dynamicsampler.py", line 1015, in save save_sampler(self, (name) File "C:\Users\Pedro\Desktop\20221104_470309\myvenv\lib\site-packages\dynesty\utils.py", line 2326, in save_sampler os.rename(tmpiname, (name) FileExistsError: [WinError 183] No se puede crear un archivo que ya existe: './data/checkpoint_it112p.pickle.tmp' -> './data/che kpoint_it112p.pickle

Bug I find [describe bug] which gives the following error: The pickle file is saved only the first time. The second time and after it fails in the line 2326 of utils.py because os.rename() is unable to overwrite the existing .pickle file. This is a problem with os.rename() in Windows.

\myvenv\lib\site-packages\dynesty\dynamicsampler.py", line 1015, in save save_sampler(self, (name) File "C:\Users\Pedro\Desktop\20221104_470309\myvenv\lib\site-packages\dynesty\utils.py", line 2326, in save_sampler os.rename(tmpiname, (name) FileExistsError: [WinError 183] No se puede crear un archivo que ya existe: './data/checkpoint_it112p.pickle.tmp' -> './data/che kpoint_it112p.pickle

The behaviour I expected was [additional description]. The expected behaviour is that after the dump into the .tmp file is completed, it is copied into the .pickle file, overwriting the existing one.

To reproduce the behaviour I see, please see [progress report/attached file/description]. The error with os.rename() can be reproduced in a few steps

import os import numpy myarray = numpy.ones((10, 10))

numpy.savetxt('test_delete.tmp', array)

First time it works because .dat doesn't exist yet

os.rename('test_delete.tmp', 'test_delete.dat')

numpy.savetxt('test_delete.tmp', array)

Second time it fails (in Windows) because .dat already exists

os.rename('test_delete.tmp', 'test_delete.dat')

Additional context [Add any other context about the problem here.] Using os.replace() works fine in Linux and Windows.

segasai commented 1 year ago

Thanks for the report! This 3b543e7 should fix it.

segasai commented 1 year ago

I assume that it is fixed.