def test_pf_kurucz_H2O(): with pt.cd('outputs/'): pf_data, isotopes, temp = pf.kurucz( f'{pc.ROOT}tests/inputs/mock_h2opartfn.dat', outfile='PF_kurucz_H2O.dat') np.testing.assert_equal(temp, np.arange(10, 51, 10.0)) np.testing.assert_allclose( pf_data, np.array([[1.328, 3.349, 6.192, 9.417, 12.962], [1.33, 3.361, 6.217, 9.457, 13.017], [1.332, 3.373, 6.24, 9.492, 13.066], [1.399, 2.945, 5.085, 7.617, 10.476]])) np.testing.assert_equal( isotopes, np.array(['1H1H16O', '1H1H17O', '1H1H18O', '1H2H16O'])) pf_read, iso, temp_read = io.read_pf('outputs/PF_kurucz_H2O.dat') np.testing.assert_allclose( pf_data, np.array([[1.328, 3.349, 6.192, 9.417, 12.962], [1.33, 3.361, 6.217, 9.457, 13.017], [1.332, 3.373, 6.24, 9.492, 13.066], [1.399, 2.945, 5.085, 7.617, 10.476]])) np.testing.assert_equal(temp_read, np.arange(10, 51, 10.0)) np.testing.assert_equal( iso, np.array(['1H1H16O', '1H1H17O', '1H1H18O', '1H2H16O']))
def main(): cfile = 'run_HATP-41b_tsiaras/mcmc_HATP-41b_tsiaras_w00000-mc.cfg' folder, cfile = os.path.split(cfile) _, planet, dset = folder.split('_') with pt.cd(folder): pyrat = pb.run(cfile, run_step='init', no_logfile=True) with np.load(pyrat.ret.mcmcfile) as mcmc: post, zchain, zmask = mc3.utils.burn(mcmc) texnames = mcmc['texnames'] bestp = mcmc['bestp'] imol = np.in1d(mcmc['ifree'], pyrat.ret.imol) posteriors = pyrat.ret.posterior = post u, uind, uinv = np.unique(posteriors[:, 0], return_index=True, return_inverse=True) nunique = np.size(u) # Get mean molecular mass: ZX_ratio = np.zeros(nunique, np.double) params = pyrat.ret.params for k in range(nunique): params[imol] = posteriors[uind[k], imol] ZX_ratio[k] = ZX(params, pyrat) iN2 = pyrat.ret.pnames.index('log(N2)') # Metal mass fraction relative to solar values, i.e., [Z/X]: posteriors[:, iN2] = ZX_ratio[uinv] bestp[iN2] = ZX(bestp, pyrat) texnames[iN2] = r"$[Z/X]$" posteriors = roll(posteriors, indices=pyrat.ret.imol[:-1]) bestp = roll(bestp, indices=pyrat.ret.imol[:-1]) texnames = roll(texnames, indices=pyrat.ret.imol[:-1]) texnames[4] = texnames[4].replace('f', 'X') rect = [0.105, 0.11, 0.99, 0.99] ranges = [(100, 3000), (1.49, 1.68), (-4.5, 2.6), (-5.5, 2.5), (-7., -0.45)] plt.figure(316, (6, 6)) plt.clf() axes, cax = mc3.plots.pairwise(posteriors, pnames=texnames, ranges=ranges, fignum=8, rect=rect, nbins=20) ax = axes[1][2] plt.text(0.15, 0.125, 'cloudy', rotation=45, transform=ax.transAxes) plt.text(0.70, 0.25, 'heavy', rotation=45, transform=ax.transAxes) plt.text(0.75, 0.67, 'deplet', rotation=90, transform=ax.transAxes) ax = axes[3][3] plt.text(0.05, 0.30, 'cloudy', rotation=305, transform=ax.transAxes) plt.text(0.55, 0.75, 'heavy', rotation=0, transform=ax.transAxes) plt.text(0.65, 0.10, 'deplet', rotation=0, transform=ax.transAxes) plt.savefig(f'plots/WFC3_{planet}_{dset}_correlation.pdf') plt.savefig(f'plots/WFC3_{planet}_{dset}_correlation.png', dpi=300)
def test_pf_tips(): expected_temp = np.array([1] + [i for i in range(5, 19, 5)] + [i for i in range(20, 5001, 10)], float) with open(pc.ROOT + 'pyratbay/data/tips_2017.pkl', 'rb') as p: expected_pf = pickle.load(p)['H2O'] with pt.cd('outputs/'): pf_data, isotopes, temp = pf.tips('H2O', outfile='default') np.testing.assert_equal(temp, expected_temp) np.testing.assert_equal(pf_data[0], expected_pf['161']) assert isotopes == list(expected_pf.keys()) pf_read, iso, temp_read = io.read_pf('outputs/PF_tips_H2O.dat') np.testing.assert_allclose(pf_read[0, :], expected_pf['161'], rtol=1e-7) np.testing.assert_allclose(temp_read, expected_temp, rtol=1e-7) assert list(iso) == list(expected_pf.keys())
def test_pf_exomol_listed_single(): epf = f'{pc.ROOT}tests/outputs/14N-1H4__MockBYTe.pf' expected_temp = np.arange(1, 6) expected_pf = np.logspace(0, 1, 5) mock_pf(epf, expected_temp, expected_pf) with pt.cd('outputs/'): pf_data, isotopes, temp = pf.exomol([epf], outfile='default') np.testing.assert_allclose(pf_data[0, :], expected_pf) np.testing.assert_allclose(temp, expected_temp) assert list(isotopes) == ['41111'] pf_read, iso, temp_read = io.read_pf('outputs/PF_exomol_NH4.dat') np.testing.assert_allclose(pf_read[0, :], expected_pf, rtol=1e-5) np.testing.assert_allclose(temp_read, expected_temp, rtol=1e-7) assert list(iso) == ['41111']
def test_pf_exomol_two(): epf1 = f'{pc.ROOT}tests/outputs/14N-1H4__MockBYTe.pf' epf2 = f'{pc.ROOT}tests/outputs/15N-1H4__MockBYTe.pf' expected_temp1 = np.arange(1, 6) expected_temp2 = np.arange(1, 9) expected_pf1 = np.logspace(0, 1, 5) expected_pf2 = np.logspace(0, 1, 8) mock_pf(epf1, expected_temp1, expected_pf1) mock_pf(epf2, expected_temp2, expected_pf2) with pt.cd('outputs/'): pf_data, isotopes, temp = pf.exomol([epf1, epf2], outfile='default') np.testing.assert_allclose(pf_data[0, :], expected_pf1, rtol=1e-5) np.testing.assert_allclose(pf_data[1, :], expected_pf2[:5], rtol=1e-5) np.testing.assert_allclose(temp, expected_temp1) assert list(isotopes) == ['41111', '51111'] pf_read, iso, temp_read = io.read_pf('outputs/PF_exomol_NH4.dat') np.testing.assert_allclose(pf_read[0, :], expected_pf1, rtol=1e-5) np.testing.assert_allclose(pf_read[1, :], expected_pf2[:5], rtol=1e-5) np.testing.assert_allclose(temp_read, expected_temp1) assert list(iso) == ['41111', '51111']
def test_pands(capfd): with pt.cd('outputs/'): pf.kurucz(f'{ROOT}tests/inputs/mock_h2opartfn.dat', outfile='default') pb.run('configs/tli_pands_test.cfg') captured = capfd.readouterr() caps = [ "Read command-line arguments from configuration file:", "'configs/tli_pands_test.cfg'", "Reading input database files:", "tests/inputs/mock_h2ofastfix.bin", "There are 1 input database file(s).", "Initial TLI wavelength (um): 2.500 ( 4000.000 cm-1)", "Final TLI wavelength (um): 2.501 ( 3998.401 cm-1)", "Database (1/1): 'Partridge & Schwenke (1997)' (H2O molecule)", "Number of temperatures: 5", "Number of isotopes: 4", "Process P&S H2O database between records 38 and 10,220.", "[9625 207 219 132]", "Writing 10,183 transition lines.", "/pands_H2O_2.500-2.501um_test.tli'.", ] for cap in caps: assert cap in captured.out
def test_pf_kurucz_TiO(): with pt.cd('outputs/'): pf_data, isotopes, temp = pf.kurucz( f'{pc.ROOT}tests/inputs/mock_tiopart.dat', outfile='default') np.testing.assert_allclose( pf_data, np.array([[28.829, 54.866, 81.572, 110.039, 141.079], [28.97, 55.151, 82.002, 110.625, 141.834], [29.107, 55.425, 82.417, 111.19, 142.564], [29.24, 55.692, 82.821, 111.741, 143.273], [29.369, 55.95, 83.212, 112.273, 143.96]])) np.testing.assert_equal(temp, np.arange(10, 51, 10.0)) np.testing.assert_equal(isotopes, np.array(['66', '76', '86', '96', '06'])) pf_read, iso, temp_read = io.read_pf('outputs/PF_kurucz_TiO.dat') np.testing.assert_allclose( pf_read, np.array([[28.829, 54.866, 81.572, 110.039, 141.079], [28.97, 55.151, 82.002, 110.625, 141.834], [29.107, 55.425, 82.417, 111.19, 142.564], [29.24, 55.692, 82.821, 111.741, 143.273], [29.369, 55.95, 83.212, 112.273, 143.96]])) np.testing.assert_equal(temp_read, np.arange(10, 51, 10.0)) np.testing.assert_equal(iso, np.array(['66', '76', '86', '96', '06']))
'run_feng2020/mcmc_WASP43b_integrated_phase0.81.cfg', 'run_feng2020/mcmc_WASP43b_integrated_phase0.88.cfg', 'run_feng2020/mcmc_WASP43b_integrated_phase0.94.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.06.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.12.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.19.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.25.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.31.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.38.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.44.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.50.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.56.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.62.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.69.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.75.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.81.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.88.cfg', 'run_feng2020/mcmc_WASP43b_resolved_phase0.94.cfg', ] for cfg in retrievals: with pt.cd(os.path.dirname(cfg)): pyrat = pb.run(os.path.basename(cfg), run_step='init', no_logfile=True) mcmc = np.load(pyrat.ret.mcmcfile) pyrat.ret.posterior, _, _ = mc3.utils.burn(mcmc) pyrat.percentile_spectrum() pfile = pyrat.ret.mcmcfile.replace('.npz', '.pickle') io.save_pyrat(pyrat, pfile) mcmc.close()
'run_jwst_09TP_01Q', 'run_jwst_16TP_16Q', ] modes = [ 'integrated', 'resolved', ] nmodels = len(model_names) nmodes = len(modes) nphase = len(obs_phase) k = 2 model = f'mcmc_model_WASP43b_{modes[0]}_phase{obs_phase[0]:.2f}.cfg' with pt.cd(model_names[k]): pyrat = pb.run(model, run_step='init', no_logfile=True) nspec = pyrat.obs.ndata nlayers = pyrat.atm.nlayers nprofiles = 5 data = np.zeros((nmodels, nmodes, nphase, nspec)) uncerts = np.zeros((nmodels, nmodes, nphase, nspec)) temp_posteriors = np.zeros((nmodels, nmodes, nphase, nprofiles, nlayers)) posteriors = np.zeros((nmodels, nmodes, nphase), dtype=object) #for k,j,i in product(range(nmodels), range(nmodes), range(nphase)): for j, i in product(range(nmodes), range(nphase)): if k != 2 and i > 8: continue model = f'mcmc_model_WASP43b_{modes[j]}_phase{obs_phase[i]:.2f}.cfg'
'run_WASP-101b_wakeford', 'run_WASP-103b_kreidberg', 'run_XO-1b_tsiaras', ] ntargets = len(targets) # Extract values: names = [None] * ntargets flat_chisq = np.zeros(ntargets) flat_rchisq = np.zeros(ntargets) flat_bic = np.zeros(ntargets) for i in range(ntargets): cfile = [f for f in os.listdir(targets[i]) if f.endswith(".cfg")][0] with cd(targets[i]): pyrat = pb.run(cfile, run_step='dry', no_logfile=True) data = pyrat.obs.data uncert = pyrat.obs.uncert indparams = [data] params = np.array([np.mean(data)]) pstep = np.array([1.0]) mc3_fit = mc3.fit(data, uncert, sfit, params, indparams, pstep) dof = len(data) - len(params) names[i] = cfile.split('_')[1] + ' ' + cfile.split('_')[2] flat_chisq[i] = mc3_fit['best_chisq'] flat_rchisq[i] = mc3_fit['best_chisq'] / dof flat_bic[i] = mc3_fit['best_chisq'] + len(params) * np.log(len(data))
def main(): # Only those with ADI > 3: cfiles = [ 'run_HATP-18b_tsiaras/mcmc_HATP-18b_tsiaras_w00000-0c.cfg', 'run_HATP-32b_tsiaras/mcmc_HATP-32b_tsiaras_w00000-0c.cfg', 'run_HATP-38b_bruno/mcmc_HATP-38b_bruno_w00000-0c.cfg', 'run_HATP-41b_tsiaras/mcmc_HATP-41b_tsiaras_w00000-0c.cfg', 'run_K2-018b_benneke/mcmc_K2-018b_benneke_w00000-0c.cfg', 'run_WASP-043b_stevenson/mcmc_WASP-043b_stevenson_w00000-0c.cfg', 'run_WASP-063b_kilpatrick/mcmc_WASP-063b_kilpatrick_w000h0-0c.cfg', 'run_WASP-069b_tsiaras/mcmc_WASP-069b_tsiaras_w00000-0c.cfg', 'run_WASP-103b_kreidberg/mcmc_WASP-103b_kreidberg_w00000-0c.cfg', 'run_XO-1b_tsiaras/mcmc_XO-1b_tsiaras_w00000-0c.cfg', ] ntargets = len(cfiles) names = np.array([None for _ in range(ntargets)]) posteriors = [None for _ in range(ntargets)] teq = np.zeros(ntargets) for i in range(ntargets): folder, cfile = os.path.split(cfiles[i]) _, planet, dset = folder.split('_') name = f'{planet} {dset.capitalize()}' names[i] = name.replace('-0', '-') with pt.cd(folder): pyrat = pb.run(cfile, run_step='init', no_logfile=True) teq[i], _ = pa.equilibrium_temp(pyrat.phy.tstar, pyrat.phy.rstar, pyrat.phy.smaxis) with np.load(pyrat.ret.mcmcfile) as mcmc: posteriors[i], zchain, zmask = mc3.utils.burn(mcmc) tposteriors = np.array([post[:, 0] for post in posteriors]) ci1 = np.zeros((2, ntargets)) for i in range(ntargets): pdf, xpdf, HPDmin = mc3.stats.cred_region(tposteriors[i]) ci1[:, i] = -np.amin(xpdf[pdf > HPDmin]), np.amax(xpdf[pdf > HPDmin]) # Print some stats: decimals = [0, 3, 2, 2, 2, 2] split = [None, None, -4, None, None, None, None, -2, -4, None] table = [] for j, posterior in enumerate(posteriors): texts = [] for i in range(len(posterior.T)): dec = decimals[i] text = '' pdf, xpdf, hpd_min = mc3.stats.cred_region(posterior[:, i]) if i == 2 and split[j] is not None: x = xpdf[xpdf < split[j]] p = pdf[xpdf < split[j]] mode = x[np.argmax(p)] lo = mode - np.amin(x[p > hpd_min]) hi = -(mode - np.amax(x[p > hpd_min])) text = f'${mode:.{dec}f}_{{-{lo:.{dec}f}}}^{{+{hi:.{dec}f}}}$ and ' pdf = pdf[xpdf > split[j]] xpdf = xpdf[xpdf > split[j]] mode = xpdf[np.argmax(pdf)] lo = mode - np.amin(xpdf[pdf > hpd_min]) hi = -(mode - np.amax(xpdf[pdf > hpd_min])) text += f'${mode:.{dec}f}_{{-{lo:.{dec}f}}}^{{+{hi:.{dec}f}}}$' texts.append(text) text = " & ".join(texts) table.append(text) table_text = " \\\\\n".join(table) print(table_text) # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: # Temperatures: lw = 2.0 fs = 14 y = np.zeros(ntargets) plt.figure(22, (6.5, 4.75)) plt.clf() plt.subplots_adjust(0.12, 0.1, 0.99, 0.99) ax = plt.subplot(111) plt.errorbar(teq, y, yerr=ci1, capsize=5.0, capthick=lw, lw=lw, ls='none', color='orangered', label='This work') plt.plot(teq, teq, "o", color='mediumblue') plt.ylim(ymin=0) ax.set_xlim(200, 2560) ax.tick_params(labelsize=fs - 2, direction='in', which='both') for xt, name in zip(teq, names): name = name.split()[0] plt.text(xt - 61, xt + 45, name.split()[0], va='bottom', rotation=90, fontsize=fs - 4) ax.set_xlabel('Equilibrium temperature (K)', fontsize=fs) ax.set_ylabel('Retrieved temperature (K)', fontsize=fs) plt.savefig('plots/WFC3_sample_temperature.pdf') # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: # H2O abundances: idx_H2O = 2 H2O_posts = [post[:, idx_H2O] for post in posteriors] nx = 1 ny = ntargets rect = [0.1, 0.06, 0.95, 0.99] margin = 0.0 ranges = [(-12, 0) for _ in range(ntargets)] pnames = ['' for _ in range(ntargets)] fs = 13 plt.figure(32, (6, 8)) plt.clf() axes = [ mc3.plots.subplotter(rect, margin, i + 1, nx, ny) for i in range(ntargets) ] for i in range(ntargets): mc3.plots.histogram(H2O_posts[i], pnames=pnames[i:i + 1], quantile=0.683, ranges=ranges[i:i + 1], nbins=60, axes=axes[i:i + 1], lw=2.0, fs=fs - 1, theme='blue') plt.text(0.02, 0.75, names[i], transform=axes[i].transAxes) if i < ntargets - 1: axes[i].set_xticklabels([]) axes[i].set_yticks([]) axes[i].set_xlabel('$\\log_{10}(X_{\\rm H2O})$', fontsize=fs) axes[4].set_ylabel('Marginal posterior density' + 15 * ' ', fontsize=fs) plt.setp(axes[i].xaxis.get_majorticklabels(), rotation=0) plt.savefig('plots/WFC3_sample_H2O_abundance.pdf')
nprofiles = 3 nlayers = 61 nwave = 17048 data = np.zeros((nmodes, nphase, nspec)) uncerts = np.zeros((nmodes, nphase, nspec)) tpost = np.zeros((nmodes, nphase, nprofiles, nlayers)) posteriors = np.zeros((nmodes, nphase), dtype=object) median_fr = np.zeros((nmodes, nphase, nwave)) hi_fr = np.zeros((nmodes, nphase, nwave)) lo_fr = np.zeros((nmodes, nphase, nwave)) for j, i in product(range(nmodes), range(nphase)): pickle_file = f'MCMC_WASP43b_{modes[j]}_phase{obs_phase[i]:.2f}.pickle' print(f'{modes[j]}: phase {obs_phase[i]:.2f}') with pt.cd('run_feng2020/'): pyrat = io.load_pyrat(pickle_file) posteriors[j, i] = pyrat.ret.posterior[:, -4:] rprs = pyrat.phy.rplanet / pyrat.phy.rstar starflux = pyrat.spec.starflux median_fr[j, i] = pyrat.ret.spec_median / starflux * rprs**2 lo_fr[j, i] = pyrat.ret.spec_low1 / starflux * rprs**2 hi_fr[j, i] = pyrat.ret.spec_high1 / starflux * rprs**2 data[j, i] = pyrat.obs.data uncerts[j, i] = pyrat.obs.uncert ifree = pyrat.ret.pstep[pyrat.ret.itemp] > 0 itemp = np.arange(np.sum(ifree)) tpost[j, i] = pa.temperature_posterior(pyrat.ret.posterior[:, itemp], pyrat.atm.tmodel,
'run_WASP-063b_kilpatrick/mcmc_WASP-063b_kilpatrick_w000h0-mc.cfg', 'run_WASP-063b_tsiaras/mcmc_WASP-063b_tsiaras_w000h0-mc.cfg', 'run_WASP-067b_bruno/mcmc_WASP-067b_bruno_w00000-mc.cfg', 'run_WASP-067b_tsiaras/mcmc_WASP-067b_tsiaras_w00000-mc.cfg', 'run_WASP-069b_tsiaras/mcmc_WASP-069b_tsiaras_w00000-mc.cfg', 'run_WASP-074b_tsiaras/mcmc_WASP-074b_tsiaras_w00000-mc.cfg', 'run_WASP-080b_tsiaras/mcmc_WASP-080b_tsiaras_w00000-mc.cfg', 'run_WASP-101b_tsiaras/mcmc_WASP-101b_tsiaras_w00000-mc.cfg', 'run_WASP-101b_wakeford/mcmc_WASP-101b_wakeford_w00000-mc.cfg', 'run_WASP-103b_kreidberg/mcmc_WASP-103b_kreidberg_wmdm00-mc.cfg', 'run_XO-1b_tsiaras/mcmc_XO-1b_tsiaras_w00000-mc.cfg', ] ntargets = len(cfiles) for cfile in cfiles: folder, cfg = os.path.split(cfile) _, planet, dset = folder.split('_') with pt.cd(folder): pyrat = pb.run(cfg, run_step='init', no_logfile=True) with np.load(pyrat.ret.mcmcfile) as mcmc: post, zchain, zmask = mc3.utils.burn(mcmc) accept_rate = mcmc['acceptance_rate'] pyrat.ret.posterior = post # I want to evaluate less than ~1e5 unique samples: nmax = int(1e5 / (6 * 0.01 * accept_rate)) pyrat.percentile_spectrum(nmax) pfile = pyrat.ret.mcmcfile.replace('.npz', '.pickle') io.save_pyrat(pyrat, pfile)
def test_pf_tips_outfile(outfile): with pt.cd('outputs/'): pf_data, isotopes, temp = pf.tips('HCN', outfile=outfile) assert 'PF_tips_HCN.dat' in os.listdir('outputs/') os.remove('outputs/PF_tips_HCN.dat')