def print_FID_Rumi(self): np.save(self.metricpath + 'FID_pos.npy', np.array(self.FID_vec_pos)) np.save(self.metricpath + 'FID_neg.npy', np.array(self.FID_vec_neg)) path = self.metricpath #Colab has issues with latex. Better not waste time printing on Colab. Use the NPY later, offline,... if self.latex_plot_flag: from matplotlib.backends.backend_pgf import PdfPages plt.rcParams.update({ "pgf.texsystem": "pdflatex", "font.family": "helvetica", # use serif/main font for text elements "font.size": 12, "text.usetex": True, # use inline math for ticks "pgf.rcfonts": False, # don't setup fonts from rc parameters }) else: from matplotlib.backends.backend_pdf import PdfPages vals = list(np.array(self.FID_vec_pos)[:, 0]) locs = list(np.array(self.FID_vec_pos)[:, 1]) with PdfPages(path + 'FID_plot_pos.pdf') as pdf: fig1 = plt.figure(figsize=(3.5, 3.5)) ax1 = fig1.add_subplot(111) ax1.cla() ax1.get_xaxis().set_visible(True) ax1.get_yaxis().set_visible(True) ax1.plot(locs, vals, c='r', label='FID vs. Iterations') ax1.legend(loc='upper right') pdf.savefig(fig1) plt.close(fig1) vals = list(np.array(self.FID_vec_neg)[:, 0]) locs = list(np.array(self.FID_vec_neg)[:, 1]) with PdfPages(path + 'FID_plot_neg.pdf') as pdf: fig1 = plt.figure(figsize=(3.5, 3.5)) ax1 = fig1.add_subplot(111) ax1.cla() ax1.get_xaxis().set_visible(True) ax1.get_yaxis().set_visible(True) ax1.plot(locs, vals, c='r', label='FID vs. Iterations') ax1.legend(loc='upper right') pdf.savefig(fig1) plt.close(fig1)
def main(input_files, verbose, output): setup_logging(verbose=verbose) eventlist_list = [] for f in input_files: eventlist_list.append(EventList.read(f)) events = eventlist_list[0] for e in eventlist_list[1:]: events.stack(e) figures = [] figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) events.plot_energy(ax=ax) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) events.plot_energy_offset(ax=ax) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) events.plot_offset2_distribution(ax=ax) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) events.plot_time(ax=ax) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) pdf.savefig(fig)
def test_pdf_pages(system): rc_pdflatex = { 'font.family': 'serif', 'pgf.rcfonts': False, 'pgf.texsystem': system, } mpl.rcParams.update(rc_pdflatex) fig1, ax1 = plt.subplots() ax1.plot(range(5)) fig1.tight_layout() fig2, ax2 = plt.subplots(figsize=(3, 2)) ax2.plot(range(5)) fig2.tight_layout() path = os.path.join(result_dir, f'pdfpages_{system}.pdf') md = { 'Author': 'me', 'Title': 'Multipage PDF with pgf', 'Subject': 'Test page', 'Keywords': 'test,pdf,multipage', 'ModDate': datetime.datetime( 1968, 8, 1, tzinfo=datetime.timezone(datetime.timedelta(0))), 'Trapped': 'Unknown' } with PdfPages(path, metadata=md) as pdf: pdf.savefig(fig1) pdf.savefig(fig2) pdf.savefig(fig1) assert pdf.get_pagecount() == 3
def main(input_files, verbose, cuts, output, source_ra, source_dec,): setup_logging(verbose=verbose) if cuts: with open(cuts) as f: selection = yaml.safe_load(f).get("selection") else: selection = None cache = Path(output).with_suffix(".h5") if cache.exists(): events = Table.read(cache) ontime = events.meta["ontime"] else: plot_data = {} observations, obstime = read_lst_dl2_runs(input_files) log.info(combined.keys()) source=SkyCoord(ra=source_ra*u.deg, dec=source_dec*u.deg, frame='icrs') for run_id, run_data in observations:: if selection: mask = create_mask_selection(run_data, selection) run_data = combined[mask] theta_on, off_thetas = calc_wobble_thetas(run_data, source=source) thetas = QTable() thetas['theta_on'] = theta_on.to_value(u.deg) for i, theta_off in enumerate(off_thetas): thetas[f'theta_off_{i}'] = theta_off.to_value(u.deg) thetas["energy"] = run_data["reco_energy"] events = vstack(thetas) events.meta["obstime"] = ontime events.write(cache, serialize_meta=True) log.info(f"Loaded {len(events)} events") figures = [] figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot(events, ontime, ax) ax.set_title('All energies') energy_selection = [ (0*u.GeV, 50*u.GeV), (50*u.GeV, 100*u.GeV), (100*u.GeV, 200*u.GeV), (200*u.GeV, 500*u.GeV), (500*u.GeV, 1000*u.GeV), (1*u.TeV, 100*u.TeV), (100*u.GeV, 100*u.TeV), ] for low, high in energy_selection: selection = (events[energy] > low) & (events[energy] <= high) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot(events[selection], ontime, ax) ax.set_title(f'{low} - {high}') if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) pdf.savefig(fig)
def print_ClassProbs(self): path = self.metricpath if self.latex_plot_flag: from matplotlib.backends.backend_pgf import PdfPages plt.rcParams.update({ "pgf.texsystem": "pdflatex", "font.family": "helvetica", # use serif/main font for text elements "font.size": 12, "text.usetex": True, # use inline math for ticks "pgf.rcfonts": False, # don't setup fonts from rc parameters }) else: from matplotlib.backends.backend_pdf import PdfPages plt.rc('text', usetex=False) vals = list(np.array(self.class_prob_vec)[:, 0][-1]) locs = list(np.arange(10)) with PdfPages(path + 'ClassProbs_stem_' + str(self.total_count.numpy()) + '.pdf') as pdf: fig1 = plt.figure(figsize=(3.5, 3.5)) ax1 = fig1.add_subplot(111) ax1.cla() ax1.get_xaxis().set_visible(True) ax1.get_yaxis().set_visible(True) ax1.set_ylim([0, 0.5]) ax1.stem(vals, label='alpha_p=' + str(self.alphap)) ax1.legend(loc='upper right') pdf.savefig(fig1) plt.close(fig1) with PdfPages(path + 'ClassProbs_plot_' + str(self.total_count.numpy()) + '.pdf') as pdf: fig1 = plt.figure(figsize=(3.5, 3.5)) ax1 = fig1.add_subplot(111) ax1.cla() ax1.get_xaxis().set_visible(True) ax1.get_yaxis().set_visible(True) ax1.plot(locs, vals, c='r', label='alpha_p=' + str(self.alphap)) ax1.legend(loc='upper right') pdf.savefig(fig1) plt.close(fig1)
def test_pdf_pages_metadata_check(monkeypatch, system): # Basically the same as test_pdf_pages, but we keep it separate to leave # pikepdf as an optional dependency. pikepdf = pytest.importorskip('pikepdf') monkeypatch.setenv('SOURCE_DATE_EPOCH', '0') mpl.rcParams.update({'pgf.texsystem': system}) fig, ax = plt.subplots() ax.plot(range(5)) md = { 'Author': 'me', 'Title': 'Multipage PDF with pgf', 'Subject': 'Test page', 'Keywords': 'test,pdf,multipage', 'ModDate': datetime.datetime(1968, 8, 1, tzinfo=datetime.timezone(datetime.timedelta(0))), 'Trapped': 'True' } path = os.path.join(result_dir, f'pdfpages_meta_check_{system}.pdf') with PdfPages(path, metadata=md) as pdf: pdf.savefig(fig) with pikepdf.Pdf.open(path) as pdf: info = {k: str(v) for k, v in pdf.docinfo.items()} # Not set by us, so don't bother checking. if '/PTEX.FullBanner' in info: del info['/PTEX.FullBanner'] if '/PTEX.Fullbanner' in info: del info['/PTEX.Fullbanner'] # Some LaTeX engines ignore this setting, and state themselves as producer. producer = info.pop('/Producer') assert producer == f'Matplotlib pgf backend v{mpl.__version__}' or ( system == 'lualatex' and 'LuaTeX' in producer) assert info == { '/Author': 'me', '/CreationDate': 'D:19700101000000Z', '/Creator': f'Matplotlib v{mpl.__version__}, https://matplotlib.org', '/Keywords': 'test,pdf,multipage', '/ModDate': 'D:19680801000000Z', '/Subject': 'Test page', '/Title': 'Multipage PDF with pgf', '/Trapped': '/True', }
def main(configuration_path, performance_path, model_path, output, key): """ Create some performance evaluation plots for the separator """ logging.basicConfig(level=logging.INFO) log = logging.getLogger() log.info("Loading perfomance data") df = fact.io.read_data(performance_path, key=key) log.info("Loading model") model = joblib.load(model_path) model_config = AICTConfig.from_yaml(configuration_path).separator log.info("Creating performance plots. ") figures = [] # Plot rocs figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_roc(df, model, score_column=model_config.output_name, ax=ax) # Plot hists of probas figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_scores( df, model, score_column=model_config.output_name, ax=ax, xlabel=model_config.output_name, ) # Plot hists of probas figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_precision_recall(df, model, ax=ax, score_column=model_config.output_name) # Plot feature importances if hasattr(model, "feature_importances_"): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) features = model_config.features plot_feature_importances(model, features, ax=ax) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0) pdf.savefig(fig)
def output_graphs(filename: str, summary: ExpenseSummaryMatrix) -> None: plt.rcParams["figure.figsize"] = (11.69, 8.27) # A4 size # Sort the categories from highest to lowest amount of expenses totals = summary.totals_by_category() summary.expenses = dict( sorted(summary.expenses.items(), key=lambda item: totals[item[0]], reverse=True)) with PdfPages(filename, metadata={"Title": "SettleUpGraphs"}) as pdf: pdf.savefig(pie_chart_by_category(summary)) pdf.savefig(bar_chart_by_category(summary)) pdf.savefig(stacked_bar_chart_by_name(summary))
def main(configuration_path, performance_path, model_path, output, key): ''' Create some performance evaluation plots for the separator ''' logging.basicConfig(level=logging.INFO) log = logging.getLogger() log.info('Loading perfomance data') df = fact.io.read_data(performance_path, key=key) log.info('Loading model') model = joblib.load(model_path) model_config = AICTConfig.from_yaml(configuration_path).separator log.info('Creating performance plots. ') figures = [] # Plot rocs figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_roc(df, model, ax=ax) # Plot hists of probas figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_probabilities(df, model, ax=ax) # Plot hists of probas figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_precision_recall(df, model, ax=ax) # Plot feature importances if hasattr(model, 'feature_importances_'): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) features = model_config.features plot_feature_importances(model, features, ax=ax) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: pdf.savefig(fig)
async def main( input_files, output, ): cache = Path(output).with_suffix(".h5") figs = [] if cache.exists(): plot_data = read_plot_data(cache, data_structure) else: plot_data = data_structure.copy() obstime = 0 * u.s observations = {} runs = await asyncio.gather( *[asyncio.create_task(load_run(f)) for f in input_files]) for run in runs: observations[run[0]["obs_id"]] = run run_time = (run["time"][-1] - run["time"][0]).to(u.s) #run["weights"] = 1 / run_time.to_value(u.s) obstime += run_time combined = vstack(list(observations.values())) #combined["weights"] = 1 / obstime.to_value(u.s) combined["delta_t_sec"] = (combined["time"] - combined["time"][0]).sec last = combined["delta_t_sec"].max() time_bins = np.linspace(0, last, 20) if isinstance(combined, Table): combined = combined.to_pandas() combined.set_index(["obs_id", "event_id"]) for col in cols: plot_data[col]["bins"] = pd.Series(time_bins) plot_data[col]["values"] = bin_df(combined, "delta_t_sec", col, bins=time_bins) save_plot_data(cache, plot_data) for col, data in plot_data.items(): figs.append(asyncio.create_task(plot_feature(data["values"], col))) figs = await asyncio.gather(*figs) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figs: fig.tight_layout(pad=0) pdf.savefig(fig)
def plot(file, binnum='all', pdf=None, yrange=[1e-6, 2]): "Plot figures of stellar profile analysis." d = fits.getdata(file, 'PSF_SCATTERING') nbins = len(d) if binnum == 'all': bins = np.arange(nbins) elif ',' in binnum: bins = np.array(binnum.split(',')).astype(int) else: bins = np.array([binnum]) print("Make plots for spectral bins: {}".format(bins)) plt.close() if pdf is not None: print("Write multi-page pdf: {}".format(pdf)) from matplotlib.backends.backend_pgf import PdfPages p = PdfPages(pdf) for bn in bins: xmax = d[bn]['PARS_MAX_POS'] wcenter = d[bn]['PARS_WAVE_CENTER'] wrange = d[bn]['PARS_WAVE_RANGE'] maxfrac = d[bn]['PARS_MAXFRAC'] win = d[bn]['PARS_WIN'] f = plt.figure() ax = f.add_subplot() ax.plot(d[bn]['PRF_X'] - xmax, d[bn]['PRF'], color='k') comps = d[bn]['FIT_COMPS'] for k in range(comps.shape[1]): ax.plot(d[bn]['PSF_SCAT_X'], comps[:, k], color='C1', lw=0.5) ax.plot(d[bn]['PSF_SCAT_X'], d[bn]['PSF_SCAT_Y'], color='C0') ax.plot(d[bn]['PRF_X'] - xmax, d[bn]['FIT'], color='red') ax.axvline(0, linestyle=':') ax.axhline(maxfrac, linestyle=':') ax.set_yscale('log') ax.set_xlim(-win, win) ax.set_ylim(yrange) ax.set_title(r"Bin {} $\lambda$ {:.1f} $\Delta \lambda$ " "{:.1f}-{:.1f}".format(bn, wcenter, wrange[0], wrange[1])) ax.secondary_xaxis('top', functions=(lambda x: x + xmax, lambda x: x - xmax)) if pdf is not None: p.savefig(f) p.close() plt.show()
def test_pdf_pages_lualatex(): rc_pdflatex = { 'font.family': 'serif', 'pgf.rcfonts': False, 'pgf.texsystem': 'lualatex' } mpl.rcParams.update(rc_pdflatex) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.plot(range(5)) fig.tight_layout() md = {'author': 'me', 'title': 'Multipage PDF with pgf'} path = os.path.join(result_dir, 'pdfpages_lua.pdf') with PdfPages(path, metadata=md) as pdf: pdf.savefig(fig) pdf.savefig(fig) assert pdf.get_pagecount() == 2
def test_pdf_pages(): rc_pdflatex = { 'font.family': 'serif', 'pgf.rcfonts': False, 'pgf.texsystem': 'pdflatex', } mpl.rcParams.update(rc_pdflatex) fig1 = plt.figure() ax1 = fig1.add_subplot(1, 1, 1) ax1.plot(range(5)) fig1.tight_layout() fig2 = plt.figure(figsize=(3, 2)) ax2 = fig2.add_subplot(1, 1, 1) ax2.plot(range(5)) fig2.tight_layout() with PdfPages(os.path.join(result_dir, 'pdfpages.pdf')) as pdf: pdf.savefig(fig1) pdf.savefig(fig2)
def print_PR(self): path = self.metricpath if self.latex_plot_flag: from matplotlib.backends.backend_pgf import PdfPages plt.rcParams.update({ "pgf.texsystem": "pdflatex", "font.family": "helvetica", # use serif/main font for text elements "font.size": 12, "text.usetex": True, # use inline math for ticks "pgf.rcfonts": False, # don't setup fonts from rc parameters }) else: from matplotlib.backends.backend_pdf import PdfPages with PdfPages(path + 'PR_plot.pdf') as pdf: for PR in self.PR_vec: fig1 = plt.figure(figsize=(3.5, 3.5), dpi=400) ax1 = fig1.add_subplot(111) ax1.cla() ax1.set_xlim([0, 1]) ax1.set_ylim([0, 1]) ax1.get_xaxis().set_visible(True) ax1.get_yaxis().set_visible(True) precision, recall = PR[0] ax1.plot(recall, precision, color='g', linestyle='solid', alpha=0.5, linewidth=3) ax1.set_xlabel('RECALL') ax1.set_ylabel('PRECISION') title = 'PR at Iteration ' + str(PR[1]) plt.title(title, fontsize=8) pdf.savefig(fig1, bbox_inches='tight', dpi=400) plt.close(fig1)
def main(infile, verbose, output): setup_logging(verbose=verbose) figures = [] try: magic = QTable.read("magic_sensitivity_2014.ecsv") except Exception as e: log.warning(e) magic = None sens = QTable.read(infile, hdu="SENSITIVITY") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_sensitivity(sens[1:-1], ax, label="LST-1") if magic: for k in filter( lambda k: k.startswith("sensitivity_") or k.startswith("e_"), magic.colnames ): magic[k].info.format = ".3g" magic["reco_energy_low"] = magic["e_min"] magic["reco_energy_high"] = magic["e_max"] magic["reco_energy_center"] = magic["e_center"] magic["flux_sensitivity"] = magic["sensitivity_lima_5off"] plot_sensitivity(magic, ax, label="MAGIC") ax.legend() figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_sensitivity_gp(sens[1:-1], ax) aeff = QTable.read(infile, hdu="EFFECTIVE_AREA") aeff_only_gh = QTable.read(infile, hdu="EFFECTIVE_AREA_ONLY_GH") aeff_no_cuts = QTable.read(infile, hdu="EFFECTIVE_AREA_NO_CUTS") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_aeff(aeff[0], ax, label="gh + theta") plot_aeff(aeff_only_gh[0], ax, label="Only GH") plot_aeff(aeff_no_cuts[0], ax, label="No Cuts") ax.legend() edisp = QTable.read(infile, hdu="ENERGY_DISPERSION") edisp_only_gh = QTable.read(infile, hdu="ENERGY_DISPERSION_ONLY_GH") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_edisp(edisp[0], ax, label="gh + theta") plot_edisp(edisp_only_gh[0], ax, label="only gh") angres = QTable.read(infile, hdu="ANGULAR_RESOLUTION") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_angular_resolution(angres, ax) energres = QTable.read(infile, hdu="ENERGY_BIAS_RESOLUTION") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_energy_bias_resolution(energres, ax) bkg = QTable.read(infile, hdu="BACKGROUND")[0] rad_max = QTable.read(infile, hdu="RAD_MAX")[0] figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_background(bkg, rad_max, ax) thetacuts = QTable.read(infile, hdu="THETA_CUTS") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_theta_cuts(thetacuts, ax) ghcuts = QTable.read(infile, hdu="GH_CUTS") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_gh_cuts(ghcuts, ax) signal = QTable.read(infile, hdu="SIGNAL") signal_gh = QTable.read(infile, hdu="SIGNAL_GH") signal_cuts = QTable.read(infile, hdu="SIGNAL_CUTS") figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_efficiency({"NO CUTS": signal, "ONLY GH": signal_gh, "CUTS": signal_cuts}, ax) ax.legend() if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) pdf.savefig(fig)
plt.grid(True) plt.minorticks_on() #plt.text(df1.loc[df1_shape[0]//2,'0_date'], 10, 'text here') #plt.legend('best') plt.plot_date('0_date', i, data=df1, xdate=True, linestyle ='solid', linewidth=1, markersize=2.5) mule=mule+1 plt.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None) plt.savefig('time plots.pdf', papertype='letter', orientation='portrait', facecolor="0.9" ) #2.8 multiple page pdf from matplotlib.backends.backend_pgf import PdfPages import matplotlib.pyplot as plt with PdfPages('multipage.pdf') as pdf: # page 1 plt.plot([2, 1, 3]) pdf.savefig() # page 2 plt.cla() plt.plot([3, 1, 2]) pdf.savefig() #3 seaborn #https://seaborn.pydatarelplot.org/generated/seaborn.relplot.html#seaborn.relplot #relplot, line or scatter sns.relplot(x='A', y='B', hue="C", legend="brief", data=df2, kind='scatter')
def main(outdir, output): runs = [ f'{outdir}/dl2_v0.5.1_LST-1.Run02113.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02114.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02115.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02116.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02117.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02130.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02131.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02132.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run02133.h5' ] df = pd.DataFrame() for i, run in enumerate(runs): df = pd.concat([df, read_h5py(run, key='events', columns=columns)], ignore_index=True) df_runs = [] for i, run in enumerate(runs): df_temp = read_h5py(run, key='events', columns=columns) df_runs.append(df_temp) figures = [] theta2_cut = 0.04 gammaness_threshold = 0.6 figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(df, theta2_cut, gammaness_threshold, df, ax=ax, coord='mrk 421', n_offs=3) ax.set_title('Mrk 421 coordinates, n_offs = 3') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(df, theta2_cut, gammaness_threshold, df, ax=ax, coord='mrk 421', n_offs=5) #ax.set_title('Mrk 421 coordinates, n_offs = 5') #figures.append(plt.figure()) #ax = figures[-1].add_subplot(1, 1, 1) #plotting.theta2(df, theta2_cut, gammaness_threshold, ax=ax, range=None) #ax.set_title('Mrk 421 camera center') #mrk 421 coordinates #figures.append(plt.figure()) #ax = figures[-1].add_subplot(1, 1, 1) #plotting.plot2D_runs(df_runs, runs, 'mrk 421', gammaness_threshold, ax) #saving with PdfPages(output) as pdf: for fig in figures: fig.tight_layout() pdf.savefig(fig)
def main(config, outputfile): with open(config) as f: config = yaml.load(f) datasets = config['datasets'] n_bins = config.get('n_bins', 100) if config.get('event_selection') is not None: masks = create_masks(config) else: masks = None # get columns available in all datasets and calculate weights weights = calc_all_weights(datasets, masks) common_columns = get_common_columns(datasets) # select columns columns = config.get('include_columns') if columns is not None: def included(column): return any( fnmatch(column, include) for include in columns ) common_columns = list(filter(included, common_columns)) columns = sorted(list(common_columns), key=str.lower) # exclude columns using glob pattern if config.get('exclude_columns') is not None: def excluded(column): return not any( fnmatch(column, exclude) for exclude in config['exclude_columns'] ) columns = list(filter(excluded, columns)) fig = plt.figure(constrained_layout=True) ax_hist = fig.add_subplot(1, 1, 1) with PdfPages(outputfile) as pdf: for i, column in enumerate(tqdm(columns)): kwargs = config.get('columns').get(column, {}) kwargs['n_bins'] = kwargs.get('n_bins', n_bins) if 'transform' in kwargs: kwargs['transform'] = eval(kwargs['transform']) dfs = read_dfs_for_column(datasets, column, masks=masks) if i == 0: print_event_rates(weights, datasets) ax_hist.cla() try: plot_hists(dfs, weights, column, datasets, ax=ax_hist, **kwargs) # fig.tight_layout(pad=0) pdf.savefig(fig) except IOError as e: print(f'Could not plot column {column}') print(e)
def main(configuration_path, performance_path, data_path, disp_model_path, sign_model_path, output, key, key_data): ''' Create some performance evaluation plots for the disp model''' logging.basicConfig(level=logging.INFO) log = logging.getLogger() config = AICTConfig.from_yaml(configuration_path) model_config = config.disp log.info('Loading perfomance data') df = fact.io.read_data(performance_path, key=key) columns = model_config.columns_to_read_train if model_config.coordinate_transformation == 'CTA': camera_unit = r'\mathrm{m}' else: camera_unit = r'\mathrm{mm}' log.info('Loading original data') df_data = fact.io.read_data(data_path, key=key_data, columns=columns) log.info('Loading disp model') disp_model = joblib.load(disp_model_path) log.info('Loading sign model') sign_model = joblib.load(sign_model_path) df_data = convert_units(df_data, model_config) figures = [] # Plot confusion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title('Reconstructed vs. True |disp| (log color scale)') plot_regressor_confusion( df, log_xy=False, ax=ax, label_column='disp', prediction_column='disp_prediction' ) ax.set_xlabel(r'$|disp|_{\mathrm{MC}} \,\, / \,\, ' + camera_unit + '$') ax.set_ylabel(r'$|disp|_{\mathrm{Est}} \,\, / \,\, ' + camera_unit + '$') # Plot confusion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title('Reconstructed vs. True |disp| (linear color scale)') plot_regressor_confusion( df, log_xy=False, log_z=False, ax=ax, label_column='disp', prediction_column='disp_prediction', ) ax.set_xlabel(r'$|disp|_{\mathrm{MC}} \,\, / \,\, ' + camera_unit + '$') ax.set_ylabel(r'$|disp|_{\mathrm{Est}} \,\, / \,\, ' + camera_unit + '$') # Plot ROC figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_roc(df, sign_model, ax=ax, label_column='sign', score_column='sign_score') # Plot hists of probas figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_scores( df, sign_model, ax=ax, classnames={-1.0: r'$-$', 1.0: r'$+$'}, label_column='sign', score_column='sign_score', ) # Plot feature importances sign if hasattr(sign_model, 'feature_importances_'): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title('Feature Importance sign') features = model_config.features plot_feature_importances(sign_model, features, ax=ax) # Plot feature importances disp if hasattr(disp_model, 'feature_importances_'): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title(r'Feature Importance |disp|') features = model_config.features plot_feature_importances(disp_model, features, ax=ax) # Plot true_delta - delta figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_true_delta_delta(df_data, model_config, ax) if config.true_energy_column in df.columns: fig = plot_energy_dependent_disp_metrics( df, config.true_energy_column, energy_unit=config.energy_unit ) figures.append(fig) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) pdf.savefig(fig)
def main(output, data, source, cuts_file, theta2_cut, threshold, n_offs, n_jobs): outdir = output.split('/')[0] src = SkyCoord.from_name(source) if n_jobs == -1: n_jobs = cpu_count() with Pool(n_jobs) as pool: results = np.array(pool.starmap(calculation.read_run_calculate_thetas, [(run, columns, threshold, src, n_offs) for run in data]), dtype=object) df_selected = pd.concat(results[:, 0], ignore_index=True) ontime = np.sum(results[:, 1]) theta = np.concatenate(results[:, 2]) df_selected5 = pd.concat(results[:, 3], ignore_index=True) theta_off = np.concatenate(results[:, 4]) # use pyirf cuts gh_cuts = table.QTable.read(cuts_file, hdu='GH_CUTS') theta_cuts_opt = table.QTable.read(cuts_file, hdu='THETA_CUTS_OPT') with Pool(n_jobs) as pool: results = np.array(pool.starmap(calculation.read_run_calculate_thetas, [(run, columns, gh_cuts, src, n_offs) for run in data]), dtype=object) df_pyirf = pd.concat(results[:, 0], ignore_index=True) theta_pyirf = np.concatenate(results[:, 2]) df_pyirf5 = pd.concat(results[:, 3], ignore_index=True) theta_off_pyirf = np.concatenate(results[:, 4]) n_on = np.count_nonzero( evaluate_binned_cut( theta_pyirf, df_pyirf.gamma_energy_prediction.to_numpy() * u.TeV, theta_cuts_opt, operator.le)) n_off = np.count_nonzero( evaluate_binned_cut( theta_off_pyirf, df_pyirf5.gamma_energy_prediction.to_numpy() * u.TeV, theta_cuts_opt, operator.le)) li_ma = li_ma_significance(n_on, n_off, 1 / n_offs) n_exc_mean = n_on - (1 / n_offs) * n_off n_exc_std = np.sqrt(n_on + (1 / n_offs)**2 * n_off) ############################################################################################################## # plots ############################################################################################################## figures = [] figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(theta.deg**2, theta_off.deg**2, 1 / n_offs, theta2_cut, threshold, source, ontime=ontime, ax=ax) ax.set_title('Theta calculated in ICRS using astropy') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(theta_pyirf.deg**2, theta_off_pyirf.deg**2, 1 / n_offs, theta2_cut, r'\mathrm{energy-dependent}', source, ontime=ontime, ax=ax) ax.set_title(r'Energy-dependent $t_\gamma$ optimised using pyirf') # plot using pyirf theta cuts figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.hist(theta_pyirf.deg**2, bins=100, range=[0, 1], histtype='step', color='r', label='ON') ax.hist(theta_off_pyirf.deg**2, bins=100, range=[0, 1], histtype='stepfilled', color='tab:blue', alpha=0.5, label='OFF', weights=np.full_like(theta_off_pyirf.deg**2, 1 / n_offs)) txt = rf'''Source: {source}, $t_\mathrm{{obs}} = {ontime.to_value(u.hour):.2f} \mathrm{{h}}$ $N_\mathrm{{on}} = {n_on},\, N_\mathrm{{off}} = {n_off},\, \alpha = {1/n_offs:.2f}$ $N_\mathrm{{exc}} = {n_exc_mean:.0f} \pm {n_exc_std:.0f},\, S_\mathrm{{Li&Ma}} = {li_ma:.2f}$ ''' ax.text(0.5, 0.95, txt, transform=ax.transAxes, va='top', ha='center') ax.set_xlabel(r'$\theta^2 \,\, / \,\, \mathrm{deg}^2$') ax.set_xlim(0, 1) ax.legend() ax.set_title( r'Energy-dependent $t_\gamma$ and $\theta_\mathrm{max}^2$ optimised using pyirf' ) ############################################################################################################## # sensitivity ############################################################################################################## sensitivity_bins = add_overflow_bins( create_bins_per_decade(10**-1.8 * u.TeV, 10**2.41 * u.TeV, bins_per_decade=5)) # gh_cuts and theta_cuts_opt in line 113f gammas = plotting.to_astropy_table( df_pyirf, # df_pyirf has pyirf gh cuts already applied column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta_pyirf, t_obs=ontime) background = plotting.to_astropy_table(df_pyirf5, column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta_off_pyirf, t_obs=ontime) gammas["selected_theta"] = evaluate_binned_cut(gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le) background["selected_theta"] = evaluate_binned_cut( background["theta"], background["reco_energy"], theta_cuts_opt, operator.le) # calculate sensitivity signal_hist = create_histogram_table(gammas[gammas["selected_theta"]], bins=sensitivity_bins) background_hist = estimate_background( background[background["selected_theta"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cuts_opt, alpha=1 / n_offs, background_radius=MAX_BG_RADIUS, ) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=1 / n_offs) # scale relative sensitivity by Crab flux to get the flux sensitivity spectrum = CRAB_HEGRA sensitivity["flux_sensitivity"] = ( sensitivity["relative_sensitivity"] * spectrum(sensitivity['reco_energy_center'])) ############################################################################################################## # sensitivity using unoptimised cuts ############################################################################################################## gammas_unop = plotting.to_astropy_table( df_selected, # df_selected has gammaness > threshold already applied column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta, t_obs=ontime) background_unop = plotting.to_astropy_table(df_selected5, column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta_off, t_obs=ontime) gammas_unop["selected_theta"] = gammas_unop["theta"].to_value( u.deg) <= np.sqrt(0.03) background_unop["selected_theta"] = background_unop["theta"].to_value( u.deg) <= np.sqrt(0.03) theta_cut_unop = theta_cuts_opt theta_cut_unop['cut'] = np.sqrt(0.03) * u.deg # calculate sensitivity signal_hist_unop = create_histogram_table( gammas_unop[gammas_unop["selected_theta"]], bins=sensitivity_bins) background_hist_unop = estimate_background( background_unop[background_unop["selected_theta"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cut_unop, alpha=1 / n_offs, background_radius=MAX_BG_RADIUS, ) sensitivity_unop = calculate_sensitivity(signal_hist_unop, background_hist_unop, alpha=1 / n_offs) # scale relative sensitivity by Crab flux to get the flux sensitivity sensitivity_unop["flux_sensitivity"] = ( sensitivity_unop["relative_sensitivity"] * spectrum(sensitivity_unop['reco_energy_center'])) # write fits file and create plot hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_unop, name="SENSITIVITY_UNOP") ] fits.HDUList(hdus).writeto(f'{outdir}/sensitivity_{source}.fits.gz', overwrite=True) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) for s, label in zip([sensitivity, sensitivity_unop], [ 'pyirf optimised cuts', rf'$\theta^2 < {theta2_cut}$ and gh_score$> {threshold}$' ]): plotting.plot_sensitivity(s, label=label, ax=ax) # plot Magic sensitivity for reference magic = table.QTable.read('notebooks/magic_sensitivity_2014.ecsv') plotting.plot_sensitivity(magic, label='MAGIC 2014', ax=ax, magic=True) ax.set_title( f'Minimal Flux Satisfying Requirements for 50 hours \n(based on {ontime.to_value(u.hour):.2f}h of {source} observations)' ) # save plots with PdfPages(output) as pdf: for fig in figures: fig.tight_layout() pdf.savefig(fig)
def plot(precision_recall_pairs, labels=None, out_path=None, colors = None, styles = None, legend_loc='lower left', dpi=300): """Plots precision recall curves for distributions. Creates the PRD plot for the given data and stores the plot in a given path. Args: precision_recall_pairs: List of prd_data to plot. Each item in this list is a 2D array of precision and recall values for the same number of ratios. labels: Optional list of labels of same length as list_of_prd_data. The default value is None. out_path: Output path for the resulting plot. If None, the plot will be opened via plt.show(). The default value is None. legend_loc: Location of the legend. The default value is 'lower left'. dpi: Dots per inch (DPI) for the figure. The default value is 150. Raises: ValueError: If labels is a list of different length than list_of_prd_data. """ if labels is not None and len(labels) != len(precision_recall_pairs): raise ValueError( 'Length of labels %d must be identical to length of ' 'precision_recall_pairs %d.' % (len(labels), len(precision_recall_pairs))) # fig = plt.figure(figsize=(3.5, 3.5), dpi=dpi) # plot_handle = fig.add_subplot(111) # plot_handle.tick_params(axis='both', which='major', labelsize=12) # for i in range(len(precision_recall_pairs)): # precision, recall = precision_recall_pairs[i] # label = labels[i] if labels is not None else None # plt.plot(recall, precision, label=label, alpha=0.5, linewidth=3) # if labels is not None: # plt.legend(loc=legend_loc) plt.rcParams.update({ "pgf.texsystem": "pdflatex", "font.family": "helvetica", # use serif/main font for text elements "font.serif": [], "font.size": 12, "text.usetex": True, # use inline math for ticks "pgf.rcfonts": False, # don't setup fonts from rc parameters # "pgf.preamble": [ # r"\usepackage[utf8x]{inputenc}", # r"\usepackage[T1]{fontenc}", # r"\usepackage{cmbright}", # ] }) with PdfPages(out_path, metadata={'author': 'Siddarth Asokan'}) as pdf: fig1 = plt.figure(figsize=(3.5, 3.5), dpi=dpi) ax1 = fig1.add_subplot(111) ax1.cla() ax1.set_xlim([0, 1]) ax1.set_ylim([0, 1]) ax1.get_xaxis().set_visible(True) ax1.get_yaxis().set_visible(True) for i in range(len(precision_recall_pairs)): precision, recall = precision_recall_pairs[i] label = labels[i] if labels is not None else None ax1.plot(recall, precision, color = colors[i], linestyle = styles[i], label=label, alpha=0.5, linewidth=3) ax1.legend(loc =legend_loc) ax1.set_xlabel('RECALL') ax1.set_ylabel('PRECISION') pdf.savefig(fig1, bbox_inches='tight', dpi=dpi) plt.close(fig1)
def main(outdir, gamma_diff_file, gamma_file, output): offs = [ f'{outdir}/dl2_v0.5.1_LST-1.Run01837.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01840.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01841.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01842.h5' ] ons = [ f'{outdir}/dl2_v0.5.1_LST-1.Run01832.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01833.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01834.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01835.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01836.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01843.h5', f'{outdir}/dl2_v0.5.1_LST-1.Run01844.h5' ] df_off = pd.DataFrame() for i, run in enumerate(offs): df_off = pd.concat( [df_off, read_h5py(run, key='events', columns=columns)], ignore_index=True) df_on = pd.DataFrame() for i, run in enumerate(ons): df_on = pd.concat( [df_on, read_h5py(run, key='events', columns=columns)], ignore_index=True) gamma_diff = read_h5py(gamma_diff_file, key='events') gamma = read_h5py(gamma_file, key='events') figures = [] theta2_cut = 0.04 gammaness_threshold = 0.6 #theta2 camera center figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(df_on, theta2_cut, gammaness_threshold, df_off, ax) #ax.set_title('Crab camera center, total-time scaling') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(df_on, theta2_cut, gammaness_threshold, df_off, ax, alpha='manuel') #ax.set_title('Crab camera center, furthest $50\%$ scaling') #crab coordinates on_pointing = [] for i, run in enumerate(ons): df = read_h5py(run, key='events', columns=columns) on_pointing.append(df) #figures.append(plt.figure()) #ax = figures[-1].add_subplot(1, 1, 1) #plotting.plot2D_runs(on_pointing, ons, 'crab', gammaness_threshold, ax) # #figures.append(plt.figure()) #ax = figures[-1].add_subplot(1, 1, 1) #plotting.plot2D(df_on, gammaness_threshold, ax) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(df_on, 0.1, gammaness_threshold, df_off, ax, coord='crab') ax.set_title('Crab coordinates, total-time scaling') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(df_on, 0.1, gammaness_threshold, df_off, ax, alpha='manuel', coord='crab') ax.set_title('Crab coordinates, furthest $50\%$ scaling') #test plots figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.hist(gamma_diff.disp_prediction, bins=100, histtype='step') ax.set_xlabel('disp prediction') ax.set_title('gamma-diffuse testing') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.hist(gamma_diff.gammaness, bins=100, histtype='step') ax.set_xlabel('gammaness') ax.set_title('gamma-diffuse testing') #figures.append(plt.figure()) #ax = figures[-1].add_subplot(1, 1, 1) #plotting.theta2(gamma_diff, theta2_cut, gammaness_threshold, ax=ax, range=None) #ax.set_title('gamma-diffuse testing') #angular resolustion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.angular_res(gamma, 'mc_energy', ax) ax.set_title('Angular resolution (no cuts)') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) gamma['sign_prediction'] = np.sign(gamma.disp_prediction) gamma_cuts = gamma.query('sign_prediction == disp_sign') gamma_cuts = gamma_cuts.query(f'gammaness > {gammaness_threshold}') plotting.angular_res(gamma_cuts, 'mc_energy', ax) ax.set_title( f'Angular resolution (correct sign prediction & gammaness > {gammaness_threshold})' ) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.angular_res(gamma, 'mc_energy', ax, label='All events') plotting.angular_res( gamma_cuts, 'mc_energy', ax, label=rf'correct sign and $p_\gamma > {gammaness_threshold}$') #saving with PdfPages(output) as pdf: for fig in figures: fig.tight_layout() pdf.savefig(fig)
def main( input_files, protons, electrons, source_gammas, binsizes_config, output, ): observations = {} if binsizes_config: with open(binsizes_config) as f: binsizes = yaml.safe_load(f) else: binsizes = None cache = Path(output).with_suffix(".h5") if cache.exists(): plot_data = read_plot_data(cache, data_structure) else: plot_data = data_structure.copy() obstime = 0 * u.s runs = [load_run(f) for f in input_files] for run in runs: observations[run[0]["obs_id"]] = run run_time = (run["time"][-1] - run["time"][0]).to(u.s) obstime += run_time combined = vstack(list(observations.values())) if protons: protons = load_mc(protons, obstime, IRFDOC_PROTON_SPECTRUM) if electrons: electrons = load_mc(electrons, obstime, IRFDOC_ELECTRON_SPECTRUM) if protons and electrons: background = vstack([protons, electrons]) elif protons: background = protons elif electrons: background = electrons else: background = None if source_gammas: gammas = load_mc(source_gammas, obstime, CRAB_MAGIC_JHEAP2015) else: gammas = None for feature in logx: plot_data[feature]["bins"] = pd.Series(dtype=np.float64) plot_data[feature]["values"] = pd.DataFrame() if feature not in combined.keys(): log.debug(f"{feature} missing in keys: {combined.keys()}") continue if binsizes: if feature in binsizes: min_, max_, n = binsizes[feature] else: min_ = get_value(combined, feature).min() max_ = get_value(combined, feature).max() n = 30 else: min_ = get_value(combined, feature).min() max_ = get_value(combined, feature).max() n = 30 bins = np.logspace(np.log10(min_), np.log10(max_), n) plot_data[feature]["bins"] = pd.Series(bins) feature_df = pd.DataFrame() feature_df["observations"], _ = np.histogram(combined[feature], bins=bins) if background: feature_df["background"], _ = np.histogram( background[feature], weights=background["weights"], bins=bins) if gammas: feature_df["gammas"], _ = np.histogram( gammas[feature], weights=gammas["weights"], bins=bins) for run_data in runs: run_id = run_data[0]["obs_id"] log.info(f"Filling feature df for run {run_id}") # why would that happen? if feature not in run_data.keys(): log.debug(f"{feature} missing in keys: {run_data.keys()}") continue feature_df[run_id], _ = np.histogram(run_data[feature], bins=bins) plot_data[feature]["values"] = feature_df for feature in linx: plot_data[feature]["bins"] = pd.Series(dtype=np.float64) plot_data[feature]["values"] = pd.DataFrame() if feature not in combined.keys(): log.debug(f"{feature} missing in keys: {combined.keys()}") continue if binsizes: if feature in binsizes: min_, max_, n = binsizes[feature] else: min_ = get_value(combined, feature).min() max_ = get_value(combined, feature).max() n = 30 else: min_ = get_value(combined, feature).min() max_ = get_value(combined, feature).max() n = 30 bins = np.linspace(min_, max_, n) plot_data[feature]["bins"] = pd.Series(bins) feature_df = pd.DataFrame() feature_df["observations"], _ = np.histogram(combined[feature], bins=bins) if background: feature_df["background"], _ = np.histogram( background[feature], weights=background["weights"], bins=bins) if gammas: feature_df["gammas"], _ = np.histogram( gammas[feature], weights=gammas["weights"], bins=bins) for run_data in runs: run_id = run_data[0]["obs_id"] log.debug(f"{feature} missing in keys: {run_data.keys()}") # why would that happen? if feature not in run_data.keys(): log.warning( f"{feature} missing in keys: {run_data.keys()}") continue feature_df[run_id], _ = np.histogram(run_data[feature], bins=bins) plot_data[feature]["values"] = feature_df save_plot_data(cache, plot_data) figs = [] datamc = ["observations"] if background: datamc.append("background") if gammas: datamc.append("gammas") log.info(f"datamc: {datamc}") log.info(f"all: {set(plot_data[feature]['values'].keys())}") log.info(f"runs: {set(plot_data[feature]['values'].keys()) - set(datamc)}") for feature in logx: log.info(feature) if not plot_data[feature]["values"].empty: figs.append(plot(plot_data, feature, datamc, logx=True)) figs.append( plot(plot_data, feature, set(plot_data[feature]["values"].keys()) - set(datamc), logx=True)) for feature in linx: log.info(feature) if not plot_data[feature]["values"].empty: figs.append(plot(plot_data, feature, datamc, logx=False)) figs.append( plot(plot_data, feature, set(plot_data[feature]["values"].keys()) - set(datamc), logx=False)) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figs: fig.tight_layout(pad=0) pdf.savefig(fig)
def main( configuration_path, performance_path, data_path, dxdy_model_path, output, key, key_data, ): """ Create some performance evaluation plots for the dxdy model""" logging.basicConfig(level=logging.INFO) log = logging.getLogger() config = AICTConfig.from_yaml(configuration_path) model_config = config.dxdy log.info("Loading perfomance data") df = fact.io.read_data(performance_path, key=key) if config.coordinate_transformation == "CTA": camera_unit = r"\mathrm{m}" else: camera_unit = r"\mathrm{mm}" log.info('Loading original data') df_data = read_telescope_data( data_path, config, model_config.columns_to_read_train, ) log.info("Loading dxdy model") dxdy_model = joblib.load(dxdy_model_path) figures = [] # Plot confusion dx log figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True dx (log color scale)") plot_regressor_confusion( df, log_xy=False, ax=ax, label_column="dx", prediction_column="dx_prediction" ) ax.set_xlabel(r"$dx_{\mathrm{MC}} \,\, / \,\, " + camera_unit + "$") ax.set_ylabel(r"$dx_{\mathrm{Est}} \,\, / \,\, " + camera_unit + "$") # Plot confusion dx linear figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True dx (linear color scale)") plot_regressor_confusion( df, log_xy=False, log_z=False, ax=ax, label_column="dx", prediction_column="dx_prediction", ) ax.set_xlabel(r"$dx_{\mathrm{MC}} \,\, / \,\, " + camera_unit + "$") ax.set_ylabel(r"$dx_{\mathrm{Est}} \,\, / \,\, " + camera_unit + "$") # Plot confusion dy log figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) # ax.set_title('Reconstructed vs. True dy (log color scale)') ax.set_title("Rekonstruiertes vs. wahres dy") plot_regressor_confusion( df, log_xy=False, ax=ax, label_column="dy", prediction_column="dy_prediction" ) ax.set_xlabel(r"$dy_{\mathrm{MC}} \,\, / \,\, " + camera_unit + "$") ax.set_ylabel(r"$dy_{\mathrm{Est}} \,\, / \,\, " + camera_unit + "$") # Plot confusion dy linear figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True dy (linear color scale)") plot_regressor_confusion( df, log_xy=False, log_z=False, ax=ax, label_column="dy", prediction_column="dy_prediction", ) ax.set_xlabel(r"$dy_{\mathrm{MC}} \,\, / \,\, " + camera_unit + "$") ax.set_ylabel(r"$dy_{\mathrm{Est}} \,\, / \,\, " + camera_unit + "$") # Plot feature importances dxdy if hasattr(dxdy_model, "feature_importances_"): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title(r"Feature Importance dxdy") features = model_config.features plot_feature_importances(dxdy_model, features, ax=ax) # Plot true_delta - delta figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_true_delta_delta(df_data, config, ax) if config.true_energy_column in df.columns: fig = plot_energy_dependent_dxdy_metrics( df, config.true_energy_column, energy_unit=config.energy_unit ) figures.append(fig) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) pdf.savefig(fig)
def main( configuration_path, performance_path, data_path, disp_model_path, sign_model_path, output, key, key_data, ): """ Create some performance evaluation plots for the disp model""" logging.basicConfig(level=logging.INFO) log = logging.getLogger() config = AICTConfig.from_yaml(configuration_path) model_config = config.disp log.info("Loading perfomance data") df = fact.io.read_data(performance_path, key=key) if config.data_format == "CTA": camera_unit = r"\mathrm{m}" else: camera_unit = r"\mathrm{mm}" log.info("Loading original data") df_data = read_telescope_data( data_path, config, model_config.columns_to_read_train, ) log.info("Loading disp model") disp_model = joblib.load(disp_model_path) log.info("Loading sign model") sign_model = joblib.load(sign_model_path) figures = [] # Plot confusion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True |disp| (log color scale)") plot_regressor_confusion( df, log_xy=False, ax=ax, label_column="disp", prediction_column="disp_prediction", ) ax.set_xlabel(r"$|disp|_{\mathrm{MC}} \,\, / \,\, " + camera_unit + "$") ax.set_ylabel(r"$|disp|_{\mathrm{Est}} \,\, / \,\, " + camera_unit + "$") # Plot confusion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True |disp| (linear color scale)") plot_regressor_confusion( df, log_xy=False, log_z=False, ax=ax, label_column="disp", prediction_column="disp_prediction", ) ax.set_xlabel(r"$|disp|_{\mathrm{MC}} \,\, / \,\, " + camera_unit + "$") ax.set_ylabel(r"$|disp|_{\mathrm{Est}} \,\, / \,\, " + camera_unit + "$") # Plot ROC figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_roc(df, sign_model, ax=ax, label_column="sign", score_column="sign_score") # Plot hists of probas figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_scores( df, sign_model, ax=ax, classnames={-1.0: r"$-$", 1.0: r"$+$"}, label_column="sign", score_column="sign_score", ) # Plot feature importances sign if hasattr(sign_model, "feature_importances_"): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Feature Importance sign") features = model_config.features plot_feature_importances(sign_model, features, ax=ax) # Plot feature importances disp if hasattr(disp_model, "feature_importances_"): figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title(r"Feature Importance |disp|") features = model_config.features plot_feature_importances(disp_model, features, ax=ax) # Plot true_delta - delta figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plot_true_delta_delta(df_data, config, ax) if config.true_energy_column in df.columns: fig = plot_energy_dependent_disp_metrics( df, config.true_energy_column, energy_unit=config.energy_unit ) figures.append(fig) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) pdf.savefig(fig)
def main( input_files, protons, electrons, source_gammas, binsizes_config, cuts, verbose, output, ): setup_logging(verbose=verbose) observations = {} if binsizes_config: with open(binsizes_config) as f: binsizes = yaml.safe_load(f) else: binsizes = None if cuts: with open(cuts) as f: selection = yaml.safe_load(f).get("selection") else: selection = None cache = Path(output).with_suffix(".h5") if cache.exists(): plot_data = read_plot_data(cache) else: plot_data = {} observations, obstime = read_lst_dl2_runs(input_files) combined = vstack(list(observations.values())) log.info(combined.keys()) if protons: protons = load_mc(protons, obstime, IRFDOC_PROTON_SPECTRUM) if electrons: electrons = load_mc(electrons, obstime, IRFDOC_ELECTRON_SPECTRUM) if electrons and protons: background = vstack([protons, electrons]) elif protons: background = protons elif electrons: background = electrons else: background = None if source_gammas: gammas = load_mc(source_gammas, obstime, CRAB_MAGIC_JHEAP2015) else: gammas = None if selection: mask = create_mask_selection(combined, selection) combined = combined[mask] if background: mask = create_mask_selection(background, selection) background = background[mask] if gammas: mask = create_mask_selection(gammas, selection) gammas = gammas[mask] for feature in combined.keys(): plot_data[feature] = {} plot_data[feature]["bins"] = pd.Series(dtype=np.float64) plot_data[feature]["values"] = pd.DataFrame() log.info(feature) if binsizes: if feature in binsizes: min_, max_, n = binsizes[feature] else: min_ = get_value(combined, feature).min() max_ = get_value(combined, feature).max() n = 30 else: min_ = get_value(combined, feature).min() max_ = get_value(combined, feature).max() n = 30 log.debug(min_) log.debug(max_) if feature in logx: bins = np.logspace(np.log10(min_), np.log10(max_), n) else: bins = np.linspace(min_, max_, n) plot_data[feature]["bins"] = pd.Series(bins) feature_df = pd.DataFrame() feature_df["observations"], _ = np.histogram( get_value(combined, feature), bins=bins ) if background: feature_df["background"], _ = np.histogram( get_value(background, feature), weights=background["weights"], bins=bins, ) if gammas: feature_df["gammas"], _ = np.histogram( get_value(gammas, feature), weights=gammas["weights"], bins=bins ) for run_data in observations: run_id = run_data[0]["obs_id"] log.info(f"Filling feature df for run {run_id}") # why would that happen? if feature not in run_data.keys(): log.debug(f"{feature} missing in keys: {run_data.keys()}") continue feature_df[run_id], _ = np.histogram( get_value(run_data, feature), bins=bins ) plot_data[feature]["values"] = feature_df save_plot_data(cache, plot_data) figs = [] datamc = ["observations"] if protons or electrons: datamc.append("background") if source_gammas: datamc.append("gammas") for feature in combined.keys(): log.debug(f"Plotting feature: {feature}") if not plot_data[feature]["values"].empty: log.debug("And indeed there is data") figs.append( compare_datasets(plot_data, feature, datamc, logx=feature in logx) ) figs.append( compare_datasets( plot_data, feature, set(plot_data[feature]["values"].keys()) - set(datamc), logx=feature in logx, ) ) if output is None: matplotlib.pyplot.show() else: with PdfPages(output) as pdf: for fig in figs: fig.tight_layout(pad=0) pdf.savefig(fig)
def main(configuration_path, performance_path, model_path, output, key): """ Create some performance evaluation plots for the separator """ logging.basicConfig(level=logging.INFO) log = logging.getLogger() log.info("Loading perfomance data") df = fact.io.read_data(performance_path, key=key) log.info("Loading model") model = joblib.load(model_path) config = AICTConfig.from_yaml(configuration_path) model_config = config.energy energy_unit = config.energy_unit figures = [] # Plot confusion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True Energy (log color scale)") plot_regressor_confusion( df, ax=ax, label_column=model_config.target_column, prediction_column=model_config.output_name, energy_unit=energy_unit, ) # Plot confusion figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Reconstructed vs. True Energy (linear color scale)") plot_regressor_confusion( df, log_z=False, ax=ax, label_column=model_config.target_column, prediction_column=model_config.output_name, energy_unit=energy_unit, ) # Plot bias/resolution figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.set_title("Bias and Resolution") plot_bias_resolution( df, bins=15, ax=ax, label_column=model_config.target_column, prediction_column=model_config.output_name, energy_unit=energy_unit, ) if hasattr(model, "feature_importances_"): # Plot feature importances figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) features = model_config.features plot_feature_importances(model, features, ax=ax) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figures: fig.tight_layout(pad=0) pdf.savefig(fig)
DATA = [] for i in INPUT_FILE: DATA.append(np.load(i+'.npz')) MM2IN = 25.4 WIDTH = (0.5)*(210.0 - 20.0 - 30.0)/MM2IN HEIGHT = (0.5)*(297.0 - 20.0 - 30.0)/MM2IN """ INPUT_DATA is a pickled, compressed file with the following numpy arrays: * pdos, an np.array, with dimensions (ispin, numpoints), where ispin=2 and numpoints=301 (default from VASP) * eigen_energy, an np.array, with dimensions (numpoints) * eigen_adjust, a float * e_fermi, a float """ with PdfPages(OUTPUT_FILE+'.pdf') as OUTPUT: plt.rcParams.update({ 'font.family': 'serif', 'figure.figsize': [WIDTH, HEIGHT], 'savefig.dpi': 300, 'text.usetex': True, 'pgf.rcfonts': False, 'pgf.texsystem': 'pdflatex', 'pgf.preamble': [r'\usepackage{mathpazo,eulervm}\usepackage[utf8x]{inputenc}'], 'legend.loc': 'upper right', 'legend.frameon': False, 'legend.fancybox': False, }) FIG, (AX0, AX1, AX2) = plt.subplots( nrows=3, ncols=1,
def main( input_files, protons, electrons, output, ): cache = Path(output).with_suffix(".h5") plot_data = load_data({"observations": input_files, "protons": protons, "electrons": electrons}, cache) figs = [] # pixels fig, ax = plt.subplots() ax.plot( plot_data["pixels"]["values"].index, plot_data["pixels"]["values"]["median"], "k.", ) ax.plot( plot_data["pixels"]["values"].index, plot_data["pixels"]["values"]["25"], "r--", ) ax.plot( plot_data["pixels"]["values"].index, plot_data["pixels"]["values"]["75"], "r--", ) ax.plot( plot_data["pixels"]["values"].index, plot_data["pixels"]["values"]["max"], "b.", ) ax.plot( plot_data["pixels"]["values"].index, plot_data["pixels"]["values"]["min"], "g.", ) ax.set_ylim( min(plot_data["pixels"]["values"]["25"] - 3*plot_data["pixels"]["values"]["iqr"]), max(plot_data["pixels"]["values"]["75"] + 3*plot_data["pixels"]["values"]["iqr"]), ) figs.append(fig) # if mc is there! # datamc fig = compare_rates( plot_data["datamc"]["values"]["data"].values, plot_data["datamc"]["values"]["mc"].values, plot_data["datamc"]["bins"].values, l1="data", l2="mc", ) # title and axes labels figs.append(fig) # time fig, ax = plt.subplots() plot_binned_time_evolution(plot_data["time"]["values"], ax=ax) ax.set_title("Mean charge over time") ax.set_ylabel("charge [pe]") figs.append(fig) # fraction fig, ax = plt.subplots() ax.plot( plot_data["surviving"]["bins"],#["center"], plot_data["surviving"]["values"]["data"], label="data", ) if "mc" in plot_data["surviving"]["values"].keys(): ax.plot( plot_data["surviving"]["bins"],#["center"], plot_data["surviving"]["values"]["data"], label="mc", ) ax.legend() ax.set_xscale("log") ax.set_title("Fraction surviving events") ax.set_xlabel("Size") figs.append(fig) if output is None: plt.show() else: with PdfPages(output) as pdf: for fig in figs: fig.tight_layout(pad=0) pdf.savefig(fig)