def add_population_flux(axis, sn_type, pdf_type, gamma_dict, gamma, time_key, **kwargs): sens, sens_e, energy = gamma_dict.get(gamma, [np.nan, np.nan, np.nan]) if np.isnan(sens): logging.warning(f"No sensitivity for {sn_type}, {pdf_type}") z = kwargs.get("z", 2.5) pop_sens_flux = get_population_flux(energy * u.erg, get_ccsn_rate_type_function(sn_type), gamma, z) logging.debug(pop_sens_flux) default_label = (f"{float(time_key) / 364.25} yr {pdf_type}" if pdf_type == "decay" else f"{time_key} day {pdf_type}") perc = pop_sens_flux / get_diffuse_flux_at_1GeV()[0] logging.debug(f"percentage of diffuse flux: {perc.value * 100:.2f}%") default_label += f": {perc.value * 100:.1f}%" sens_flux_time_esquared = (pop_sens_flux * energy_range_gev**(-1 * gamma) * energy_range_gev**2) axis.plot( energy_range_gev, sens_flux_time_esquared, label=kwargs.get("label", default_label), ls=kwargs.get("ls", ""), color=get_sn_color(sn_type), ) return axis
def plot_sensitivity_flux_per_type(filename, gamma): plot_dict = dict() for i, (sn_type, pdf_dict) in enumerate(sn_times.items()): logging.debug(f'sntype is {sn_type}') plot_dict[sn_type] = dict() for j, pdf_type in enumerate(pdf_dict): logging.debug(f'pdf type {pdf_type}') stacked_sens_flux = get_stacked_sens_dict(pdf_type).get(sn_type, dict()) for k, (time_key, time_dict) in enumerate(stacked_sens_flux.items()): logging.debug(f'time key {time_key}') t = float(time_key)/364.25 if 'decay' in pdf_type else float(time_key) pdf_name = pdf_names(pdf_type, t) logging.debug(f'pdf name {pdf_name}') res_list = time_dict.get(gamma, list()) if len(res_list) < 1: logging.warning(f'No sensitivity results for type {sn_type}, gamma {gamma}, {pdf_name}') else: plot_dict[sn_type][pdf_name] = res_list fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(5.85*2, 3.6154988341868854 + 0.1), gridspec_kw={'hspace': 0, 'wspace': 0}, sharey='all') for i, (sn_type, pdf_dict) in enumerate(plot_dict.items()): logging.debug(f'plotting {sn_type} sensitivity') x = np.arange(len(pdf_dict.keys())) for pdf_key, res in pdf_dict.items(): logging.debug(pdf_key) y = res[0] yerr = np.array([(res[1][0], res[1][1])]).T logging.debug(f'y={y}, yerr={yerr} with shape {np.shape(yerr)}') axs[i].errorbar([pdf_key], y, yerr=yerr, color=get_sn_color(sn_type), ls='', marker='o', ms='2', capsize=5) axs[i].set_yscale('log') axs[i].set_xticks(x) axs[i].set_xticklabels(pdf_dict.keys(), rotation=45) axs[i].set_title(f'$\gamma$={gamma:.1f}\n\n{sn_type}' if i==1 else sn_type) axs[i].grid() axs[0].set_ylabel('Sensitivity flux @ 1GeV \n[GeV$^{-1}$ s$^{-1}$ cm$^{-2}$]') axs[1].set_xlabel('Time PDF') # fig.suptitle(rf'$\gamma$ = {gamma:.1f}') fig.tight_layout() logging.debug(f'saving under {filename}') fig.savefig(filename) plt.close()
def add_rate_sntype(axis, sn_type, zrange, normed=False, **kwargs): rate_fct = get_ccsn_rate_type_function(sn_type) rate = np.array([rate_fct(z) for z in zrange]) norm_to = quad(rate_fct, 0, 2.3)[0] if normed else 1 rate /= norm_to axis.plot(zrange, rate, color=kwargs.get('color', get_sn_color(sn_type)), label=kwargs.get('label', sn_type), **kwargs) return axis
def add_cumulative_rate_sntype(axis, sn_type, zrange, normed=False, **kwargs): rate_fct = get_ccsn_rate_type_function(sn_type) rate = [rate_fct(z) for z in zrange] cum_rate = np.cumsum(rate) norm_to = quad(rate_fct, 0, 2.3)[0] if normed else 1 cum_rate /= norm_to axis.plot( zrange, cum_rate, color=kwargs.get("color", get_sn_color(sn_type)), label=kwargs.get("label", sn_type), **kwargs, ) return axis
def add_cumulative_flux_sntype(axis, gamma_dict, gamma, sn_type, rate_fct, zrange, **kwargs): rate_fct = get_ccsn_rate_type_function(sn_type) sens, sens_e, energy = gamma_dict.get(gamma, [np.nan, np.nan, np.nan]) f = list() for z in zrange: f.append(get_population_flux(energy * u.erg, rate_fct, gamma, z)) def integrand(zz): return get_population_flux(energy * u.erg, rate_fct, gamma, zz) norm_to = quad(integrand, 0, 2.3)[0] f = np.array(f) / norm_to axis.plot( zrange, f, label=kwargs.get("label", sn_type), color=kwargs.get("color", get_sn_color(sn_type)), **kwargs, ) return axis
yerr = [ stacked_sens_flux[cat_name][key][gamma][1] for key in cat_dict.keys() ] yerr = np.array(yerr)[np.argsort(x_raw)] logging.debug("\n x: {0}; y: {1}; yerr: {2}".format(x, y, yerr)) flux_ax.errorbar( x, y, yerr=np.array(yerr).T, ls="--", label=cat_name, marker="", color=get_sn_color(cat_name), ) flux_ax.set_xscale("log") flux_ax.set_yscale("log") flux_ax.set_xlabel(r"$t_{\mathrm{decay}}$ in years") flux_ax.set_ylabel(r"flux in GeV$^{-1}$ s$^{-1}$ cm$^{-2}$") flux_ax.legend() fname_flux = plot_output_dir( raw + f"stacked_sensitivity_flux_gamma{gamma}_{mh_name}.pdf" ) logging.debug(f"saving figure under {fname_flux}") flux_fig.savefig(fname_flux) stacked_sens = {
# loop over SN catalogues and plot the sensitivities against the decay length for cat_name, cat_dict in stacked_sens_flux.items(): x_raw = [float(key) for key in cat_dict.keys()] x = np.array(x_raw)[np.argsort(x_raw)] / 364.25 y = [stacked_sens_flux[cat_name][key][gamma][0] for key in cat_dict.keys()] y = np.array(y)[np.argsort(x_raw)] yerr = [stacked_sens_flux[cat_name][key][gamma][1] for key in cat_dict.keys()] yerr = np.array(yerr)[np.argsort(x_raw)] logging.debug('\n x: {0}; y: {1}; yerr: {2}'.format(x, y, yerr)) flux_ax.errorbar(x, y, yerr=np.array(yerr).T, ls='--', label=cat_name, marker='', color=get_sn_color(cat_name)) flux_ax.set_xscale('log') flux_ax.set_yscale('log') flux_ax.set_xlabel(r'$t_{\mathrm{decay}}$ in years') flux_ax.set_ylabel(r'flux in GeV$^{-1}$ s$^{-1}$ cm$^{-2}$') flux_ax.legend() fname_flux = plot_output_dir(raw + f'stacked_sensitivity_flux_gamma{gamma}_{mh_name}.pdf') logging.debug(f'saving figure under {fname_flux}') flux_fig.savefig(fname_flux) stacked_sens = { cat_name: np.array( [(stacked_sens_flux[cat_name][time_key][gamma][2], float(time_key) / 364.25) for time_key in stacked_sens_flux[cat_name]], dtype='<f8'
def add_population_flux(axis, sn_type, pdf_type, gamma_dict, gamma, time_key, **kwargs): sens, sens_e, energy = gamma_dict.get(gamma, [np.nan, np.nan, np.nan]) if np.isnan(sens): logging.warning(f'No sensitivity for {sn_type}, {pdf_type}') z = kwargs.get('z', 2.5) pop_sens_flux = get_population_flux(energy * u.erg, get_ccsn_rate_type_function(sn_type), gamma, z) logging.debug(pop_sens_flux) default_label = f'{float(time_key) / 364.25} yr {pdf_type}' if pdf_type == 'decay' else \ f'{time_key} day {pdf_type}' perc = pop_sens_flux/get_diffuse_flux_at_1GeV()[0] logging.debug(f'percentage of diffuse flux: {perc.value * 100:.2f}%') default_label += f': {perc.value * 100:.1f}%' sens_flux_time_esquared = pop_sens_flux * energy_range_gev ** (-1 * energy_gamma) * \ energy_range_gev ** 2 axis.plot(energy_range_gev, sens_flux_time_esquared, label=kwargs.get('label', default_label), ls=kwargs.get('ls', ''), color=get_sn_color(sn_type)) return axis
catalogue["distance_mpc"] = np.array([1] * len(catalogue)) w = [1] * len(catalogue) else: w = 1 / catalogue["distance_mpc"]**2 w = w / sum(w) logging.debug(f"{Table(catalogue)}") this_cat_path = f'{cat_path.split(".npy")[0]}_used.npy' np.save(this_cat_path, catalogue) logging.info(f'max distance = {max(catalogue["distance_mpc"])}') fig, ax = plt.subplots() for ra, dec, w in zip(catalogue["ra_rad"], catalogue["dec_rad"], w): ax.scatter(ra, dec, alpha=w, color=get_sn_color(cat)) ax.scatter( catalogue["ra_rad"], catalogue["dec_rad"], edgecolors=get_sn_color(cat), facecolors="none", ) ax.set_xlabel("RA in rad") ax.set_ylabel("DEC in rad") ax.set_title("SNe " + cat) fn = plot_output_dir(f"{raw}/{cat}_skymap.pdf") logging.debug("saving figure under {0}".format(fn)) fig.savefig(fn) closest_src = np.sort(catalogue, order="distance_mpc")[0] cat_res = dict()
catalogue['distance_mpc'] = np.array([1] * len(catalogue)) w = [1] * len(catalogue) else: w = 1/catalogue['distance_mpc']**2 w = w/sum(w) logging.debug(f'{Table(catalogue)}') this_cat_path = f'{cat_path.split(".npy")[0]}_used.npy' np.save(this_cat_path, catalogue) logging.info(f'max distance = {max(catalogue["distance_mpc"])}') fig, ax = plt.subplots() for ra, dec, w in zip(catalogue['ra_rad'], catalogue['dec_rad'], w): ax.scatter(ra, dec, alpha=w, color=get_sn_color(cat)) ax.scatter(catalogue['ra_rad'], catalogue['dec_rad'], edgecolors=get_sn_color(cat), facecolors='none') ax.set_xlabel('RA in rad') ax.set_ylabel('DEC in rad') ax.set_title('SNe ' + cat) fn = plot_output_dir(f'{raw}/{cat}_skymap.pdf') logging.debug("saving figure under {0}".format(fn)) fig.savefig(fn) closest_src = np.sort(catalogue, order="distance_mpc")[0] cat_res = dict() for gamma in gammas: injection_energy['gamma'] = gamma