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 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 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 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 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 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])
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
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")
END OF CODE """ """ How I check where to resume running it, from the file where it crashed: crashed_file = i_2104983.txt np.where(names == 'i_2104983.txt') names[3162] names[3161] : the file that was fine, just before the one where it crashed np.where(names_upd == 'i_2104791.txt') --> 3119 --> start at location 3119 """ # # for i in range(len(names_upd)): # filename=dir_input+names_upd[i] print '\nWorking file', filename, i+1, ' out of ', len(names_upd) , ' from ', band 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(set_prior=False, fchain=chain_name) print '\nJavelin performed', i+1, 'fittings, and it should have done ', len(names_upd)
""" No need to do any restrictions, since I know that all lc's are longer then 10 pts """ def bench(secs): print 'Time elapsed: ' + str(secs) + 's' time_lasted = np.empty(0,dtype=float) for i in range(187,len(names)): start_time = clock() filename=dir_input+names[i] print '\nWorking file', filename, i+1, ' out of ', len(names), ' from ', pre 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, 'WITH JAVELIN PRIOR' cont.do_mcmc(set_prior=True,fchain=chain_name) # end_time = clock() delta_t = end_time-start_time bench(delta_t) time_lasted = np.append(time_lasted, delta_t) print '\nJavelin performed', i+1, 'fittings, and it should have done ', len(names) cols = np.column_stack((LC_length,time_lasted)) out=dir_output + 'lc_length_vs_fitting_time_'+pre+'.txt' np.savetxt(out,cols,fmt='%s')
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) if os.path.isfile("dpmap_chain.dat"): print("double echo model chain exists") dust.load_chain('dpmap_chain.dat') dust.show_hist(figout='mcmc_all', figext='pdf')
#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)
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