def rm_single(rmid, nwalker, nchain, nburn, min_lag, max_lag, fig_out): # Input and output data position and name file_con = Location.project_loca + "result/light_curve/" + str(rmid) + \ "/cont.txt" file_hbeta = Location.project_loca + "result/light_curve/" + str(rmid) + \ "/Hbetab.txt" lc_plot = Location.project_loca + "result/light_curve/" + str(rmid) + \ "/lightcurve" data_out = Location.project_loca + "result/light_curve/" + str(rmid) + \ "/cont-hbeta.txt" last_mcmc = Location.project_loca + "result/light_curve/" + str(rmid) + \ "/last_mcmc" # Fit continuum c = get_data([file_con]) cmod = Cont_Model(c) cmod.do_mcmc(threads=100, nwalkers=nwalker, nchain=nchain, nburn=nburn) # Do mcmc cy = get_data([file_con, file_hbeta], names=["Continuum", "Hbeta"]) cy.plot(figout=lc_plot, figext="png") cymod = Rmap_Model(cy) cymod.do_mcmc(conthpd=cmod.hpd, threads=100, fchain=data_out, nwalkers=nwalker, nchain=2.0 * nchain, nburn=2.0 * nburn, laglimit=[[min_lag, max_lag]]) # Output mcmc result cymod.show_hist(figout=fig_out, figext="png") cypred = cymod.do_pred() cypred.plot(set_pred=True, obs=cy, figout=last_mcmc, figext="png") return [cymod.hpd[0][2], cymod.hpd[1][2], cymod.hpd[2][2]]
def do_javelin(source_name, source_ID, zspec_driving, jd_driving, flux_driving, errflux_driving, jd_responding, flux_responding, errflux_responding): #Calculate lag with python Javelin program print "########## computing Javelin for source %d ##########" % ( source_ID) #converting lcs into a format accepted by the javelin method aa = str(np.random.randint(100)) lcd_name = 'temp/driving_lc_' + aa + '_' + str(source_ID) + '.txt' np.savetxt( lcd_name, np.transpose([ jd_driving / (1.0 + zspec_driving), flux_driving, errflux_driving ])) lcr_name = 'temp/responding_lc_' + aa + '_' + str(source_ID) + '.txt' np.savetxt( lcr_name, np.transpose([ jd_responding / (1.0 + zspec_driving), flux_responding, errflux_responding ])) #running Javelin cont = get_data([lcd_name], names=[driving_filter]) cmod = Cont_Model(cont) cmod.do_mcmc(nwalkers=100, nburn=50, nchain=100, fchain="javelin_stat/chain_cont_" + source_name + ".txt") bothdata = get_data([lcd_name, lcr_name], names=[driving_filter, responding_filter]) mod_2band = Pmap_Model(bothdata) #Rmap_Model(bothdata) mod_2band.do_mcmc(nwalkers=100, nburn=50, nchain=100, conthpd=cmod.hpd, laglimit=[[cent_lowlimit, cent_uplimit]], fchain="javelin_stat/jav_chain_all_" + source_name + ".txt") sigma, tau, lag, width, scale = np.loadtxt("javelin_stat/jav_chain_all_" + source_name + ".txt", unpack=True, usecols=[0, 1, 2, 3, 4]) centau_median = np.median(lag) centau_uperr = (stats.scoreatpercentile(lag, perclim)) - centau_median centau_loerr = centau_median - (stats.scoreatpercentile( lag, (100. - perclim))) len_chain = len(lag[np.where(lag > -2000000000000)]) return (source_ID, zspec_driving, len_chain, centau_median, centau_loerr, centau_uperr)
def obtainlag(file_con, file_line): # Load and fit continuum data c = get_data([file_con]) cmod = Cont_Model(c) cmod.do_mcmc(threads=4) # Load and fit continuum and line data cy = get_data([file_con, file_line]) cymod = Rmap_Model(cy) # Get time lag data_out = "cont-" + file_line.split(".")[0] + ".txt" cymod.do_mcmc(conthpd=cmod.hpd, threads=4, fchain=data_out)
def javelin_fit(ID): # 对ugriz五个band分别跑javelin连续谱模型拟合 # 得到五个拟合结果 band_pho = ['u', 'g', 'r', 'i', 'z'] tau_pho = [] # in ugriz order sigma_pho = [] # in ugriz order for band in band_pho: filename = "./data/light_curve/" + band + \ "_LC_" + ID + ".dat" # JAVELIN c = get_data([filename]) cmod = Cont_Model(c) cmod.do_mcmc(threads=4) # 这里只记录了med的结果: # tau_mc_low = exp(cmod.hpd[0,1]) tau_mc_med = exp(cmod.hpd[1, 1]) # tau_mc_hig = exp(cmod.hpd[2,1]) # sigma_mc_low = exp(cmod.hpd[0,0]) sigma_mc_med = exp(cmod.hpd[1, 0]) # sigma_mc_hig = exp(cmod.hpd[2,0]) tau_pho.append(tau_mc_med) sigma_pho.append(sigma_mc_med) # 返回五个tau,sigma # 顺序是urgriz return tau_pho, sigma_pho
def fitLag(linfile, linfchain, conthpd, names=None, lagrange=[50, 300], lagbinsize=1, threads=1, set_plot=False, nwalkers=100, nburn=50, nchain=50, mode="test") : """ fit the Rmap model. """ if mode == "run" : linfile = ".".join([linfile, "myrun"]) zydata = get_data(linfile, names=names) rmap = Rmap_Model(zydata) if mode == "test" : if zydata.nlc == 2 : print(rmap([np.log(2.), np.log(100), lagy, widy, scaley ])) elif zydata.nlc == 3 : print(rmap([np.log(2.), np.log(100), lagy, widy, scaley, lagz, widz, scalez])) return(None) elif mode == "show" : rmap.load_chain(linfchain, set_verbose=False) elif mode == "run" : laglimit = [[0.0, 400.0],]*(zydata.nlc-1) print(laglimit) # laglimit = "baseline" linfchain = ".".join([linfchain, "myrun"]) rmap.do_mcmc(conthpd=conthpd, lagtobaseline=0.5, laglimit=laglimit, nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=linfchain, threads=threads) if set_plot : rmap.break_chain([lagrange,]*(zydata.nlc-1)) rmap.get_hpd() if zydata.nlc == 2 : figout = "mcmc1" else : figout = "mcmc2" rmap.show_hist(bins=100, lagbinsize=lagbinsize, figout=figout, figext=figext) return(rmap.hpd)
def fitDPmap(dphfile, dphfchain, conthpd, names=None, lagrange=[-50, 300], lagbinsize=1, threads=1, set_plot=False, nwalkers=100, nburn=100, nchain=100, mode="test", fixed=None, p_fix=None): """ fit the DPmap model. """ if mode == "run": dphfile = ".".join([dphfile, "myrun"]) zydata = get_data(dphfile, names=names) dpmap = DPmap_Model(zydata) if mode == "test": print( dpmap([ np.log(sigma), np.log(tau), lagz, widz, scalez, lagy, widy, scaley ])) return (None) elif mode == "show": dpmap.load_chain(dphfchain, set_verbose=False) elif mode == "run": laglimit = [lagrange, lagrange] # laglimit = [lagrange,[-50, 300]] # FIXME temperary fix widlimit = [[0, 7.0], [0, 7.0]] # XXX here we want to avoid long smoothing width dphfchain = ".".join([dphfchain, "myrun"]) dpmap.do_mcmc(conthpd=conthpd, lagtobaseline=0.5, laglimit=laglimit, widlimit=widlimit, nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=dphfchain, threads=threads, fixed=fixed, p_fix=p_fix) if set_plot: # dpmap.break_chain([lagrange,[-50, 300]]) dpmap.break_chain([lagrange, lagrange]) dpmaphpd = dpmap.get_hpd() dpmap.show_hist(bins=100, lagbinsize=lagbinsize, figout="mcmc4", figext=figext) # if True: # zypred = dpmap.do_pred(dpmap.hpd[1,:]) # zypred.names = names # zypred.plot(set_pred=True, obs=zydata, figout="prediction", figext=figext) return (dpmap.hpd)
def fitLag(linfile, linfchain, conthpd, names=None, lagrange=[50, 300], lagbinsize=1, threads=1, set_plot=False, nwalkers=100, nburn=100, nchain=100, mode="test"): """ fit the Rmap model. """ if mode == "run": linfile = ".".join([linfile, "myrun"]) zydata = get_data(linfile, names=names) rmap = Rmap_Model(zydata) if mode == "test": if zydata.nlc == 2: print(rmap([np.log(sigma), np.log(tau), lagy, widy, scaley])) elif zydata.nlc == 3: print( rmap([ np.log(sigma), np.log(tau), lagy, widy, scaley, lagz, widz, scalez ])) return (None) elif mode == "show": rmap.load_chain(linfchain, set_verbose=False) elif mode == "run": laglimit = [ [0.0, tau], ] * (zydata.nlc - 1) print(laglimit) # laglimit = "baseline" linfchain = ".".join([linfchain, "myrun"]) rmap.do_mcmc(conthpd=conthpd, lagtobaseline=0.5, laglimit=laglimit, nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=linfchain, threads=threads) if set_plot: rmap.break_chain([ lagrange, ] * (zydata.nlc - 1)) rmap.get_hpd() if zydata.nlc == 2: figout = "mcmc1" else: figout = "mcmc2" rmap.show_hist(bins=100, lagbinsize=lagbinsize, figout=figout, figext=figext) return (rmap.hpd)
def fitCon(confile, confchain, names=None, threads=1, set_plot=False, nwalkers=100, nburn=100, nchain=100, mode="test"): """ fit the continuum model. """ if mode == "run": confile = ".".join([confile, "myrun"]) zydata = get_data(confile, names=names) cont = Cont_Model(zydata, "drw") if mode == "test": print(cont([np.log(sigma), np.log(tau)], set_retq=True)) return (None) elif mode == "show": cont.load_chain(confchain) elif mode == "run": confchain = ".".join([confchain, "myrun"]) cont.do_mcmc(nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=confchain, threads=1) if set_plot: cont.show_hist(bins=100, figout="mcmc0", figext=figext) return (cont.hpd)
def showfit(linhpd, linfile, names=None, set_plot=False, mode="test", model='Rmap'): if mode == "run": linfile = ".".join([linfile, "myrun"]) print linfile zydata = get_data(linfile, names=names) if model == 'Rmap' or model == 'Rmap2': rmap = Rmap_Model(zydata) elif model == 'Pmap': rmap = Pmap_Model(zydata) elif model == 'DPmap': rmap = DPmap_Model(zydata) if mode == "test": return (None) else: zypred = rmap.do_pred(linhpd[1, :]) zypred.names = names print names if set_plot: zypred.plot(set_pred=True, obs=zydata, figout="prediction_" + model, figext=figext)
def computeLag(self, string, dirName, fileName, results, i): # Creating light curve using the LightCurve class LC = LightCurve.LightCurve(self.length) # Initialising light curve LC.LCdir = dirName LC.genContMag(1.0, self.mag, self.tau, self.sf, i) # Preparing necessary qualities LC.genCont() # Creating continuum light curve LC.genLineTH(self.tLag) # Creating emission line light curve LC.printFunction(LC.length, LC.t, LC.cont, LC.line, string, i) # Printing light curves to file # Converting generated light curves into files accepted by Javelin self.convertToJavelin(string, dirName, i) print 'Finished creating LC, over to the Javelin part of the code \n' # Finding the continuum and emission line files for Javelin to use contFile = dirName + string + "Cont" + str(i) + ".dat" emissionFile = dirName + string + "Emission" + str(i) + ".dat" chainFile = dirName + "Chains/MyChain" + string + str(i) + ".dat" logPFile = dirName + "Chains/LogP" + string + str(i) + ".dat" # Fitting continuum data according to Javelin description on their websites c = get_data([contFile], names=["Continuum"]) cmod = Cont_Model(c) cmod.do_mcmc(nwalkers=100) # Fitting the continuum and emission line data according to Javelin description # Rmap_Model is the spectroscopic reverberation mapping model if not os.path.exists(dirName+"Chains"): # Check whether directory exists os.makedirs(dirName+"Chains") # End if-statement cy = get_data([contFile, emissionFile], names=["Continuum","EmissionLine"]) cymod = Rmap_Model(cy) cymod.do_mcmc(conthpd=cmod.hpd, nwalkers=100, nburn=500, nchain=1000, laglimit=[[self.lLag,self.uLag]], lagtobaseline=1.0, fchain=chainFile, flogp=logPFile) # Extracting results from Javelin cymod.get_hpd() # Obtain Highest Posterior Density (HPD) intervals savedHPD = cymod.hpd # 3x2 array w/18%, 50% and 84% values fro log sigma and log tau lagVals = savedHPD[:,2] # Extracting the estimated value of the lag if (results != '0'): # Printing results from run to file results[i,:] = np.array([self.tLag, lagVals[1], lagVals[0], lagVals[2]])[np.newaxis] np.savetxt(fileName, results[results[:, 0] > 0])
def fitPmap(phofile, phofchain, conthpd, names=None, lagrange=[50, 300], lagbinsize=1, threads=1, set_plot=False, nwalkers=100, nburn=100, nchain=100, mode="test", fixed=None, p_fix=None): """ fit the Pmap model. """ if mode == "run": phofile = ".".join([phofile, "myrun"]) zydata = get_data(phofile, names=names) pmap = Pmap_Model(zydata) if mode == "test": print(pmap([np.log(sigma), np.log(tau), lagy, widy, scaley, 1.0])) return (None) elif mode == "show": pmap.load_chain(phofchain, set_verbose=False) elif mode == "run": # laglimit = [[50.0, 130.0]] # XXX here we want to avoid 180 day limit. laglimit = [ lagrange, ] # XXX here we want to avoid 180 day limit. widlimit = [[0, 7.0]] # XXX here we want to avoid long smoothing width phofchain = ".".join([phofchain, "myrun"]) pmap.do_mcmc(conthpd=conthpd, lagtobaseline=0.5, laglimit=laglimit, widlimit=widlimit, nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=phofchain, threads=threads, fixed=fixed, p_fix=p_fix) if set_plot: pmap.break_chain([ lagrange, ]) pmap.get_hpd() pmap.show_hist(bins=100, lagbinsize=lagbinsize, figout="mcmc3", figext=figext) return (pmap.hpd)
def showfit(linhpd, linfile, names=None, set_plot=False, mode="test") : if mode == "run" : linfile = ".".join([linfile, "myrun"]) print (linfile) zydata = get_data(linfile, names=names) rmap = Rmap_Model(zydata) if mode == "test" : return(None) else : zypred = rmap.do_pred(linhpd[1,:]) zypred.names = names if set_plot : zypred.plot(set_pred=True, obs=zydata, figout="prediction", figext=figext)
def showfit(linhpd, linfile, names=None, set_plot=False, mode="test") : if mode == "run" : linfile = ".".join([linfile, "myrun"]) print linfile zydata = get_data(linfile, names=names) rmap = Rmap_Model(zydata) if mode == "test" : return(None) else : zypred = rmap.do_pred(linhpd[1,:]) zypred.names = names if set_plot : zypred.plot(set_pred=True, obs=zydata, figout="prediction", figext=figext)
def test_dpmap(): tag = '.myrun' # tag = '.short' # tag = '.shorter' phofile = "dat/loopdeloop_con_yb.dat" + tag # dphfile = "dat/loopdeloop_con_yzb.dat" + tag zydata = get_data(phofile) # zydata = get_data(dphfile) # zydata.plot(marker="None", ms=1.0, ls="-", lw=2, figout="test", figext='pdf') dpmap = DPmap_Model(zydata) # print(dpmap([np.log(3.), np.log(400), 100, 2, 0.5, 0, 0, 1])) # print(dpmap([np.log(3.), np.log(400), 100, 2, 0.5, 250, 0, 0.5])) # print(dpmap([np.log(3.), np.log(400), 250, 0, 0.5, 100, 2, 0.5])) print(dpmap([np.log(3.), np.log(400), 250, 2, 0.5, 100, 2, 0.5]))
def getTrue(trufile, set_plot=False, mode="test", covfunc="drw"): """ Generating dense, error-free light curves as the input signal. Parameters --- trufile: str filename for reading/storing the light curve file. set_plot: bool Draw the light curves if True. mode: str mode of this example script: "test" is for doing nothing but to show the modules are correctly loaded; "run" is to run a full test by regenerating all the data files and results of this demo; and "show" is to load all the precalculated results and plot them. covfunc: str the covariance function for the underlying variability model. For this demo we have either the default "drw" model or the "kepler2_exp" model which mimics the short time scale cutoff seen in Kepler. """ if mode == "test": return (None) elif mode == "show": print("read true light curve signal from %s" % trufile) zydata = get_data(trufile, names=names) elif mode == "run": print("generate true light curve signal") if covfunc == "drw": print("generating DRW light curves...") # this is the fast way zydata = generateTrueLC2(covfunc="drw") else: print("generating Kepler light curves...") # slower zydata = generateTrueLC(covfunc="kepler2_exp") print("save true light curve signal to %s" % trufile) trufile = ".".join([trufile, "myrun"]) zydata.save(trufile) if set_plot: print("plot true light curve signal") zydata.plot(marker="None", ms=1.0, ls="-", lw=2, figout="signal", figext=figext) return (zydata)
def fitCon(confile, confchain, names=None, threads=1, set_plot=False, nwalkers=100, nburn=50, nchain=50, mode="test") : """ fit the continuum model. """ if mode == "run" : confile = ".".join([confile, "myrun"]) zydata = get_data(confile, names=names) cont = Cont_Model(zydata, "drw") if mode == "test" : print(cont([np.log(2.), np.log(100)], set_retq=True)) return(None) elif mode == "show" : cont.load_chain(confchain) elif mode == "run" : confchain = ".".join([confchain, "myrun"]) cont.do_mcmc(nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=confchain, threads=1) if set_plot : cont.show_hist(bins=100, figout="mcmc0", figext=figext) return(cont.hpd)
def getTrue(trufile, set_plot=False, mode="test"): """ Generating dense, error-free light curves as the input signal. """ if mode == "test" : return(None) elif mode == "show" : print("read true light curve signal from %s"%trufile) zydata = get_data(trufile, names=names) elif mode == "run" : print("generate true light curve signal") # zydata = generateTrueLC(covfunc="drw") # this is the fast way zydata = generateTrueLC2(covfunc="drw") print("save true light curve signal to %s"%trufile) trufile = ".".join([trufile, "myrun"]) zydata.save(trufile) if set_plot : print("plot true light curve signal") zydata.plot(marker="None", ms=1.0, ls="-", lw=2, figout="signal", figext=figext) return(zydata)
def fitPmap(phofile, phofchain, conthpd, names=None, lagrange=[50, 300], lagbinsize=1, threads=1, set_plot=False, nwalkers=100, nburn=50, nchain=50,mode="test") : """ fit the Pmap model. """ if mode == "run" : phofile = ".".join([phofile, "myrun"]) zydata = get_data(phofile, names=names) pmap = Pmap_Model(zydata) if mode == "test" : print(pmap([np.log(2.), np.log(100), lagy, widy, scaley, 1.0])) return(None) elif mode == "show" : pmap.load_chain(phofchain, set_verbose=False) elif mode == "run" : laglimit = [[50.0, 130.0]] # XXX here we want to avoid 180 day limit. phofchain = ".".join([phofchain, "myrun"]) pmap.do_mcmc(conthpd=conthpd, lagtobaseline=0.5, laglimit=laglimit, nwalkers=nwalkers, nburn=nburn, nchain=nchain, fburn=None, fchain=phofchain, threads=threads) if set_plot : pmap.break_chain([lagrange,]) pmap.get_hpd() pmap.show_hist(bins=100, lagbinsize=lagbinsize, figout="mcmc3", figext=figext) return(pmap.hpd)
# 02 Sep 2021 01:56:41 from javelin.lcmodel import Cont_Model, DPmap_Model from javelin.zylc import get_data import os.path data0 = get_data(['Mrk509Gband.txt'], names=[ "G-band", ]) data0.plot(figout='lc_g', figext='pdf') cont = Cont_Model(data0, "drw") if os.path.isfile("cont_chain.dat"): print("cont model chain exists") cont.load_chain('cont_chain.dat') cont.show_hist(figout='mcmc_con', bins=100, figext='pdf') else: cont.do_mcmc(nwalkers=100, nburn=100, nchain=100, fburn=None, fchain="cont_chain.dat", threads=8) conthpd = cont.hpd print conthpd data1 = get_data(["Mrk509Gband_short.txt", "Mrk509Hband_normalized.txt"], names=["G-band", "H-band"]) data1.plot(figout='lc_gh', figext='pdf') # quit() dust = DPmap_Model(data1)
#running the javelin code in easy mode from javelin.zylc import get_data from javelin.lcmodel import Cont_Model, Pmap_Model, Rmap_Model c = get_data(["continuum.txt"]) cm = Cont_Model(c) cm.do_mcmc(nwalkers=1000, nburn=200, nchain=100) cy = get_data(["continuum.txt", "hb.txt"]) cym = Pmap_Model(cy) cym.do_mcmc(conthpd=cm.hpd, nwalkers=1000, nburn=200, nchain=100) cym.show_hist() cym.get_hpd() cymhpd = cym.hpd par_best = cymhpd[1, :] cym_best = cym.do_pred(par_best) cym_best.plot(set_pred=True, obs=cy)
def do_javelin(ra, dec): #ra and dec are converted to 6 decimals format ras = "%.6f" % ra decs = "%.6f" % dec print "########## computing Javelin for source located at ra=%f and dec=%f ##########" % ( ra, dec) try: #the diver lc is loaded, we take into account the different formats used for opt and NIR data. if driving_filter != 'Q': agn_driving = lc_path + driving_filter + '/agn_' + str( ras) + '_' + str(decs) + '_' + driving_filter + '.fits' arch_driving = pf.open(agn_driving) jd_0 = 55000 head_driving = arch_driving[0].header datos_driving = arch_driving[1].data jd_driving = datos_driving['JD'] - jd_0 flux_driving = datos_driving[ 'FLUX_2'] * 1e27 #the flux value is multiplicated by 1e27 to avoid numerical errors produced by small numbers errflux_driving = datos_driving['FLUXERR_2'] * 1e27 zspec_driving = head_driving['REDSHIFT'] else: try: agn_driving = lc_path + driving_filter + '/bin3_onechip_' + str( ras) + '_' + str(decs) + '_' + field + '.fits' arch_driving = pf.open(agn_driving) except: agn_driving = lc_path + driving_filter + '/bin3_morechip_' + str( ras) + '_' + str(decs) + '_' + field + '.fits' arch_driving = pf.open(agn_driving) jd_0 = 2455000 head_driving = arch_driving[0].header datos_driving = arch_driving[1].data jd_driving = datos_driving['JD'] - jd_0 flux_driving = datos_driving[ 'fluxQ'] * 1e27 #the flux value is multiplicated by 1e27 to avoid numerical errors produced by small numbers errflux_driving = datos_driving['errfluxQ'] * 1e27 zspec_driving = head_driving['REDSHIFT'] lcd_name = 'temp/driving_lc_' + driving_filter + '_' + str( ras) + '_' + str(decs) + '.txt' np.savetxt( lcd_name, np.transpose([ jd_driving / (1.0 + zspec_driving), flux_driving, errflux_driving ])) try: #reading the responding filter data if responding_filter != 'Q': agn_responding = lc_path + responding_filter + '/agn_' + str( ras) + '_' + str(decs) + '_' + responding_filter + '.fits' arch_responding = pf.open(agn_responding) jd_0 = 55000 head_responding = arch_responding[0].header datos_responding = arch_responding[1].data jd_responding = datos_responding['JD'] - jd_0 flux_responding = datos_responding[ 'FLUX_2'] * 1e27 #the flux value is multiplicated by 1e27 to avoid numerical errors produced by small numbers errflux_responding = datos_responding['FLUXERR_2'] * 1e27 zspec_responding = head_responding['REDSHIFT'] else: try: agn_driving = lc_path + responding_filter + '/bin3_onechip_' + str( ras) + '_' + field + '.fits' arch_responding = pf.open(agn_responding) except: agn_driving = lc_path + responding_filter + '/bin3_morechip_' + str( ras) + '_' + field + '.fits' arch_responding = pf.open(agn_responding) jd_0 = 2455000 head_responding = arch_responding[0].header datos_responding = arch_responding[1].data jd_responding = datos_responding['JD'] - jd_0 flux_responding = datos_responding[ 'fluxQ'] * 1e27 #the flux value is multiplicated by 1e27 to avoid numerical errors produced by small numbers errflux_responding = datos_responding['errfluxQ'] * 1e27 zspec_responding = head_responding['REDSHIFT'] #converting lcs into a format accepted by the fortran method lcr_name = 'temp/responding_lc_' + responding_filter + '_' + str( ras) + '_' + str(decs) + '.txt' np.savetxt( lcr_name, np.transpose([ jd_responding / (1.0 + zspec_driving), flux_responding, errflux_responding ])) #running Javelin cont = get_data([lcd_name], names=[driving_filter]) cmod = Cont_Model(cont) cmod.do_mcmc(nwalkers=100, nburn=50, nchain=100, fchain=jav_stat_path + "chain_cont_" + driving_filter + "_vs_" + responding_filter + "_" + str(ras) + "_" + str(decs) + ".txt") bothdata = get_data([lcd_name, lcr_name], names=[driving_filter, responding_filter]) mod_2band = Pmap_Model(bothdata) #Rmap_Model(bothdata) mod_2band.do_mcmc(nwalkers=100, nburn=50, nchain=100, conthpd=cmod.hpd, laglimit=[[cent_lowlimit, cent_uplimit]], widlimit=widlimit, fchain=jav_stat_path + "jav_chain_all_" + driving_filter + "_vs_" + responding_filter + "_" + str(ras) + "_" + str(decs) + ".txt") sigma, tau, lag, width, scale = np.loadtxt( jav_stat_path + "jav_chain_all_" + driving_filter + "_vs_" + responding_filter + "_" + str(ras) + "_" + str(decs) + ".txt", unpack=True, usecols=[0, 1, 2, 3, 4]) centau_median = np.median(lag) centau_uperr = (stats.scoreatpercentile(lag, perclim)) - centau_median centau_loerr = centau_median - (stats.scoreatpercentile( lag, (100. - perclim))) len_chain = len(lag[np.where(lag > -2000000000000)]) return (ra, dec, zspec_driving, len_chain, centau_median, centau_loerr, centau_uperr) except: print "########## computing iccf FAILS for source located at ra=%f and dec=%f, NO RESPONDING LC available ##########" % ( ra, dec) cmd = 'rm ' + lcd_name os.system(cmd) return (ra, dec, -9999, -9999, -9999, -9999, -9999) except: print "########## computing iccf FAILS for source located at ra=%f and dec=%f, NO DRIVING LC available ##########" % ( ra, dec) return (ra, dec, -9999, -9999, -9999, -9999, -9999)
# 22 Nov 2017 17:14:12 from javelin.lcmodel import Cont_Model, Disk_Model from javelin.zylc import get_data import os.path data0 = get_data(["driver.dat",], names=["Driver",]) data0.plot() cont = Cont_Model(data0, "drw") if os.path.isfile("driver_chain.dat"): print ("cont model chain exists") cont.load_chain('driver_chain.dat') cont.show_hist(bins=100) else: cont.do_mcmc(nwalkers=100, nburn=100, nchain=200, fburn=None, fchain="driver_chain.dat", threads=1) conthpd = cont.hpd print conthpd data1 = get_data(["driver.dat", "wave2.dat", "wave3.dat", "wave4.dat"], names=["Driver", "Wave 2", "Wave 3", "Wave 4"]) data1.plot() disk1 = Disk_Model(data1, effwave=[2000., 4000., 5000., 8000.]) if os.path.isfile("thin_disk_chain.dat"): print ("disk model chain exists") disk1.load_chain('thin_disk_chain.dat') disk1.show_hist() else: disk1.do_mcmc(conthpd=conthpd, nwalkers=100, nburn=100, nchain=500, threads=1, fchain="thin_disk_chain.dat", flogp="thin_disk_flogp.dat", fburn="thin_disk_burn.dat")
from javelin.zylc import get_data from javelin.lcmodel import Cont_Model from javelin.lcmodel import Rmap_Model # for i in xrange(50): for i in xrange(1): c = get_data(["dat/continuum.dat"]) cmod = Cont_Model(c) cmod.do_mcmc(nwalkers=200, nburn=100, nchain=200, threads=1) a = cmod.get_hpd() javdata4 = get_data(["dat/continuum.dat", "dat/yelm.dat", "dat/zing.dat"], names=["Continuum", "Yelm", "Zing"]) rmap2 = Rmap_Model(javdata4) rmap2.do_mcmc(conthpd=a, nwalkers=200, nburn=100, nchain=200, threads=1) del rmap2 del c print i
# JAVELINE # import matplotlib.pyplot as plt from javelin.zylc import get_data from javelin.lcmodel import Cont_Model, Rmap_Model javdata_con = get_data([ "../data/con_test.txt", ], names=[ "con", ]) javdata_rm = get_data(["../data/con_test.txt", "../data/line_test.txt"], names=["con", "line"]) cont = Cont_Model(javdata_con) #cont.do_mcmc(fchain="mychain0.dat",nwalkers=100, nburn=200, nchain=500) cont.load_chain("mychain0.dat") cont.show_hist(bins=100) cont.get_hpd() conthpd = cont.hpd print(conthpd[1, :]) cont_best = cont.do_pred(conthpd[1, :]) x = cont_best.jlist[:] y = cont_best.mlist[:] + cont_best.blist[:] ey = cont_best.elist[:] plt.plot(x[0], y[0] + javdata_con.cont_mean) plt.fill_between(x[0], y[0] - ey[0] + javdata_con.cont_mean, y[0] + ey[0] + javdata_con.cont_mean, where=None, color='grey')
from javelin.zylc import get_data from javelin.lcmodel import Cont_Model, Rmap_Model, Pmap_Model cy = get_data(["/home/zhanghaowen/r.txt", "/home/zhanghaowen/g.txt"]) cymod = Pmap_Model(GPmodel="DRW", zydata=cy) cymod.do_mcmc() cymod.show_hist()
Runs javelin on SDSS Quasars, a selected band (filenames start as u_ , g_, etc.) from QSO_SDSS_JAV """ import numpy as np from javelin.zylc import get_data from javelin.lcmodel import Cont_Model dir_choice=['QSO_try2/', 'QSO_S82/', 'QSO_SDSS_JAV/','QSO_SDSS_chains/'] dir_input=dir_choice[2] dir_output=dir_choice[3] names=np.loadtxt(dir_input+'u_band.ls',dtype=str) for i in range(2): # len(names) filename=dir_input+names[i] print '\nWorking file', filename, i+1, ' out of ', len(names) data = get_data(filename,names=["Continuum"]) cont=Cont_Model(data) start=len(dir_input) chain_name = dir_output+'ch_'+filename[start:]+'_chain.dat' print chain_name cont.do_mcmc(fchain=chain_name)
def javelin_modeling(time, cflux, lflux, suff='', nburn=1000, nchain=500, mods=None): """Do lag calculations with Javelin Parameters: time: [n] array of time axis cflux: [2,n] continuum flux and error lflux: [2,n] line flux and error suff: suffix for printing mods: javelin models if already calculated [cont_mod, cont_line_mod]. In that case, this function just does the plotting. """ if mods is None: # write light curves to file so javelin can read them # irand = np.random.randint(100000) text = '\n'.join(['{} {} {}'.format(*x) for x in zip(time, cflux[0], cflux[1])]) with open('tmp_c%d.dat'%irand, 'w') as fp: fp.write(text) text = '\n'.join(['{} {} {}'.format(*x) for x in zip(time, lflux[0], lflux[1])]) with open('tmp_l%d.dat'%irand, 'w') as fp: fp.write(text) # continuum first # cont_lc = get_data(['tmp_c%d.dat'%irand]) cont_mod = Cont_Model(cont_lc) cont_mod.do_mcmc(set_verbose=False) #cont_mod.show_hist();return 0,cont_mod,0 # continuum and line # cont_line_lc = get_data(['tmp_c%d.dat'%irand, 'tmp_l%d.dat'%irand]) cont_line_mod = Rmap_Model(cont_line_lc) llimit = [-100, 100] pmap,_ = cont_line_mod.do_map([-0.6, 1.5, 1.0, 0.1, .2], fixed=[1,1,1,1,1], set_verbose=False) cont_line_mod.do_mcmc(conthpd=cont_mod.hpd, laglimit=[llimit], nburn=nburn, nchain=nchain, #fixed=[1,1,1,1,1], p_fix=pmap, threads=20,set_verbose=False) threads=30,set_verbose=False) os.system('rm tmp_c%d.dat tmp_l%d.dat'%(irand, irand)) else: cont_mod, cont_line_mod = mods # plot the result of javelin fit # chains = cont_line_mod.flatchain lag_javelin = plt.histogram(chains[:,2], 400, density=1) bins_cent = (lag_javelin[1][1:] + lag_javelin[1][:-1])/2 bins_err = (lag_javelin[1][1:] - lag_javelin[1][:-1])/2 percentile = lambda l: '[{:.4}, {:.4}]'.format( *np.percentile(chains[:,2], [(100-l)/2, l+(100-l)/2])) text = '# percentiles: 68, 90, 99: {}, {}, {}'.format(*[percentile(x) for x in [68, 90, 99]]) text += '\n# mean lag: {:.4} +{:.4} {:.4}'.format( np.percentile(chains[:,2],50), np.percentile(chains[:,2],68+16)-np.percentile(chains[:,2],50), np.percentile(chains[:,2],16)-np.percentile(chains[:,2],50)) print(text) text += '\ndescriptor lag_javelin{0},+- lag_javelin_prob{0}\n'.format(suff) text += '\n'.join(['{:.4} {:.4} {:.4}'.format(*x) for x in zip(bins_cent, bins_err, lag_javelin[0])]) return text, cont_mod, cont_line_mod, lag_javelin