Search code examples
pythonnumpyerror-handlingscipyemcee

Retry until no RuntimeWarning


I'm trying to fit a curve to some data using MCMC.

Due to the nature of my particular problem, occasionally (1/5 times running the code) some singularities are encountered and the code raises me a RuntimeWarning and proceeds to give a wrong answer.

/Library/Python/2.7/site-packages/emcee-2.2.1-py2.7.egg/emcee/ensemble.py:335: RuntimeWarning: invalid value encountered in subtract
/Library/Python/2.7/site-packages/emcee-2.2.1-py2.7.egg/emcee/ensemble.py:336: RuntimeWarning: invalid value encountered in greater

This basically happens because I'm taking a log of a gaussian and one of proposed values of means is equal to one of the data points.

I would like to retry running the code, perhaps using try and except, until these Runtime Warnings do not occur. Thanks!

Edit: Following a comment by @sgDysregulation I have tried:

while True:
    try:
        print "Before mcmc"
        sampler.run_mcmc(pos, 500)
        print "After mcmc"
        break
    except Exception as e:
        print "Warning detected"
        continue

where I have tried using both "pass" and "continue" statements, putting "break" inside while loop and inside "try". Also tried "RuntimeWarning" instead of "Exception".

The output from snippet above doesn't show any warnings have been detected.

Before mcmc
/Library/Python/2.7/site-packages/emcee-2.2.1-py2.7.egg/emcee/ensemble.py:335: RuntimeWarning: invalid value encountered in subtract
/Library/Python/2.7/site-packages/emcee-2.2.1-py2.7.egg/emcee/ensemble.py:336: RuntimeWarning: invalid value encountered in greater
After mcmc

Solution

  • You can use the np.errstate context manager to catch the warning as though it were an exception:

    while True:
        try:
            print("Before mcmc")
            with np.errstate(all='raise'):
                sampler.run_mcmc(pos, 500)
            print("After mcmc")
            break
        except Exception:
            print("Warning detected")
            continue