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 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 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 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 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
from javelin.zylc import get_data from javelin.lcmodel import Cont_Model from javelin.lcmodel import Rmap_Model import numpy as np c = get_data(["dat/continuum.dat"]) cmod = Cont_Model(c) cmod.do_mcmc(nwalkers=100, nburn=100, nchain=100, threads=1) cmod.show_hist() print cmod.hpd javdata4 = get_data(["dat/continuum.dat", "dat/yelm.dat"], names=["Continuum", "Yelm"]) rmap2 = Rmap_Model(javdata4) rmap2.do_mcmc(conthpd=cmod.hpd, nwalkers=100, nburn=100, nchain=100, threads=1, laglimit=[ [10, 200], ], fixed=[1, 0, 1, 0, 1], p_fix=[np.log(0.1), np.log(400), 10, 2.0, 5.0]) rmap2.show_hist() print rmap2.hpd
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 1 data if responding_filter1 != 'Q': agn_responding = lc_path + responding_filter1 + '/agn_' + str( ras) + '_' + str(decs) + '_' + responding_filter1 + '.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_filter1 + '/bin3_onechip_' + str( ras) + '_' + field + '.fits' arch_responding = pf.open(agn_responding) except: agn_driving = lc_path + responding_filter1 + '/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 lcr1_name = 'temp/responding_lc_' + responding_filter1 + '_' + str( ras) + '_' + str(decs) + '.txt' np.savetxt( lcr1_name, np.transpose([ jd_responding / (1.0 + zspec_driving), flux_responding, errflux_responding ])) #reading the responding filter 2 data if responding_filter2 != 'Q': agn_responding = lc_path + responding_filter2 + '/agn_' + str( ras) + '_' + str(decs) + '_' + responding_filter2 + '.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_filter2 + '/bin3_onechip_' + str( ras) + '_' + field + '.fits' arch_responding = pf.open(agn_responding) except: agn_driving = lc_path + responding_filter2 + '/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 lcr2_name = 'temp/responding_lc_' + responding_filter2 + '_' + str( ras) + '_' + str(decs) + '.txt' np.savetxt( lcr2_name, np.transpose([ jd_responding / (1.0 + zspec_driving), flux_responding, errflux_responding ])) #reading the responding filter 3 data if responding_filter3 != 'Q': agn_responding = lc_path + responding_filter3 + '/agn_' + str( ras) + '_' + str(decs) + '_' + responding_filter3 + '.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_filter3 + '/bin3_onechip_' + str( ras) + '_' + field + '.fits' arch_responding = pf.open(agn_responding) except: agn_driving = lc_path + responding_filter3 + '/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 lcr3_name = 'temp/responding_lc_' + responding_filter3 + '_' + str( ras) + '_' + str(decs) + '.txt' np.savetxt( lcr2_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=100, nchain=200, fchain=jav_stat_path + "chain_cont_" + driving_filter + "_vs_" + responding_filter1 + "_" + responding_filter2 + "_and_" + responding_filter3 + "_" + str(ras) + "_" + str(decs) + ".txt") bothdata = get_data([lcd_name, lcr1_name, lcr2_name, lcr2_name], names=[ driving_filter, responding_filter1, responding_filter2, responding_filter3 ]) laglimit = [[cent_lowlimit, cent_uplimit], [cent_lowlimit, cent_uplimit], [cent_lowlimit, cent_uplimit]] mod_2band = Rmap_Model(bothdata) #Rmap_Model(bothdata) mod_2band.do_mcmc(nwalkers=100, nburn=100, nchain=200, conthpd=cmod.hpd, laglimit=laglimit, fchain=jav_stat_path + "jav_chain_all_" + driving_filter + "_vs_" + responding_filter1 + "_" + responding_filter2 + "_and_" + responding_filter3 + "_" + str(ras) + "_" + str(decs) + ".txt") sigma, tau, lag1, width1, scale1, lag2, width2, scale2, lag3, width3, scale3 = np.loadtxt( jav_stat_path + "jav_chain_all_" + driving_filter + "_vs_" + responding_filter1 + "_" + responding_filter2 + "_and_" + responding_filter3 + "_" + str(ras) + "_" + str(decs) + ".txt", unpack=True, usecols=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) centau_median1 = np.median(lag1) centau_uperr1 = (stats.scoreatpercentile(lag1, perclim)) - centau_median1 centau_loerr1 = centau_median1 - (stats.scoreatpercentile( lag1, (100. - perclim))) len_chain1 = len(lag1[np.where(lag1 > -2000000000000)]) centau_median2 = np.median(lag2) centau_uperr2 = (stats.scoreatpercentile(lag2, perclim)) - centau_median2 centau_loerr2 = centau_median2 - (stats.scoreatpercentile( lag2, (100. - perclim))) len_chain2 = len(lag2[np.where(lag2 > -2000000000000)]) centau_median3 = np.median(lag3) centau_uperr3 = (stats.scoreatpercentile(lag3, perclim)) - centau_median3 centau_loerr3 = centau_median3 - (stats.scoreatpercentile( lag3, (100. - perclim))) len_chain3 = len(lag3[np.where(lag3 > -2000000000000)]) return (ra, dec, zspec_driving, len_chain1, centau_median1, centau_loerr1, centau_uperr1, len_chain2, centau_median2, centau_loerr2, centau_uperr2, len_chain3, centau_median3, centau_loerr3, centau_uperr3) except: print "########## computing iccf FAILS for source located at ra=%f and dec=%f, NO RESPONDING LCs available ##########" % ( ra, dec) cmd = 'rm ' + lcd_name os.system(cmd) return (ra, dec, -9999, -9999, -9999, -9999, -9999, -9999, -9999, -9999, -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, -9999, -9999, -9999, -9999, -9999, -9999, -9999, -9999)
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