def plot_positions_survived_events(df_gammas, df_protons, gammaness_g, gammaness_p, theta2_g, p_contained, sensitivity, energy, n_bins_energy, gammaness_bins, theta2_bins, save_figure=False, ax=None): """ Plot positions of surviving events after cuts Parameters -------- df_gammas: `pandas.DataFrame` gammas dl2 parameters df_protons: `pandas.DataFrame` protons dl2 parameters gammaness_g: `numpy.ndarray` gammaness array of gamma events gammaness_p: `numpy.ndarray` gammaness array of proton events theta2_g: `numpy.ndarray` theta2 array of gamma events p_contained: `numpy.ndarray` containment of proton events inside the ring established in camera coordinates sensitivity: `numpy.ndarray` array with sensitivity values in energy bins energy: `numpy.ndarray` energy edge bins (size n_bins_energy + 1) n_bins_energy: `int` number of bins in energy gammaness_bins: `numpy.ndarray` gammaness bins theta2_bins: `numpy.ndarray` theta2 bins Returns -------- """ e_reco_g = 10**df_gammas.mc_energy e_reco_p = 10**df_protons.mc_energy for i in range(0, n_bins_energy): fig, ax = plt.subplots() print("Energy range [GeV]: ", energy[i], energy[i + 1]) ind = np.unravel_index(np.nanargmin(sensitivity[i], axis=None), sensitivity[i].shape) events_g = df_gammas[(e_reco_g < energy[i+1]) & (e_reco_g > energy[i]) \ & (gammaness_g > gammaness_bins[ind[0]]) & (theta2_g < theta2_bins[ind[1]])] events_p = df_protons[(e_reco_p < energy[i+1]) & (e_reco_p > energy[i]) \ & (gammaness_p > gammaness_bins[ind[0]]) & p_contained] events_p.intensity.hist() ax.set_xlabel("Log(10) Intensity Protons") fig.savefig("intensity_prot%d" % i) df = pd.concat([events_g, events_p], ignore_index=True) plot_pos(df, True) if (save_figure): fig.savefig("srcpos_bin%d" % i)
def plot_positions_survived_events(df_gammas, df_protons, gammaness_g, gammaness_p, theta2_g, p_contained, sens, E, eb, g, t): e_reco_g = 10**df_gammas.mc_energy e_reco_p = 10**df_protons.mc_energy for i in range(0,eb): print(E[i], E[i+1]) ind = np.unravel_index(np.nanargmin(sens[i], axis=None), sens[i].shape) events_g = df_gammas[(e_reco_g < E[i+1]) & (e_reco_g > E[i]) \ & (gammaness_g > g[ind[0]]) & (theta2_g < t[ind[1]])] events_p = df_protons[(e_reco_p < E[i+1]) & (e_reco_p > E[i]) \ & (gammaness_p > g[ind[0]]) & p_contained] events_p.intensity.hist() plt.xlabel("Log(10) Intensity Protons") plt.savefig("intensity_prot%d" % i) plt.show() df = pd.concat([events_g, events_p], ignore_index=True) plot_pos(df, True) plt.savefig("srcpos_bin%d" % i) plt.show()
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##### selected_gammas = dl2.query('reco_type==0 & mc_type==0') if (len(selected_gammas) == 0): log.warning('No gammas selected, I will not plot any output') sys.exit() plot_dl2.plot_features(dl2) if not args.batch: plt.show() plot_dl2.energy_results(selected_gammas) if not args.batch: plt.show() plot_dl2.direction_results(selected_gammas) if not args.batch: plt.show() plot_dl2.plot_disp_vector(selected_gammas) if not args.batch: plt.show() plot_dl2.plot_pos(dl2) if not args.batch: plt.show() plot_dl2.plot_roc_gamma(dl2) if not args.batch: plt.show() plot_dl2.plot_models_features_importances(args.path_models, args.config_file) if not args.batch: plt.show() plt.hist(dl2[dl2['mc_type'] == 101]['gammaness'], bins=100) plt.hist(dl2[dl2['mc_type'] == 0]['gammaness'], bins=100) if not args.batch: plt.show()
#Apply the models to the data features = ['intensity', 'time_gradient', 'width', 'length', 'wl', 'phi', 'psi'] dl2 = reco_dl1_to_dl2.ApplyModels(data, features, RFcls_GH, RFreg_Energy, RFreg_Disp) if args.storeresults==True: #Store results if not os.path.exists(args.outdir): os.mkdir(args.outdir) outfile = args.outdir+"/dl2_events.hdf5" dl2.to_hdf(outfile, key="dl2_events", mode="w") #Plot some results plot_dl2.plot_features(dl2) plt.show() plot_dl2.plot_e(dl2) plt.show() plot_dl2.plot_disp(dl2) plt.show() plot_dl2.plot_pos(dl2) plt.show()
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()
features_sep) test['hadro_rec'] = RFcls_GH.predict(test[features_sep]) if args.storerf==True: fileE = args.path_models+"/RFreg_Energy.sav" fileD = args.path_models+"/RFreg_Disp.sav" fileH = args.path_models+"/RFcls_GH.sav" joblib.dump(RFreg_Energy, fileE) joblib.dump(RFreg_Disp, fileD) joblib.dump(RFcls_GH, fileH) #Plot some results e_cuts = [-1,np.log10(500),np.log10(1000)] for e_cut in e_cuts: test = test[test['e_rec']>e_cut] plot_dl2.plot_features(test) plt.show() plot_dl2.plot_e(test) plt.show() plot_dl2.plot_disp(test) plt.show() plot_dl2.plot_pos(test) plt.show() plot_dl2.plot_importances(RFcls_GH,features_sep) plt.show() plot_dl2.plot_ROC(RFcls_GH,test,features_sep,e_cut) plt.show()
def main(): args = parser.parse_args() custom_config = {} if args.config_file is not None: custom_config = read_configuration_file(args.config_file) config = replace_config(standard_config, custom_config) subarray_info = SubarrayDescription.from_hdf(args.gammatest) tel_id = config["allowed_tels"][0] if "allowed_tels" in config else 1 focal_length = subarray_info.tel[tel_id].optics.equivalent_focal_length reg_energy, reg_disp_norm, cls_disp_sign, cls_gh = dl1_to_dl2.build_models( args.gammafile, args.protonfile, save_models=args.save_models, 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_norm=reg_disp_norm, cls_disp_sign=cls_disp_sign, focal_length=focal_length, custom_config=config) ####PLOT SOME RESULTS##### selected_gammas = dl2.query('reco_type==0 & mc_type==0') if (len(selected_gammas) == 0): log.warning('No gammas selected, I will not plot any output') sys.exit() plot_dl2.plot_features(dl2) if not args.batch: plt.show() plot_dl2.energy_results(selected_gammas) if not args.batch: plt.show() plot_dl2.direction_results(selected_gammas) if not args.batch: plt.show() plot_dl2.plot_disp_vector(selected_gammas) if not args.batch: plt.show() plot_dl2.plot_pos(dl2) if not args.batch: plt.show() plot_dl2.plot_roc_gamma(dl2) if not args.batch: plt.show() plot_dl2.plot_models_features_importances(args.path_models, args.config_file) if not args.batch: plt.show() plt.hist(dl2[dl2['mc_type'] == 101]['gammaness'], bins=100) plt.hist(dl2[dl2['mc_type'] == 0]['gammaness'], bins=100) if not args.batch: plt.show()