def main():

    custom_config = {}
    if args.config_file is not None:
        try:
            custom_config = read_configuration_file(args.config_file)
        except ("Custom configuration could not be loaded !!!"):
            pass

    config = replace_config(standard_config, custom_config)

    reg_energy, reg_disp_vector, cls_gh = dl1_to_dl2.build_models(
        args.gammafile,
        args.protonfile,
        save_models=args.storerf,
        path_models=args.path_models,
        custom_config=config,
    )

    gammas = filter_events(
        pd.read_hdf(args.gammatest, key=dl1_params_lstcam_key),
        config["events_filters"],
    )
    proton = filter_events(
        pd.read_hdf(args.protontest, key=dl1_params_lstcam_key),
        config["events_filters"],
    )

    data = pd.concat([gammas, proton], ignore_index=True)

    dl2 = dl1_to_dl2.apply_models(data,
                                  cls_gh,
                                  reg_energy,
                                  reg_disp_vector,
                                  custom_config=config)

    ####PLOT SOME RESULTS#####

    gammas = dl2[dl2.gammaness >= 0.5]
    protons = dl2[dl2.gammaness < 0.5]
    gammas.reco_type = 0
    protons.reco_type = 1

    focal_length = 28 * u.m
    src_pos_reco = utils.reco_source_position_sky(
        gammas.x.values * u.m, gammas.y.values * u.m,
        gammas.reco_disp_dx.values * u.m, gammas.reco_disp_dy.values * u.m,
        focal_length, gammas.mc_alt_tel.values * u.rad,
        gammas.mc_az_tel.values * u.rad)

    plot_dl2.plot_features(dl2)
    plt.show()

    plot_dl2.plot_e(gammas, 10, 1.5, 3.5)
    plt.show()

    plot_dl2.calc_resolution(gammas)
    plt.show()

    plot_dl2.plot_e_resolution(gammas, 10, 1.5, 3.5)
    plt.show()

    plot_dl2.plot_disp_vector(gammas)
    plt.show()

    try:
        ctaplot.plot_theta2(
            gammas.mc_alt,
            np.arctan(np.tan(gammas.mc_az)),
            src_pos_reco.alt.rad,
            np.arctan(np.tan(src_pos_reco.az.rad)),
            bins=50,
            range=(0, 1),
        )
        plt.show()
        ctaplot.plot_angular_res_per_energy(
            src_pos_reco.alt.rad, np.arctan(np.tan(src_pos_reco.az.rad)),
            gammas.mc_alt, np.arctan(np.tan(gammas.mc_az)), gammas.mc_energy)
        plt.show()
    except:
        pass

    regression_features = config["regression_features"]
    classification_features = config["classification_features"]

    plt.show()
    plot_dl2.plot_pos(dl2)
    plt.show()
    plot_dl2.plot_ROC(cls_gh, dl2, classification_features, -1)
    plt.show()
    plot_dl2.plot_importances(cls_gh, classification_features)
    plt.show()
    plot_dl2.plot_importances(reg_energy, regression_features)
    plt.show()
    plot_dl2.plot_importances(reg_disp_vector, regression_features)
    plt.show()

    plt.hist(dl2[dl2['mc_type'] == 101]['gammaness'], bins=100)
    plt.hist(dl2[dl2['mc_type'] == 0]['gammaness'], bins=100)
    plt.show()
def main():
    ntelescopes_gamma = 4
    ntelescopes_protons = 4
    n_bins_energy = 20  #  Number of energy bins
    n_bins_gammaness = 11  #  Number of gammaness bins
    n_bins_theta2 = 10  #  Number of theta2 bins
    obstime = 50 * 3600 * u.s
    noff = 5

    energy, best_sens, result, units, gcut, tcut = find_best_cuts_sensitivity(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_cuts,
        args.dl2_file_p_cuts, ntelescopes_gamma, ntelescopes_protons,
        n_bins_energy, n_bins_gammaness, n_bins_theta2, noff, obstime)

    # For testing using fixed cuts
    # gcut = np.ones(eb) * 0.8
    # tcut = np.ones(eb) * 0.01

    energy, best_sens, result, units, dl2 = sensitivity(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_sens,
        args.dl2_file_p_sens, 1, 1, 20, gcut, tcut * (u.deg**2), noff, obstime)

    dl2.to_hdf('test_sens.h5', key='data')
    result.to_hdf('test_sens.h5', key='results')

    tab = Table.from_pandas(result)

    for i, key in enumerate(tab.columns.keys()):
        tab[key].unit = units[i]
        if key == 'sensitivity':
            continue
        tab[key].format = '8f'

    egeom = np.sqrt(energy[1:] * energy[:-1])

    plt.plot(egeom[:-1], tab['hadron_rate'], label='Hadron rate', marker='o')
    plt.plot(egeom[:-1], tab['gamma_rate'], label='Gamma rate', marker='o')
    plt.legend()
    plt.xscale('log')
    plt.xlabel('Energy (GeV)')
    plt.ylabel('events / min')
    plt.show()

    gammas_mc = dl2[dl2.mc_type == 0]
    protons_mc = dl2[dl2.mc_type == 101]

    sns.distplot(gammas_mc.gammaness, label='gammas')
    sns.distplot(protons_mc.gammaness, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    sns.distplot(gammas_mc.mc_energy, label='gammas')
    sns.distplot(protons_mc.mc_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()

    sns.distplot(gammas_mc.reco_energy.apply(np.log10), label='gammas')
    sns.distplot(protons_mc.reco_energy.apply(np.log10), label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    ctaplot.plot_theta2(gammas_mc.reco_alt,
                        gammas_mc.reco_az,
                        gammas_mc.mc_alt,
                        gammas_mc.mc_az,
                        range=(0, 1),
                        bins=100)
    plt.show()
    plt.figure(figsize=(12, 8))
    ctaplot.plot_angular_res_per_energy(
        gammas_mc.reco_alt,
        gammas_mc.reco_az,
        gammas_mc.mc_alt,
        gammas_mc.mc_az,
        10**(gammas_mc.reco_energy - 3),
    )

    ctaplot.plot_angular_res_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()

    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_resolution(gammas_mc.mc_energy, gammas_mc.reco_energy)
    ctaplot.plot_energy_resolution_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()

    ctaplot.plot_energy_resolution(gammas_mc.mc_energy, gammas_mc.reco_energy)
    ctaplot.plot_energy_bias(10**(gammas_mc.mc_energy - 3),
                             10**(gammas_mc.reco_energy - 3))
    plt.show()

    gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl1file_gammas)
    emin = gamma_ps_simu_info.energy_range_min.value
    emax = gamma_ps_simu_info.energy_range_max.value
    total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse
    spectral_index = gamma_ps_simu_info.spectral_index
    area = (gamma_ps_simu_info.max_scatter_range.value -
            gamma_ps_simu_info.min_scatter_range.value)**2 * np.pi
    ctaplot.plot_effective_area_per_energy_power_law(
        emin,
        emax,
        total_number_of_events,
        spectral_index,
        10**(gammas_mc.reco_energy - 3)[gammas_mc.tel_id == 1],
        area,
        label='selected gammas',
        linestyle='--')

    ctaplot.plot_effective_area_cta_requirements('north', color='black')
    plt.legend()
    plt.tight_layout()
    plt.show()
Exemple #3
0
def main():

    nfiles_gammas = 0.5  # 100*0.5 #Pointlike gammas
    nfiles_protons = 0.5  # 5000*0.8*0.5

    eb = 20  # Number of energy bins
    gb = 11  # Number of gammaness bins
    tb = 10  # Number of theta2 bins
    obstime = 50 * 3600 * u.s
    noff = 5

    E, best_sens, result, units, gcut, tcut = sensitivity.find_best_cuts_sens(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_cuts,
        args.dl2_file_p_cuts, nfiles_gammas, nfiles_protons, eb, gb, tb, noff,
        obstime)
    E, best_sens, result, units, dl2 = sensitivity.sens(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_sens,
        args.dl2_file_p_sens, nfiles_gammas, nfiles_protons, eb, gcut,
        tcut * (u.deg**2), noff, obstime)
    # plt.show()
    plot_utils.sens_plot(eb, E, best_sens)
    plt.show()

    tab = Table.from_pandas(result)

    for i, key in enumerate(tab.columns.keys()):
        tab[key].unit = units[i]
        if key == 'sensitivity':
            continue
        tab[key].format = '8f'

    print(tab)
    emed = np.sqrt(E[1:] * E[:-1])

    plt.plot(emed[:-1], tab['hadron_rate'], label='Hadron rate', marker='o')
    plt.plot(emed[:-1], tab['gamma_rate'], label='Gamma rate', marker='o')
    plt.legend()
    plt.xscale('log')
    plt.xlabel('Energy (GeV)')
    plt.ylabel('events / min')
    plt.show()

    gammas_mc = dl2[dl2.mc_type == 0]
    protons_mc = dl2[dl2.mc_type == 101]
    good_gammas = dl2

    sns.distplot(gammas_mc.gammaness, label='gammas')
    sns.distplot(protons_mc.gammaness, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    sns.distplot(gammas_mc.mc_energy, label='gammas')
    sns.distplot(protons_mc.mc_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    sns.distplot(gammas_mc.log_reco_energy, label='gammas')
    sns.distplot(protons_mc.log_reco_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    ctaplot.plot_theta2(gammas_mc.reco_alt,
                        gammas_mc.reco_az,
                        gammas_mc.mc_alt,
                        gammas_mc.mc_az,
                        range=(0, 1),
                        bins=100)
    plt.show()
    plt.figure(figsize=(12, 8))
    ctaplot.plot_angular_res_per_energy(
        gammas_mc.reco_alt,
        gammas_mc.reco_az,
        gammas_mc.mc_alt,
        gammas_mc.mc_az,
        gammas_mc.reco_energy,
    )

    ctaplot.plot_angular_res_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_resolution(10**(gammas_mc.mc_energy - 3),
                                   10**(gammas_mc.reco_energy - 3))

    ctaplot.plot_energy_resolution_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()

    ctaplot.plot_energy_bias(10**(gammas_mc.mc_energy - 3),
                             10**(gammas_mc.reco_energy - 3))
    plt.show()

    gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl1file_gammas)
    emin = gamma_ps_simu_info.energy_range_min.value
    emax = gamma_ps_simu_info.energy_range_max.value
    total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse
    spectral_index = gamma_ps_simu_info.spectral_index
    area = (gamma_ps_simu_info.max_scatter_range.value -
            gamma_ps_simu_info.min_scatter_range.value)**2 * np.pi
    ctaplot.plot_effective_area_per_energy_power_law(
        emin,
        emax,
        total_number_of_events,
        spectral_index,
        gammas_mc.reco_energy[gammas_mc.tel_id == 1],
        area,
        label='selected gammas',
        linestyle='--',
    )

    ctaplot.plot_effective_area_cta_requirements('north', color='black')
    plt.legend()
    plt.tight_layout()
    plt.show()
Exemple #4
0
sns.distplot(protons_mc.reco_energy, label='protons')
plt.legend()
plt.tight_layout()
plt.show()
ctaplot.plot_theta2(gammas_mc.reco_alt,
                    gammas_mc.reco_az,
                    gammas_mc.mc_alt,
                    gammas_mc.mc_az,
                    range=(0, 1),
                    bins=100)
plt.show()
plt.figure(figsize=(12, 8))
ctaplot.plot_angular_res_per_energy(
    gammas_mc.reco_alt,
    gammas_mc.reco_az,
    gammas_mc.mc_alt,
    gammas_mc.mc_az,
    10**(gammas_mc.reco_energy - 3),
)

ctaplot.plot_angular_res_cta_requirements('north', color='black')

plt.legend()
plt.tight_layout()
plt.show()
plt.figure(figsize=(12, 8))
ctaplot.plot_energy_resolution(10**(gammas_mc.mc_energy - 3),
                               10**(gammas_mc.reco_energy - 3))

ctaplot.plot_energy_resolution_cta_requirements('north', color='black')
Exemple #5
0
    plot_dl2.plot_disp_vector(gammas)
    plt.show()

    ctaplot.plot_theta2(
        gammas.mc_alt,
        np.arctan(np.tan(gammas.mc_az)),
        src_pos_reco.alt.rad,
        np.arctan(np.tan(src_pos_reco.az.rad)),
        bins=50,
        range=(0, 1),
    )

    plt.show()
    ctaplot.plot_angular_res_per_energy(src_pos_reco.alt.rad,
                                        np.arctan(np.tan(src_pos_reco.az.rad)),
                                        gammas.mc_alt,
                                        np.arctan(np.tan(gammas.mc_az)),
                                        10**(gammas.mc_energy - 3))
    plt.show()

    features_ = [
        'intensity', 'width', 'length', 'x', 'y', 'psi', 'phi', 'wl',
        'skewness', 'kurtosis', 'r', 'time_gradient', 'intercept', 'leakage',
        'n_islands', 'e_rec', 'disp_dx_rec', 'disp_dy_rec'
    ]

    plt.show()
    plot_dl2.plot_pos(dl2)
    plt.show()
    plot_dl2.plot_ROC(cls_gh, dl2, features_, -1)
    plt.show()