예제 #1
0
teff = cat['Teff'][inds]
logg = cat['logg'][inds]
mh = cat['FeH'][inds]
alpham = cat['alphaM'][inds]
ak = 0.05*np.ones(len(inds))
lab = np.vstack((teff,logg,mh,alpham,ak))

ds = dataset.Dataset(
        wl, ids, norm_flux, norm_ivar, lab, ids, norm_flux, norm_ivar)

ds.test_label_vals = lab.T

# generate model test spectra
m.infer_spectra(ds)

Cinv = ds.test_ivar / (1 + ds.test_ivar*m.scatters**2)
#res = Cinv*(ds.test_flux - m.model_spectra)**2
res = (ds.test_flux - m.model_spectra)

# get height above the plane
c = SkyCoord(ra, dec, unit='deg')
lat = np.abs(c.icrs.galactic.b)

for ii in range(0, len(ids)):
    prefix = ids[ii].split(".")[0]
    plot(
            ii, wl, norm_flux, norm_ivar, m.model_spectra, m.coeffs, 
            m.scatters, m.chisq, m.pivots, 6200, 6700, 
            [5577, 5890, 5896, 6384, 6300, 6363, 6562, 6584], 
            "%s.png" %prefix) 
예제 #2
0
def run_one_date(date):
    # load a spectrum
    ds = load_dataset(date)
    names = np.array([get_name(val) for val in ds.test_ID])
    nobj = len(ds.test_ID)
    print("%s obj" %nobj)
    inds = np.arange(nobj)
    m = load_model()
    model_spec = get_model_spectra(ds, m)
    resid = get_residuals(ds, m)

    print("get data to fit")
    x,y,yerr = get_data_to_fit(ds,m,resid)
    print("fit Gaussian")
    fits = np.array([fit_li(x,yval,yerrval) for yval,yerrval in zip(y,yerr)])
    popt = fits[:,0]
    pcov = fits[:,1]

    # get rid of the failed fits
    print("purge failed tests")
    lens = np.array([len(val) for val in popt])
    bad = np.where(lens==1)[0]
    names_keep = np.delete(names, bad)
    inds_keep = np.delete(inds, bad)
    x_keep = np.delete(x, bad, axis=0)
    y_keep = np.delete(y, bad, axis=0)
    yerr_keep = np.delete(yerr, bad, axis=0)
    med_err = np.median(yerr_keep, axis=1)
    popt_keep = np.array([x for x in popt if np.array(x).any() != [0]])
    pcov_keep = np.array([x for x in pcov if np.array(x).any() != [0]])

    amps = popt_keep[:,0]
    amp_err = np.sqrt(pcov_keep[:,0,0])
    center = popt_keep[:,1]
    width = popt_keep[:,2]

    print("find candidates")
    keep = select(med_err, amps, amp_err, width)

    cands = names_keep[keep]
    outf = open('%s_candidates.txt' %date, 'w')
    outf.write("%s Candidates Total\n" %len(ds.test_ID))
    for val in cands: outf.write("%s.fits\n" %val)
    outf.close()

    inds_cands = inds_keep[keep]
    popt_cands = popt_keep[keep]
    pcov_cands = pcov_keep[keep]
    x_cands = x_keep[keep]
    y_cands = y_keep[keep]
    yerr_cands = yerr_keep[keep]
    
    print("first plot")
    out = [plot_fit(
            popt_val, pcov_val, x_val, y_val, yerr_val,
            figname="%s_%s_fit.png" %(date, name_val)) for
            popt_val, pcov_val, x_val, y_val, yerr_val, name_val in 
            zip(popt_cands, pcov_cands, x_cands, y_cands, yerr_cands, cands)]

    print("second plot")
    out = [plot(
            ii, ds.wl, ds.test_flux, ds.test_ivar, model_spec,
            m.coeffs, m.scatters, m.chisqs, m.pivots, 
            figname="%s_%s_spec.png" %(date,name)) for ii,name in zip(inds_cands, cands)]
예제 #3
0
m.scatters = scat
m.pivots = pivot
m.scales = np.ones(len(pivot))

# labels
labeldir = "/Users/annaho/Github_Repositories/TheCannon/data/LAMOST/Label_Transfer"
inputf = pyfits.open("%s/Ho_et_all_catalog_v2.fits" % labeldir)
cat = inputf[1].data
inputf.close()

inds = np.array([np.where(cat['id'] == val)[0][0] for val in ids])
teff = cat['cannon_teff'][inds]
logg = cat['cannon_logg'][inds]
mh = cat['cannon_m_h'][inds]
alpham = cat['cannon_alpha_m'][inds]
ak = 0.05 * np.ones(len(inds))
lab = np.vstack((teff, logg, mh, alpham, ak))

ds = dataset.Dataset(wl, ids, norm_flux, norm_ivar, lab, ids, norm_flux,
                     norm_ivar)

ds.test_label_vals = lab.T

# generate model test spectra
m.infer_spectra(ds)

for ii in range(0, len(ids)):
    prefix = ids[ii].split(".")[0]
    plot(ii, wl, norm_flux, norm_ivar, m.model_spectra, m.coeffs, m.scatters,
         m.chisq, m.pivots, 7200, 8000, "%s.png" % prefix)
예제 #4
0
def run_one_date(date):
    # load a spectrum
    ds = load_dataset(date)
    names = np.array([get_name(val) for val in ds.test_ID])
    nobj = len(ds.test_ID)
    print("%s obj" % nobj)
    inds = np.arange(nobj)
    m = load_model()
    model_spec = get_model_spectra(ds, m)
    resid = get_residuals(ds, m)

    print("get data to fit")
    x, y, yerr = get_data_to_fit(ds, m, resid)
    print("fit Gaussian")
    fits = np.array(
        [fit_li(x, yval, yerrval) for yval, yerrval in zip(y, yerr)])
    popt = fits[:, 0]
    pcov = fits[:, 1]

    # get rid of the failed fits
    print("purge failed tests")
    lens = np.array([len(val) for val in popt])
    bad = np.where(lens == 1)[0]
    names_keep = np.delete(names, bad)
    inds_keep = np.delete(inds, bad)
    x_keep = np.delete(x, bad, axis=0)
    y_keep = np.delete(y, bad, axis=0)
    yerr_keep = np.delete(yerr, bad, axis=0)
    med_err = np.median(yerr_keep, axis=1)
    popt_keep = np.array([x for x in popt if np.array(x).any() != [0]])
    pcov_keep = np.array([x for x in pcov if np.array(x).any() != [0]])

    amps = popt_keep[:, 0]
    amp_err = np.sqrt(pcov_keep[:, 0, 0])
    center = popt_keep[:, 1]
    width = popt_keep[:, 2]

    print("find candidates")
    keep = select(med_err, amps, amp_err, width)

    cands = names_keep[keep]
    outf = open('%s_candidates.txt' % date, 'w')
    outf.write("%s Candidates Total\n" % len(ds.test_ID))
    for val in cands:
        outf.write("%s.fits\n" % val)
    outf.close()

    inds_cands = inds_keep[keep]
    popt_cands = popt_keep[keep]
    pcov_cands = pcov_keep[keep]
    x_cands = x_keep[keep]
    y_cands = y_keep[keep]
    yerr_cands = yerr_keep[keep]

    print("first plot")
    out = [
        plot_fit(popt_val,
                 pcov_val,
                 x_val,
                 y_val,
                 yerr_val,
                 figname="%s_%s_fit.png" % (date, name_val))
        for popt_val, pcov_val, x_val, y_val, yerr_val, name_val in zip(
            popt_cands, pcov_cands, x_cands, y_cands, yerr_cands, cands)
    ]

    print("second plot")
    out = [
        plot(ii,
             ds.wl,
             ds.test_flux,
             ds.test_ivar,
             model_spec,
             m.coeffs,
             m.scatters,
             m.chisqs,
             m.pivots,
             figname="%s_%s_spec.png" % (date, name))
        for ii, name in zip(inds_cands, cands)
    ]
예제 #5
0
m.scales = np.ones(len(pivot))

# labels
labeldir = "/Users/annaho/Github_Repositories/TheCannon/data/LAMOST/Label_Transfer"
inputf = pyfits.open("%s/Ho_et_all_catalog_v2.fits" %labeldir)
cat = inputf[1].data
inputf.close()

inds = np.array([np.where(cat['id']==val)[0][0] for val in ids])
teff = cat['cannon_teff'][inds]
logg = cat['cannon_logg'][inds]
mh = cat['cannon_m_h'][inds]
alpham = cat['cannon_alpha_m'][inds]
ak = 0.05*np.ones(len(inds))
lab = np.vstack((teff,logg,mh,alpham,ak))

ds = dataset.Dataset(
        wl, ids, norm_flux, norm_ivar, lab, ids, norm_flux, norm_ivar)

ds.test_label_vals = lab.T

# generate model test spectra
m.infer_spectra(ds)

for ii in range(0,len(ids)):
    prefix = ids[ii].split(".")[0]
    plot(
            ii, wl, norm_flux, norm_ivar, 
            m.model_spectra, m.coeffs, m.scatters, m.chisq, m.pivots, 
            7200, 8000, "%s.png" %prefix)