def psf_minus_model_hists(epoch, model, features, filters, fgal=0.5,
                          idx=-1):
    """
    Plot the histogram of psf-model for the data and denoised data.
    """
    fs = 5
    w = 0.1
    nb = 50
    mags = [19.5, 20.5, 21.5]

    Xsingle, Xsinglecov = fetch_prepped_s82data(epoch, fgal, features, filters)
    Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch, fgal, features, filters,
                                              use_single=False)

    f = pl.figure(figsize=(3 * fs, fs))
    for i in range(len(mags)):
        ind = (Xsingle[:, 0] > mags[i] - w) & (Xsingle[:, 0] < mags[i] + w)
        #ind = ind & (Xsingle[:, idx] < 0.3)
        ind = ind & (Xcoadd[:, idx] < 0.03)
        a, m, v = model.posterior(Xsingle[ind], Xsinglecov[ind])
        posts = np.zeros_like(Xsingle[ind])
        for j in range(Xsingle[ind].shape[0]):
            posts[j] = np.median(model.sample(a[j], m[j], v[j], size=1000),
                                 axis=0)

        print i, mags[i]
        pl.subplot(1, 3, i + 1)
        pl.hist(Xsingle[ind, idx], nb, alpha=0.3, color='k')
        pl.hist(posts[:, idx], nb, alpha=0.3, color='r')
        pl.title('$r=%0.1f$' % mags[i])
        pl.xlim(-0.2, 0.2)
        pl.xlabel('psf - model')
    f.savefig('../../plots/phot_and_morph/foo.png')
def psf_minus_model_hists(epoch, model, features, filters, fgal=0.5, idx=-1):
    """
    Plot the histogram of psf-model for the data and denoised data.
    """
    fs = 5
    w = 0.1
    nb = 50
    mags = [19.5, 20.5, 21.5]

    Xsingle, Xsinglecov = fetch_prepped_s82data(epoch, fgal, features, filters)
    Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch,
                                              fgal,
                                              features,
                                              filters,
                                              use_single=False)

    f = pl.figure(figsize=(3 * fs, fs))
    for i in range(len(mags)):
        ind = (Xsingle[:, 0] > mags[i] - w) & (Xsingle[:, 0] < mags[i] + w)
        #ind = ind & (Xsingle[:, idx] < 0.3)
        ind = ind & (Xcoadd[:, idx] < 0.03)
        a, m, v = model.posterior(Xsingle[ind], Xsinglecov[ind])
        posts = np.zeros_like(Xsingle[ind])
        for j in range(Xsingle[ind].shape[0]):
            posts[j] = np.median(model.sample(a[j], m[j], v[j], size=1000),
                                 axis=0)

        print i, mags[i]
        pl.subplot(1, 3, i + 1)
        pl.hist(Xsingle[ind, idx], nb, alpha=0.3, color='k')
        pl.hist(posts[:, idx], nb, alpha=0.3, color='r')
        pl.title('$r=%0.1f$' % mags[i])
        pl.xlim(-0.2, 0.2)
        pl.xlabel('psf - model')
    f.savefig('../../plots/phot_and_morph/foo.png')
def plot_contours_and_data(epoch, model, features, filters, fgal=0.5, idx=-1):
    """
    Plot the data and the contours for stars and galaxies.
    """
    from astroML.plotting.tools import draw_ellipse

    Xsingle, Xsinglecov = fetch_prepped_s82data(epoch, fgal, features, filters)
    Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch, fgal, features, filters,
                                              use_single=False)

    sind = Xcoadd[:, idx] < 0.03
    gind = Xcoadd[:, idx] > 0.03

    fs = 5
    ms = 1
    f = pl.figure(figsize=(3 * fs, 2 * fs))
    Nstar = len(np.where(model.fixed_means[:, idx] != np.inf)[0])
    pl.subplot(231)
    idx = [0, -1]
    for i in range(Nstar):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx], model.V[i, idx][:, idx], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.plot(Xsingle[sind][::10, idx[0]], Xsingle[sind][::10, idx[1]], '.k',
            ms=ms)
    pl.xlim(18,22)
    pl.ylim(-0.1, 0.5)
    pl.subplot(232)
    idx = [2, 1]
    for i in range(Nstar):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx], model.V[i, idx][:, idx], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.plot(Xsingle[sind][::10, idx[0]], Xsingle[sind][::10, idx[1]], '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 6)
    pl.subplot(233)
    idx = [3, 4]
    for i in range(Nstar):
        draw_ellipse(model.mu[i, idx], model.V[i, idx][:, idx], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.plot(Xsingle[sind][::10, idx[0]], Xsingle[sind][::10, idx[1]], '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 3)
    pl.subplot(234)
    idx = [0, -1]
    for i in range(Nstar, model.n_components):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx], model.V[i, idx][:, idx], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.plot(Xsingle[gind][::10, idx[0]], Xsingle[gind][::10, idx[1]], '.k',
            ms=ms)
    pl.xlim(18,22)
    pl.ylim(-0.1, 0.5)
    pl.subplot(235)
    idx = [2, 1]
    for i in range(Nstar, model.n_components):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx], model.V[i, idx][:, idx], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.plot(Xsingle[gind][::10, idx[0]], Xsingle[gind][::10, idx[1]], '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 6)
    pl.subplot(236)
    idx = [3, 4]
    for i in range(Nstar, model.n_components):
        draw_ellipse(model.mu[i, idx], model.V[i, idx][:, idx], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.plot(Xsingle[gind][::10, idx[0]], Xsingle[gind][::10, idx[1]], '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 3)
    f.savefig('../../plots/phot_and_morph/foo.png')
    fname = 'xdmodel_%s_%d_%d_%d_%d_%s.pkl' % (data, Nr, K, n_iter, Nstar,
                                               message)

    f = open(os.environ['sgdata'] + fname, 'rb')
    model = cPickle.load(f)
    f.close()
 
    if True:
        plot_contours_and_data(epoch, model, features, filters, idx=-1)

    if False:
        psf_minus_model_hists(epoch, model, features, filters, idx=-1)

    if False:
        epoch = 10
        Xsingle, Xsinglecov = fetch_prepped_s82data(epoch)
        Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch, use_single=False)

        N = 5000
        Xsingle = Xsingle[:N]
        Xsinglecov = Xsinglecov[:N]
        Xcoadd = Xcoadd[:N]
        Xcoaddcov = Xcoaddcov[:N]
        
        f = open(os.environ['sgdata'] + 'xd_w_32_10_2.pkl')
        model = cPickle.load(f)
        f.close()
        
        post_alpha, post_mu, post_V = model.posterior(Xsingle, Xsinglecov)
        posts = np.zeros_like(Xsingle)
        for i in range(N):
def plot_contours_and_data(epoch, model, features, filters, fgal=0.5, idx=-1):
    """
    Plot the data and the contours for stars and galaxies.
    """
    from astroML.plotting.tools import draw_ellipse

    Xsingle, Xsinglecov = fetch_prepped_s82data(epoch, fgal, features, filters)
    Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch,
                                              fgal,
                                              features,
                                              filters,
                                              use_single=False)

    sind = Xcoadd[:, idx] < 0.03
    gind = Xcoadd[:, idx] > 0.03

    fs = 5
    ms = 1
    f = pl.figure(figsize=(3 * fs, 2 * fs))
    Nstar = len(np.where(model.fixed_means[:, idx] != np.inf)[0])
    pl.subplot(231)
    idx = [0, -1]
    for i in range(Nstar):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx],
                     model.V[i, idx][:, idx],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.plot(Xsingle[sind][::10, idx[0]],
            Xsingle[sind][::10, idx[1]],
            '.k',
            ms=ms)
    pl.xlim(18, 22)
    pl.ylim(-0.1, 0.5)
    pl.subplot(232)
    idx = [2, 1]
    for i in range(Nstar):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx],
                     model.V[i, idx][:, idx],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.plot(Xsingle[sind][::10, idx[0]],
            Xsingle[sind][::10, idx[1]],
            '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 6)
    pl.subplot(233)
    idx = [3, 4]
    for i in range(Nstar):
        draw_ellipse(model.mu[i, idx],
                     model.V[i, idx][:, idx],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.plot(Xsingle[sind][::10, idx[0]],
            Xsingle[sind][::10, idx[1]],
            '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 3)
    pl.subplot(234)
    idx = [0, -1]
    for i in range(Nstar, model.n_components):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx],
                     model.V[i, idx][:, idx],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.plot(Xsingle[gind][::10, idx[0]],
            Xsingle[gind][::10, idx[1]],
            '.k',
            ms=ms)
    pl.xlim(18, 22)
    pl.ylim(-0.1, 0.5)
    pl.subplot(235)
    idx = [2, 1]
    for i in range(Nstar, model.n_components):
        print i, model.V[i, idx][:, idx]
        draw_ellipse(model.mu[i, idx],
                     model.V[i, idx][:, idx],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.plot(Xsingle[gind][::10, idx[0]],
            Xsingle[gind][::10, idx[1]],
            '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 6)
    pl.subplot(236)
    idx = [3, 4]
    for i in range(Nstar, model.n_components):
        draw_ellipse(model.mu[i, idx],
                     model.V[i, idx][:, idx],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.plot(Xsingle[gind][::10, idx[0]],
            Xsingle[gind][::10, idx[1]],
            '.k',
            ms=ms)
    pl.xlim(-2, 3)
    pl.ylim(-1, 3)
    f.savefig('../../plots/phot_and_morph/foo.png')
    fname = 'xdmodel_%s_%d_%d_%d_%d_%s.pkl' % (data, Nr, K, n_iter, Nstar,
                                               message)

    f = open(os.environ['sgdata'] + fname, 'rb')
    model = cPickle.load(f)
    f.close()

    if True:
        plot_contours_and_data(epoch, model, features, filters, idx=-1)

    if False:
        psf_minus_model_hists(epoch, model, features, filters, idx=-1)

    if False:
        epoch = 10
        Xsingle, Xsinglecov = fetch_prepped_s82data(epoch)
        Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch, use_single=False)

        N = 5000
        Xsingle = Xsingle[:N]
        Xsinglecov = Xsinglecov[:N]
        Xcoadd = Xcoadd[:N]
        Xcoaddcov = Xcoaddcov[:N]

        f = open(os.environ['sgdata'] + 'xd_w_32_10_2.pkl')
        model = cPickle.load(f)
        f.close()

        post_alpha, post_mu, post_V = model.posterior(Xsingle, Xsinglecov)
        posts = np.zeros_like(Xsingle)
        for i in range(N):