Example #1
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')
Example #2
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')
def singleBandSampleExample(
    datafilename="SDSSJ000051.56+001202.5.fit", 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, 0.0]
            SF = covarFunc(gamma=0.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.0,
        gammas,
        "k,",
        ylabel=r"$\gamma$",
        xlabel=r"\log A",
        xrange=[-9.21 / 2.0, 0.0],
        yrange=[0.0, 1.25],
        bins=50,
        onedhists=True,
    )
    bovy_plot.bovy_end_print(basefilename + "_sample_2d.png")
Example #4
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')
Example #5
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')
Example #6
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')