def lnprob(theta, *args):
    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf

    return lnlike(theta, *args) + lp


if __name__ == '__main__':
    # create model
    ftot = 3.
    fwhm = 37.
    snr = 100
    dx = 4
    nx = 32
    m = model(ftot, fwhm, nx=nx, dx=dx)
    #mNoisy = initialize(m.source,dx)
    mNoisy = addNoise(m.source, dx)
    noise = mNoisy - m.source

    plt.subplot(121)
    plt.imshow(m.source)
    plt.subplot(122)
    plt.imshow(mNoisy)

    plt.figure()
    plt.imshow(noise)
    #plt.show()

    # fetch scattering kernel
    sigma = getKernel()
def lnprob(theta,*args):
    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf

    return -1 * (lnlike(theta,*args) + lp)

if __name__ == '__main__':
    # create model
    ftot = 3.
    fwhm = 37.
    snr = 100
    #dx = 1; nx = 128;
    dx = 4; nx = 32;
    m = model(ftot,fwhm,nx=nx,dx=dx)
    #mNoisy = initialize(m.source,dx)
    mNoisy = addNoise(m.source,dx)
    noise = mNoisy - m.source

    plt.subplot(121); plt.imshow(m.source)
    plt.subplot(122); plt.imshow(mNoisy)

    plt.figure(); plt.imshow(noise)
    #plt.show()

    # fetch scattering kernel
    sigma = getKernel(__screenfile__)
    kernel = m.broaden(sigma)

    # estimate noise
def lnprob(theta,m,d,w):
    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf

    return lnlike(theta,m,d,w) + lp

if __name__ == '__main__':

    # create our model
    nx = 128
    dx = 1.
    ftot = 3.
    fwhm = 37.
    snr = 100
    m = model(ftot,fwhm,nx,dx)
    #w = m/m.sum()

    # add noise
    noise = m.source.max()/snr*np.random.randn(m.nx,m.nx)
    mNoisy = m.source + noise
    w = 1./noise.std()**2

    # initialize sampler
    initial = [ftot,fwhm]
    ndim = len(initial)
    nwalkers = 100
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, \
            args=[m,mNoisy,w])

    # burn

def lnprob(theta, *args):
    """ minimize this """
    lp = lnprior(theta)
    ans = (lp + lnlike(theta, *args)) if np.isfinite(lp) else -np.inf
    return -1 * ans


if __name__ == "__main__":
    # create model
    ftot = 3.0
    fwhm = 37.0
    dx = 4.0
    nx = 32
    m = model(ftot, fwhm, dx=dx, nx=nx)
    # mNoisy = initialize(m,dx)
    mNoisy = addNoise(m.source, dx)
    mTrue = m.source
    noise = mNoisy - m.source
    mTrue = m.source.copy()

    # check that our model and simulation have matching resolutions
    hdr = slimscat.fetch_hdr(__screenfile__)
    assert hdr["dx"] == dx

    # plt.subplot(121); plt.imshow(m.source); plt.subplot(122); plt.imshow(mNoisy)
    # plt.figure(); plt.imshow(noise)
    # plt.show()

    # fetch scattering kernel
    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf

    return lnlike(theta, m, d, w) + lp


if __name__ == '__main__':

    # create our model
    nx = 128
    dx = 1.
    ftot = 3.
    fwhm = 37.
    snr = 100
    m = model(ftot, fwhm, nx, dx)
    #w = m/m.sum()

    # add noise
    noise = m.source.max() / snr * np.random.randn(m.nx, m.nx)
    mNoisy = m.source + noise
    w = 1. / noise.std()**2

    # initialize sampler
    initial = [ftot, fwhm]
    ndim = len(initial)
    nwalkers = 100
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, \
            args=[m,mNoisy,w])

    # burn
    #return -0.5 * np.sum(w*(mBroad-d)**2)

def lnprob(theta,*args):
    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf

    return lnlike(theta,*args) + lp

if __name__ == '__main__':
    # create model
    ftot = 3.
    fwhm = 37.
    snr = 100
    dx = 4; nx = 32;
    m = model(ftot,fwhm,nx=nx,dx=dx)
    #mNoisy = initialize(m.source,dx)
    mNoisy = addNoise(m.source,dx)
    noise = mNoisy - m.source

    plt.subplot(121); plt.imshow(m.source)
    plt.subplot(122); plt.imshow(mNoisy)

    plt.figure(); plt.imshow(noise)
    #plt.show()

    # fetch scattering kernel
    sigma = getKernel()
    kernel = m.broaden(sigma)

    # estimate noise
def lnprob(theta, *args):
    ''' minimize this '''
    lp = lnprior(theta)
    ans = (lp + lnlike(theta, *args)) \
     if np.isfinite(lp) else -np.inf
    return -1 * ans


if __name__ == '__main__':
    # create model
    ftot = 3.
    fwhm = 37.
    dx = 4.
    nx = 32
    m = model(ftot, fwhm, dx=dx, nx=nx)
    #mNoisy = initialize(m,dx)
    mNoisy = addNoise(m.source, dx)
    mTrue = m.source
    noise = mNoisy - m.source
    mTrue = m.source.copy()

    # check that our model and simulation have matching resolutions
    hdr = slimscat.fetch_hdr(__screenfile__)
    assert hdr['dx'] == dx

    #plt.subplot(121); plt.imshow(m.source); plt.subplot(122); plt.imshow(mNoisy)
    #plt.figure(); plt.imshow(noise)
    #plt.show()

    # fetch scattering kernel