Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
def plot_difference_individual(sn_types, filename):

    fig, axs = plt.subplots(len(sn_types))

    for i, sn_type in enumerate(sn_types):
        ax = axs[i]
        N = {}

        cats = []
        for pdf_type in sn_times[sn_type]:
            for pdf_time in sn_times[sn_type][pdf_type]:
                cats.append(pdf_names(pdf_type, pdf_time))

        for flagged in [True, False]:
            N["flagged" if flagged else "unflagged"] = []

            for cat in cats:
                catarr = np.load(
                    updated_sn_catalogue_name(sn_type,
                                              pdf_name=cat,
                                              flagged=flagged))
                N["flagged" if flagged else "unflagged"].append(len(catarr))

        x = np.arange(len(cats))
        width = 0.4

        r1 = ax.bar(x + width / 2, N["unflagged"], width, label="new")
        r2 = ax.bar(x - width / 2, N["flagged"], width, label="old")

        ax.set_ylabel(f"SN type {sn_type}")
        ax.set_xticks(x)
        ax.set_xticklabels(cats)
        ax.set_ylim(np.array(ax.get_ylim()) * [1, 1.1])

        autolabel(r1, ax)
        autolabel(r2, ax)

    axs[-1].set_xlabel("PDF type")
    axs[0].set_title("catalogues with wrong classifications")
    axs[-1].legend()

    fig.savefig(filename)
    plt.close()
Ejemplo n.º 3
0
        # get the time pdfs for this catalogue
        time_pdfs = sn_time_pdfs(cat, pdf_type=pdf_type)

        # Loop over time PDFs
        for llh_time in time_pdfs:

            # set up an empty results array for this time pdf
            time_res = dict()

            logging.debug(f'time_pdf is {llh_time}')

            time_key = str(llh_time['decay_time'])

            pdf_time = llh_time['decay_time'] / 364.25
            pdf_name = pdf_names(pdf_type, pdf_time)
            cat_path = updated_sn_catalogue_name(cat)  # , pdf_name=pdf_name)
            logging.debug('catalogue path: ' + str(cat_path))

            # load catalogue and select the closest source
            # that serves for estimating a good injection scale later
            catalogue = np.load(cat_path)
            logging.debug('catalogue dtype: ' + str(catalogue.dtype))
            closest_src = np.sort(catalogue, order="distance_mpc")[0]

            time_name = name + time_key + "/"

            # set up the likelihood dictionary
            llh_dict = {
                "llh_name": "standard",
                "llh_energy_pdf": llh_energy,