def simulation_example(path, zi=6., name=None): ''' Sometimes, it's useful to start with a simulation object, and then load your snapshot ''' import seren3 sim = None # init # Load our simulation if (name is not None): # load by name sim = seren3.load(name) else: # Just init from the path sim = seren3.init(path) print sim # Now, lets load the snapshot which is closest to the desired redshift ioutput = sim.redshift(zi) # output number snapshot = sim[ioutput] # There are also interpolators for age -> redshift and vise versa, for our chosen cosmology age_func = sim.age_func(zmax=100., zmin=0.) z_func = sim.redshift_func(zmax=100., zmin=0.) age_of_universe = age_func(snapshot.z) redshift = z_func(age_of_universe) print snapshot.z, redshift, age_of_universe
def plot_RT2_panels(**kwargs): import seren3 sim = seren3.load("RT2_nohm") ioutputs = [106, 100, 90, 80, 70, 60] plot_panels(sim, ioutputs, 2, 3, **kwargs)
def _tmp_2(): import seren3 sim_names = ["RT2_nohm", "RT5_nohm"] sims = [seren3.load(n) for n in sim_names] ioutputs = [42, 48, 60, 70, 80, 90, 100, 106] ppaths = ["%s/pickle/" % sim.path for sim in sims] cols = ["r", "b"] plot_Mc_var(sims, [ioutputs, ioutputs], ppaths, sim_names, cols, False, **kwargs)
def plot_Mc_z_xHII_by_names(simnames, labels, colours, NN, fix_alpha=False, **kwargs): import seren3 sims = [seren3.load(name) for name in simnames] ioutputs = [[42, 48, 60, 70, 80, 90, 100, 106] for n in simnames] pickle_paths = ["%s/pickle/" % sim.path for sim in sims] ann_paths = [sim.path for sim in sims] plot_Mc_z_xHII(sims, ioutputs, pickle_paths, ann_paths, labels, \ colours, NN, fix_alpha=False, **kwargs)
# coding: utf-8 # In[39]: import seren3 iout = 108 # sim = seren3.load("RT2") sin_sim = seren3.load("BPASS_SIN") bin_sim = seren3.load("BPASS_BIN") sin_snap = sin_sim[iout] bin_snap = bin_sim[iout] cosmo = sin_snap.cosmo import matplotlib import matplotlib.pylab as plt from seren3.analysis.plots import fit_scatter, fit_median nbins = 5 # In[60]: import numpy as np import pickle import random from seren3.scripts.mpi import write_fesc_hid_dict def load_fesc(snapshot):
import seren3 iout = 108 # sim = seren3.load("RT2") sin_sim = seren3.load("2Mpc_BPASS_SIN") bin_sim = seren3.load("2Mpc_BPASS_BIN") sin_snap = sin_sim[iout] bin_snap = bin_sim[iout] cosmo = sin_snap.cosmo import matplotlib import matplotlib.pylab as plt from seren3.analysis.plots import fit_scatter, fit_median nbins = 5 # In[60]: import numpy as np import pickle import random from seren3.scripts.mpi import write_fesc_hid_dict def load_fesc(snapshot): db = write_fesc_hid_dict.load_db(snapshot.path, snapshot.ioutput) hids = db.keys() mvir = np.zeros(len(hids)) fesc = np.zeros(len(hids)) nphotons = np.zeros(len(hids))
def test2(sim_labels, the_mass_bins=[7., 8., 9., 10.], **kwargs): import numpy as np import matplotlib.pylab as plt from seren3.array import SimArray from pymses.utils import constants as C import matplotlib.gridspec as gridspec import seren3 sim1 = seren3.load("RT2_nohm") sim2 = seren3.load("RAMSES") snap1 = sim1[106] snap2 = sim2[93] info = snap1.info cols = None if "cols" not in kwargs: from seren3.utils import plot_utils cols = plot_utils.ncols(len(the_mass_bins), cmap=kwargs.pop("cmap", "Set1"))[::-1] # cols = plot_utils.ncols(2, cmap=kwargs.pop("cmap", "Set1"))[::-1] else: cols = kwargs.pop("cols") z = (1. / info["aexp"]) - 1. fig = plt.figure(figsize=(10, 8)) gs = gridspec.GridSpec(4, 9, wspace=0., hspace=0.) ax1 = fig.add_subplot(gs[1:-1, :4]) ax2 = fig.add_subplot(gs[:1, :4], sharex=ax1) axs = np.array([ax1, ax2]) nbins = 7 ax = ax1 binned_cdf1 = plot(snap1.path, snap1.ioutput, "%s/pickle/" % snap1.path, "nHI", ax=None, nbins=nbins) binned_cdf2 = plot(snap2.path, snap2.ioutput, "%s/pickle/" % snap2.path, "nHI", ax=None, nbins=nbins) text_pos = (7.2, 0.1) text = 'z = %1.2f' % z # ax.text(text_pos[0], text_pos[1], text, color="k", size=18) # for bcdf, ls in zip([binned_cdf1, binned_cdf2], ["-", "--"]): # label = "All" # x,y,std,stderr,n = bcdf["all"] # e = ax.errorbar(x, y, yerr=std, color="k", label=label,\ # fmt="o", markerfacecolor="w", mec='k', capsize=2, capthick=2, elinewidth=2, linestyle=ls, linewidth=2.) for bcdf, ls in zip([binned_cdf1, binned_cdf2], ["-", "--"]): for ibin, c in zip(range(len(the_mass_bins)), cols): # for ibin, c in zip([0, len(the_mass_bins)-1], cols): x, y, std, stderr, n = bcdf[ibin] if ibin == len(the_mass_bins) - 1: upper = r"$\infty$" else: upper = "%i" % the_mass_bins[ibin + 1] lower = "%i" % the_mass_bins[ibin] label = "[%s, %s)" % (lower, upper) if (ls == "--"): label = None upper_err = [] lower_err = [] for i in range(len(y)): ue = 0. le = 0. if (y[i] + std[i] > 1): ue = 1. - y[i] else: ue = std[i] if (y[i] - std[i] < 0): le = y[i] else: le = std[i] upper_err.append(ue) lower_err.append(le) yerr = [lower_err, upper_err] e = ax.errorbar(x, y, yerr=yerr, color=c, label=label,\ fmt="o", markerfacecolor=c, mec='k', capsize=2, capthick=2, elinewidth=2, linestyle=ls, linewidth=2.) for sl, ls in zip(sim_labels, ["-", "--"]): ax.plot([2, 4], [-100, -100], linewidth=2., linestyle=ls, color="k", label=sl) # ax.legend(prop={"size":18}, loc="lower right") ax.set_ylim(-0.05, 1.2) ax = ax2 linestyles = ["-", "--", "-.", ":"] for ibin, c, ls in zip(range(len(the_mass_bins)), cols, linestyles): # for ibin, c, ls in zip([0, len(the_mass_bins)-1], cols, linestyles): x1, y1, std1, stderr1, n1 = binned_cdf1[ibin] x2, y2, std2, stderr2, n2 = binned_cdf2[ibin] # ydiv = y1/y2 ydiv = y1 - y2 # error_prop = np.abs(ydiv) * np.sqrt( (std1/y1)**2 + (std2/y2)**2 ) error_prop = np.sqrt((std1)**2 + (std2)**2) print ibin, error_prop e = ax.errorbar(x1, ydiv, yerr=error_prop, color=c,\ fmt="o", markerfacecolor=c, mec='k', capsize=2, capthick=2, elinewidth=2, linestyle=ls, linewidth=2.) axs[0].set_xlabel(r"log$_{10}$ n$_{\mathrm{HI}}$ [m$^{-3}$]") # axs[0].set_xlabel(r"log$_{10}$ T [K]") axs[0].set_ylabel(r"CDF") axs[1].set_ylabel(r"$\Delta$ CDF") plt.setp(ax2.get_xticklabels(), visible=False) # ax = axs[0] # leg = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), # fancybox=True, shadow=False, ncol=3, prop={"size":18}) # leg.set_title(r"log$_{10}$(Mvir) [M$_{\odot}$/h]", prop = {'size':'x-large'}) n_star = SimArray(info["n_star"].express(C.H_cc), "cm**-3").in_units("m**-3") for axi in axs.flatten(): axi.vlines(np.log10(n_star), -1, 2, color='k', linestyle='-.') # axi.vlines(np.log10(2e4), -5, 5, color='k', linestyle='-.') axs[1].set_ylim(-0.2, 1.3) # axs[1].set_ylim(0.2, -1.3) ################################################################################## ax1 = fig.add_subplot(gs[1:-1, 5:]) ax2 = fig.add_subplot(gs[:1, 5:], sharex=ax1) axs = np.array([ax1, ax2]) ax = ax1 binned_cdf1 = plot(snap1.path, snap1.ioutput, "%s/pickle/" % snap1.path, "T2_minus_Tpoly", ax=None, nbins=nbins) binned_cdf2 = plot(snap2.path, snap2.ioutput, "%s/pickle/" % snap2.path, "T2_minus_Tpoly", ax=None, nbins=nbins) text_pos = (7.2, 0.1) text = 'z = %1.2f' % z # ax.text(text_pos[0], text_pos[1], text, color="k", size=18) # for bcdf, ls in zip([binned_cdf1, binned_cdf2], ["-", "--"]): # label = "All" # x,y,std,stderr,n = bcdf["all"] # e = ax.errorbar(x, y, yerr=std, color="k", label=label,\ # fmt="o", markerfacecolor="w", mec='k', capsize=2, capthick=2, elinewidth=2, linestyle=ls, linewidth=2.) for bcdf, ls in zip([binned_cdf1, binned_cdf2], ["-", "--"]): for ibin, c in zip(range(len(the_mass_bins)), cols): # for ibin, c in zip([0, len(the_mass_bins)-1], cols): x, y, std, stderr, n = bcdf[ibin] if ibin == len(the_mass_bins) - 1: upper = r"$\infty$" else: upper = "%i" % the_mass_bins[ibin + 1] lower = "%i" % the_mass_bins[ibin] label = "[%s, %s)" % (lower, upper) if (ls == "--"): label = None upper_err = [] lower_err = [] for i in range(len(y)): ue = 0. le = 0. if (y[i] + std[i] > 1): ue = 1. - y[i] else: ue = std[i] if (y[i] - std[i] < 0): le = y[i] else: le = std[i] upper_err.append(ue) lower_err.append(le) yerr = [lower_err, upper_err] e = ax.errorbar(x, y, yerr=yerr, color=c, label=label,\ fmt="o", markerfacecolor=c, mec='k', capsize=2, capthick=2, elinewidth=2, linestyle=ls, linewidth=2.) for sl, ls in zip(sim_labels, ["-", "--"]): ax.plot([2, 4], [-100, -100], linewidth=2., linestyle=ls, color="k", label=sl) # ax.legend(prop={"size":18}, loc="lower right") ax.set_ylim(-0.05, 1.2) ax = ax2 linestyles = ["-", "--", "-.", ":"] for ibin, c, ls in zip(range(len(the_mass_bins)), cols, linestyles): # for ibin, c, ls in zip([0, len(the_mass_bins)-1], cols, linestyles): x1, y1, std1, stderr1, n1 = binned_cdf1[ibin] x2, y2, std2, stderr2, n2 = binned_cdf2[ibin] # ydiv = y1/y2 ydiv = y1 - y2 # error_prop = np.abs(ydiv) * np.sqrt( (std1/y1)**2 + (std2/y2)**2 ) error_prop = np.sqrt((std1)**2 + (std2)**2) print ibin, error_prop e = ax.errorbar(x1, ydiv, yerr=error_prop, color=c,\ fmt="o", markerfacecolor=c, mec='k', capsize=2, capthick=2, elinewidth=2, linestyle=ls, linewidth=2.) # axs[0].set_xlabel(r"log$_{10}$ n$_{\mathrm{HI}}$ [m$^{-3}$]") axs[0].set_xlabel(r"log$_{10}$ T - T$_{\mathrm{J}}$ [K/$\mu$]") # axs[0].set_ylabel(r"CDF") # axs[1].set_ylabel(r"$\Delta$ CDF") plt.setp(ax2.get_xticklabels(), visible=False) ax = axs[0] leg = ax.legend(loc='upper center', bbox_to_anchor=(-0.15, -0.2), fancybox=True, shadow=False, ncol=3, prop={"size": 18}) leg.set_title(r"log$_{10}$(Mvir) [M$_{\odot}$/h] : z = %1.2f" % snap1.z, prop={'size': 'x-large'}) n_star = SimArray(info["n_star"].express(C.H_cc), "cm**-3").in_units("m**-3") for axi in axs.flatten(): # axi.vlines(np.log10(n_star), -1, 2, color='k', linestyle='-.') axi.vlines(np.log10(2e4), -5, 5, color='k', linestyle='-.') # axs[1].set_ylim(-0.2, 1.3) axs[1].set_ylim(0.2, -1.3) # plt.yscale("log") # plt.tight_layout() plt.savefig("/home/ds381/rt2_hd_cdf_nHI_T_z6_15.pdf", format="pdf") plt.show()
def plot_fb_panels(sim_name, ioutputs, sim_label, weight="mw", weight_label="M", **kwargs): import numpy as np import seren3 import matplotlib.pylab as plt from seren3.analysis.plots import fit_scatter fig, axes = plt.subplots(nrows=4, ncols=len(ioutputs), sharex=True, sharey=True, figsize=(10, 10)) fig.subplots_adjust(hspace=0.05) fig.subplots_adjust(wspace=0.05) sim = seren3.load(sim_name) for i in range(len(ioutputs)): # ioutput = sim.redshift(zi) ioutput = ioutputs[i] snapshot = sim[ioutput] pickle_path = "%s/pickle/" % snapshot.path axs = axes[:, i] axs[0].set_title("z = %1.2f" % (snapshot.z)) log_mvir, fb, ftidal, xHII, T, T_U, pid = neural_net2.load_training_arrays( snapshot, pickle_path=pickle_path, weight=weight) cosmo = snapshot.cosmo cosmic_mean_b = cosmo["omega_b_0"] / cosmo["omega_M_0"] y_min = 0. y_max = (fb / cosmic_mean_b).max() fb_cosmic_mean = fb / cosmic_mean_b mvir = 10**log_mvir log_xHI = np.log10(1. - xHII) log_ftidal = np.log10(ftidal) bc, mean, std, sterr = fit_scatter(log_mvir, fb_cosmic_mean, nbins=10, ret_sterr=True) def _plot(mvir, fb, carr, ax, **kwargs): ax.errorbar(10**bc, mean, yerr=std, linewidth=3., color="k", linestyle="--") return ax.scatter(mvir, fb, c=carr, **kwargs) labels = [r"log$_{10} \langle F_{\mathrm{tidal}} \rangle_{t_{\mathrm{dyn}}}$",\ r"$\langle x_{\mathrm{HII}} \rangle_{\mathrm{%s}}$" % weight_label,\ # r"log$_{10} \langle x_{\mathrm{HII}} \rangle_{\mathrm{%s}}$" % weight_label,\ r"log$_{10} \langle T \rangle_{\mathrm{%s}}$/$T_{\mathrm{vir}}$" % weight_label ,\ r"log$_{10}$ T/|U|"] count = 0 cbar = None for ax, carr, lab in zip(axs.flatten(), [np.log10(ftidal), xHII, T, T_U], labels): ax.errorbar(10**bc, mean, yerr=std, linewidth=1.5, color="k", linestyle="--") plt.xlim(5e6, 1.5e10) sp = None if (count == 0): sp = ax.scatter(mvir, fb_cosmic_mean, c=carr, vmin=-1, vmax=4, **kwargs) if (count == 2): sp = ax.scatter(mvir, fb_cosmic_mean, c=carr, vmin=-0.5, vmax=1.5, **kwargs) else: sp = ax.scatter(mvir, fb_cosmic_mean, c=carr, **kwargs) if (count == 1): cbar = fig.colorbar(sp, ax=ax, ticks=[1., 0.75, 0.5, 0.25, 0.]) else: cbar = fig.colorbar(sp, ax=ax) if (i == len(ioutputs) - 1): cbar.set_label(lab) # ax.set_xlabel(r"log$_{10}$ M$_{\mathrm{vir}}$ [M$_{\odot}$/h]") if (i == 0): ax.set_ylabel( r"f$_{\mathrm{b}}$[$\Omega_{\mathrm{b}}$/$\Omega_{\mathrm{M}}$]" ) ax.set_xscale("log") count += 1 ax.set_ylim(y_min, y_max) axs[-1].set_xlabel(r"M$_{\mathrm{vir}}$ [M$_{\odot}$/h]") for ax in axes.flatten(): ax.set_xlim(5e6, 1.5e10) fig.tight_layout()