def Stack_spec_normwmean(spec,redshifts, wv): flgrid=np.zeros([len(spec),len(wv)]) errgrid=np.zeros([len(spec),len(wv)]) for i in range(len(spec)): wave, flux, error = np.array(Readfile(spec[i], 1)) wave /= (1 + redshifts[i]) mask = np.array([wave[0] < U < wave[-1] for U in wv]) ifl=interp1d(wave,flux) ier=interp1d(wave,error) reg = np.arange(4000, 4210, 1) Cr = np.trapz(ifl(reg), reg) flgrid[i][mask] = ifl(wv[mask]) / Cr errgrid[i][mask] = ier(wv[mask]) / Cr ################ flgrid=np.transpose(flgrid) errgrid=np.transpose(errgrid) weigrid=errgrid**(-2) infmask=np.isinf(weigrid) weigrid[infmask]=0 ################ stack,err=np.zeros([2,len(wv)]) for i in range(len(wv)): stack[i]=np.sum(flgrid[i]*weigrid[[i]])/np.sum(weigrid[i]) err[i]=1/np.sqrt(np.sum(weigrid[i])) ################ ###take out nans IDX=[U for U in range(len(wv)) if stack[U] > 0] return wv[IDX], stack[IDX], err[IDX]
def Analyze_JWST_LH( chifits, specz, metal, age, tau, age_conv='/fdata/scratch/vestrada78840/data/tau_scale_nirspec.dat'): ####### Get maximum age max_age = Oldest_galaxy(specz) ####### Read in file chi = np.load(chifits).T chi[:, len(age[age <= max_age]):, :] = 1E5 ####### Get scaling factor for tau reshaping ultau = np.append(0, np.power(10, np.array(tau)[1:] - 9)) scale = Readfile(age_conv) overhead = np.zeros(len(scale)).astype(int) for i in range(len(scale)): amt = [] for ii in range(len(age)): if age[ii] > scale[i][-1]: amt.append(1) overhead[i] = sum(amt) ######## Reshape likelihood to get average age instead of age when marginalized newchi = np.zeros(chi.shape) for i in range(len(chi)): if i == 0: newchi[i] = chi[i] else: frame = interp2d(metal, scale[i], chi[i])(metal, age[:-overhead[i]]) newchi[i] = np.append(frame, np.repeat([np.repeat(1E5, len(metal))], overhead[i], axis=0), axis=0) ####### Create normalize probablity marginalized over tau P = np.exp(-newchi.T.astype(np.float128) / 2) prob = np.trapz(P, ultau, axis=2) C = np.trapz(np.trapz(prob, age, axis=1), metal) prob /= C #### Get Z and t posteriors PZ = np.trapz(prob, age, axis=1) Pt = np.trapz(prob.T, metal, axis=1) return prob.T, PZ, Pt
infmask=np.isinf(weigrid) weigrid[infmask]=0 ################ stack,err=np.zeros([2,len(wv)]) for i in range(len(wv)): stack[i]=np.sum(flgrid[i]*weigrid[[i]])/np.sum(weigrid[i]) err[i]=1/np.sqrt(np.sum(weigrid[i])) ################ ###take out nans IDX=[U for U in range(len(wv)) if stack[U] > 0] return wv[IDX], stack[IDX], err[IDX] ids,lmass,rshift=np.array(Readfile('masslist_sep28.dat',1,is_float=False)) lmass,rshift=np.array([lmass,rshift]).astype(float) nlist=glob('spec_stacks/*') IDS=[] for i in range(len(ids)): if 10.87<lmass[i] and 1<rshift[i]<1.75: IDS.append(i) speclist=[] for i in range(len(ids[IDS])): for ii in range(len(nlist)): if ids[IDS][i]==nlist[ii][12:18]: speclist.append(nlist[ii])
stack, err = np.zeros([2, len(wv)]) for i in range(len(wv)): stack[i] = np.sum(flgrid[i] * weigrid[[i]]) / np.sum(weigrid[i]) err[i] = 1 / np.sqrt(np.sum(weigrid[i])) ################ ###take out nans # IDX = [U for U in range(len(wv)) if stack[U] > 0] return wv, stack, err """get galaxies""" ###get list of spectra ids, speclist, lmass, rshift = np.array( Readfile('masslist_dec8.dat', 1, is_float=False)) lmass, rshift = np.array([lmass, rshift]).astype(float) IDS = [] for i in range(len(ids)): if 1 <= rshift[i] <= 1.75: IDS.append(i) metal = np.array([ 0.0020, 0.0025, 0.0031, 0.0039, 0.0049, 0.0061, 0.0068, 0.0077, 0.0085, 0.0096, 0.0106, 0.012, 0.0132, 0.014, 0.0150, 0.0164, 0.018, 0.019, 0.021, 0.024, 0.027, 0.03 ]) age = [ 0.5, 0.57, 0.65, 0.74, 0.84, 0.96, 1.1, 1.25, 1.42, 1.62, 1.85, 2.11, 2.4,
def Mag(band): magnitude=25-2.5*np.log10(band) return magnitude cat_data=Table.read('../../../Clear_data/goodsn_3dhst.v4.3.cat', format='ascii') IDS=np.array(cat_data.field('id')) ra=np.array(cat_data.field('ra')) dec=np.array(cat_data.field('dec')) J=np.array(cat_data.field('f_F125W')) star=np.array(cat_data.field('class_star')) fast_data=fits.open('../../../Desktop/catalogs_for_CLEAR/goodsn_3dhst.v4.1.cats/Fast/goodsn_3dhst.v4.1.fout.FITS')[1].data lmass=np.array(fast_data.field('lmass')) RF_dat=Readfile('../../../Desktop/catalogs_for_CLEAR/goodsn_3dhst.v4.1.cats/RF_colors/goodsn_3dhst.v4.1.master.RF',27) ids=np.array(RF_dat[0]) z=np.array(RF_dat[1]) u=np.array(RF_dat[3]) v=np.array(RF_dat[7]) j=np.array(RF_dat[9]) ######Get colors uv=Mag(u)-Mag(v) vj=Mag(v)-Mag(j) print len(IDS)-len(ids) INDEX1=[]
import numpy as np from vtl.Readfile import Readfile from scipy.interpolate import interp1d from glob import glob import matplotlib.pyplot as plt import seaborn as sea from spec_id import Scale_model sea.set(style='white') sea.set(style='ticks') sea.set_style({"xtick.direction": "in", "ytick.direction": "in"}) """read in data""" ###read in data speclist,zsmax,zschi,zs,zsl,zsh,zpsmax,zpschi,\ zps,zpsl,zpsh=np.array(Readfile('stack_redshifts_10-6.dat',is_float=False)) zsmax, zschi, zs, zsl, zsh, zpsmax, zpschi, zps, zpsl, zpsh = np.array( [zsmax, zschi, zs, zsl, zsh, zpsmax, zpschi, zps, zpsl, zpsh]).astype(float) # print speclist[0][18:24] ### set index and file path to models IDS = 45 fp = '/Users/Vince.ec/fsps_models_for_fit/fsps_spec/' print speclist[IDS][18:24] ### read data and models mwv, mfl = np.array(Readfile(fp + 'm0.015_a1.5_t8.0_spec.dat')) wv, fl, er = np.array(Readfile(speclist[IDS])) """plotting""" ### zps w = wv / (1 + zsmax[IDS]) Mfl = interp1d(mwv, mfl)(w)
def MC_fit(galaxy, metal, age, tau, sim_m, sim_a, sim_t, specz, name, repeats=1000, delayed_tau=True, age_conv='/fdata/scratch/vestrada78840/data/tau_scale_ntau.dat'): mlist = np.zeros(repeats) alist = np.zeros(repeats) ultau = np.append(0, np.power(10, np.array(tau[1:]) - 9)) spec = Gen_sim(galaxy, specz, sim_m, sim_a, sim_t, delayed_tau=delayed_tau) ###############Get model list mfl = np.zeros([len(metal) * len(age) * len(tau), len(spec.gal_wv_rf)]) for i in range(len(metal)): for ii in range(len(age)): for iii in range(len(tau)): spec.Sim_spec(metal[i], age[ii], tau[iii]) mfl[i * len(age) * len(tau) + ii * len(tau) + iii] = spec.mfl convtau = np.array([ 0, 8.0, 8.3, 8.48, 8.6, 8.7, 8.78, 8.85, 8.9, 8.95, 9.0, 9.04, 9.08, 9.11, 9.15, 9.18, 9.2, 9.23, 9.26, 9.28, 9.3, 9.32, 9.34, 9.36, 9.38, 9.4, 9.41, 9.43, 9.45, 9.46, 9.48 ]) convage = np.arange(.5, 6.1, .1) mt = [U for U in range(len(convtau)) if convtau[U] in tau] ma = [ U for U in range(len(convage)) if np.round(convage[U], 1) in np.round(age, 1) ] convtable = Readfile(age_conv) scale = convtable[mt[0]:mt[-1] + 1, ma[0]:ma[-1] + 1] overhead = np.zeros(len(scale)).astype(int) for i in range(len(scale)): amt = [] for ii in range(len(age)): if age[ii] > scale[i][-1]: amt.append(1) overhead[i] = sum(amt) for xx in range(repeats): spec.Perturb_flux() chi = np.sum(((spec.flx_err - mfl) / spec.gal_er)**2, axis=1).reshape([len(metal), len(age), len(tau)]).astype(np.float128).T ######## Reshape likelihood to get average age instead of age when marginalized newchi = np.zeros(chi.shape) for i in range(len(chi)): if i == 0: newchi[i] = chi[i] else: frame = interp2d(metal, scale[i], chi[i])(metal, age[:-overhead[i]]) newchi[i] = np.append(frame, np.repeat([np.repeat(1E5, len(metal))], overhead[i], axis=0), axis=0) ####### Create normalize probablity marginalized over tau prob = np.exp(-newchi.T.astype(np.float128) / 2) P = np.trapz(prob, ultau, axis=2) C = np.trapz(np.trapz(P, age, axis=1), metal) #### Get Z and t posteriors PZ = np.trapz(P / C, age, axis=1) Pt = np.trapz(P.T / C, metal, axis=1) mlist[xx], ml, mh = Median_w_Error_cont(PZ, metal) alist[xx], ml, mh = Median_w_Error_cont(Pt, age) np.save('/home/vestrada78840/mcerr/' + name, [mlist, alist]) return
def Analyze_LH_cont_feat( contfits, featfits, specz, metal, age, tau, age_conv='/fdata/scratch/vestrada78840/data/tau_scale_ntau.dat'): ####### Get maximum age max_age = Oldest_galaxy(specz) ####### Read in file Cchi = np.load(contfits).T Fchi = np.load(featfits).T Fchi[:, len(age[age <= max_age]):, :] = 1E5 Cchi[:, len(age[age <= max_age]):, :] = 1E5 ####### Get scaling factor for tau reshaping ultau = np.append(0, np.power(10, np.array(tau)[1:] - 9)) convtau = np.array([ 0, 8.0, 8.3, 8.48, 8.6, 8.7, 8.78, 8.85, 8.9, 8.95, 9.0, 9.04, 9.08, 9.11, 9.15, 9.18, 9.2, 9.23, 9.26, 9.28, 9.3, 9.32, 9.34, 9.36, 9.38, 9.4, 9.41, 9.43, 9.45, 9.46, 9.48 ]) convage = np.arange(.5, 6.1, .1) mt = [U for U in range(len(convtau)) if convtau[U] in tau] ma = [ U for U in range(len(convage)) if np.round(convage[U], 1) in np.round(age, 1) ] convtable = Readfile(age_conv) scale = convtable[mt[0]:mt[-1] + 1, ma[0]:ma[-1] + 1] overhead = np.zeros(len(scale)).astype(int) for i in range(len(scale)): amt = [] for ii in range(len(age)): if age[ii] > scale[i][-1]: amt.append(1) overhead[i] = sum(amt) ######## Reshape likelihood to get average age instead of age when marginalized newCchi = np.zeros(Cchi.shape) newFchi = np.zeros(Fchi.shape) for i in range(len(Cchi)): if i == 0: newCchi[i] = Cchi[i] newFchi[i] = Fchi[i] else: cframe = interp2d(metal, scale[i], Cchi[i])(metal, age[:-overhead[i]]) newCchi[i] = np.append(cframe, np.repeat([np.repeat(1E5, len(metal))], overhead[i], axis=0), axis=0) fframe = interp2d(metal, scale[i], Fchi[i])(metal, age[:-overhead[i]]) newFchi[i] = np.append(fframe, np.repeat([np.repeat(1E5, len(metal))], overhead[i], axis=0), axis=0) ####### Create normalize probablity marginalized over tau cprob = np.exp(-newCchi.T.astype(np.float128) / 2) Pc = np.trapz(cprob, ultau, axis=2) Cc = np.trapz(np.trapz(Pc, age, axis=1), metal) fprob = np.exp(-newFchi.T.astype(np.float128) / 2) Pf = np.trapz(fprob, ultau, axis=2) Cf = np.trapz(np.trapz(Pf, age, axis=1), metal) comb_prob = cprob / Cc * fprob / Cf prob = np.trapz(comb_prob, ultau, axis=2) C0 = np.trapz(np.trapz(prob, age, axis=1), metal) prob /= C0 #### Get Z and t posteriors PZ = np.trapz(prob, age, axis=1) Pt = np.trapz(prob.T, metal, axis=1) return prob.T, PZ, Pt
age = [0.5, 0.57, 0.65, 0.74, 0.84, 0.96, 1.1, 1.25, 1.42, 1.62, 1.85, 2.11, 2.4, 2.74, 3.12, 3.56, 4.05, 4.62, 5.26, 6.0] modeldat=[] filepath = '../../../fsps_models_for_fit/models/' for i in range(len(age)): modeldat.append(filepath + 'm0.019_a%s_z1.2_model.dat' % age[i]) d4=np.zeros(len(modeldat)) d4nc=np.zeros(len(modeldat)) for i in range(len(modeldat)): w,f,e=np.array(Readfile(modeldat[i],1)) w/=2.2 d4[i]=D4000(w,f) # plt.plot(w, f) mr = [[3910, 3990], [4082, 4122], [4250, 4330], [4830, 4890], [4990, 5030]] Mask = np.zeros(len(w)) for ii in range(len(mr)): for iii in range(len(w)): if mr[ii][0] <= w[iii] <= mr[ii][1]: Mask[iii] = 1 maskw = np.ma.masked_array(w, Mask) x3, x2, x1, x0 = np.ma.polyfit(maskw, f, 3, w=1 / e ** 2) C0 = x3 * w ** 3 + x2 * w ** 2 + x1 * w + x0