Beispiel #1
0
def sdss_results_plot(x, samples, mus, Vs, fs=4, ind=[0, -2], tol=0.175):
    """
    Plot the results of the XD run.
    """
    fig = pl.figure(figsize=(3 * fs, fs))
    fig.subplots_adjust(left=0.1, right=0.95,
                        bottom=0.1, top=0.95,
                        wspace=0.25, hspace=0.25)
       

    pl.subplot(131)
    pl.plot(x[:, 0], x[:, -2], '.k', alpha=0.5, ms=1)
    pl.plot([17, 23], [tol, tol], 'r', lw=2)
    pl.xlim(17, 23)
    pl.ylim(-0.1, 0.4)
    pl.xlabel('r magnitude')
    pl.ylabel('\'star\' - \'galaxy\' mag.')
    
    ax = pl.subplot(132)
    for i in range(mus.shape[0]):
        draw_ellipse(mus[i, ind], Vs[i, ind][:, ind], scales=[2], ax=ax,
                     ec='k', fc='gray', alpha=0.2)
    pl.xlim(17, 23)
    pl.ylim(-0.1, 0.4)
    pl.xlabel('r magnitude')

    pl.subplot(133)
    pl.plot(samples[:, 0], samples[:, -2], '.k', alpha=0.5, ms=1)
    pl.plot([17, 23], [tol, tol], 'r', lw=2)
    pl.xlim(17, 23)
    pl.ylim(-0.1, 0.4)
    pl.xlabel('r magnitude')
    
    pl.show()
    
Beispiel #2
0
def fake_data_plot(x_true, y_true, x, y, samples=None, mus=None, Vs=None):
    """
    Two column plot of fake data in xd-demo.ipynb
    """
    fig = pl.figure(figsize=(5, 3.75))
    fig.subplots_adjust(left=0.1, right=0.95,
                        bottom=0.1, top=0.95,
                        wspace=0.02, hspace=0.02)

    if samples == None:
        ax1 = fig.add_subplot(121)
        ax2 = fig.add_subplot(122)
        ax3 = ax4 = None
        xcheck=(None, None)
    else:
        ax1 = fig.add_subplot(221)
        ax2 = fig.add_subplot(222)

        ax3 = fig.add_subplot(223)
        ax3.scatter(samples[:, 0], samples[:, 1], s=4, lw=0, c='k')

        ax4 = fig.add_subplot(224)
        for i in range(mus.shape[0]):
            draw_ellipse(mus[i], Vs[i], scales=[2], ax=ax4,
                         ec='k', fc='gray', alpha=0.2)

        xcheck=(0, 1)
    ax1.scatter(x_true, y_true, s=4, lw=0, c='k')
    ax2.scatter(x, y, s=4, lw=0, c='k')

    titles = ["True Distribution", "Noisy Distribution",
              "Extreme Deconvolution\n  resampling",
              "Extreme Deconvolution\n  cluster locations"]

    ax = [ax1, ax2, ax3, ax4]

    for i in range(4):
        if ax[i] is not None:
            ax[i].set_xlim(-1, 13)
            ax[i].set_ylim(-6, 16)

            ax[i].xaxis.set_major_locator(pl.MultipleLocator(4))
            ax[i].yaxis.set_major_locator(pl.MultipleLocator(5))

            ax[i].text(0.05, 0.95, titles[i],
                       ha='left', va='top', transform=ax[i].transAxes)

            if i in xcheck:
                ax[i].xaxis.set_major_formatter(pl.NullFormatter())
            else:
                ax[i].set_xlabel('$x$')

            if i in (1, 3):
                ax[i].yaxis.set_major_formatter(pl.NullFormatter())
            else:
                ax[i].set_ylabel('$y$')

    pl.show()
Beispiel #3
0
def plot_cond_model(xdgmm, cond_xdgmm, y):
    plt.clf()
    setup_text_plots(fontsize=16, usetex=True)
    fig = plt.figure(figsize=(12, 9))

    ax1 = fig.add_subplot(111)
    for i in range(xdgmm.n_components):
        draw_ellipse(xdgmm.mu[i],
                     xdgmm.V[i],
                     scales=[2],
                     ax=ax1,
                     ec='None',
                     fc='gray',
                     alpha=0.2)

    ax1.plot([-2, 15], [y, y], color='blue', linewidth=2)
    ax1.set_xlim(-1, 13)
    ax1.set_ylim(-6, 16)
    ax1.set_xlabel('$x$', fontsize=18)
    ax1.set_ylabel('$y$', fontsize=18)

    ax2 = ax1.twinx()
    x = np.array([np.linspace(-2, 14, 1000)]).T

    gmm = skl_GMM(n_components=cond_xdgmm.n_components, covariance_type='full')
    gmm.means_ = cond_xdgmm.mu
    gmm.weights_ = cond_xdgmm.weights
    gmm.covars_ = cond_xdgmm.V

    logprob, responsibilities = gmm.score_samples(x)

    pdf = np.exp(logprob)
    ax2.plot(x,
             pdf,
             color='red',
             linewidth=2,
             label='Cond. dist. of $x$ given $y=' + str(y) + '\pm 0.05$')
    ax2.legend()
    ax2.set_ylabel('Probability', fontsize=18)
    ax2.set_ylim(0, 0.52)
    ax1.set_xlim(-1, 13)
    plt.show()
Beispiel #4
0
def plotXD2d(ax, raveTwins, raveStar, raveCutMatched, chisqApass, mean, cov, ngauss=2, fracMaxPlot=0.01, size=50, alpha_points=1., alpha_ellipse=1.):

    temp = raveCutMatched['TEFF'][raveTwins]/1000.
    temp_err = raveCutMatched['E_TEFF'][raveTwins]/1000.

    logg = raveCutMatched['LOGG'][raveTwins]
    logg_err = raveCutMatched['E_LOGG'][raveTwins]

    feh = raveCutMatched['FE_H'][raveTwins]
    feh_err = raveCutMatched['E_FE_H'][raveTwins]

    gaussianArea = np.exp(-chisqApass/2.)

    weighty = gaussianArea > fracMaxPlot*np.max(gaussianArea)

    X = np.vstack([temp, logg, feh]).T
    ax[0].set_title(neff(gaussianArea))
    ax[0].scatter(X[:,0][weighty], X[:,1][weighty], cmap='plasma_r', c = gaussianArea[weighty],
               norm=mpl.colors.Normalize(), lw=0, alpha=alpha_points,s=size)
    ax[0].errorbar(raveCutMatched['TEFF'][raveStar]/1000., raveCutMatched['LOGG'][raveStar],
                  xerr=raveCutMatched['E_TEFF'][raveStar]/1000., yerr=raveCutMatched['E_LOGG'][raveStar],fmt='o',lw=2, zorder=100)
    for scale, alpha in zip([1, 2], [1.0, 0.5]):
        for i in range(ngauss):
            draw_ellipse(mean[i][0:2], cov[i][0:2], scales=[scale], ax=ax[0],
                 ec='k', fc="None", alpha=alpha_ellipse, zorder=99, lw=2)

    ax[1].scatter(X[:,0][weighty], X[:,2][weighty], cmap='plasma_r', c = gaussianArea[weighty],
               norm=mpl.colors.Normalize(), lw=0, alpha=alpha_points, s=size)
    ax[1].errorbar(raveCutMatched['TEFF'][raveStar]/1000., raveCutMatched['FE_H'][raveStar],
                  xerr=raveCutMatched['E_TEFF'][raveStar]/1000., yerr=raveCutMatched['E_FE_H'][raveStar],fmt='o',lw=2, zorder=100)

    for scale, alpha in zip([1, 2], [1.0, 0.5]):
        for i in range(ngauss):
            draw_ellipse(mean[i][[0,2]], cov[i][[0,2]], scales=[scale], ax=ax[1],
                 ec='k', fc="None", alpha=alpha_ellipse, zorder=99, lw=2)

    ax[2].scatter(X[:,1][weighty], X[:,2][weighty], cmap='plasma_r', c = gaussianArea[weighty],
               norm=mpl.colors.Normalize(), lw=0, alpha=alpha_points, s=size)
    ax[2].errorbar(raveCutMatched['LOGG'][raveStar], raveCutMatched['FE_H'][raveStar],
                  xerr=raveCutMatched['E_LOGG'][raveStar], yerr=raveCutMatched['E_FE_H'][raveStar],fmt='o',lw=2, zorder=100)

    for scale, alpha in zip([1, 2], [1.0, 0.5]):
        for i in range(ngauss):
            draw_ellipse(mean[i][1:3], cov[i][1:3], scales=[scale], ax=ax[2],
                 ec='k', fc="None", alpha=alpha_ellipse, zorder=99, lw=2)

    ax[0].set_xlim(teff_lim)
    ax[0].set_xlabel('T$_\mathrm{eff}$')
    ax[1].set_xlim(teff_lim)
    ax[1].set_xlabel('T$_\mathrm{eff}$')
    ax[2].set_xlim(log_g_lim)
    ax[2].set_xlabel('log g')
    ax[0].set_ylim(log_g_lim)
    ax[0].set_ylabel('log g')
    ax[1].set_ylim(feh_lim)
    ax[1].set_ylabel('Fe/H')
    ax[2].set_ylim(feh_lim)
    ax[2].set_ylabel('Fe/H')
    plt.tight_layout()
Beispiel #5
0
def parabolaTest(n):
    x = 4 * np.random.randn(n)
    y = x**2 + 0.2 * np.random.randn(n)
    x += 0.2 * np.random.randn(n)

    dx = 0.2 * np.ones(len(x))
    dy = 0.2 * np.ones(len(y))

    n, clf, grid, minX, maxX, minY, maxY = deconFit.findNumComponents(
        x, y, dx, dy)

    fig = plt.figure(figsize=(4, 7))
    ax = fig.add_subplot(211)

    for i in range(clf.n_components):
        draw_ellipse(clf.mu[i],
                     clf.V[i],
                     scales=[2],
                     ax=ax,
                     ec='k',
                     fc='gray',
                     alpha=0.5)

    pts = clf.mu
    covars = clf.V

    pts = np.transpose(pts)

    vals, vect = np.linalg.eig(covars)
    inds = np.argmax(np.abs(vals), axis=1)
    vects = []
    for i in range(len(vect)):
        vects.append(vect[i, :, inds[i]])
    vect = np.array(vects)

    pth = path(pts, covars, clf.alpha, vertical=False)

    t = np.array(list(range(len(pth))))
    tRan = np.linspace(min(t), max(t), num=200)
    xRan = interpolator(t, pts[0, pth], vect[:, 0], tRan)
    yRan = interpolator(t, pts[1, pth], vect[:, 1], tRan)

    print(vect)

    plt.rc('font', family='serif', size=15)
    plt.scatter(x, y, c='k')
    plt.plot(xRan, xRan**2, label='$Y=X^2$', c='r')
    plt.plot(xRan, yRan, label='Fit')
    plt.xlabel('X')
    plt.ylabel('Y')

    plt.legend()

    ax = fig.add_subplot(212)

    for i in range(clf.n_components):
        draw_ellipse(clf.mu[i],
                     clf.V[i],
                     scales=[2],
                     ax=ax,
                     ec='k',
                     fc='gray',
                     alpha=0.2)

    pth = np.array(pth)

    pth = pth[np.argsort(clf.mu[pth, 1])]

    t = np.array(list(range(len(pth))))
    tRan = np.linspace(min(t), max(t), num=200)
    xRan = interpolator(t, pts[0, pth], vect[:, 0], tRan)
    yRan = interpolator(t, pts[1, pth], vect[:, 1], tRan)

    print(vect)

    plt.scatter(x, y, c='k')
    plt.plot(xRan, xRan**2, label='$Y=X^2$', c='r')
    plt.plot(xRan, yRan, label='Fit')
    plt.xlabel('X')
    plt.ylabel('Y')

    plt.savefig('../Plots/parabola.pdf')
Beispiel #6
0
def xdgmm():

	all_data = oc_data

	# Get the data in a format for XDGMM

	elements = ("[Fe/H]", "[Mg/Fe]")

	data = np.zeros(map(len, (all_data, elements)))
	uncertainties = np.zeros(map(len, (all_data, elements, elements)))

	# Fill in the data
	for i, element in enumerate(elements):
		data[:, i] = all_data[element]

	# Fill in the uncertainties
	diag = np.arange(len(elements))
	for i, row in enumerate(all_data):
		uncertainties[i][diag, diag] = \
			[row["e_{0}".format(element)] for element in elements]

	# Where the data are missing (e.g. non-finite), set the value
	# as zero and the uncertainty as very large.
	data[~np.isfinite(data)] = 0
	uncertainties[~np.isfinite(uncertainties)] = 1000

	classifier = astroML.density_estimation.XDGMM(n_components=20, n_iter=500)
	classifier.fit(data, uncertainties)

	# Sample the classifier
	samples = classifier.sample(len(all_data))

	# Plot some results
	fig = plt.figure(figsize=(4.025, 7.70))
	fig.subplots_adjust(left=0.20, bottom=0.07, right=0.95, top=0.95,
		wspace=0.20, hspace=0.05)

	# Plot observed data
	ax_observed = fig.add_subplot(311)
	ax_observed.errorbar(all_data[elements[0]], all_data[elements[1]],
		fmt=None, ecolor="#666666", xerr=all_data["e_" + elements[0]],
		yerr=all_data["e_" + elements[1]], zorder=-1)
	ax_observed.scatter(all_data[elements[0]], all_data[elements[1]],
		facecolor="k")

	ax_observed.text(0.05, 0.95, "Observed Data", ha="left", va="top",
		transform=ax_observed.transAxes)

	ax_observed.set_xlim(-0.5, 0.5)
	ax_observed.set_ylim(-0.5, 0.5)
	ax_observed.set_xticklabels([""] * len(ax_observed.get_xticks()))
	ax_observed.yaxis.set_major_locator(MaxNLocator(5))

	ax_observed.set_ylabel(elements[1])

	# Plot sampled data
	ax_sampled = fig.add_subplot(312, sharex=ax_observed, sharey=ax_observed)
	ax_sampled.scatter(samples[:, 0], samples[:, 1],
		facecolor="k")
	ax_sampled.text(0.05, 0.95, "Extreme Deconvolution\nresampling",
		ha="left", va="top", transform=ax_sampled.transAxes)

	ax_sampled.set_xticklabels([""] * len(ax_sampled.get_xticks()))
	ax_sampled.set_ylabel(elements[1])

	# Plot cluster data
	ax_clusters = fig.add_subplot(313, sharey=ax_observed)
	ax_clusters.text(0.05, 0.95, "Clusters", ha="left", va="top",
		transform=ax_clusters.transAxes)

	for i in range(classifier.n_components):
		draw_ellipse(classifier.mu[i], classifier.V[i], scales=[2],
			ax=ax_clusters, ec='k', fc='gray', alpha=0.2)

	ax_clusters.set_xlim(ax_sampled.get_xlim())
	ax_clusters.set_xlabel(elements[0])
	ax_clusters.set_ylabel(elements[1])

	raise a
Beispiel #7
0
def plot_sample(x_true, y_true, x, y, sample, xdgmm):
    setup_text_plots(fontsize=16, usetex=True)
    plt.clf()
    fig = plt.figure(figsize=(12, 9))
    fig.subplots_adjust(left=0.1,
                        right=0.95,
                        bottom=0.1,
                        top=0.95,
                        wspace=0.02,
                        hspace=0.02)

    ax1 = fig.add_subplot(221)
    ax1.scatter(x_true, y_true, s=4, lw=0, c='k')

    ax2 = fig.add_subplot(222)

    ax2.scatter(x, y, s=4, lw=0, c='k')

    ax3 = fig.add_subplot(223)
    ax3.scatter(sample[:, 0], sample[:, 1], s=4, lw=0, c='k')

    ax4 = fig.add_subplot(224)
    for i in range(xdgmm.n_components):
        draw_ellipse(xdgmm.mu[i],
                     xdgmm.V[i],
                     scales=[2],
                     ax=ax4,
                     ec='k',
                     fc='gray',
                     alpha=0.2)

    titles = [
        "True Distribution", "Noisy Distribution",
        "Extreme Deconvolution\n  resampling",
        "Extreme Deconvolution\n  cluster locations"
    ]

    ax = [ax1, ax2, ax3, ax4]

    for i in range(4):
        ax[i].set_xlim(-1, 13)
        ax[i].set_ylim(-6, 16)

        ax[i].xaxis.set_major_locator(plt.MultipleLocator(4))
        ax[i].yaxis.set_major_locator(plt.MultipleLocator(5))

        ax[i].text(0.05,
                   0.95,
                   titles[i],
                   ha='left',
                   va='top',
                   transform=ax[i].transAxes)

        if i in (0, 1):
            ax[i].xaxis.set_major_formatter(plt.NullFormatter())
        else:
            ax[i].set_xlabel('$x$', fontsize=18)

        if i in (1, 3):
            ax[i].yaxis.set_major_formatter(plt.NullFormatter())
        else:
            ax[i].set_ylabel('$y$', fontsize=18)

    plt.show()
Beispiel #8
0
    ax_list[3].plot(Nclusters, BICs / Npts, ls, c='k',
                    label="N=%i" % Npts)

    clf = clfs[np.argmin(BICs)]
    log_dens = clf.score(Xgrid).reshape((70, 70))

    # scatter the points
    ax.plot(X[:, 0], X[:, 1], ',k', alpha=0.3, zorder=1)

    # plot the components
    for i in range(clf.n_components):
        mean = clf.means_[i]
        cov = clf.covars_[i]
        if cov.ndim == 1:
            cov = np.diag(cov)
        draw_ellipse(mean, cov, ax=ax, fc='none', ec='k', zorder=2)

    # label the plot
    ax.text(0.05, 0.95, "N = %i points" % Npts,
            ha='left', va='top', transform=ax.transAxes,
            bbox=dict(fc='w', ec='k'))

    ax.set_xlim(-5, 105)
    ax.set_ylim(-5, 105)


ax_list[0].xaxis.set_major_formatter(plt.NullFormatter())
ax_list[2].yaxis.set_major_formatter(plt.NullFormatter())

for i in (0, 1):
    ax_list[i].set_ylabel('$y$')
Beispiel #9
0
fig.subplots_adjust(left=0.1, right=0.95,
                    bottom=0.1, top=0.95,
                    wspace=0.02, hspace=0.02)

ax1 = fig.add_subplot(221)
ax1.scatter(x_true, y_true, s=4, lw=0, c='k')

ax2 = fig.add_subplot(222)
ax2.scatter(x, y, s=4, lw=0, c='k')

ax3 = fig.add_subplot(223)
ax3.scatter(sample[:, 0], sample[:, 1], s=4, lw=0, c='k')

ax4 = fig.add_subplot(224)
for i in range(clf.n_components):
    draw_ellipse(clf.mu[i], clf.V[i], scales=[2], ax=ax4,
                 ec='k', fc='gray', alpha=0.2)

titles = ["True Distribution", "Noisy Distribution",
          "Extreme Deconvolution\n  resampling",
          "Extreme Deconvolution\n  cluster locations"]

ax = [ax1, ax2, ax3, ax4]

for i in range(4):
    ax[i].set_xlim(-1, 13)
    ax[i].set_ylim(-6, 16)

    ax[i].xaxis.set_major_locator(plt.MultipleLocator(4))
    ax[i].yaxis.set_major_locator(plt.MultipleLocator(5))

    ax[i].text(0.05, 0.95, titles[i],
Beispiel #10
0
# plot AIC/BIC
ax = fig.add_subplot(132)
ax.plot(N, AIC, '-k', label='AIC')
ax.plot(N, BIC, ':k', label='BIC')
ax.legend(loc=1)
ax.set_xlabel('N components')
plt.setp(ax.get_yticklabels(), fontsize=7)

# plot best configurations for AIC and BIC
ax = fig.add_subplot(133)
ax.imshow(np.exp(log_dens),
          origin='lower', interpolation='nearest', aspect='auto',
          extent=[FeH_bins[0], FeH_bins[-1],
                  alphFe_bins[0], alphFe_bins[-1]],
          cmap=plt.cm.binary)

ax.scatter(gmm_best.means_[:, 0], gmm_best.means_[:, 1], c='w')
for mu, C, w in zip(gmm_best.means_, gmm_best.covars_, gmm_best.weights_):
    draw_ellipse(mu, C, scales=[1.5], ax=ax, fc='none', ec='k')

ax.text(0.93, 0.93, "Converged",
        va='top', ha='right', transform=ax.transAxes)

ax.set_xlim(-1.101, 0.101)
ax.set_ylim(alphFe_bins[0], alphFe_bins[-1])
ax.xaxis.set_major_locator(plt.MultipleLocator(0.3))
ax.set_xlabel(r'$\rm [Fe/H]$')
ax.set_ylabel(r'$\rm [\alpha/Fe]$')

plt.show()
Beispiel #11
0
def contours_and_data(epoch, model, features, filters, figname, fgal=0.5,
                      idx=-1, data='s82', N=60000):
    """
    Plot the data and contours for objects called star/galaxy.
    """
    if data == 's82':
        # fetch Stripe 82 data
        X, Xcov = fetch_prepped_s82data(epoch, fgal, features, filters)
        Xcoadd, Xcoaddcov = fetch_prepped_s82data(epoch, fgal, features,
                                                  filters, use_single=False)
        sind = np.abs(Xcoadd[:, idx]) < 0.03
        gind = np.abs(Xcoadd[:, idx]) > 0.03

    else:
        # fetch DR10 data
        X, Xcov = fetch_prepped_dr10data(N, fgal, features, filters)
        sind = np.abs(X[:, idx]) < 0.145
        gind = np.abs(X[:, idx]) > 0.145

    # unpickle the XD model
    if type(model) == str: 
        f = open(model, 'rb')
        model = cPickle.load(f)
        f.close()

    fs = 5
    ms = 1
    lsize = 20
    idx = [[0, -1], [2, 3], [3, 4]]
    xlim = [(18., 22), (-0.5, 2.5), (-0.5, 2)]
    ylim = [(-0.1, 0.5), (-0.5, 2.5), (-0.5, 1.5)]
    xlab = ['psfmag $r$', 'modelmag $g-r$', 'modelmag $r-i$']
    ylab = ['psfmag - modelmag $r$', 'modelmag $r-i$', 'modelmag $i-z$']

    f = pl.figure(figsize=(3 * fs, 3 * fs))
    Nstar = len(np.where(model.fixed_means[:, idx] != np.inf)[0])
    pl.subplots_adjust(wspace=0.3)
    for i in range(1, 10):
        k = (i - 1) % 3
        if i < 4:
            ind = np.arange(X.shape[0], dtype=np.int)
            rng = range(model.n_components)
        elif 3 < i < 7:
            ind = sind
            rng = range(Nstar)
        else:
            ind = gind
            rng = range(Nstar, model.n_components)
        ax = pl.subplot(3, 3, i)
        for j in rng:
            if model.alpha[j] > 1.e-3:
                draw_ellipse(model.mu[j, idx[k]],
                             model.V[j, idx[k]][:, idx[k]],
                             scales=[2], ec='k', fc='gray', alpha=0.2)
        pl.plot(X[ind][::10, idx[k][0]],
                X[ind][::10, idx[k][1]], '.k',ms=ms)
        pl.xlim(xlim[k])
        pl.ylim(ylim[k])
        pl.xlabel(xlab[k], fontsize=lsize)
        pl.ylabel(ylab[k], fontsize=lsize)
        if ('psf' in ylab[k]) & ('model' in ylab[k]):
            ytick = ['%0.1f' % v for v in np.linspace(-.1, 0.4, 6)]
            ytick[0] = ''
            ax.set_yticklabels(ytick)
            if i == 1:
                s = 'All'
            elif i == 3:
                s = '"Stars"'
            else:
                s = '"Galaxies"'
            ax.text(-.3, 0.5, s, ha='center', va='center', fontsize=25,
                      rotation='vertical', transform=ax.transAxes)
    f.savefig(figname, bbox_inches='tight')
    dx = 0.2 + 0.5 * np.random.random(N)
    dy = 0.2 + 0.5 * np.random.random(N)

    x = x_true + np.random.normal(0, dx)
    y = y_true + np.random.normal(0, dy)

    # stack the results for computation
    X = np.vstack([x, y]).T
    Xerr = np.zeros(X.shape + X.shape[-1:])
    diag = np.arange(X.shape[-1])
    Xerr[:, diag, diag] = np.vstack([dx**2, dy**2]).T

    K = 10
    iter_total = 100
    fixed_means = np.array([[np.inf, np.inf] for _ in range(K)])
    fixed_means[2] = [3., -3.]
    aligned_covs = [(2, [1]), (3, [1])]
    model = constrained_GMM(X, K, iter_total, fixed_means, aligned_covs)

    for i in range(K):
        draw_ellipse(model.means_[i],
                     model.covars_[i],
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)
    pl.scatter(x, y, marker='.', color='k')
    pl.xlim(0, 12)
    pl.ylim(-5, 15)
    pl.savefig(os.environ['xdplots'] + 'foo.png')
Beispiel #13
0
ymin = min(dec)
ymax = max(dec)
ax1 = fig.add_subplot(131)
# for i in range(newZ.shape[0]):
ax1.scatter(newZ[:, 0], newZ[:, 1], s=0.5, lw=0, c='k')
ax2 = fig.add_subplot(132)
ax2.scatter(rasamp, decsamp, s=0.5, lw=0, c='k')

ax3 = fig.add_subplot(133)
print "clf.n_components", clf.n_components

for i in range(clf.n_components):
    print clf.mu[i, 0:2], clf.V[i, 0:2, 0:2]
    draw_ellipse(clf.mu[i, 0:2],
                 clf.V[i, 0:2, 0:2],
                 scales=[2],
                 ec='k',
                 fc='gray',
                 alpha=0.2,
                 ax=ax3)
ax1.set_xlim([xmin, xmax])
ax2.set_xlim([xmin, xmax])
ax3.set_xlim([xmin, xmax])
ax1.set_ylim([ymin, ymax])
ax2.set_ylim([ymin, ymax])
ax3.set_ylim([ymin, ymax])
ax1.set_title("Binnws Data willman1")
ax2.set_title("Extreme Deconvolution Resampling ")
ax3.set_title("Extreme Deconvolution Clusters location")
plt.show()
Beispiel #14
0
    # plot the BIC
    ax_list[3].plot(Nclusters, BICs / Npts, ls, c="k", label="N=%i" % Npts)

    clf = clfs[np.argmin(BICs)]
    log_dens = clf.score(Xgrid).reshape((70, 70))

    # scatter the points
    ax.plot(X[:, 0], X[:, 1], ".", c="gray", ms=1, zorder=1)

    # plot the components
    for i in range(clf.n_components):
        mean = clf.means_[i]
        cov = clf.covars_[i]
        if cov.ndim == 1:
            cov = np.diag(cov)
        draw_ellipse(mean, cov, ax=ax, fc="none", ec="k", zorder=2)

    # label the plot
    ax.text(0.05, 0.95, "N = %i points" % Npts, ha="left", va="top", transform=ax.transAxes, bbox=dict(fc="w", ec="k"))

    ax.set_xlim(-5, 105)
    ax.set_ylim(-5, 105)


ax_list[0].xaxis.set_major_formatter(plt.NullFormatter())
ax_list[2].yaxis.set_major_formatter(plt.NullFormatter())

for i in (0, 1):
    ax_list[i].set_ylabel("y")

for j in (1, 2):
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')
Beispiel #16
0
# only plot 1/10 of the stars for clarity
ax1 = fig.add_subplot(221)
ax1.scatter(Y[::10, 2], Y[::10, 3], s=9, lw=0, c='k')

ax2 = fig.add_subplot(222)
ax2.scatter(X[::10, 2], X[::10, 3], s=9, lw=0, c='k')

ax3 = fig.add_subplot(223)
ax3.scatter(X_sample[::10, 2], X_sample[::10, 3], s=9, lw=0, c='k')

ax4 = fig.add_subplot(224)
for i in range(clf.n_components):
    draw_ellipse(clf.mu[i, 2:4],
                 clf.V[i, 2:4, 2:4],
                 scales=[2],
                 ec='k',
                 fc='gray',
                 alpha=0.2,
                 ax=ax4)

titles = [
    "Standard Stars", "Single Epoch", "Extreme Deconvolution\n  resampling",
    "Extreme Deconvolution\n  cluster locations"
]
ax = [ax1, ax2, ax3, ax4]

for i in range(4):
    ax[i].set_xlim(-0.6, 1.8)
    ax[i].set_ylim(-0.6, 1.8)

    ax[i].xaxis.set_major_locator(plt.MultipleLocator(0.5))
Beispiel #17
0
def covint(p):

   zmin , zmax = p
   X , Xerr = extractor(zmin , zmax) 	
   filename = "results3/gr_result_z_"+str(zmin)+"_"+str(zmax)+".txt"
   pmem = np.loadtxt(filename)[12:]

   y , yerr = X[pmem>0.8,:] , Xerr[pmem>0.8,:] 
   ncomp = 1
   clf = compute_XD_results(y, yerr, n_components = ncomp, n_iter=50)
   
   #print clf.mu , clf.V

   palette = itertools.cycle(sns.color_palette())
   fig, axs = plt.subplots(1, 2, figsize=(10.5, 5) , sharex = True, sharey = True)
   axs[0].scatter(X[:,0] , X[:,1] , s = 0.5 , c = next(palette), label = str(zmin)+"<$z$<"+str(zmax))
   axs[0].set_xlabel(r"$u-g$")
   axs[0].set_ylabel(r"$g-r$")
   axs[0].legend(loc = 'best', fontsize = 10)
   axs[1].scatter(X[pmem>0.8,0] , X[pmem>0.8,1] , s = 0.5 , c = next(palette) , label = r"$p_{\mathrm{red}}>0.8$")
   axs[1].set_xlabel(r"$u-g$")
   
   for i in range(ncomp):
       draw_ellipse(clf.mu[i][:2], clf.V[i][:2,:2], scales=[2], ax=axs[1],
                        ec='k', fc='blue', alpha=0.2 )
   
   plt.legend(loc = 'best' , fontsize = 10)
   fig.tight_layout()
   plt.savefig("/home/vakili/public_html/files/covariance/covariance_80_ug_gr_"+str(zmin)+"_"+str(zmax)+".png")

   palette = itertools.cycle(sns.color_palette())
   fig, axs = plt.subplots(1, 2, figsize=(10.5, 5), sharex = True, sharey = True)
   axs[0].scatter(X[:,1] , X[:,2] , s = 0.5 , c = next(palette), label = str(zmin)+"<$z$<"+str(zmax))
   axs[0].set_xlabel(r"$g-r$")
   axs[0].set_ylabel(r"$r-i$")
   axs[0].legend(loc = 'best', fontsize = 10)


   axs[1].scatter(X[pmem>0.8,1] , X[pmem>0.8,2] , s = 0.5 , c = next(palette), label = r"$p_{\mathrm{red}}>0.8$")
   axs[1].set_xlabel(r"$g-r$")
   for i in range(ncomp):
       draw_ellipse(clf.mu[i][1:], clf.V[i][1:,1:], scales=[2], ax=axs[1],
                        ec='k', fc='blue', alpha=0.2)
   plt.legend(loc = 'best' , fontsize = 10)
   fig.tight_layout()
   plt.savefig("/home/vakili/public_html/files/covariance/covariance_80_gr_ri_"+str(zmin)+"_"+str(zmax)+".png")
   
   
   palette = itertools.cycle(sns.color_palette())
   fig, axs = plt.subplots(1, 2, figsize=(10.5, 5), sharex = True, sharey = True)
   axs[0].scatter(X[:,0] , X[:,2] , s = 0.5 , c = next(palette), label = str(zmin)+"<$z$<"+str(zmax))
   axs[0].set_xlabel(r"$u-g$")
   axs[0].set_ylabel(r"$r-i$")
   axs[0].legend(loc = 'best', fontsize = 10)

   axs[1].scatter(X[pmem>0.8,0] , X[pmem>0.8,2] , s = 0.5 , c = next(palette), label = r"$p_{\mathrm{red}}>0.8$")
   axs[1].set_xlabel(r"$u-g$")
  
   for i in range(ncomp):
       draw_ellipse(np.delete(clf.mu[i],1,0) , np.delete(np.delete(clf.V[i],1,1),1,0) , scales=[2], ax=axs[1],
                        ec='k', fc='blue', alpha=0.2)
   plt.legend(loc = 'best' , fontsize = 10)
   fig.tight_layout()
   plt.savefig("/home/vakili/public_html/files/covariance/covariance_80_ug_ri_"+str(zmin)+"_"+str(zmax)+".png")

   return np.array(clf.V[0])   
Beispiel #18
0
ax.plot(N, AIC, "-k", label="AIC")
ax.plot(N, BIC, ":k", label="BIC")
ax.legend(loc=1)
ax.set_xlabel("N components")
plt.setp(ax.get_yticklabels(), fontsize=7)

# plot best configurations for AIC and BIC
ax = fig.add_subplot(133)
ax.imshow(
    np.exp(log_dens),
    origin="lower",
    interpolation="nearest",
    aspect="auto",
    extent=[FeH_bins[0], FeH_bins[-1], alphFe_bins[0], alphFe_bins[-1]],
    cmap=plt.cm.binary,
)

ax.scatter(gmm_best.means_[:, 0], gmm_best.means_[:, 1], c="w")
for mu, C, w in zip(gmm_best.means_, gmm_best.covars_, gmm_best.weights_):
    draw_ellipse(mu, C, scales=[1.5], ax=ax, fc="none", ec="k")

ax.text(0.93, 0.93, "Converged", va="top", ha="right", transform=ax.transAxes)

ax.set_xlim(-1.101, 0.101)
ax.set_ylim(alphFe_bins[0], alphFe_bins[-1])
ax.xaxis.set_major_locator(plt.MultipleLocator(0.3))
ax.set_xlabel(r"$\rm [Fe/H]$")
ax.set_ylabel(r"$\rm [\alpha/Fe]$")

plt.show()
Beispiel #19
0
                    bottom=0.1, top=0.95,
                    wspace=0.02, hspace=0.02)

# only plot 1/10 of the stars for clarity
ax1 = fig.add_subplot(221)
ax1.scatter(Y[::10, 2], Y[::10, 3], s=9, lw=0, c='k')

ax2 = fig.add_subplot(222)
ax2.scatter(X[::10, 2], X[::10, 3], s=9, lw=0, c='k')

ax3 = fig.add_subplot(223)
ax3.scatter(X_sample[::10, 2], X_sample[::10, 3], s=9, lw=0, c='k')

ax4 = fig.add_subplot(224)
for i in range(clf.n_components):
    draw_ellipse(clf.mu[i, 2:4], clf.V[i, 2:4, 2:4], scales=[2],
                 ec='k', fc='gray', alpha=0.2, ax=ax4)

titles = ["Standard Stars", "Single Epoch",
          "Extreme Deconvolution\n  resampling",
          "Extreme Deconvolution\n  cluster locations"]
ax = [ax1, ax2, ax3, ax4]

for i in range(4):
    ax[i].set_xlim(-0.6, 1.8)
    ax[i].set_ylim(-0.6, 1.8)

    ax[i].xaxis.set_major_locator(plt.MultipleLocator(0.5))
    ax[i].yaxis.set_major_locator(plt.MultipleLocator(0.5))

    ax[i].text(0.05, 0.95, titles[i],
               ha='left', va='top', transform=ax[i].transAxes)
    # plot the BIC
    ax_list[3].plot(Nclusters, BICs / Npts, ls, c='k', label="N=%i" % Npts)

    clf = clfs[np.argmin(BICs)]
    log_dens = clf.score(Xgrid).reshape((70, 70))

    # scatter the points
    ax.plot(X[:, 0], X[:, 1], ',k', alpha=0.3, zorder=1)

    # plot the components
    for i in range(clf.n_components):
        mean = clf.means_[i]
        cov = clf.covars_[i]
        if cov.ndim == 1:
            cov = np.diag(cov)
        draw_ellipse(mean, cov, ax=ax, fc='none', ec='k', zorder=2)

    # label the plot
    ax.text(0.05,
            0.95,
            "N = %i points" % Npts,
            ha='left',
            va='top',
            transform=ax.transAxes,
            bbox=dict(fc='w', ec='k'))

    ax.set_xlim(-5, 105)
    ax.set_ylim(-5, 105)

ax_list[0].xaxis.set_major_formatter(plt.NullFormatter())
ax_list[2].yaxis.set_major_formatter(plt.NullFormatter())
Beispiel #21
0
# plot AIC/BIC
ax = fig.add_subplot(132)
ax.plot(N, AIC, '-k', label='AIC')
ax.plot(N, BIC, ':k', label='BIC')
ax.legend(loc=1)
ax.set_xlabel('N components')
plt.setp(ax.get_yticklabels(), fontsize=7)

# plot best configurations for AIC and BIC
ax = fig.add_subplot(133)
ax.imshow(np.exp(log_dens),
          origin='lower',
          interpolation='nearest',
          aspect='auto',
          extent=[FeH_bins[0], FeH_bins[-1], alphFe_bins[0], alphFe_bins[-1]],
          cmap=plt.cm.binary)

ax.scatter(gmm_best.means_[:, 0], gmm_best.means_[:, 1], c='w')
for mu, C, w in zip(gmm_best.means_, gmm_best.covars_, gmm_best.weights_):
    draw_ellipse(mu, C, scales=[1.5], ax=ax, fc='none', ec='k')

ax.text(0.93, 0.93, "Converged", va='top', ha='right', transform=ax.transAxes)

ax.set_xlim(-1.101, 0.101)
ax.set_ylim(alphFe_bins[0], alphFe_bins[-1])
ax.xaxis.set_major_locator(plt.MultipleLocator(0.3))
ax.set_xlabel(r'$\rm [Fe/H]$')
ax.set_ylabel(r'$\rm [\alpha/Fe]$')

plt.show()
ax1 = fig.add_subplot(221)
ax1.scatter(x_true, y_true, s=4, lw=0, c='k')

ax2 = fig.add_subplot(222)
ax2.scatter(x, y, s=4, lw=0, c='k')

ax3 = fig.add_subplot(223)
ax3.scatter(sample[:, 0], sample[:, 1], s=4, lw=0, c='k')

ax4 = fig.add_subplot(224)
for i in range(clf.n_components):
    draw_ellipse(clf.mu[i],
                 clf.V[i],
                 scales=[2],
                 ax=ax4,
                 ec='k',
                 fc='gray',
                 alpha=0.2)

titles = [
    "True Distribution", "Noisy Distribution",
    "Extreme Deconvolution\n  resampling",
    "Extreme Deconvolution\n  cluster locations"
]

ax = [ax1, ax2, ax3, ax4]

for i in range(4):
    ax[i].set_xlim(-1, 13)
    ax[i].set_ylim(-6, 16)
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')
Beispiel #24
0

clf = clfs[np.argmin(BICs)]
#log_dens = clf.score(Xgrid).reshape((70, 70))

###########Gaussian Mixture Model

# scatter the points
ax = plt.subplot(111)
ax.plot(X[:, 0], X[:, 1], ',k', alpha=0.3, zorder=1)
for i in range(clf.n_components):
    mean = clf.means_[i]
    cov = clf.covariances_[i]
    if cov.ndim == 1:
        cov = np.diag(cov)
    draw_ellipse(mean, cov, ax=ax, fc='none', ec=['b','r'][i], zorder=2)

plt.plot([], label='Predicted Distribution of Circadian Proteins', color="blue",linewidth=.5)
plt.plot([], label='Predicted Distribution of Non-Circadian Proteins', color="red",linewidth=.5)
plt.plot([], label='Probability of Being Drawn From Circadian Distribution', color="black",linewidth=.5)
plt.plot([], label='Z Score Corresponding to p=0.05', color="black",linewidth=.5, linestyle='--')
plt.legend(loc="lower left")
ax.set_xlabel(r"WT Z Score", fontsize=12)
ax.set_ylabel(r'$\Delta$CSP-1 Z Score', fontsize=12)
plt.title('Gaussian Mixture Model of Z Score with 2 Components', fontsize=16)
ax.scatter(X[:, 0], X[:, 1],color='k', alpha=.3, zorder=1,s=1.5)
x, y = np.meshgrid(np.linspace(-4., 5.,1000),np.linspace(-4., 5.,1000))
XX = np.array([x.ravel(), y.ravel()]).T
Z = clf.predict_proba(XX)[:,0]
Z = Z.reshape(x.shape)
CS = ax.contour(x, y, Z, color='k', levels=np.linspace(.9, 1, 11),linewidths=.75)
    x_true += np.random.normal(0, dx, N)
    y_true += np.random.normal(0, dy, N)

    # add noise to get the "observed" distribution
    dx = 0.2 + 0.5 * np.random.random(N)
    dy = 0.2 + 0.5 * np.random.random(N)

    x = x_true + np.random.normal(0, dx)
    y = y_true + np.random.normal(0, dy)

    # stack the results for computation
    X = np.vstack([x, y]).T
    Xerr = np.zeros(X.shape + X.shape[-1:])
    diag = np.arange(X.shape[-1])
    Xerr[:, diag, diag] = np.vstack([dx ** 2, dy ** 2]).T

    K = 10
    iter_total = 100
    fixed_means = np.array([[np.inf, np.inf] for _ in range(K)])
    fixed_means[2] = [3., -3.]
    aligned_covs = [(2, [1]), (3, [1])]
    model = constrained_GMM(X, K, iter_total, fixed_means, aligned_covs)

    for i in range(K):
        draw_ellipse(model.means_[i], model.covars_[i], scales=[2],
                     ec='k', fc='gray', alpha=0.2)
    pl.scatter(x, y, marker='.', color='k')
    pl.xlim(0, 12)
    pl.ylim(-5, 15)
    pl.savefig(os.environ['xdplots'] + 'foo.png')