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 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])
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') x = javdata_con.jlist[:] y = javdata_con.mlist[:] + javdata_con.blist[:] ey = javdata_con.elist[:] plt.errorbar(x[0], y[0] + javdata_con.cont_mean, yerr=ey[0], ls='none', marker='o') plt.show() rmap1 = Rmap_Model(javdata_rm) #rmap1.do_mcmc(conthpd=conthpd, fchain="mychain1.dat", laglimit=[[0, 10],], nwalkers=100, nburn=200, nchain=1000) rmap1.load_chain("mychain1.dat") rmap1.show_hist() rmap1.get_hpd() rmap1hpd = rmap1.hpd par_best = rmap1hpd[1, :] print(par_best) javdata_best = rmap1.do_pred(par_best) javdata_best.plot(set_pred=True, obs=javdata_rm)