Exemple #1
0
def fitExampleDRW(datafilename="../data/SDSSJ203817.37+003029.8.fits"):
    """
    NAME:
       fitExampleDRW
    PURPOSE:
       Fit an example of a DRW structure function GP covariance function
       to an SDSS quasar in g and r
    INPUT:
       datafilename
    OUTPUT:
    HISTORY:
       2010-02-21 - Written - Bovy (NYU)
    """
    nu.random.seed(1)
    # Get data
    file = fu.table_fields(datafilename)

    mjd_g = nu.array(file.mjd_g) / 365.25
    g = nu.array(file.g)
    err_g = nu.array(file.err_g)
    mjd_r = nu.array(file.mjd_r) / 365.25
    r = nu.array(file.r)
    err_r = nu.array(file.err_r)

    mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7)  # Adjust for non-g
    g = g[mask]
    g -= nu.mean(g)
    err_g = err_g[mask]
    mjd_g = mjd_g[mask]
    mjd_g -= nu.amin(mjd_g)
    meanErr_g = nu.mean(err_g)

    r = r[mask]
    r -= nu.mean(r)
    err_r = err_r[mask]
    mjd_r = mjd_r[mask]
    mjd_r -= nu.amin(mjd_r)
    meanErr_r = nu.mean(err_r)

    listx = [(t, "g") for t in mjd_g]
    listx.extend([(t, "r") for t in mjd_r])
    listy = [m for m in g]
    listy.extend([m for m in r])
    listy = nu.array(listy)
    noise = [m for m in err_g]
    noise.extend([m for m in err_r])
    noise = nu.array(noise)
    trainSet = trainingSet(listx=listx, listy=listy, noise=noise)

    SF = covarFunc(S=1.0, tau=0.2, B=0.1, C=0.005, gammagr=0.5, Gammagr=0.01)

    # Train
    print "Training ..."
    outcovarFunc = trainGP(trainSet, SF, useDerivs=False)

    print "Best-fit:"
    print outcovarFunc._dict

    return None
Exemple #2
0
def fitExampleDRW(datafilename='../data/SDSSJ203817.37+003029.8.fits'):
    """
    NAME:
       fitExampleDRW
    PURPOSE:
       Fit an example of a DRW structure function GP covariance function
       to an SDSS quasar in g and r
    INPUT:
       datafilename
    OUTPUT:
    HISTORY:
       2010-02-21 - Written - Bovy (NYU)
    """
    nu.random.seed(1)
    #Get data
    file = fu.table_fields(datafilename)

    mjd_g = nu.array(file.mjd_g) / 365.25
    g = nu.array(file.g)
    err_g = nu.array(file.err_g)
    mjd_r = nu.array(file.mjd_r) / 365.25
    r = nu.array(file.r)
    err_r = nu.array(file.err_r)

    mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7)  #Adjust for non-g
    g = g[mask]
    g -= nu.mean(g)
    err_g = err_g[mask]
    mjd_g = mjd_g[mask]
    mjd_g -= nu.amin(mjd_g)
    meanErr_g = nu.mean(err_g)

    r = r[mask]
    r -= nu.mean(r)
    err_r = err_r[mask]
    mjd_r = mjd_r[mask]
    mjd_r -= nu.amin(mjd_r)
    meanErr_r = nu.mean(err_r)

    listx = [(t, 'g') for t in mjd_g]
    listx.extend([(t, 'r') for t in mjd_r])
    listy = [m for m in g]
    listy.extend([m for m in r])
    listy = nu.array(listy)
    noise = [m for m in err_g]
    noise.extend([m for m in err_r])
    noise = nu.array(noise)
    trainSet = trainingSet(listx=listx, listy=listy, noise=noise)

    SF = covarFunc(S=1., tau=.2, B=.1, C=.005, gammagr=.5, Gammagr=.01)

    #Train
    print "Training ..."
    outcovarFunc = trainGP(trainSet, SF, useDerivs=False)

    print "Best-fit:"
    print outcovarFunc._dict

    return None
def get_possibleSegueTargets():
    """
    NAME:
       get_possibleSegueTargets
    PURPOSE:
       get the possible Segue targets from the CAS
    INPUT:
       parser - from optParser
    OUTPUT:
    HISTORY:
       2010-12-21 - Written - Bovy (NYU)
       2011-07-07 - Adapted for SEGUE - Bovy
    """
    caspwdfile= open('caspwd','r')
    casusr= caspwdfile.readline().rstrip()
    caspwd= caspwdfile.readline().rstrip()
    caspwdfile.close()
    #Read plate centers file
    platefile= os.path.join(os.getenv('DATADIR'),'bovy',
                            'segue-local','segueplates.fits')
    platecenters= pyfits_utils.table_fields(platefile)[::-1]
    nplates= len(platecenters.ra)
    done= []
    for plate in platecenters:
        print "Working on plate "+str(plate.plate)
        if plate.plate in done:
            print str(plate.plate)+" already done!"
            continue
        done.append(plate.plate)
        tmpsavefilename= os.path.join(os.getenv('DATADIR'),'bovy',
                                      'segue-local','segueplates',
                                      '%i.fit' % plate.plate)
        if os.path.exists(tmpsavefilename):
            print "file "+tmpsavefilename+" exists"
            print "Delete file "+tmpsavefilename+" before running this to update the sample from the CAS"
        else:
            dbname= prepare_sql(plate)
            subprocess.call([_PYTHON,_CASJOBS,casusr,caspwd,'querywait',
                             '@tmp.sql'])
            subprocess.call([_PYTHON,_CASJOBS,casusr,caspwd,
                             'outputdownloaddelete',dbname,tmpsavefilename])
Exemple #4
0
def get_qsodata():
    """
    NAME:
       get_qsodata
    PURPOSE:
       get the QSO sample from the CAS
    INPUT:
       parser - from optParser
    OUTPUT:
    HISTORY:
       2010-12-21 - Written - Bovy (NYU)
    """
    caspwdfile = open('caspwd', 'r')
    casusr = caspwdfile.readline().rstrip()
    caspwd = caspwdfile.readline().rstrip()
    caspwdfile.close()
    #Read qso file
    s82file = '../data/S82qsos.fits'
    s82qsos = pyfits_utils.table_fields(s82file)[::-1]
    nqso = len(s82qsos.ra)
    done = []
    for qso in s82qsos:
        print "Working on QSO " + qso.oname.strip().replace(' ', '')
        if qso.oname.strip().replace(' ', '') in done:
            print qso.oname.strip().replace(' ', '') + " already done!"
            return
        done.append(qso.oname.strip().replace(' ', ''))
        tmpsavefilename = '../data/s82qsos/' + qso.oname.strip().replace(
            ' ', '') + '.fit'
        if os.path.exists(tmpsavefilename):
            print "file " + tmpsavefilename + " exists"
            print "Delete file " + tmpsavefilename + " before running this to update the sample from the CAS"
        else:
            dbname = prepare_sql(qso)
            subprocess.call(
                [_PYTHON, _CASJOBS, casusr, caspwd, 'querywait', '@tmp.sql'])
            subprocess.call([
                _PYTHON, _CASJOBS, casusr, caspwd, 'outputdownloaddelete',
                dbname, tmpsavefilename
            ])
Exemple #5
0
def get_qsodata():
    """
    NAME:
       get_qsodata
    PURPOSE:
       get the QSO sample from the CAS
    INPUT:
       parser - from optParser
    OUTPUT:
    HISTORY:
       2010-12-21 - Written - Bovy (NYU)
    """
    caspwdfile= open('caspwd','r')
    casusr= caspwdfile.readline().rstrip()
    caspwd= caspwdfile.readline().rstrip()
    caspwdfile.close()
    #Read qso file
    s82file= '../data/S82qsos.fits'
    s82qsos= pyfits_utils.table_fields(s82file)[::-1]
    nqso= len(s82qsos.ra)
    done= []
    for qso in s82qsos:
        print "Working on QSO "+qso.oname.strip().replace(' ', '')
        if qso.oname.strip().replace(' ', '') in done:
            print qso.oname.strip().replace(' ', '')+" already done!"
            return
        done.append(qso.oname.strip().replace(' ', ''))
        tmpsavefilename= '../data/s82qsos/'+qso.oname.strip().replace(' ', '')+'.fit'
        if os.path.exists(tmpsavefilename):
            print "file "+tmpsavefilename+" exists"
            print "Delete file "+tmpsavefilename+" before running this to update the sample from the CAS"
        else:
            dbname= prepare_sql(qso)
            subprocess.call([_PYTHON,_CASJOBS,casusr,caspwd,'querywait',
                             '@tmp.sql'])
            subprocess.call([_PYTHON,_CASJOBS,casusr,caspwd,
                             'outputdownloaddelete',dbname,tmpsavefilename])
Exemple #6
0
def dumpFits(parser):
    (options,args)= parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        return
    if options.outfilename is None:
        print "-o filename options needs to be set ..."
        print "Returning ..."
        return None
    #load fits
    if os.path.exists(args[0]):
        savefile= open(args[0],'rb')
        params= pickle.load(savefile)
        type= pickle.load(savefile)
        band= pickle.load(savefile)
        mean= pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("input file does not exist ...")
    #dump fits, first the arrays
    cols= []
    #Get basic quasar stuff
    qsos= open_qsos(options.inputfilename)
    qsoDict= {}
    ii=0
    for qso in qsos:
        qsoDict[qso.oname.strip().replace(' ', '')+'.fit']= ii
        ii+= 1
    paramsKeys= params[params.keys()[0]].keys()
    qsoKeys= params.keys()
    for key in paramsKeys:
        thiscol= []
        for qso in qsoKeys:
            try:
                thiscol.append(params[qso][key][0])
            except IndexError:
                thiscol.append(params[qso][key])
        cols.append(pyfits.Column(name=key,format='D',
                                  array=numpy.array(thiscol)))
    if not options.star and not options.nuvx and not options.uvx \
            and not options.rrlyrae and not options.nuvxall:
        #RA, Dec, name, z, key
        thiscol= []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].ra)
        cols.append(pyfits.Column(name='ra',format='D',
                                  array=numpy.array(thiscol)))
        thiscol= []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].dec)
        cols.append(pyfits.Column(name='dec',format='D',
                                  array=numpy.array(thiscol)))
        thiscol= []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].oname)
        cols.append(pyfits.Column(name='oname',format='30A',
                                  array=numpy.array(thiscol)))
        thiscol= []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].z)
        cols.append(pyfits.Column(name='z',format='D',
                                  array=numpy.array(thiscol)))
    else:
        if options.star:
            dir= '../data/star/'
        elif options.nuvx:
            dir= '../data/nuvx/'
        elif options.uvx:
            dir= '../data/uvx/'
        elif options.nuvxall:
            dir= '../data/nuvx_all/'
        elif options.rrlyrae:
            dir= '../data/rrlyrae/'
        #RA, Dec, key
        racol= []
        deccol= []
        if options.rrlyrae: idcol= []
        for qso in qsoKeys:
            #open relevant file
            file= fu.table_fields(os.path.join(dir,qso))
            indx= (file.mjd_g == 0.)
            if True in indx:
                deccol.append(file.dec[indx][0])
                racol.append(file.ra[indx][0])
            else:
                deccol.append(file.dec[0])
                racol.append(file.ra[0])
            if options.rrlyrae: idcol.append(file.id[0])
        cols.append(pyfits.Column(name='dec',format='D',
                                  array=numpy.array(deccol)))
        cols.append(pyfits.Column(name='ra',format='D',
                                  array=numpy.array(racol)))
        if options.rrlyrae:
            cols.append(pyfits.Column(name='ID',format='K',
                                      array=numpy.array(idcol)))
    thiscol= []
    for qso in qsoKeys:
        thiscol.append(qso)
    cols.append(pyfits.Column(name='key',format='30A',
                              array=numpy.array(thiscol)))
    columns= pyfits.ColDefs(cols)
    tbhdu= pyfits.new_table(columns)
    tbhdu.writeto(options.outfilename)
    #create header
    outfile= pyfits.open(options.outfilename)
    hdr= outfile[1].header
    hdr.update('type',type,'type of covariance fit')
    hdr.update('band',type,'band(s) fit')
    hdr.update('mean',type,'type of mean fit')
    return None
Exemple #7
0
def showExamplegr(filename='../data/SDSSJ203817.37+003029.8.fits',
                constraints=None,basefilename='SDSSJ203817.37+003029.8'):
    """
    NAME:
       showExamplegr
    PURPOSE:
       show an example of a power-law structure function GP covariance function
       fit to an SDSS quasar for g and r
    INPUT:
       filename - filename with the data
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
    OUTPUT:
       writes several plots
    HISTORY:
       2010-08-11 - Written - Bovy (NYU)
    """
    file= fu.table_fields(filename)
    mjd_g= nu.array(file.mjd_g)/365.25
    g= nu.array(file.g)
    err_g= nu.array(file.err_g)
    mjd_r= nu.array(file.mjd_r)/365.25
    r= nu.array(file.r)
    err_r= nu.array(file.err_r)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    r= r[mask]
    r-= nu.mean(r)
    err_r= err_r[mask]
    mjd_r= mjd_r[mask]
    mjd_r-= nu.amin(mjd_r)
    meanErr_r= nu.mean(err_r)
    
    meanErr_gr= nu.mean(nu.sqrt(err_r**2.+err_g**2.))

    nu.random.seed(4)
    nGP=5
    nx=201
    params_mean= ()
    from powerlawSFgr import covarFunc
    params= {'logGamma': array([-9.27821954]), 'logGammagr': array([-19.85172122]), 'gamma': array([ 0.45932629]), 'gammagr': array([ 0.29784021])}
    cf= covarFunc(**params)
    params_covar= (cf)
    ndata= len(g)
    if constraints is None:
        listx= [(t,'g') for t in mjd_g]
        listx.extend([(t,'r') for t in mjd_r])
        listy= [m for m in g]
        listy.extend([m for m in r])
        listy= nu.array(listy)
        noise= [m for m in err_g]
        noise.extend([m for m in err_r])
        noise= nu.array(noise)
        trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        constraints= trainSet
    else:
        constraints= nu.array([])

    useconstraints= constraints
    txs= nu.linspace(-0.1,6.5,nx)
    xs= [(txs[ii],'g') for ii in range(nx)]
    xs.extend([(txs[ii],'r') for ii in range(nx)])
    GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints,tiny_cholesky=.000001)
    thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints)
    thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints)
    plot.bovy_print()
    pyplot.plot(txs,GPsamples[0,:nx],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(mjd_g,g,'k.',zorder=5,ms=10)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(txs,GPsamples[ii,:nx],'-',color=str(0.25+ii*.5/(nGP-1)))
    pyplot.plot(txs,thismean[:nx],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_full.ps')


    plot.bovy_print()
    pyplot.figure()
    pyplot.plot(txs,GPsamples[0,nx-1:-1],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(mjd_r,r,'k.',zorder=5,ms=10)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(txs,GPsamples[ii,nx-1:-1],'-',color=str(0.25+ii*.5/(nGP-1)))
    pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_r,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_fullr.ps')


    plot.bovy_print()
    pyplot.figure()
    ii= 0
    colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
    colors= colors-nu.mean(colors)
    pyplot.plot(txs,colors,'-',color='0.25')
    if isinstance(constraints,trainingSet):
        plot.bovy_plot(mjd_g,g-r,'k.',zorder=5,ms=10,overplot=True)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
        colors= colors-nu.mean(colors)
        pyplot.plot(txs,colors,'-',color=str(0.25+ii*.5/(nGP-1)))
    plotthismean= nu.zeros(nx)
    for ii in range(nx):
        plotthismean[ii]= thismean[ii]-thismean[ii+nx]
    pyplot.plot(txs,plotthismean,'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.18,yerr=meanErr_gr,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    if isinstance(constraints,trainingSet):
        pyplot.ylim(-0.25,.25)
    else:
        pass #pyplot.ylim(-10.,10.)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_color.ps')


    plot.bovy_print()
    pyplot.figure()
    ii= 2
    colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
    pyplot.plot(colors,GPsamples[ii,:nx],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(g-r,g,'k.',zorder=5,ms=10)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
        pyplot.plot(colors,GPsamples[ii,0:nx],'-',color=str(0.25+ii*.5/(nGP-1)))
    colors= nu.array([thismean[jj]-thismean[jj+nx] for jj in range(nx)])
    pyplot.plot(colors,thismean[:nx],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(.13,-0.3,yerr=meanErr_g,xerr=meanErr_gr,color='k')
    pyplot.xlabel(r'$g-r-\langle g-r\rangle\ [\mathrm{mag}]$')
    pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-.2,.2)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_ggr.ps')

    return

    plot.bovy_print()
    pyplot.plot(xs,GPsamples[0,:],'-',color='0.25')
    if not constraints == []:
        plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True)
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints == []:
        pyplot.errorbar(6.15,-0.1,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$m-\langle m\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(3,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_zoom.ps')


    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        pyplot.loglog(sc.arange(1.,len(GPsamples[ii,:])/2)*(xs[1]-xs[0]),
                      2.*sc.var(GPsamples[ii,:])-2.*sc.correlate(GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),"same")[1:len(GPsamples[ii,:])/2][::-1]/len(GPsamples[ii,:]),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline= [(xs[1]-xs[0]),xs[len(xs)/2]]
    pyplot.loglog(xline,nu.exp(-3.02045715)*nu.array(xline)**(0.5868293),'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function}$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename+'_structfunc.ps')
Exemple #8
0
def dumpFits(parser):
    (options, args) = parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        return
    if options.outfilename is None:
        print "-o filename options needs to be set ..."
        print "Returning ..."
        return None
    #load fits
    if os.path.exists(args[0]):
        savefile = open(args[0], 'rb')
        params = pickle.load(savefile)
        type = pickle.load(savefile)
        band = pickle.load(savefile)
        mean = pickle.load(savefile)
        savefile.close()
    else:
        raise IOError("input file does not exist ...")
    #dump fits, first the arrays
    cols = []
    #Get basic quasar stuff
    qsos = open_qsos(options.inputfilename)
    qsoDict = {}
    ii = 0
    for qso in qsos:
        qsoDict[qso.oname.strip().replace(' ', '') + '.fit'] = ii
        ii += 1
    paramsKeys = params[params.keys()[0]].keys()
    qsoKeys = params.keys()
    for key in paramsKeys:
        thiscol = []
        for qso in qsoKeys:
            try:
                thiscol.append(params[qso][key][0])
            except IndexError:
                thiscol.append(params[qso][key])
        cols.append(
            pyfits.Column(name=key, format='D', array=numpy.array(thiscol)))
    if not options.star and not options.nuvx and not options.uvx \
            and not options.rrlyrae and not options.nuvxall:
        #RA, Dec, name, z, key
        thiscol = []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].ra)
        cols.append(
            pyfits.Column(name='ra', format='D', array=numpy.array(thiscol)))
        thiscol = []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].dec)
        cols.append(
            pyfits.Column(name='dec', format='D', array=numpy.array(thiscol)))
        thiscol = []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].oname)
        cols.append(
            pyfits.Column(name='oname',
                          format='30A',
                          array=numpy.array(thiscol)))
        thiscol = []
        for qso in qsoKeys:
            thiscol.append(qsos[qsoDict[qso]].z)
        cols.append(
            pyfits.Column(name='z', format='D', array=numpy.array(thiscol)))
    else:
        if options.star:
            dir = '../data/star/'
        elif options.nuvx:
            dir = '../data/nuvx/'
        elif options.uvx:
            dir = '../data/uvx/'
        elif options.nuvxall:
            dir = '../data/nuvx_all/'
        elif options.rrlyrae:
            dir = '../data/rrlyrae/'
        #RA, Dec, key
        racol = []
        deccol = []
        if options.rrlyrae: idcol = []
        for qso in qsoKeys:
            #open relevant file
            file = fu.table_fields(os.path.join(dir, qso))
            indx = (file.mjd_g == 0.)
            if True in indx:
                deccol.append(file.dec[indx][0])
                racol.append(file.ra[indx][0])
            else:
                deccol.append(file.dec[0])
                racol.append(file.ra[0])
            if options.rrlyrae: idcol.append(file.id[0])
        cols.append(
            pyfits.Column(name='dec', format='D', array=numpy.array(deccol)))
        cols.append(
            pyfits.Column(name='ra', format='D', array=numpy.array(racol)))
        if options.rrlyrae:
            cols.append(
                pyfits.Column(name='ID', format='K', array=numpy.array(idcol)))
    thiscol = []
    for qso in qsoKeys:
        thiscol.append(qso)
    cols.append(
        pyfits.Column(name='key', format='30A', array=numpy.array(thiscol)))
    columns = pyfits.ColDefs(cols)
    tbhdu = pyfits.new_table(columns)
    tbhdu.writeto(options.outfilename)
    #create header
    outfile = pyfits.open(options.outfilename)
    hdr = outfile[1].header
    hdr.update('type', type, 'type of covariance fit')
    hdr.update('band', type, 'band(s) fit')
    hdr.update('mean', type, 'type of mean fit')
    return None
Exemple #9
0
def fitExamplegr(datafilename='../data/SDSSJ203817.37+003029.8.fits'):
    """
    NAME:
       fitExamplegr
    PURPOSE:
       Fit an example of a power-law structure function GP covariance function
       to an SDSS quasar in g and r
    INPUT:
       datafilename
    OUTPUT:
    HISTORY:
       2010-02-21 - Written - Bovy (NYU)
    """
    nu.random.seed(1)
    #Get data
    file= fu.table_fields(datafilename)

    mjd_g= nu.array(file.mjd_g)/365.25
    g= nu.array(file.g)
    err_g= nu.array(file.err_g)
    mjd_r= nu.array(file.mjd_r)/365.25
    r= nu.array(file.r)
    err_r= nu.array(file.err_r)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    r= r[mask]
    r-= nu.mean(r)
    err_r= err_r[mask]
    mjd_r= mjd_r[mask]
    mjd_r-= nu.amin(mjd_r)
    meanErr_r= nu.mean(err_r)

    listx= [(t,'g') for t in mjd_g]
    listx.extend([(t,'r') for t in mjd_r])
    listy= [m for m in g]
    listy.extend([m for m in r])
    listy= nu.array(listy)
    noise= [m for m in err_g]
    noise.extend([m for m in err_r])
    noise= nu.array(noise)
    trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
    
    #SF= covarFunc(Gamma=0.0058,gamma=0.5,Gammagr=0.0058,gammagr=0.5)
    SF= covarFunc(logl=-1.37742591,loga=-3.47341754,
                  loglgr=-2.3777,logagr= -1.)

    #Train
    print "Training ..."
    outcovarFunc= trainGP(trainSet,SF,useDerivs=False)

    print "Best-fit:"
    print outcovarFunc._dict

    return None
Exemple #10
0
def fitExample(N=None,datafilename='../data/SDSSJ203817.37+003029.8.fits',
               band='g'):
    """
    NAME:
       fitExample
    PURPOSE:
       Fit an example of a power-law structure function GP covariance function
       to an SDSS quasar
    INPUT:
       N - use a random subsample of N points
       datafilename
       band - band to fit
    OUTPUT:
    HISTORY:
       2010-02-21 - Written - Bovy (NYU)
    """
    nu.random.seed(1)
    #Get data
    file= fu.table_fields(datafilename)
    if band == 'u':
        mjd_g= nu.array(file.mjd_u)/365.25
        g= nu.array(file.u)
        err_g= nu.array(file.err_u)
    elif band == 'g':
        mjd_g= nu.array(file.mjd_g)/365.25
        g= nu.array(file.g)
        err_g= nu.array(file.err_g)
    elif band == 'r':
        mjd_g= nu.array(file.mjd_r)/365.25
        g= nu.array(file.r)
        err_g= nu.array(file.err_r)
    elif band == 'i':
        mjd_g= nu.array(file.mjd_i)/365.25
        g= nu.array(file.i)
        err_g= nu.array(file.err_i)
    elif band == 'z':
        mjd_g= nu.array(file.mjd_z)/365.25
        g= nu.array(file.z)
        err_g= nu.array(file.err_z)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    trainSet= trainingSet(listx=mjd_g,listy=g,noise=err_g)
    SF= covarFunc(a=.0001,l=.001)
    #SF= covarFunc(gamma=.2,A=0.000524)
    #SF= covarFunc(a=0.18,l=0.005)
    
    #Train
    print "Training ..."
    outcovarFunc= trainGP(trainSet,SF,useDerivs=False)

    print "Best-fit:"
    print outcovarFunc._dict

    return None
Exemple #11
0
def dualBandExample(filename='../data/SDSSJ203817.37+003029.8.fits',
                    constraints=None,basefilename='SDSSJ203817.37+003029.8',
                    covarType='SF'):
    """
    NAME:
       dualBandExample
    PURPOSE:
       show an example of a power-law structure function GP covariance function
       fit to an SDSS quasar for g and r
    INPUT:
       filename - filename with the data
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
       covarType - 'SF' or 'DRW'
    OUTPUT:
       writes several plots
    HISTORY:
       2010-08-11 - Written - Bovy (NYU)
    """
    file= fu.table_fields(filename)
    mjd_g= nu.array(file.mjd_g)/365.25
    g= nu.array(file.g)
    err_g= nu.array(file.err_g)
    mjd_r= nu.array(file.mjd_r)/365.25
    r= nu.array(file.r)
    err_r= nu.array(file.err_r)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    r= r[mask]
    r-= nu.mean(r)
    err_r= err_r[mask]
    mjd_r= mjd_r[mask]
    mjd_r-= nu.amin(mjd_r)
    meanErr_r= nu.mean(err_r)
    
    meanErr_gr= nu.mean(nu.sqrt(err_r**2.+err_g**2.))

    nu.random.seed(4)
    nGP=5
    nx=201
    params_mean= ()
    if covarType == 'SF':
        from powerlawSFgr import covarFunc
        params= {'logGamma': array([-7.33271548]), 'logGammagr': array([-10.5]), 'gamma': array([ 0.4821092]), 'gammagr': array([ 0.5])}
        params= {'logGamma': array([-7.79009776]), 'logGammagr': array([-28.0487848]), 'gamma': array([ 0.45918053]), 'gammagr': array([ 0.21333858])}
    else:
        from OUgr import covarFunc
        params= {'logl': array([ 1.94844503]), 'loglgr': array([ 7.36282174]), 'logagr2': array([ 1.0196474]), 'loga2': array([-0.00588868])}
        params= {'logl':-1.37742591,'loga':-3.47341754,
                 'loglgr': -2.3777,'logagr': -4.}
        params= {'logl': array([-1.38968195]), 'loglgr': array([-2.46684501]), 'logagr2': array([-6.62320832]), 'loga2': array([-3.52099305])}
    paramsSF= params
    cf= covarFunc(**params)
    params_covar= (cf)
    ndata= len(g)
    if constraints is None:
        listx= [(t,'g') for t in mjd_g]
        listx.extend([(t,'r') for t in mjd_r])
        listy= [m for m in g]
        listy.extend([m for m in r])
        listy= nu.array(listy)
        noise= [m for m in err_g]
        noise.extend([m for m in err_r])
        noise= nu.array(noise)
        trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        constraints= trainSet
    else:
        constraints= nu.array([])

    useconstraints= constraints
    txs= nu.linspace(-0.1,6.5,nx)
    xs= [(txs[ii],'g') for ii in range(nx)]
    xs.extend([(txs[ii],'r') for ii in range(nx)])
    GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints,tiny_cholesky=.00000001)
    thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints)
    thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints)
    #Calculate loglike
    if isinstance(constraints,trainingSet):
        (params,packing)= pack_params(cf)
        covarFuncName= inspect.getmodule(cf).__name__
        thisCovarClass= __import__(covarFuncName)
        loglike= marginalLikelihood(params,constraints,packing,
                                    thisCovarClass)

    plot.bovy_print()
    pyplot.plot(txs,GPsamples[0,:nx],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(mjd_g,g,'k.',zorder=5,ms=10)
    title= re.split(r'_',basefilename)[0]
    if covarType == 'SF':
        method= '\mathrm{power-law\ structure\ functions}'
    else:
        method= '\mathrm{damped\ random\ walk}'
    plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True)
    if isinstance(constraints,trainingSet):
        plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' %(-loglike),
                       top_left=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(txs,GPsamples[ii,:nx],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(txs,thismean[:nx],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    pyplot.ylim(-0.6,0.6)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_fullg.ps')


    plot.bovy_print()
    pyplot.figure()
    pyplot.plot(txs,GPsamples[0,nx-1:-1],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(mjd_r,r,'k.',zorder=5,ms=10)
    #plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(txs,GPsamples[ii,nx-1:-1],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_r,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    pyplot.ylim(-0.6,0.6)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_fullr.ps')


    plot.bovy_print()
    pyplot.figure()
    ii= 0
    colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
    if not isinstance(constraints,trainingSet):
        colors= colors-nu.mean(colors)
    pyplot.plot(txs,colors,'-',color='0.25')
    if isinstance(constraints,trainingSet):
        plot.bovy_plot(mjd_g,g-r,'k.',zorder=5,ms=10,overplot=True)
    #plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
        if not isinstance(constraints,trainingSet):
            colors= colors-nu.mean(colors)
        pyplot.plot(txs,colors,'-',color=str(0.25+ii*.5/(nGP-1)))
    plotthismean= nu.zeros(nx)
    for ii in range(nx):
        plotthismean[ii]= thismean[ii]-thismean[ii+nx]
    #pyplot.plot(txs,plotthismean,'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.18,yerr=meanErr_gr,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    if isinstance(constraints,trainingSet):
        pyplot.ylim(-0.25,.25)
    else:
        pass #pyplot.ylim(-10.,10.)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_color.ps')

    if covarType == 'DRW':
        return

    #Plot structure functions

    #g
    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        thisSample= GPsamples[ii,:nx]
        pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]),
                      2.*sc.var(thisSample)\
                          -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline= [(txs[1]-txs[0]),txs[len(txs)/2]]
    pyplot.loglog(xline,(nu.exp(paramsSF['logGamma'])*nu.array(xline))**(paramsSF['gamma']),'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ g$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename+'_structfuncg.ps')


    #r
    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        thisSample= GPsamples[ii,nx-1:-1]
        pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]),
                      2.*sc.var(thisSample)\
                          -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline= [(txs[1]-txs[0]),txs[len(txs)/2]]
    pyplot.loglog(xline,(nu.exp(paramsSF['logGamma'])*nu.array(xline))**(paramsSF['gamma']),'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ r$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename+'_structfuncr.ps')


    #g-r
    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        thisSample= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
        pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]),
                      2.*sc.var(thisSample)\
                          -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline= [(txs[1]-txs[0]),txs[len(txs)/2]]
    pyplot.loglog(xline,(nu.exp(paramsSF['logGammagr'])*nu.array(xline))**(paramsSF['gammagr']),'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{color\ structure\ function}$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename+'_structfuncgr.ps')
Exemple #12
0
def singleBandExample(filename='../data/SDSSJ203817.37+003029.8.fits',
                      band='g',
                      constraints=None,
                      basefilename='SDSSJ203817.37+003029.8',
                      covarType='SF'):
    """
    NAME:
       singleBandExample
    PURPOSE:
       show an example of a power-law structure function or damped-random walk
       GP covariance function fit to an SDSS quasar
    INPUT:
       filename - filename with the data
       band - band to consider
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
       covarType - 'SF' or 'DRW'
    OUTPUT:
       writes several plots
    HISTORY:
       2010-06-20 - Written - Bovy (NYU)
    """
    file = fu.table_fields(filename)
    if band == 'u':
        mjd_g = nu.array(file.mjd_u) / 365.25
        g = nu.array(file.u)
        err_g = nu.array(file.err_u)
    elif band == 'g':
        mjd_g = nu.array(file.mjd_g) / 365.25
        g = nu.array(file.g)
        err_g = nu.array(file.err_g)
    elif band == 'r':
        mjd_g = nu.array(file.mjd_r) / 365.25
        g = nu.array(file.r)
        err_g = nu.array(file.err_r)
    elif band == 'i':
        mjd_g = nu.array(file.mjd_i) / 365.25
        g = nu.array(file.i)
        err_g = nu.array(file.err_i)
    elif band == 'z':
        mjd_g = nu.array(file.mjd_z) / 365.25
        g = nu.array(file.z)
        err_g = nu.array(file.err_z)

    mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7)  #Adjust for non-g
    g = g[mask]
    g -= nu.mean(g)
    err_g = err_g[mask]
    mjd_g = mjd_g[mask]
    mjd_g -= nu.amin(mjd_g)
    meanErr_g = nu.mean(err_g)

    nu.random.seed(4)
    nGP = 5
    nx = 201
    params_mean = ()
    if covarType == 'SF':
        from powerlawSF import covarFunc
        params = {'gamma': array([0.49500723]), 'logA': array([-3.36044037])}
    else:
        from OU_ARD import covarFunc
        params = {'logl': array([-1.37742591]), 'loga2': array([-3.47341754])}
    cf = covarFunc(**params)
    params_covar = (cf)
    ndata = len(g)
    if constraints is None:
        listx = mjd_g
        listy = g
        noise = err_g
        trainSet = trainingSet(listx=listx, listy=listy, noise=noise)
        constraints = trainSet
    else:
        constraints = None

    useconstraints = constraints
    xs = nu.linspace(-0.1, 6.5, nx)
    GPsamples = eval_gp(xs,
                        mean,
                        covar, (),
                        params_covar,
                        nGP=nGP,
                        constraints=useconstraints)
    thismean = calc_constrained_mean(xs, mean, params_mean, covar,
                                     params_covar, useconstraints)
    thiscovar = calc_constrained_covar(xs, covar, params_covar, useconstraints)
    #If unconstrained, subtract the mean
    if constraints is None:
        for ii in range(nGP):
            GPsamples[ii, :] = GPsamples[ii, :] - nu.mean(GPsamples[ii, :])

    #Calculate loglike
    if not constraints is None:
        (params, packing) = pack_params(cf)
        covarFuncName = inspect.getmodule(cf).__name__
        thisCovarClass = __import__(covarFuncName)
        loglike = marginalLikelihood(params, constraints, packing,
                                     thisCovarClass)
    plot.bovy_print()
    pyplot.plot(xs, GPsamples[0, :], '-', color='0.25')
    if not constraints is None:
        plot.bovy_plot(mjd_g, g, 'k.', zorder=5, ms=10, overplot=True)
    title = re.split(r'_', basefilename)[0]
    if covarType == 'SF':
        method = '\mathrm{power-law\ structure\ function}'
    else:
        method = '\mathrm{damped\ random\ walk}'
    plot.bovy_text(r'$\mathrm{' + title + '\ / \ ' + method + r'}$',
                   title=True)
    if not constraints is None:
        plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' %
                       (-loglike),
                       top_left=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(nGP):
        pyplot.plot(xs,
                    GPsamples[ii, :],
                    '-',
                    color=str(0.25 + ii * .5 / (nGP - 1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints is None:
        pyplot.errorbar(6.15, -0.25, yerr=meanErr_g, color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$' + band + r'-\langle ' + band +
                  r' \rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1, 6.5)
    if constraints is None:
        pyplot.ylim(-.6, .6)
    else:
        pyplot.ylim(-.6, .6)
    plot._add_ticks()
    plot.bovy_end_print(basefilename + '_full.ps')

    plot.bovy_print()
    pyplot.plot(xs, GPsamples[0, :], '-', color='0.25')
    if not constraints is None:
        plot.bovy_plot(mjd_g, g, 'k.', zorder=5, ms=10, overplot=True)
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1, nGP):
        pyplot.plot(xs,
                    GPsamples[ii, :],
                    '-',
                    color=str(0.25 + ii * .5 / (nGP - 1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints == []:
        pyplot.errorbar(6.15, -0.25, yerr=meanErr_g, color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$' + band + '-\langle ' + band +
                  '\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(3, 6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename + '_zoom.ps')

    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        pyplot.loglog(
            sc.arange(1.,
                      len(GPsamples[ii, :]) / 2) * (xs[1] - xs[0]),
            2. * sc.var(GPsamples[ii, :]) -
            2. * sc.correlate(GPsamples[ii, :] - sc.mean(GPsamples[ii, :]),
                              GPsamples[ii, :] - sc.mean(GPsamples[ii, :]),
                              "same")[1:len(GPsamples[ii, :]) / 2][::-1] /
            len(GPsamples[ii, :]),
            color=str(0.25 + ii * .5 / (nGP - 1)))
    xline = [(xs[1] - xs[0]), xs[len(xs) / 2]]
    pyplot.loglog(xline,
                  nu.exp(-3.36044037) * nu.array(xline)**(0.49500723), 'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function}$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename + '_structfunc.ps')
Exemple #13
0
def singleBandExample(filename='../data/SDSSJ203817.37+003029.8.fits',band='g',
                      constraints=None,basefilename='SDSSJ203817.37+003029.8',
                      covarType='SF'):
    """
    NAME:
       singleBandExample
    PURPOSE:
       show an example of a power-law structure function or damped-random walk
       GP covariance function fit to an SDSS quasar
    INPUT:
       filename - filename with the data
       band - band to consider
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
       covarType - 'SF' or 'DRW'
    OUTPUT:
       writes several plots
    HISTORY:
       2010-06-20 - Written - Bovy (NYU)
    """
    file= fu.table_fields(filename)
    if band == 'u':
        mjd_g= nu.array(file.mjd_u)/365.25
        g= nu.array(file.u)
        err_g= nu.array(file.err_u)
    elif band == 'g':
        mjd_g= nu.array(file.mjd_g)/365.25
        g= nu.array(file.g)
        err_g= nu.array(file.err_g)
    elif band == 'r':
        mjd_g= nu.array(file.mjd_r)/365.25
        g= nu.array(file.r)
        err_g= nu.array(file.err_r)
    elif band == 'i':
        mjd_g= nu.array(file.mjd_i)/365.25
        g= nu.array(file.i)
        err_g= nu.array(file.err_i)
    elif band == 'z':
        mjd_g= nu.array(file.mjd_z)/365.25
        g= nu.array(file.z)
        err_g= nu.array(file.err_z)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    nu.random.seed(4)
    nGP=5
    nx=201
    params_mean= ()
    if covarType == 'SF':
        from powerlawSF import covarFunc
        params= {'gamma': array([ 0.49500723]), 'logA': array([-3.36044037])}
    else:
        from OU_ARD import covarFunc
        params= {'logl': array([-1.37742591]), 'loga2': array([-3.47341754])}
    cf= covarFunc(**params)
    params_covar= (cf)
    ndata= len(g)
    if constraints is None:
        listx= mjd_g
        listy= g
        noise= err_g
        trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        constraints= trainSet
    else:
        constraints= None

    useconstraints= constraints
    xs= nu.linspace(-0.1,6.5,nx)
    GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints)
    thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints)
    thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints)
    #If unconstrained, subtract the mean
    if constraints is None:
        for ii in range(nGP):
            GPsamples[ii,:]= GPsamples[ii,:]-nu.mean(GPsamples[ii,:])

    #Calculate loglike
    if not constraints is None:
        (params,packing)= pack_params(cf)
        covarFuncName= inspect.getmodule(cf).__name__
        thisCovarClass= __import__(covarFuncName)
        loglike= marginalLikelihood(params,constraints,packing,
                                    thisCovarClass)
    plot.bovy_print()
    pyplot.plot(xs,GPsamples[0,:],'-',color='0.25')
    if not constraints is None:
        plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True)
    title= re.split(r'_',basefilename)[0]
    if covarType == 'SF':
        method= '\mathrm{power-law\ structure\ function}'
    else:
        method= '\mathrm{damped\ random\ walk}'
    plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True)
    if not constraints is None:
        plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' %(-loglike),
                       top_left=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(nGP):
        pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints is None:
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$'+band+r'-\langle '+band+r' \rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    if constraints is None:
        pyplot.ylim(-.6,.6)
    else:
        pyplot.ylim(-.6,.6)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_full.ps')

    plot.bovy_print()
    pyplot.plot(xs,GPsamples[0,:],'-',color='0.25')
    if not constraints is None:
        plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True)
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints == []:
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$'+band+'-\langle '+band+'\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(3,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_zoom.ps')


    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        pyplot.loglog(sc.arange(1.,len(GPsamples[ii,:])/2)*(xs[1]-xs[0]),
                      2.*sc.var(GPsamples[ii,:])-2.*sc.correlate(GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),"same")[1:len(GPsamples[ii,:])/2][::-1]/len(GPsamples[ii,:]),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline= [(xs[1]-xs[0]),xs[len(xs)/2]]
    pyplot.loglog(xline,nu.exp(-3.36044037)*nu.array(xline)**(0.49500723),'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function}$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename+'_structfunc.ps')
Exemple #14
0
def showExample(filename='../data/SDSSJ203817.37+003029.8.fits',band='g',
                constraints=None,basefilename='SDSSJ203817.37+003029.8'):
    """
    NAME:
       showExample
    PURPOSE:
       show an example of a power-law structure function GP covariance function
       fit to an SDSS quasar
    INPUT:
       filename - filename with the data
       band - band to consider
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
    OUTPUT:
       writes several plots
    HISTORY:
       2010-06-20 - Written - Bovy (NYU)
    """
    file= fu.table_fields(filename)
    if band == 'u':
        mjd_g= nu.array(file.mjd_u)/365.25
        g= nu.array(file.u)
        err_g= nu.array(file.err_u)
    elif band == 'g':
        mjd_g= nu.array(file.mjd_g)/365.25
        g= nu.array(file.g)
        err_g= nu.array(file.err_g)
    elif band == 'r':
        mjd_g= nu.array(file.mjd_r)/365.25
        g= nu.array(file.r)
        err_g= nu.array(file.err_r)
    elif band == 'i':
        mjd_g= nu.array(file.mjd_i)/365.25
        g= nu.array(file.i)
        err_g= nu.array(file.err_i)
    elif band == 'z':
        mjd_g= nu.array(file.mjd_z)/365.25
        g= nu.array(file.z)
        err_g= nu.array(file.err_z)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)


    nu.random.seed(4)
    nGP=5
    nx=201
    params_mean= ()
    from powerlawSFgr import covarFunc
    params= {'logA': array([ 1.63714183]), 'gamma': array([ 0.60216581])}
    params= {'gamma': array([ 0.49500723]), 'logA': array([-3.36044037])}
    cf= covarFunc(**params)
    params_covar= (cf)
    ndata= len(g)
    if constraints is None:
        listx= mjd_g
        listy= g
        noise= err_g
        trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        constraints= trainSet
    else:
        constraints= None

    useconstraints= constraints
    xs= nu.linspace(-0.1,6.5,nx)
    GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints)
    thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints)
    thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints)
    plot.bovy_print()
    pyplot.plot(xs,GPsamples[0,:],'-',color='0.25')
    if not constraints is None:
        plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(nGP):
        pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints == []:
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$m-\langle m\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    pyplot.ylim(-1.,1.)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_full.ps')

    plot.bovy_print()
    pyplot.plot(xs,GPsamples[0,:],'-',color='0.25')
    if not constraints is None:
        plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True)
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1)))
    #pyplot.plot(xs,thismean,'k-',linewidth=2)
    if not constraints == []:
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$m-\langle m\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(3,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_zoom.ps')


    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        pyplot.loglog(sc.arange(1.,len(GPsamples[ii,:])/2)*(xs[1]-xs[0]),
                      2.*sc.var(GPsamples[ii,:])-2.*sc.correlate(GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),"same")[1:len(GPsamples[ii,:])/2][::-1]/len(GPsamples[ii,:]),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline= [(xs[1]-xs[0]),xs[len(xs)/2]]
    pyplot.loglog(xline,nu.exp(-3.02045715)*nu.array(xline)**(0.5868293),'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function}$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename+'_structfunc.ps')
Exemple #15
0
def dualBandExample(filename='../data/SDSSJ203817.37+003029.8.fits',
                    constraints=None,
                    basefilename='SDSSJ203817.37+003029.8',
                    covarType='SF'):
    """
    NAME:
       dualBandExample
    PURPOSE:
       show an example of a power-law structure function GP covariance function
       fit to an SDSS quasar for g and r
    INPUT:
       filename - filename with the data
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
       covarType - 'SF' or 'DRW'
    OUTPUT:
       writes several plots
    HISTORY:
       2010-08-11 - Written - Bovy (NYU)
    """
    file = fu.table_fields(filename)
    mjd_g = nu.array(file.mjd_g) / 365.25
    g = nu.array(file.g)
    err_g = nu.array(file.err_g)
    mjd_r = nu.array(file.mjd_r) / 365.25
    r = nu.array(file.r)
    err_r = nu.array(file.err_r)

    mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7)  #Adjust for non-g
    g = g[mask]
    g -= nu.mean(g)
    err_g = err_g[mask]
    mjd_g = mjd_g[mask]
    mjd_g -= nu.amin(mjd_g)
    meanErr_g = nu.mean(err_g)

    r = r[mask]
    r -= nu.mean(r)
    err_r = err_r[mask]
    mjd_r = mjd_r[mask]
    mjd_r -= nu.amin(mjd_r)
    meanErr_r = nu.mean(err_r)

    meanErr_gr = nu.mean(nu.sqrt(err_r**2. + err_g**2.))

    nu.random.seed(4)
    nGP = 5
    nx = 201
    params_mean = ()
    if covarType == 'SF':
        from powerlawSFgr import covarFunc
        params = {
            'logGamma': array([-7.33271548]),
            'logGammagr': array([-10.5]),
            'gamma': array([0.4821092]),
            'gammagr': array([0.5])
        }
        params = {
            'logGamma': array([-7.79009776]),
            'logGammagr': array([-28.0487848]),
            'gamma': array([0.45918053]),
            'gammagr': array([0.21333858])
        }
    else:
        from OUgr import covarFunc
        params = {
            'logl': array([1.94844503]),
            'loglgr': array([7.36282174]),
            'logagr2': array([1.0196474]),
            'loga2': array([-0.00588868])
        }
        params = {
            'logl': -1.37742591,
            'loga': -3.47341754,
            'loglgr': -2.3777,
            'logagr': -4.
        }
        params = {
            'logl': array([-1.38968195]),
            'loglgr': array([-2.46684501]),
            'logagr2': array([-6.62320832]),
            'loga2': array([-3.52099305])
        }
    paramsSF = params
    cf = covarFunc(**params)
    params_covar = (cf)
    ndata = len(g)
    if constraints is None:
        listx = [(t, 'g') for t in mjd_g]
        listx.extend([(t, 'r') for t in mjd_r])
        listy = [m for m in g]
        listy.extend([m for m in r])
        listy = nu.array(listy)
        noise = [m for m in err_g]
        noise.extend([m for m in err_r])
        noise = nu.array(noise)
        trainSet = trainingSet(listx=listx, listy=listy, noise=noise)
        constraints = trainSet
    else:
        constraints = nu.array([])

    useconstraints = constraints
    txs = nu.linspace(-0.1, 6.5, nx)
    xs = [(txs[ii], 'g') for ii in range(nx)]
    xs.extend([(txs[ii], 'r') for ii in range(nx)])
    GPsamples = eval_gp(xs,
                        mean,
                        covar, (),
                        params_covar,
                        nGP=nGP,
                        constraints=useconstraints,
                        tiny_cholesky=.00000001)
    thismean = calc_constrained_mean(xs, mean, params_mean, covar,
                                     params_covar, useconstraints)
    thiscovar = calc_constrained_covar(xs, covar, params_covar, useconstraints)
    #Calculate loglike
    if isinstance(constraints, trainingSet):
        (params, packing) = pack_params(cf)
        covarFuncName = inspect.getmodule(cf).__name__
        thisCovarClass = __import__(covarFuncName)
        loglike = marginalLikelihood(params, constraints, packing,
                                     thisCovarClass)

    plot.bovy_print()
    pyplot.plot(txs, GPsamples[0, :nx], '-', color='0.25')
    if isinstance(constraints, trainingSet):
        pyplot.plot(mjd_g, g, 'k.', zorder=5, ms=10)
    title = re.split(r'_', basefilename)[0]
    if covarType == 'SF':
        method = '\mathrm{power-law\ structure\ functions}'
    else:
        method = '\mathrm{damped\ random\ walk}'
    plot.bovy_text(r'$\mathrm{' + title + '\ / \ ' + method + r'}$',
                   title=True)
    if isinstance(constraints, trainingSet):
        plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' %
                       (-loglike),
                       top_left=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1, nGP):
        pyplot.plot(txs,
                    GPsamples[ii, :nx],
                    '-',
                    color=str(0.25 + ii * .5 / (nGP - 1)))
    #pyplot.plot(txs,thismean[:nx],'k-',linewidth=2)
    if isinstance(constraints, trainingSet):
        pyplot.errorbar(6.15, -0.25, yerr=meanErr_g, color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1, 6.5)
    pyplot.ylim(-0.6, 0.6)
    plot._add_ticks()
    plot.bovy_end_print(basefilename + '_fullg.ps')

    plot.bovy_print()
    pyplot.figure()
    pyplot.plot(txs, GPsamples[0, nx - 1:-1], '-', color='0.25')
    if isinstance(constraints, trainingSet):
        pyplot.plot(mjd_r, r, 'k.', zorder=5, ms=10)
    #plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1, nGP):
        pyplot.plot(txs,
                    GPsamples[ii, nx - 1:-1],
                    '-',
                    color=str(0.25 + ii * .5 / (nGP - 1)))
    #pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2)
    if isinstance(constraints, trainingSet):
        pyplot.errorbar(6.15, -0.25, yerr=meanErr_r, color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1, 6.5)
    pyplot.ylim(-0.6, 0.6)
    plot._add_ticks()
    plot.bovy_end_print(basefilename + '_fullr.ps')

    plot.bovy_print()
    pyplot.figure()
    ii = 0
    colors = nu.array(
        [GPsamples[ii, jj] - GPsamples[ii, jj + nx] for jj in range(nx)])
    if not isinstance(constraints, trainingSet):
        colors = colors - nu.mean(colors)
    pyplot.plot(txs, colors, '-', color='0.25')
    if isinstance(constraints, trainingSet):
        plot.bovy_plot(mjd_g, g - r, 'k.', zorder=5, ms=10, overplot=True)
    #plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1, nGP):
        colors = nu.array(
            [GPsamples[ii, jj] - GPsamples[ii, jj + nx] for jj in range(nx)])
        if not isinstance(constraints, trainingSet):
            colors = colors - nu.mean(colors)
        pyplot.plot(txs, colors, '-', color=str(0.25 + ii * .5 / (nGP - 1)))
    plotthismean = nu.zeros(nx)
    for ii in range(nx):
        plotthismean[ii] = thismean[ii] - thismean[ii + nx]
    #pyplot.plot(txs,plotthismean,'k-',linewidth=2)
    if isinstance(constraints, trainingSet):
        pyplot.errorbar(6.15, -0.18, yerr=meanErr_gr, color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1, 6.5)
    if isinstance(constraints, trainingSet):
        pyplot.ylim(-0.25, .25)
    else:
        pass  #pyplot.ylim(-10.,10.)
    plot._add_ticks()
    plot.bovy_end_print(basefilename + '_color.ps')

    if covarType == 'DRW':
        return

    #Plot structure functions

    #g
    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        thisSample = GPsamples[ii, :nx]
        pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]),
                      2.*sc.var(thisSample)\
                          -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline = [(txs[1] - txs[0]), txs[len(txs) / 2]]
    pyplot.loglog(xline, (nu.exp(paramsSF['logGamma']) *
                          nu.array(xline))**(paramsSF['gamma']), 'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ g$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename + '_structfuncg.ps')

    #r
    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        thisSample = GPsamples[ii, nx - 1:-1]
        pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]),
                      2.*sc.var(thisSample)\
                          -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline = [(txs[1] - txs[0]), txs[len(txs) / 2]]
    pyplot.loglog(xline, (nu.exp(paramsSF['logGamma']) *
                          nu.array(xline))**(paramsSF['gamma']), 'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ r$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename + '_structfuncr.ps')

    #g-r
    plot.bovy_print()
    pyplot.figure()
    for ii in range(nGP):
        thisSample = nu.array(
            [GPsamples[ii, jj] - GPsamples[ii, jj + nx] for jj in range(nx)])
        pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]),
                      2.*sc.var(thisSample)\
                          -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample),
                      color=str(0.25+ii*.5/(nGP-1)))
    xline = [(txs[1] - txs[0]), txs[len(txs) / 2]]
    pyplot.loglog(xline, (nu.exp(paramsSF['logGammagr']) *
                          nu.array(xline))**(paramsSF['gammagr']), 'k--')
    pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$\mathrm{color\ structure\ function}$')
    #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True)
    plot.bovy_end_print(basefilename + '_structfuncgr.ps')
Exemple #16
0
def showExampleDRW(filename='../data/SDSSJ203817.37+003029.8.fits',
                constraints=None,basefilename='SDSSJ203817.37+003029.8'):
    """
    NAME:
       showExampleDRW
    PURPOSE:
       show an example of a pDRW structure function GP covariance function
       fit to an SDSS quasar for g and r
    INPUT:
       filename - filename with the data
       constraints - if None, use all constraints, if [] use no constraints!
       basefilename - basefilename for plots
    OUTPUT:
       writes several plots
    HISTORY:
       2010-08-11 - Written - Bovy (NYU)
    """
    file= fu.table_fields(filename)
    mjd_g= nu.array(file.mjd_g)/365.25
    g= nu.array(file.g)
    err_g= nu.array(file.err_g)
    mjd_r= nu.array(file.mjd_r)/365.25
    r= nu.array(file.r)
    err_r= nu.array(file.err_r)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    r= r[mask]
    r-= nu.mean(r)
    err_r= err_r[mask]
    mjd_r= mjd_g#mjd_r[mask]
    #mjd_r-= nu.amin(mjd_r)
    meanErr_r= nu.mean(err_r)
    
    meanErr_gr= nu.mean(nu.sqrt(err_r**2.+err_g**2.))

    nu.random.seed(4)
    nGP=5
    nx=201
    params_mean= ()
    from drwcross import covarFunc
    #params= {'logS': array([1.63714183]), 'logtau': array([0.]), 'logB': array([ 2.32288434])}
    #params= {'logtau': array([-1.36399325]), 'logB': array([-519.45950737]), 'logS': array([-3.51531852])}
    params= {'logS': array([-1.08795813]), 'logtau': array([ 1.2790154]), 'logB': array([-1.01029709]), 'logC': array([-79.88349478]), 'gammagr': array([ 7.90536799]), 'logGammagr': array([ 2.8001978])}
    cf= covarFunc(**params)
    params_covar= (cf)
    ndata= len(g)
    if constraints is None:
        listx= [(t,'g') for t in mjd_g]
        listx.extend([(t,'r') for t in mjd_r])
        listy= [m for m in g]
        listy.extend([m for m in r])
        listy= nu.array(listy)
        noise= [m for m in err_g]
        noise.extend([m for m in err_r])
        noise= nu.array(noise)
        trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        constraints= trainSet
    else:
        constraints= nu.array([])

    useconstraints= constraints
    txs= nu.linspace(-0.1,6.5,nx)
    xs= [(txs[ii],'g') for ii in range(nx)]
    xs.extend([(txs[ii],'r') for ii in range(nx)])
    GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints,tiny_cholesky=.000001)
    thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints)
    thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints)
    plot.bovy_print()
    pyplot.plot(txs,GPsamples[0,:nx],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(mjd_g,g,'k.',zorder=5,ms=10)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(txs,GPsamples[ii,:nx],'-',color=str(0.25+ii*.5/(nGP-1)))
    pyplot.plot(txs,thismean[:nx],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_full.ps')


    plot.bovy_print()
    pyplot.figure()
    pyplot.plot(txs,GPsamples[0,nx-1:-1],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(mjd_r,r,'k.',zorder=5,ms=10)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        pyplot.plot(txs,GPsamples[ii,nx-1:-1],'-',color=str(0.25+ii*.5/(nGP-1)))
    pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.25,yerr=meanErr_r,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_fullr.ps')


    plot.bovy_print()
    pyplot.figure()
    ii= 0
    colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
    colors= colors-nu.mean(colors)
    pyplot.plot(txs,colors,'-',color='0.25')
    if isinstance(constraints,trainingSet):
        plot.bovy_plot(mjd_g,g-r,'k.',zorder=5,ms=10,overplot=True)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
        colors= colors-nu.mean(colors)
        pyplot.plot(txs,colors,'-',color=str(0.25+ii*.5/(nGP-1)))
    plotthismean= nu.zeros(nx)
    for ii in range(nx):
        plotthismean[ii]= thismean[ii]-thismean[ii+nx]
    pyplot.plot(txs,plotthismean,'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(6.15,-0.18,yerr=meanErr_gr,color='k')
    pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$')
    pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-0.1,6.5)
    if isinstance(constraints,trainingSet):
        pyplot.ylim(-0.25,.25)
    else:
        pass #pyplot.ylim(-10.,10.)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_color.ps')


    plot.bovy_print()
    pyplot.figure()
    ii= 2
    colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
    pyplot.plot(colors,GPsamples[ii,:nx],'-',color='0.25')
    if isinstance(constraints,trainingSet):
        pyplot.plot(g-r,g,'k.',zorder=5,ms=10)
    plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True)
    #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75')
    for ii in range(1,nGP):
        colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)])
        pyplot.plot(colors,GPsamples[ii,0:nx],'-',color=str(0.25+ii*.5/(nGP-1)))
    colors= nu.array([thismean[jj]-thismean[jj+nx] for jj in range(nx)])
    pyplot.plot(colors,thismean[:nx],'k-',linewidth=2)
    if isinstance(constraints,trainingSet):
        pyplot.errorbar(.13,-0.3,yerr=meanErr_g,xerr=meanErr_gr,color='k')
    pyplot.xlabel(r'$g-r-\langle g-r\rangle\ [\mathrm{mag}]$')
    pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$')
    pyplot.xlim(-.2,.2)
    plot._add_ticks()
    plot.bovy_end_print(basefilename+'_ggr.ps')

    return
Exemple #17
0
def singleBandSampleExample(
        datafilename='../data/SDSSJ203817.37+003029.8.fits',
        band='g',
        nsamples=1000,
        basefilename='SDSSJ203817.37+003029.8'):
    """
    NAME:
       singleBandSampleExample
    PURPOSE:
       Sample an example of a power-law structure function GP covariance 
       function to an SDSS quasar
    INPUT:
       datafilename
       band - band to fit
       nsamples - number of samples to use
       basefilename
    OUTPUT:
    HISTORY:
       2010-08-08 - Written - Bovy (NYU)
    """
    nu.random.seed(1)
    #Get data
    file = fu.table_fields(datafilename)
    if band == 'u':
        mjd_g = nu.array(file.mjd_u) / 365.25
        g = nu.array(file.u)
        err_g = nu.array(file.err_u)
    elif band == 'g':
        mjd_g = nu.array(file.mjd_g) / 365.25
        g = nu.array(file.g)
        err_g = nu.array(file.err_g)
    elif band == 'r':
        mjd_g = nu.array(file.mjd_r) / 365.25
        g = nu.array(file.r)
        err_g = nu.array(file.err_r)
    elif band == 'i':
        mjd_g = nu.array(file.mjd_i) / 365.25
        g = nu.array(file.i)
        err_g = nu.array(file.err_i)
    elif band == 'z':
        mjd_g = nu.array(file.mjd_z) / 365.25
        g = nu.array(file.z)
        err_g = nu.array(file.err_z)
    mjd_r = nu.array(file.mjd_r) / 365.25
    r = nu.array(file.r)
    err_r = nu.array(file.err_r)
    mjd_i = nu.array(file.mjd_i) / 365.25
    i = nu.array(file.i)
    err_i = nu.array(file.err_i)

    mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7)
    g = g[mask]
    g -= nu.mean(g)
    err_g = err_g[mask]
    mjd_g = mjd_g[mask]
    mjd_g -= nu.amin(mjd_g)
    meanErr_g = nu.mean(err_g)

    r = r[mask]
    r -= nu.mean(r)
    err_r = err_r[mask]
    mjd_r = mjd_r[mask]
    mjd_r -= nu.amin(mjd_r)
    meanErr_r = nu.mean(err_r)

    i = i[mask]
    i -= nu.mean(i)
    err_i = err_i[mask]
    mjd_i = mjd_i[mask]
    mjd_i -= nu.amin(mjd_i)
    meanErr_i = nu.mean(err_i)

    savefilename = basefilename + '.sav'
    if os.path.exists(savefilename):
        savefile = open(savefilename, 'rb')
        gammas = pickle.load(savefile)
        if 'gr' in basefilename:
            logGammas = pickle.load(savefile)
            gammagrs = pickle.load(savefile)
            logGammagrs = pickle.load(savefile)
        else:
            logAs = pickle.load(savefile)
        out = pickle.load(savefile)
        savefile.close()
    else:
        if 'gri' in basefilename:
            raise NotImplementedError
            from powerlawSFgr import covarFunc
            params = {
                'logGamma': array([-7.79009776]),
                'logGammagr': array([-28.0487848]),
                'gamma': array([0.45918053]),
                'gammagr': array([0.21333858])
            }
            SF = covarFunc(**params)
            listx = [(t, 'g') for t in mjd_g]
            listx.extend([(t, 'r') for t in mjd_r])
            listx.extend([(t, 'i') for t in mjd_i])
            listy = [m for m in g]
            listy.extend([m for m in r])
            listy.extend([m for m in i])
            listy = nu.array(listy)
            noise = [m for m in err_g]
            noise.extend([m for m in err_r])
            noise.extend([m for m in err_i])
            noise = nu.array(noise)
            trainSet = trainingSet(listx=listx, listy=listy, noise=noise)
        elif 'gr' in basefilename:
            raise NotImplementedError
            from powerlawSFgr import covarFunc
            params = {
                'logGamma': array([-7.79009776]),
                'logGammagr': array([-28.0487848]),
                'gamma': array([0.45918053]),
                'gammagr': array([0.21333858])
            }
            SF = covarFunc(**params)
            listx = [(t, 'g') for t in mjd_g]
            listx.extend([(t, 'r') for t in mjd_r])
            listy = [m for m in g]
            listy.extend([m for m in r])
            listy = nu.array(listy)
            noise = [m for m in err_g]
            noise.extend([m for m in err_r])
            noise = nu.array(noise)
            trainSet = trainingSet(listx=listx, listy=listy, noise=noise)
        else:
            from gp.powerlawSF import covarFunc
            trainSet = trainingSet(listx=mjd_g, listy=g, noise=err_g)
            params = [0., 0.]
            SF = covarFunc(gamma=.2, A=0.000524)  #Power
            #SF= covarFunc(a=.0001,l=.001) #OU

        #Train
        print "Training ..."
        outcovarFunc = trainGP(trainSet, SF, useDerivs=False)

        print "Best-fit:"
        print outcovarFunc._dict

        print "Sampling ..."
        out = sampleGP(trainSet,
                       outcovarFunc,
                       nsamples=nsamples,
                       step=[0.2 for ii in range(len(params))])

        gammas = nu.array([o._dict['gamma'] for o in out]).reshape(len(out))
        if 'gr' in basefilename:
            gammagrs = nu.array([o._dict['gammagr']
                                 for o in out]).reshape(len(out))
            logGammas = nu.array([o._dict['logGamma']
                                  for o in out]).reshape(len(out))
            logGammagrs = nu.array([o._dict['logGammagr']
                                    for o in out]).reshape(len(out))
        else:
            logAs = nu.array([o._dict['logA'] for o in out]).reshape(len(out))
            print nu.mean(logAs), nu.sqrt(nu.var(logAs))
        print nu.mean(gammas), nu.sqrt(nu.var(gammas))

        #Save
        savefile = open(savefilename, 'wb')
        pickle.dump(gammas, savefile)
        if 'gr' in basefilename:
            pickle.dump(logGammas, savefile)
            pickle.dump(gammagrs, savefile)
            pickle.dump(logGammagrs, savefile)
        else:
            pickle.dump(logAs, savefile)
        pickle.dump(out, savefile)
        savefile.close()

    #if not 'gr' in basefilename:
    #    logGammas= logAs/gammas

    #Plot
    bovy_plot.bovy_print()
    bovy_plot.scatterplot(logAs / 2.,
                          gammas,
                          'k,',
                          ylabel=r'$\gamma$',
                          xlabel=r'\log A',
                          xrange=[-9.21 / 2., 0.],
                          yrange=[0., 1.25],
                          bins=50,
                          onedhists=True)
    bovy_plot.bovy_end_print(basefilename + '_sample_2d.png')
Exemple #18
0
def singleBandSampleExample(datafilename='../data/SDSSJ203817.37+003029.8.fits',
                  band='g',nsamples=1000,
                  basefilename='SDSSJ203817.37+003029.8'):
    """
    NAME:
       singleBandSampleExample
    PURPOSE:
       Sample an example of a power-law structure function GP covariance 
       function to an SDSS quasar
    INPUT:
       datafilename
       band - band to fit
       nsamples - number of samples to use
       basefilename
    OUTPUT:
    HISTORY:
       2010-08-08 - Written - Bovy (NYU)
    """
    nu.random.seed(1)
    #Get data
    file= fu.table_fields(datafilename)
    if band == 'u':
        mjd_g= nu.array(file.mjd_u)/365.25
        g= nu.array(file.u)
        err_g= nu.array(file.err_u)
    elif band == 'g':
        mjd_g= nu.array(file.mjd_g)/365.25
        g= nu.array(file.g)
        err_g= nu.array(file.err_g)
    elif band == 'r':
        mjd_g= nu.array(file.mjd_r)/365.25
        g= nu.array(file.r)
        err_g= nu.array(file.err_r)
    elif band == 'i':
        mjd_g= nu.array(file.mjd_i)/365.25
        g= nu.array(file.i)
        err_g= nu.array(file.err_i)
    elif band == 'z':
        mjd_g= nu.array(file.mjd_z)/365.25
        g= nu.array(file.z)
        err_g= nu.array(file.err_z)
    mjd_r= nu.array(file.mjd_r)/365.25
    r= nu.array(file.r)
    err_r= nu.array(file.err_r)
    mjd_i= nu.array(file.mjd_i)/365.25
    i= nu.array(file.i)
    err_i= nu.array(file.err_i)

    mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)
    g= g[mask]
    g-= nu.mean(g)
    err_g= err_g[mask]
    mjd_g= mjd_g[mask]
    mjd_g-= nu.amin(mjd_g)
    meanErr_g= nu.mean(err_g)

    r= r[mask]
    r-= nu.mean(r)
    err_r= err_r[mask]
    mjd_r= mjd_r[mask]
    mjd_r-= nu.amin(mjd_r)
    meanErr_r= nu.mean(err_r)

    i= i[mask]
    i-= nu.mean(i)
    err_i= err_i[mask]
    mjd_i= mjd_i[mask]
    mjd_i-= nu.amin(mjd_i)
    meanErr_i= nu.mean(err_i)

    savefilename= basefilename+'.sav'
    if os.path.exists(savefilename):
        savefile= open(savefilename,'rb')
        gammas= pickle.load(savefile)
        if 'gr' in basefilename:
            logGammas= pickle.load(savefile)
            gammagrs= pickle.load(savefile)
            logGammagrs= pickle.load(savefile)
        else:
            logAs= pickle.load(savefile)
        out= pickle.load(savefile)
        savefile.close()
    else:
        if 'gri' in basefilename:
            raise NotImplementedError
            from powerlawSFgr import covarFunc
            params= {'logGamma': array([-7.79009776]), 
                     'logGammagr': array([-28.0487848]), 
                     'gamma': array([ 0.45918053]), 
                     'gammagr': array([ 0.21333858])}
            SF= covarFunc(**params)
            listx= [(t,'g') for t in mjd_g]
            listx.extend([(t,'r') for t in mjd_r])
            listx.extend([(t,'i') for t in mjd_i])
            listy= [m for m in g]
            listy.extend([m for m in r])
            listy.extend([m for m in i])
            listy= nu.array(listy)
            noise= [m for m in err_g]
            noise.extend([m for m in err_r])
            noise.extend([m for m in err_i])
            noise= nu.array(noise)
            trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        elif 'gr' in basefilename:
            raise NotImplementedError
            from powerlawSFgr import covarFunc
            params= {'logGamma': array([-7.79009776]), 
                     'logGammagr': array([-28.0487848]), 
                     'gamma': array([ 0.45918053]), 
                     'gammagr': array([ 0.21333858])}
            SF= covarFunc(**params)
            listx= [(t,'g') for t in mjd_g]
            listx.extend([(t,'r') for t in mjd_r])
            listy= [m for m in g]
            listy.extend([m for m in r])
            listy= nu.array(listy)
            noise= [m for m in err_g]
            noise.extend([m for m in err_r])
            noise= nu.array(noise)
            trainSet= trainingSet(listx=listx,listy=listy,noise=noise)
        else:
            from gp.powerlawSF import covarFunc
            trainSet= trainingSet(listx=mjd_g,listy=g,noise=err_g)
            params= [0.,0.]
            SF= covarFunc(gamma=.2,A=0.000524) #Power
            #SF= covarFunc(a=.0001,l=.001) #OU
    
        #Train
        print "Training ..."
        outcovarFunc= trainGP(trainSet,SF,useDerivs=False)
        
        print "Best-fit:"
        print outcovarFunc._dict
        
        print "Sampling ..."
        out= sampleGP(trainSet,outcovarFunc,nsamples=nsamples,
                      step=[0.2 for ii in range(len(params))])
        
        gammas= nu.array([o._dict['gamma'] for o in out]).reshape(len(out))
        if 'gr' in basefilename:
            gammagrs= nu.array([o._dict['gammagr'] for o in out]).reshape(len(out))
            logGammas= nu.array([o._dict['logGamma'] for o in out]).reshape(len(out))
            logGammagrs= nu.array([o._dict['logGammagr'] for o in out]).reshape(len(out))
        else:
            logAs= nu.array([o._dict['logA'] for o in out]).reshape(len(out))
            print nu.mean(logAs),nu.sqrt(nu.var(logAs))
        print nu.mean(gammas),nu.sqrt(nu.var(gammas))
        
        #Save
        savefile= open(savefilename,'wb')
        pickle.dump(gammas,savefile)
        if 'gr' in basefilename:
            pickle.dump(logGammas,savefile)
            pickle.dump(gammagrs,savefile)
            pickle.dump(logGammagrs,savefile)
        else:
            pickle.dump(logAs,savefile)
        pickle.dump(out,savefile)
        savefile.close()

    #if not 'gr' in basefilename:
    #    logGammas= logAs/gammas

    #Plot
    bovy_plot.bovy_print()
    bovy_plot.scatterplot(logAs/2.,gammas,'k,',
                          ylabel=r'$\gamma$',xlabel=r'\log A',
                          xrange=[-9.21/2.,0.],
                          yrange=[0.,1.25],
                          bins=50,onedhists=True)
    bovy_plot.bovy_end_print(basefilename+'_sample_2d.png')