joshspeagle / dynesty

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

Resuming of interrupted runs #386

Closed segasai closed 2 years ago

segasai commented 2 years ago

This a preliminary version of code that allows resuming of dynesty runs. The current interface is this is to run:

    dns.run_nested(checkpoint_file='xx.pkl')

And to resume

   dns = dds.DynamicSampler.restore('xx.pkl')
    dns.run_nested(resume=True)

This was used to test it

import dynesty
import dynesty.dynamicsampler as dds
import numpy as np
import multiprocessing as mp
import os, time

def like(x):
    return -.5 * np.sum(x**2)

def ptform(x):
    return 20 * x - 10

def interrupter(pid, dt):
    time.sleep(dt)
    os.kill(pid, 9)

def start_interrupter(pid, dt):
    pp = mp.Process(target=interrupter, args=(pid, dt))
    pp.start()
    return pp

def fit():
    ndim = 2
    dns = dynesty.DynamicNestedSampler(like,
                                       ptform,
                                       ndim,
                                       nlive=100,
                                       rstate=np.random.default_rng(1))
    dns.run_nested(checkpoint_file='xx.pkl')

def fit_resume():
    dns = dds.DynamicSampler.restore('xx.pkl')
    print('resuming')
    dns.run_nested(resume=True)

def doall():
    pp = mp.Process(target=fit)
    pp.start()
    fit_pid = pp.pid
    delay = 15
    pp = start_interrupter(fit_pid, delay)
    time.sleep(delay + 1)
    fit_resume()

doall()

There are quite a few things that need fixing.

I only implemented interruption for run_nested interface. I don't plan to implement interruption of an iterator interface.

coveralls commented 2 years ago

Pull Request Test Coverage Report for Build 2957311805


Changes Missing Coverage Covered Lines Changed/Added Lines %
py/dynesty/dynamicsampler.py 130 133 97.74%
py/dynesty/utils.py 41 49 83.67%
py/dynesty/dynesty.py 132 142 92.96%
<!-- Total: 319 340 93.82% -->
Files with Coverage Reduction New Missed Lines %
py/dynesty/sampler.py 2 91.16%
<!-- Total: 2 -->
Totals Coverage Status
Change from base Build 2911341179: 0.04%
Covered Lines: 3831
Relevant Lines: 4230

💛 - Coveralls
segasai commented 2 years ago

This is starting to get ready for review.

segasai commented 2 years ago

Thanks for taking a look!

segasai commented 2 years ago

I think this is ready for a final review and merge.

If we feel that more docs/examples are needed that may be done after the merge. The docs for the branch should be available here https://dynesty.readthedocs.io/en/resuming/

segasai commented 2 years ago

The only thing extra I can think of here, is of some way of usilng say dill instead of pickle. I don't quite know if it's better to just always use dill or think of an option to switch the pickler. (but this stuff should go after this is merged)

segasai commented 2 years ago

Hi @joshspeagle If you could take a look/rereview the PR that would be great, otherwise, I will aim to merge this early next week. Thanks

segasai commented 2 years ago

Thanks, @joshspeagle ! Merging