def compare_gmd(dict, folder, include_met=False): ''' Compare mass calculated from Prospector with mass calculated from FAST :param dict: dictionary, with mass calculated from FAST (output of get_fast() function) :param folder: output folder in which Prospector output of relevant galaxies are stored :return: mass_diff, which is a dictionary with the FAST mass, Prospector mass stored for each object ''' exists = [] out = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/out/' + folder for thing in os.listdir(out): if thing.endswith(".h5"): exists.append(thing) ### NEW COMP mass_diff = {} met_dict = {} dust_dict = {} count = 0 for thing in exists: for key in dict: if thing.startswith(key): get = gmd.printer(out + thing) mass_diff[key] = [dict[key][1], get[0]] dust_dict = get[1] met_dict[key] = get[2] print(count) if include_met: return mass_diff, met_dict # , dust_dict else: return mass_diff # mass_diff[key][0] is the FAST mass, mass_diff[key][1] is the Prospector mass
# GET FAST MASS with open(mass) as tm: for line in tm: if line[0] != '#': cols = line.split() print(cols[0], str(fc_ob)) if cols[0] == str(fc_ob): fast_mass = cols[2] print(fast_mass) # GET PROSP MASS out = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/out/' + out_fold for plop in os.listdir(out): if plop.startswith(str( eelg_objs[i])) and plop.endswith(".h5"): get = gmd.printer(out + plop) prosp_mass = get[0] print(prosp_mass) print('these!') print(fast_file) with open(fast_file, 'r') as ffile: wls = [] flx = [] for line in ffile: if line[0] != '#': cols = line.split() wls.append(float(cols[0]) / (1 + red)) # 1 Jy = 10-23 erg s-1 cm-2 Hz-1 --> erg * 10^-19 # so 1 erg s-1 cm-2 Hz-1 = 10^23 Jy # so 10^-19 erg s-1 cm-2 Hz-1 = 10^4 Jy # ergs
if file.endswith(".h5"): eelgs1.append(file) sfgs1 = [] for file in os.listdir(l_out): if file.endswith(".h5"): sfgs1.append(file) for glxy in eelgs: c += 1 print(c) # file = glxy[0] + '_' + glxy[1] + '_' + glxy[2] + '_extra_out.pkl' file = pkls + glxy + '_extra_out.pkl' if os.path.exists(file): for mfile in eelgs1: if mfile.startswith(glxy): use_mfile = mfile get_e = gmd.printer(out + use_mfile, percs=False, quiet=True) nummy += 1 # temp = randraw(file) # temp[0] lists num=1000 random posterior samples; temp[1] = time vector temp = randraw( file, get_e[np.random.randint(len(get_e))], num=n_samps ) # temp[0] lists num=1000 random posterior samples; temp[1] = time vector # temp = bootdraw(file) # temp[0] lists num=1000 random posterior samples; temp[1] = time vector draws.append(temp[0]) # append random draw of ssfr # boots.append(bootstrap(temp[0])) t1.append(temp[1]) print(len(temp[0])) # 22, 1000 else: print(file) lfile_loc = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/sfg_ssfrs' for glxy in lbgs:
pklsf = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/' + pkls[ 1] for file in os.listdir(outl): if file.endswith(".h5"): fasts.append(file) # get_e = np.zeros(shape=(4, len(gals))) # 4 rows (dust, mass, gaslogz, logzsol), each row as long as eelgs onedraw = np.zeros(shape=(4, len(gals), 10**3)) # *10**3)) onedrawf = np.zeros(shape=(4, len(fasts), 10**3)) # *10**3)) for i in range(len(gals)): if os.path.exists(oute + gals[i]): # print('reg', printer(oute + gals[i])) # print('fast', printer(outl + fasts[i], percs=False, fast=True)) onedraw[:, i, :] = gmd.printer(oute + gals[i], percs=False, draw1=True) onedrawf[:, i, :] = gmd.printer(outl + fasts[i], percs=False, fast=True, draw1=True) # for k in range(10**3): glxy = gnames[i] # fgal = fnames[i] file = pkls + glxy + '_extra_out.pkl' # ffile = pklsf + fgal + '_extra_out.pkl' print(i) randomdraws = sfh.randraw( file, onedraw[0, i, np.random.randint(10**3)])[0] # size 22, num # note: above random randint chooses a random value of mass for galaxy
print(time, 'time') frac = np.zeros(shape=(len(eelgs), 10**3)) s_frac = frac frac2 = np.zeros(shape=(len(eelgs), 10**3)) frac100 = np.zeros(shape=(len(eelgs), 10**3)) regs = np.zeros(shape=(len(eelgs), 10**3)) frac_l = np.zeros(shape=(len(lbgs), 10**3)) if write: eel_file = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/eelg_fracs.txt' sfg_file = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/sfg_fracs.txt' with open(eel_file, 'w+') as write_eel: write_eel.write('# ID SSFR\n\n') for i in range(len(eelgs)): print(i) get_e = gmd.printer(out + eelgs1[i], percs=False) for k in range(10**3): # print((get_e[np.random.randint(len(get_e))])) # print(sfh[i][np.random.randint(len(sfh[i]))]) regs[i, k] = np.percentile(sfh[i], [16., 50., 84.])[1] * time / \ (10 ** np.percentile(get_e, [16., 50., 84.])[1]) s_frac[i, k] = sfh[i][np.random.randint(len(sfh[i]))] frac[i, k] = sfh[i][np.random.randint(len(sfh[i]))] * time / ( 10**get_e[np.random.randint(len(get_e))]) frac2[i, k] = second[i][np.random.randint(len(second[i]))] * time / \ (10 ** get_e[np.random.randint(len(get_e))]) frac100[i, k] = frac[i, k] + frac2[i, k] print(np.percentile(s_frac[i], [16., 50., 84.]), 'sfr') write_eel.write( eelgs[i] + ' ' +
# for ind in range(len(allkeys)): #offsets = np.zeros(shape=(80, 6)) # [mass, dust, metal, sfh1, sfh2, sfh1/sfh2] #meds = np.zeros(shape=(80, 6)) # [mass, dust, metal, sfh1, sfh2, sfh1/sfh2] offsets = np.zeros( shape=(100, 6)) # [mass, dust, metal, sfh1, sfh2, sfh1/sfh2] # BUCKET meds = np.zeros( shape=(100, 6)) # [mass, dust, metal, sfh1, sfh2, sfh1/sfh2] # BUCKET #for i in range(20, 100): # for each galaxy (0 through 99) for i in range(100): # for each galaxy (0 through 99) # i = allkeys[ind] print(i, gals[str(i)]) # i = key #j = i - 20 # BUCKET if os.path.exists(oute + gals[str(i)]): #onedraw[j, :, :] = gmd.printer(oute + gals[str(i)], percs=False, sfhtest=True, draw1=True) onedraw[i, :, :] = gmd.printer(oute + gals[str(i)], percs=False, sfhtest=True, draw1=True) # BUCKET # pars = git + 'eetest/sfhtest_' + str(i) + '_params.py' pars = git + 'new/sfhtest_' + str(i) + '_params.py' #i = j # BUCKET with open(pars, 'r') as parfile: for line in parfile: if line.startswith("# Codename: "): spaces = 0 inmass, indust, inmet, insfr1, insfr2 = '', '', '', '', '' for char in line: if char == ' ' or char == '_': spaces += 1 elif spaces == 2: inmass += char
def printer(out_file, cvg=-1000, masstest=False, obj_true=None): objname = '' count = 0 field = '' for i in kwargs['outname']: if i == '_': count += 1 elif count == 0: objname += i elif count == 1: field += i elif count == 2: break print(field) res, pr, mod = bread.results_from(out_file) # print(res['chain'], 'chain') # ''' # # PRINT TRACE SHOWING HOW ITERATIONS CONVERGE FOR EACH PARAMETER bread.param_evol(res) # print tracefig plt.show() # plt.savefig('delete' + '_tracefig2.png', bbox_inches='tight') # FIND WALKER, ITERATION THAT GIVE MAX (AND MIN) PROBABILITY # print(prob) prob = res['lnprobability'][..., cvg:] print('max', prob.max()) row = prob.argmax() / len(prob[0]) col = prob.argmax() - row * len(prob[0]) walker, iteration = row, col print(walker, iteration) # ''' # truths = None print(objname) if masstest: print('hi?') import get_mass_dust as gmd oute = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/out/out_efico/' for file in os.listdir(oute): if file.endswith(".h5") and file.startswith(obj_true): print(file, 'file') params = gmd.printer(oute + file, percs=False, masstest=True) mass = params[0] dust = params[6] met = params[7] gasmet = params[8] sfh = [] for i in [1, 2, 3, 4, 5]: sfh.append(params[i]) truths = [np.log10(10**mass + 6.5*10**8), sfh[0], sfh[1], sfh[2], sfh[3], sfh[4], dust, met, gasmet] # PRINT CORNERFIG CONTOURS/HISTOGRAMS FOR EACH PARAMETER # truths now corrected for masses of efico output + 6.5e8 # truths = [9.466 + 6.5e8, 0.22, 0.12, 0.22, 0.12, 0.1, 0.0737, -1.79, -0.33] # 12552 # truths = [9.83 + 6.5e8, 0.43, 0.08, 0.04, 0.12, 0.1, 0.32, -1.57, -0.35] # 12105 # truths = [9.51 + 6.5e8, 0.80, 0.02, 0.01, 0.04, 0.04, 0.54, -1.94, -0.31] # 21442 print(truths) bread.subtriangle(res, start=cvg, thin=5, truths=truths, show_titles=True) # set start by when kl converges! plt.show() # plt.savefig('delete' + '_ctracefig2.png', bbox_inches='tight') # For FAST: truths = [mass, age, tau, dust2] (for 1824: [9.78, 0.25, -1., 0.00]) return objname, field, res, mod, walker, iteration
def load_model(objname, field, agelims=[], **extras): # REDSHIFT # open file, load data photname, zname, filtername, filts = get_names(field) with open(photname, 'r') as f: hdr = f.readline().split() dtype = np.dtype([(hdr[1], 'S20')] + [(n, np.float) for n in hdr[2:]]) dat = np.loadtxt(photname, comments='#', delimiter=' ', dtype=dtype) with open(zname, 'r') as fz: hdr_z = fz.readline().split() dtype_z = np.dtype([(hdr_z[1], 'S20')] + [(n, np.float) for n in hdr_z[2:]]) zout = np.loadtxt(zname, comments='#', delimiter=' ', dtype=dtype_z) idx = dat['id'] == objname # creates array of True/False: True when dat[id] = objname # zred = zout['z_spec'][idx][0] # use z_spec # if zred == -99: # if z_spec doesn't exist # zred = zout['z_peak'][idx][0] # use z_phot zred = 3.5 print(zred, 'zred') # CALCULATE AGE OF THE UNIVERSE (TUNIV) AT REDSHIFT ZRED tuniv = WMAP9.age(zred).value print(tuniv, 'tuniv') n = [p['name'] for p in model_params] # model_params[n.index('tage')]['prior_args']['maxi'] = tuniv # NONPARAMETRIC SFH # NEW ''' agelims = [0.0, 8.0, 8.7, 9.0, (9.0 + (np.log10(tuniv*1e9) - 9.0)/3), (9.0 + 2 * (np.log10(tuniv*1e9) - 9.0)/3), np.log10(tuniv*1e9)] ''' agelims = [0.0, 7.7, 8.0, 9.0, (9.0 + (np.log10(tuniv*1e9) - 9.0)/3), (9.0 + 2 * (np.log10(tuniv*1e9) - 9.0)/3), np.log10(tuniv*1e9)] ncomp = len(agelims) - 1 agebins = np.array([agelims[:-1], agelims[1:]]) # why agelims[1:] instead of agelims[0:]? # print(agebins, 'grab these agebins') # INSERT REDSHIFT INTO MODEL PARAMETER DICTIONARY zind = n.index('zred') model_params[zind]['init'] = zred # model_params[n.index('tage')]['init'] = tuniv # set tage to the age of the Universe # set dust and metallicity to be the same dust and metallicity you find in the actual fits: get_out = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/out/out_efico/' for plop in os.listdir(get_out): if plop.startswith(str(objname)) and plop.endswith(".h5"): # print(str(objname)) get = gmd.printer(get_out + plop) # [mass, dust, metal, gasmet] model_params[n.index('logmass')]['init'] = 9.9 # 9. # get[0] model_params[n.index('dust2')]['init'] = 0.3 # get[1] model_params[n.index('logzsol')]['init'] = -0.06 # -1.7 # get[2] model_params[n.index('gas_logz')]['init'] = get[3] # SET UP AGEBINS model_params[n.index('agebins')]['N'] = ncomp model_params[n.index('agebins')]['init'] = agebins.T # FRACTIONAL MASS INITIALIZATION # NEW # N-1 bins, last is set by x = 1 - np.sum(sfr_fraction) model_params[n.index('sfr_fraction')]['N'] = ncomp-1 model_params[n.index('sfr_fraction')]['prior_args'] = { 'maxi': np.full(ncomp-1, 1.0), 'mini': np.full(ncomp-1, 0.0), # NOTE: ncomp instead of ncomp-1 makes the prior take into # account the implicit Nth variable too } eelg_frac1 = 0.3 # mfrac in most recent bin eelg_frac2 = 0.05 # mfrac in second most recent bin sf_inits = np.zeros(ncomp-1)+(1.-(eelg_frac1 + eelg_frac2))/(ncomp-2.) sf_inits[0] = eelg_frac1 sf_inits[1] = eelg_frac2 # print(sf_inits, 'look at me!!!') model_params[n.index('sfr_fraction')]['init'] = sf_inits # np.zeros(ncomp-1)+1./ncomp model_params[n.index('sfr_fraction')]['init_disp'] = 0.02 # CREATE MODEL model = BurstyModel(model_params) print('model') # print(model_params) # print(model_params[n.index('sfr_fraction')]) return model
def preplot(order=None, keys='e', file=None, line_idx=5, line_idx2=[5, 3], ratio=[False, False], outfold=None, fs_text=30, fs=20, color='purple', font={'fontname': 'Times'}, types=False): ''' :param keys: :param files: :param line_idx: single idx for desired line flux or EW, OR [idx1, idx2] for desired line ratios :param line_idx2: (if no ssfr) single idx for desired line flux or EW, OR [idx1, idx2] for desired line ratios :param ratio: :param fs_text: :param fs: :param font: :return: ''' lines = [ '[NII]6585', '[OII]3726', r'H$\alpha$6563', '[OII]3729', '[OIII]4960', '[OIII]5007', r'H$\beta$4861', r'H$\delta$4102' ] # names = '[NII]6585', '[OII]3726', 'Halpha6563', '[OII]3729', '[OIII]4960', '[OIII]5007', 'Hbeta4861', 'Hdelta4102' if keys != 's': ew, ew_order = list_ewflux(file, line_idx=line_idx, ratio=ratio[0]) # line1 # xy[1] = list_ewflux(files[1], line_idx=line_idx2, ratio=ratio[1]) # line2 labels = [ r'Stellar Mass [$\log_{10}(\textrm{M} / \textrm{M}_\odot$)]', lines[line_idx] + r' Equivalent Width [\AA]' ] # labels[1] = lines[line_idx2[0]] + '/' + lines[line_idx2[1]] else: # want ssfr ew = list_ssfr(file) # ssfr labels = [ r'Stellar Mass [log_{10}(M / M$_\odot$)]', 'SSFR [Gyr$^{-1}$]' ] out = outfold gals1 = [] for file in os.listdir(out): if file.endswith(".h5"): gals1.append(file) gals = [] relevent_idcs = [] for ln in range(len(order)): for id in range(len(gals1)): if gals1[id].startswith(str(order[ln])): if order[ln] == 11462 or order[ln] == 21076 or order[ ln] == 21442: relevent_idcs.append(ln) gals.append(gals1[id]) # print('1', gals) # print('2', order) # print('3', ew_order) # yay is good! mass = [] for i in range(len(gals)): mass.append(gmd.printer(out + gals[i], percs=True)[0]) # median mass if types: return ew, mass, labels, relevent_idcs else: return ew, mass, labels
def return_fracs(gals, order, pklfolder='pkl_efico/', outfold='out_efico/', base='fico', ls=False): import ssfr_now as sfnow eelgs, lbgs1 = sa.get_gal_lists(base=[base, base], normal=True) git = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/' out = git + 'out/' + outfold f_folder = git + pklfolder gals1 = gals if ls: gals1 = [] for i in range(len(lbgs1)): file = f_folder + lbgs1[i] + '_extra_out.pkl' if os.path.exists(file): for ga in gals: print(str(ga)) if file.startswith(f_folder + str(ga)): gals1.append(lbgs1[i]) # print(lbgs1) print(len(gals1), 'look') ''' ordered = [] for go in range(len(order)): for ga in range(len(gals1)): if gals1[ga].startswith(order[go]): ordered.append(gals1[ga]) ''' ordered = gals1 time = 0.5 * 10**8 sfh = [] highfracs = [] # print('masses', masses) frac = np.zeros(shape=(len(ordered), 10**3)) for ord in range(len(ordered)): # print(ordered[ord]) # file = f_folder + str(glxy) + '*_extra_out.pkl' for pkls in os.listdir(f_folder): if pkls.startswith(str( ordered[ord])) and pkls.endswith('_extra_out.pkl'): file = f_folder + pkls sfh.append(sfnow.get_sfh(file)[0]) # 1st bin # for i in range(len(ordered)): # print(ord) file_i = None for outs in os.listdir(out): if outs.startswith(str(ordered[ord])) and outs.endswith('.h5'): file_i = out + outs get_e = gmd.printer(file_i, percs=False, quiet=True) for k in range(10**3): frac[ord, k] = sfh[ord][np.random.randint(len( sfh[ord]))] * time / (10**get_e[np.random.randint(len(get_e))]) # print(frac) all_true = [] for fr in range(len(frac)): for k in range(10**3): all_true.append(frac[fr, k]) # print(all_fracs) return np.percentile(all_true, [16., 50., 84.])
def mass_match_plot(x1, y1, color, order, lims=[9.5, 10.], errs=True, outfold=None, ssfr_file=None, fs_text=30, fs=20, font={'fontname': 'Times'}): x = [] y = [] do_these = [] for i in range(len(x1)): if lims[0] <= x1[i] <= lims[1]: # 9.55 <= x1[i] <= 9.9: do_these.append(order[i]) x.append(x1[i]) y.append(y1[i]) plt.scatter(x=x, y=y, color=color, s=fs * 2) if errs: ''' errx = np.percentile(x, [16., 50., 84.]) erry = np.percentile(y, [16., 50., 84.]) plt.errorbar(x=[errx[1]], y=[erry[1]], xerr=[[errx[1] - errx[0]], [errx[2] - errx[1]]], yerr=[[erry[1] - erry[0]], [erry[2] - erry[1]]], color=color, fmt='*') ''' X, Y, Z = density_estimation(x, y) # levels = np.arange(0.5, np.amax(Z), 0.02) + 0.02 levels = np.arange(np.amax(Z) / 7., np.amax(Z), np.amax(Z) / 7.) + (np.amax(Z) / 7.) if color == 'b': cmap = 'Blues' else: cmap = 'Purples' plt.contourf(X, Y, Z, levels=levels, cmap=cmap, alpha=0.5) # levels=levels # [0.1, 0.2, 0.5, 1., 25.] out = outfold gals1 = [] for file in os.listdir(out): if file.endswith(".h5"): gals1.append(file) gals = [] for ln in range(len(do_these)): for id in range(len(gals1)): if gals1[id].startswith(str(do_these[ln])): gals.append(gals1[id]) mass = [] dust = [] met = [] for i in range(len(gals)): get = gmd.printer(out + gals[i], percs=True) # median mass, dust, gasmet, metallicity mass.append(get[0]) dust.append(get[1]) met.append(get[2]) ssfrs = [] counter = 0 with open(ssfr_file, 'r') as ssfr: for line in ssfr: print(line) if line[0] != '#': cols = line.split() print(cols) for dt in range(len(do_these)): if cols[0].startswith(str(do_these[dt])): ssfrs.append( float(cols[1]) * 10**9 ) # ssfrs are stored in 2nd column in file; convert to Gyr^-1 counter += 1 return mass, dust, met, ssfrs, do_these