def read_results(filename): res, obs, mod = reader.results_from(path_res + filename) # update data table res['run_params']['data_table'] = path_wdir + 'data/halo7d_with_phot.fits' mod = reader.get_model(res) # update filters filternames = [str(ii) for ii in obs['filters']] obs['filters'] = load_filters(filternames, directory=filter_folder) # load sps sps = reader.get_sps(res) return (res, obs, mod, sps)
def get_results(cluster, ID, binNum, results_type='dynesty', version=''): # convenience function infile = '{}/h5/{}_ID_{}_bin_{}{}.h5'.format(cluster, cluster, ID, binNum, version) result, obs, _ = reader.results_from(infile, dangerous=True) model = reader.get_model(result) sps = reader.get_sps(result) # t_hr = result['sampling_duration']/3600 # print('The sampling took {:.2f} hours'.format(t_hr)) return result, obs, model, sps
# -------------- # Read command line arguments # -------------- sargv = sys.argv argdict = {'restart_from': '', 'niter': 1024} clargs = model_setup.parse_args(sargv, argdict=argdict) # ---------- # Result object and Globals # ---------- result, global_obs, global_model = pr.results_from(clargs["restart_from"]) is_emcee = (len(result["chain"].shape) == 3) & (result["chain"].shape[0] > 1) assert is_emcee, "Result file does not have a chain of the proper shape." # SPS Model instance (with libraries check) sps = pr.get_sps(result) run_params = result["run_params"] run_params.update(clargs) # Noise model (this should be doable via read_results) from prospect.models.model_setup import import_module_from_string param_file = (result['run_params'].get('param_file', ''), result.get("paramfile_text", '')) path, filename = os.path.split(param_file[0]) modname = filename.replace('.py', '') user_module = import_module_from_string(param_file[1], modname) spec_noise, phot_noise = user_module.load_gp(**run_params) # ----------------- # LnP function as global # ------------------
N = res['lnprobability'].shape[0] * res['lnprobability'].shape[1] # all values print('N is', N) imaxmult = np.argpartition(res['lnprobability'], -N, axis=None)[-N:] csz = res["chain"].shape i, j = np.unravel_index(imax, res['lnprobability'].shape) theta_max = res['chain'][i, j, :].copy() flatchain = res["chain"].reshape(csz[0] * csz[1], csz[2]) max_percentile = np.zeros(model.ndim) for i in range(model.ndim): max_percentile[i] = stats.percentileofscore(flatchain[:, i], theta_max[i]) print('max percentile', max_percentile[i]) sps = reader.get_sps(res) # generate fake obs to get full resolution spectra fake_obs = obs.copy() fake_obs['spectrum'] = None fake_obs['wavelength'] = None spec, phot, x = model.predict(theta_max, obs=obs, sps=sps) full_spec = model.predict(theta_max, obs=fake_obs, sps=sps)[0] wave_eff = [f.wave_effective for f in res['obs']['filters']] print('Maximum posterior probability parameters: ') file = open(args.path + "/Analysis/bestfit_params.txt", "w") for i in range(len(res['theta_labels'])):
print('now reading files') galaxy_num = "{:03d}".format(galaxy) infile = prosp_dir + '/galaxy_' + str(int(galaxy)) + '_pagboff.h5' globfiles = glob.glob(infile) for prosp_output in glob.glob(infile): print(prosp_output) res, obs, mod = pread.results_from(prosp_output) pdfile = pd_dir + '/grid_physical_properties.305_galaxy' + str( int(galaxy)) + '.npz' pd_data = np.load(pdfile) int_mass = np.log10(np.sum(pd_data['grid_star_mass']) / 1.989e33) print('sps and model') #sps = build_sps() sps = pread.get_sps(res) #mod = build_model() thetas = mod.theta_labels() thetas_50 = [] thetas_16 = [] thetas_84 = [] print('quantiles for all thetas') for theta in thetas: idx = thetas.index(theta) chain = [item[idx] for item in res['chain']] quan = quantile(chain, [.16, .5, .84]) thetas_50.append(quan[1]) thetas_16.append(quan[0]) thetas_84.append(quan[2])
import numpy as np import matplotlib.pyplot as plt import prospect.io.read_results as reader results_type = 'dynesty' # 'emcee' or 'dynesty' verbose = True plots = True # get the results and observation dictionaries result, obs, _ = reader.results_from('demo_params_{}.h5'.format(results_type), dangerous=True) # also get the sps and model objects. This works if using a parameter file with # `build_*` methods sps = reader.get_sps(result) model = reader.get_model(result) # print the contents of the `results` dictionary if verbose: print(result.keys()) # STEP 1: invetigate the parameter traces if plots: if results_type == 'emcee': chosen = np.random.choice(result['run_params']['nwalkers'], size=10, replace=False) tracefig = reader.traceplot(result, figsize=(16, 9), chains=chosen) else: tracefig = reader.traceplot(result, figsize=(16, 9))
textFile.close() # Calculate Prospector attenuation curves # Take bestfit parameters from dust fit, use only # non-dust related parameters with nodust model # to give dust-free spectrum if attBool: nodust_path = '/scratch/ntf229/RT_fit/projects/' + galaxies[ i] + '/maxLevel13/wavelengths601/numPhotons1e9/niter4096/inc0/nodust/' + phot[ k] + '/walkers512/Prospector_files/fit.h5' dust_path = '/scratch/ntf229/RT_fit/projects/' + galaxies[ i] + '/maxLevel13/wavelengths601/numPhotons1e9/niter4096/inc0/dust/dustFraction0.2/maxTemp8000/' + phot[ k] + '/walkers512/Prospector_files/fit.h5' nodust_res, nodust_obs, nodust_model = reader.results_from( nodust_path) nodust_sps = reader.get_sps(nodust_res) dust_res, dust_obs, dust_model = reader.results_from(dust_path) dust_sps = reader.get_sps(dust_res) # generate fake obs to get full resolution spectra nodust_fake_obs = nodust_obs.copy() nodust_fake_obs['spectrum'] = None nodust_fake_obs['wavelength'] = None nodust_theta_max = [ estMasses[i, k], estLogzsol[i, k], estAges[i, k], estTaus[i, k] ] # non-dust related bestfit parameters from dust fit nodust_full_spec = nodust_model.predict(nodust_theta_max, obs=nodust_fake_obs, sps=nodust_sps)[0] dust_fake_obs = dust_obs.copy()
# -------------- # Read command line arguments # -------------- sargv = sys.argv argdict = {'restart_from': '', 'niter': 1024} clargs = model_setup.parse_args(sargv, argdict=argdict) # ---------- # Result object and Globals # ---------- result, global_obs, global_model = pr.results_from(clargs["restart_from"]) is_emcee = (len(result["chain"].shape) == 3) & (result["chain"].shape[0] > 1) assert is_emcee, "Result file does not have a chain of the proper shape." # SPS Model instance (with libraries check) sps = pr.get_sps(result) run_params = result["run_params"] run_params.update(clargs) # Noise model (this should be doable via read_results) from prospect.models.model_setup import import_module_from_string param_file = (result['run_params'].get('param_file', ''), result.get("paramfile_text", '')) path, filename = os.path.split(param_file[0]) modname = filename.replace('.py', '') user_module = import_module_from_string(param_file[1], modname) spec_noise, phot_noise = user_module.load_gp(**run_params) # ----------------- # LnP function as global # ------------------
lw=1., label='True Attenuation') #plt.xscale('log') plt.xlim([900, 7000]) plt.xlabel('$\lambda$ [$\AA$]', fontsize=23) plt.ylim([0, 5]) plt.ylabel('A$_{\lambda}$ / A$_{V}$', fontsize=23) plt.legend(loc='best', fontsize=15) plt.title('Galaxy ' + str(gal) + ': KC Attenuation', fontsize=23) #plt.savefig('/Users/sidneylower/Documents/prosp_final_plots/attenuation_curve'+str(gal)+'.png', dpi=300) # In[9]: sps_dir = pread.get_sps(res_dir) thetas_dir, theta_best_dir = get_best(res_dir) dust2_idx_dir = [i for i, s in enumerate(thetas_dir) if 'dust2' in s] dust2_dir = theta_best_dir[dust2_idx_dir[0]] dustindex_idx_dir = [i for i, s in enumerate(thetas_dir) if 'dust_index' in s] dust_index_dir = theta_best_dir[dustindex_idx_dir[0]] # In[10]: sps_dust1 = pread.get_sps(res_dust1) thetas_dust1, theta_best_dust1 = get_best(res_dust1) dust2_idx_dust1 = [i for i, s in enumerate(thetas_dust1) if 'dust2' in s] dust2_dust1 = theta_best_dust1[dust2_idx_dust1[0]] dustindex_idx_dust1 = [ i for i, s in enumerate(thetas_dust1) if 'dust_index' in s ]