Пример #1
0
def plot_1D_distribution ( table_ , **kwargs ) :
	if table_.n_indep_vars() != 1 :
		msg.error("plotting.plot_1D_distribution","Table has {0} independent variables where 1 was expected".format(table_.n_indep_vars()))
		return
	fig = plt.figure(figsize=(10,5))
	ax = fig.add_subplot(111)
	legend_char_width = 53
	specific_errors = []
	for requested_error in kwargs.get("errors",[]) :
		if requested_error not in table_._dep_var._symerrors and requested_error not in table_._dep_var._asymerrors_up :
			msg.warning("plotting.plot_1D_distribution","Specified error {0} not found in dependent variable {1}. Ignoring.".format(requested_error,table_._dep_var._name))
			continue
		specific_errors.append(requested_error)
	if len(specific_errors) > 0 :
		x, y, [ex_lo,ex_hi], [ey_lo,ey_hi], labels, keys = get_1D_distribution(table_,specific_errors)
		ey_lo_sys, ey_hi_sys, ey_lo_stat, ey_hi_stat = [], [], [], []
		str_tot_legend = kwargs.get("label","distribution") + " ( " + " + ".join(keys) + " )"
		str_tot_legend = "\n".join([str_tot_legend[legend_char_width*i:min(len(str_tot_legend),legend_char_width*(i+1))] for i in range(int(len(str_tot_legend)/legend_char_width)+1)])
	else :
		x, y, [ex_lo,ex_hi], [ey_lo,ey_hi], labels, keys = get_1D_distribution(table_)
		x, y, [ex_lo,ex_hi], [ey_lo_sys,ey_hi_sys], labels, sys_keys = get_1D_distribution(table_,"sys")
		x, y, [ex_lo,ex_hi], [ey_lo_stat,ey_hi_stat], labels, stat_keys = get_1D_distribution(table_,"stat")
		str_tot_legend = kwargs.get("label","distribution") + " ( " + " + ".join(keys) + " )"
		str_tot_legend = "\n".join([str_tot_legend[legend_char_width*i:min(len(str_tot_legend),legend_char_width*(i+1))] for i in range(int(len(str_tot_legend)/legend_char_width)+1)])
		str_sys_legend = kwargs.get("label","distribution") + " ( " + " + ".join(sys_keys) + " )"
		str_sys_legend = "\n".join([str_sys_legend[legend_char_width*i:min(len(str_sys_legend),legend_char_width*(i+1))] for i in range(int(len(str_sys_legend)/legend_char_width)+1)])
	if sum([np.fabs(el) for el in ey_hi_sys+ey_lo_sys]) > 0 :
		ax.errorbar(x, y, yerr=[ey_lo_sys,ey_hi_sys], c='royalblue', linewidth=18, linestyle='None', marker='None', alpha=0.4, label=str_sys_legend)
	if sum([np.fabs(el) for el in ey_hi_stat+ey_lo_stat]) > 0 :
		ax.errorbar(x, y, yerr=[ey_lo_stat,ey_hi_stat], c='indianred', linewidth=6, linestyle='None', marker='None', alpha=0.6, label=kwargs.get("label","distribution") + " ( stat )")
	ax.errorbar(x, y, yerr=[ey_lo,ey_hi], xerr=[ex_lo,ex_hi], c='k', linewidth=2, linestyle='None', marker='+', alpha=1, label=str_tot_legend)
	if labels :
		ax.set_xticks(x)
		ax.set_xticklabels(table_._indep_vars[0]._bin_labels,rotation=90)
	plt.subplots_adjust(left=0.1, right=0.5, top=0.95, bottom=0.4)
	if "legend_loc" in kwargs : ax.legend(loc=kwargs.get("legend_loc","best"))
	else : ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
	try : plt.xlabel(kwargs.get("xlabel",table_._indep_vars[0].name().replace("\\\\","\\").replace(r"\text{",r"{\rm ")))
	except : plt.xlabel("<error reading xlabel")
	try : plt.ylabel(kwargs.get("ylabel",table_._dep_var.name().replace("\\\\","\\").replace(r"\text{",r"{\rm ")))
	except : plt.ylabel("<error reading ylabel")
	plt.title(kwargs.get("title",""))
	xlim = kwargs.get("xlim",[x[0]-np.fabs(ex_lo[0]),x[-1]+np.fabs(ex_hi[-1])])
	ylim = kwargs.get("ylim",None)
	ax.axis(xmin=xlim[0],xmax=xlim[1])
	if ylim : ax.axis(ymin=ylim[0],ymax=ylim[1])
	if kwargs.get("logy",False) is True : plt.yscale("log")
	if kwargs.get("logx",False) is True : plt.xscale("log")
	plt.grid()
	if kwargs.get("show",False) :
		plt.show()
	if kwargs.get("save",False) :
		fig.savefig ( document , format='pdf' )
		plt.close(fig)
Пример #2
0
#  =================================== #
if __name__ == "__main__":
    #
    #  Welcome
    #
    msg.info("inspect_yaml.py", "Running program")
    #
    #  Get input files and settings
    #
    yamls_to_load, roots_to_load, do_show, do_print_all, do_not_make_matrix, save_file = parse_inputs(
        sys.argv[1:])
    do_save = len(save_file) > 0
    if do_save: plotter.set_save_file(save_file)
    if not do_show and not do_save and not do_print_all:
        msg.warning(
            "inspect_yaml.py",
            "Neither --save, --show nor --print specified. Falling back to --print."
        )
        do_print_all = True
        #
        #  Load input files
        #
    my_tables = DistributionContainer("my_tables")
    HD._make_matrix_if_possible = not do_not_make_matrix
    if len(yamls_to_load) > 0:
        HD.load_yaml_files_from_list(my_tables, yamls_to_load)
    if len(roots_to_load) > 0:
        RT.load_root_files_from_list(my_tables, roots_to_load)
    #
    #  Print contents (if --print was called then go into excruciating detail)
    #
    my_tables.print_keys()
Пример #3
0
#  =================================== #
if __name__ == "__main__":
    #
    #  Welcome
    #
    msg.info("plot_contents_of_yaml.py", "Running program")
    #
    #  Get input files and settings
    #
    yamls_to_load, roots_to_load, do_show, do_print_all, do_not_make_matrix, save_file = parse_inputs(
        sys.argv[1:])
    do_save = len(save_file) > 0
    if do_save: plotter.set_save_file(save_file)
    if not do_show and not do_save and not do_print_all:
        msg.warning(
            "plot_contents_of_yaml.py",
            "Neither --save, --show nor --print specified. Falling back to --print."
        )
        do_print_all = True
        #
        #  Load input files
        #
    my_tables = DistributionContainer("my_tables")
    HD._make_matrix_if_possible = not do_not_make_matrix
    if len(yamls_to_load) > 0:
        HD.load_yaml_files_from_list(my_tables, yamls_to_load)
    if len(roots_to_load) > 0:
        RT.load_root_files_from_list(my_tables, roots_to_load)
    #
    #  Print contents (if --print was called then go into excruciating detail)
    #
    my_tables.print_keys()
Пример #4
0
def reformat_2D_bins_as_matrix_using_labels(dist_2D_):
    if type(dist_2D_) != HEPDataTable:
        msg.fatal("HEP_data_helpers.reformat_2D_bins_as_matrix_using_labels",
                  "argument must be of type HEPDataTable")
    dep_var = dist_2D_._dep_var
    if len(dist_2D_._indep_vars) != 2:
        msg.fatal(
            "HEP_data_helpers.reformat_2D_bins_as_matrix_using_labels",
            "HEPDataTable {0} has {1} independent_variable where 2 are required"
            .format(dep_var._name, len(dist_2D_._indep_vars)))
    n_vals = len(dep_var)
    old_bin_labels_x = dist_2D_._indep_vars[0]._bin_labels
    old_bin_labels_y = dist_2D_._indep_vars[1]._bin_labels
    old_n_bins_x = len(old_bin_labels_x)
    old_n_bins_y = len(old_bin_labels_y)
    if dep_var._values.shape == (old_bin_labels_x, old_bin_labels_y):
        return
    if n_vals == old_n_bins_x == old_n_bins_y:
        bin_labels_x = [y for y in {x for x in old_bin_labels_x}]
        bin_labels_x = natsorted(bin_labels_x, alg=ns.IGNORECASE)
        bin_labels_y = [y for y in {x for x in old_bin_labels_y}]
        bin_labels_y = natsorted(bin_labels_y, alg=ns.IGNORECASE)
        new_n_bins_x = len(bin_labels_x)
        new_n_bins_y = len(bin_labels_y)
        new_values = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
        for x, y, v in zip(old_bin_labels_x, old_bin_labels_y,
                           dep_var._values):
            new_values[bin_labels_x.index(x), bin_labels_y.index(y)] = v
        dep_var._values = new_values
        for key, values in dep_var._symerrors.items():
            new_error = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
            for x, y, v in zip(old_bin_labels_x, old_bin_labels_y, values):
                new_error[bin_labels_x.index(x), bin_labels_y.index(y)] = v
            dep_var._symerrors[key] = new_error
        for key, values in dep_var._asymerrors_up.items():
            new_error = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
            for x, y, v in zip(old_bin_labels_x, old_bin_labels_y, values):
                new_error[bin_labels_x.index(x), bin_labels_y.index(y)] = v
            dep_var._asymerrors_up[key] = new_error
        for key, values in dep_var._asymerrors_dn.items():
            new_error = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
            for x, y, v in zip(old_bin_labels_x, old_bin_labels_y, values):
                new_error[bin_labels_x.index(x), bin_labels_y.index(y)] = v
            dep_var._asymerrors_dn[key] = new_error
        dist_2D_._indep_vars[0].set_bin_labels(bin_labels_x)
        dist_2D_._indep_vars[1].set_bin_labels(bin_labels_y)
        return
    if n_vals == old_n_bins_x * old_n_bins_y:
        new_values = np.array(np.zeros(shape=(old_n_bins_x, old_n_bins_y)))
        print(dep_var._values)
        print(dep_var._values.shape)
        for x_idx in range(len(old_bin_labels_x)):
            for y_idx in range(len(old_bin_labels_y)):
                new_values[x_idx,
                           y_idx] = dep_var._values[x_idx +
                                                    old_n_bins_x * y_idx]
        dep_var._values = new_values
        for key, values in dist_2D_._symerrors.items():
            new_error = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
            for x_idx in range(len(old_bin_labels_x)):
                for y_idx in range(len(old_bin_labels_y)):
                    new_error[x_idx,
                              y_idx] = values[x_idx + old_n_bins_x * y_idx]
            dep_var._symerrors[key] = new_error
        for key, values in dist_2D_._asymerrors_up.items():
            new_error = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
            for x_idx in range(len(old_bin_labels_x)):
                for y_idx in range(len(old_bin_labels_y)):
                    new_error[x_idx,
                              y_idx] = values[x_idx + old_n_bins_x * y_idx]
            dep_var._asymerrors_up[key] = new_error
        for key, values in dist_2D_._asymerrors_dn.items():
            new_error = np.array(np.zeros(shape=(new_n_bins_x, new_n_bins_y)))
            for x_idx in range(len(old_bin_labels_x)):
                for y_idx in range(len(old_bin_labels_y)):
                    new_error[x_idx,
                              y_idx] = values[x_idx + old_n_bins_x * y_idx]
            dep_var._asymerrors_dn[key] = new_error
        return
    msg.warning(
        "HEP_data_helpers.reformat_2D_bins_as_matrix_using_labels",
        "HEPDataTable {0} is not a matrix... assuming it is a non-factorisable distribution and returning with nothing done"
        .format(dep_var._name),
        verbose_level=0)
    return
def parse_inputs(argv_):
    #  Get arguments
    try:
        opts, rest = getopt.getopt(argv_, "hrv:s:", [
            "help", "recursive", "show", "save=", "verbosity=", "num=", "den="
        ])
    except getopt.GetoptError as err:
        msg.error(
            "plot_ratios.py",
            "The following error was thrown whilst parsing command-line arguments"
        )
        print(">>>>>>>>\n", err, "\n<<<<<<<<")
        msg.error("plot_ratios.py", "Falling back to to --help...")
        print_help()
        msg.fatal("plot_ratios.py", "Command-line arguments not recognised.")
    #  Parse arguments
    do_recurse = False
    do_show = False
    save_file = ""
    num_tag, den_tag = None, None
    for opt, arg in opts:
        if opt in ['-h', "--help"]:
            print_help()
            sys.exit(0)
        if opt in [
                '-r',
                "--recursive",
        ]:
            msg.info("plot_ratios.py",
                     "Config: using recursion if needed",
                     verbose_level=0)
            do_recurse = True
        if opt in [
                "--num",
        ]:
            num_tag = str(arg)
            msg.info("plot_ratios.py",
                     "Config: numerators will be identified using the tag {0}".
                     format(num_tag),
                     verbose_level=0)
        if opt in [
                "--den",
        ]:
            den_tag = str(arg)
            msg.info(
                "plot_ratios.py",
                "Config: denominators will be identified using the tag {0}".
                format(den_tag),
                verbose_level=0)
        if opt in ["--show"]:
            msg.info("plot_contents_of_yaml.py",
                     "Config: showing all distributions found",
                     verbose_level=0)
            do_show = True
        if opt in ['-s', "--save"]:
            save_file = str(arg)
            if save_file[-4:] != ".pdf": save_file = save_file + ".pdf"
            msg.info("plot_contents_of_yaml.py",
                     "Config: saving plots to {0}".format(save_file),
                     verbose_level=0)
        if opt in ['-v', "--verbosity"]:
            msg.info("plot_ratios.py",
                     "Config: setting verbosity to {0}".format(arg),
                     verbose_level=0)
            try:
                msg.VERBOSE_LEVEL = int(arg)
            except:
                msg.fatal(
                    "plot_ratios.py",
                    "Could not cast verbosity level {0} to integer".format(
                        arg))
    yaml_files = hlp.keep_only_yaml_files(argv_, recurse=do_recurse)
    root_files = hlp.keep_only_root_files(argv_, recurse=do_recurse)
    if num_tag is None:
        num_tag = "measured"
        msg.warning(
            "plot_ratios.py",
            "No --num provided, falling back to \"{0}\"".format(num_tag))
    if den_tag is None:
        den_tag = "expected"
        msg.warning(
            "plot_ratios.py",
            "No --den provided, falling back to \"{0}\"".format(den_tag))
    #  Return
    return num_tag, den_tag, do_show, save_file, yaml_files, root_files
def plot_ratio_2D(table_num_, table_den_, **kwargs):
    labels_x, labels_y, values_den = plotter.get_2D_distribution(table_den_)
    labels_x, labels_y, values_num = plotter.get_2D_distribution(table_num_)
    values = np.divide(values_num, values_den)
    indep_var = table_num_._indep_vars
    fig = plotter.plt.figure(figsize=(20, 40))
    #		ADD PLOT OF TABLE 1
    ax = fig.add_subplot(421)
    plotter.plot_2D_distribution_on_current_axes(table_num_,
                                                 fontsize="small",
                                                 **kwargs)
    try:
        plotter.plt.title(
            ("$\\bf{TABLE}$ $\\bf{1:}$  " + table_num_._dep_var._name).replace(
                "\\\\", "\\").replace(r"\text{", r"{\rm "))
    except:
        msg.warning(
            "plot_ratio_2D",
            "could not render observable name for TABLE 1 - no title given to plot"
        )
    #		ADD PLOT OF TABLE 2
    ax = fig.add_subplot(422)
    plotter.plot_2D_distribution_on_current_axes(table_den_,
                                                 fontsize="small",
                                                 **kwargs)
    try:
        plotter.plt.title(
            ("$\\bf{TABLE}$ $\\bf{1:}$  " + table_den_._dep_var._name).replace(
                "\\\\", "\\").replace(r"\text{", r"{\rm "))
    except:
        msg.warning(
            "plot_ratio_2D",
            "could not render observable name for TABLE 1 - no title given to plot"
        )
    #		ADD PLOT OF RATIO
    x_label, y_label = "", ""
    try:
        x_label = kwargs.get(
            "x_label",
            indep_var[0].name().replace("\\\\",
                                        "\\").replace(r"\text{", r"{\rm "))
    except Exception:
        pass
    try:
        y_label = kwargs.get(
            "y_label",
            indep_var[1].name().replace("\\\\",
                                        "\\").replace(r"\text{", r"{\rm "))
    except Exception:
        pass
    ax = fig.add_subplot(423)
    make_2D_plot_from_values(ax,
                             values,
                             x_label,
                             y_label,
                             title="$\\bf{TABLE 1}$  /  $\\bf{TABLE 2}$",
                             y_central_value=1.,
                             labels_x=labels_x,
                             labels_y=labels_y,
                             **kwargs)
    #		ADD PLOTS OF TABLE 1 RELATIVE UNCERTAINTIES
    [ey_lo_n, ey_hi_n], err_keys_n = plotter.get_table_errors(table_num_)
    rel_err_hi_n, rel_err_lo_n = np.divide(ey_hi_n, values_num), np.divide(
        ey_lo_n, values_num)
    ax = fig.add_subplot(425)
    make_2D_plot_from_values(
        ax,
        rel_err_hi_n,
        x_label,
        y_label,
        title="TABLE 1 relative uncertainty (up, combined sources)",
        y_central_value=0.,
        labels_x=labels_x,
        labels_y=labels_y,
        **kwargs)
    ax = fig.add_subplot(426)
    make_2D_plot_from_values(
        ax,
        rel_err_lo_n,
        x_label,
        y_label,
        title="TABLE 1 relative uncertainty (down, combined sources)",
        y_central_value=0.,
        labels_x=labels_x,
        labels_y=labels_y,
        **kwargs)
    #		ADD PLOTS OF TABLE 2 RELATIVE UNCERTAINTIES
    [ey_lo_d, ey_hi_d], err_keys_d = plotter.get_table_errors(table_den_)
    rel_err_hi_d, rel_err_lo_d = np.divide(ey_hi_d, values_den), np.divide(
        ey_lo_d, values_den)
    ax = fig.add_subplot(427)
    make_2D_plot_from_values(
        ax,
        rel_err_hi_d,
        x_label,
        y_label,
        title="TABLE 2 relative uncertainty (up, combined sources)",
        y_central_value=0.,
        labels_x=labels_x,
        labels_y=labels_y,
        **kwargs)
    ax = fig.add_subplot(428)
    make_2D_plot_from_values(
        ax,
        rel_err_lo_d,
        x_label,
        y_label,
        title="TABLE 2 relative uncertainty (down, combined sources)",
        y_central_value=0.,
        labels_x=labels_x,
        labels_y=labels_y,
        **kwargs)
    #		Finish up
    if kwargs.get("show", False):
        plotter.plt.show()
    if kwargs.get("save", False):
        fig.savefig(plotter.document, format='pdf')
    plotter.plt.close(fig)
    #		Return chi2 and Table1/Table2 values
    chi2 = hlp.get_chi2(values_num, ey_lo_n, ey_hi_n, values_den, ey_lo_d,
                        ey_hi_d)
    return chi2, values.flatten()
def plot_ratio_1D(table_num_, table_den_, **kwargs):
    x_n, y_n, [ex_lo_n, ex_hi_n], [
        ey_lo_n, ey_hi_n
    ], labels, keys_num = plotter.get_1D_distribution(table_num_)
    x_d, y_d, [ex_lo_d, ex_hi_d], [
        ey_lo_d, ey_hi_d
    ], labels, keys_den = plotter.get_1D_distribution(table_den_)
    chi2 = hlp.get_chi2(y_n, ey_lo_n, ey_hi_n, y_d, ey_lo_d, ey_hi_d)
    for i in range(len(x_n)):
        if x_n[i] == x_d[i]: continue
        msg.error("plot_ratio_1D", "Arguments do not have the same binning")
        raise ValueError(
            "Ratio of distributions with bin centres at {0} and {1}", x_n, x_d)
    fig = plotter.plt.figure(figsize=(10, 10))
    ax1 = fig.add_subplot(211)
    legend_char_width = 53
    str_num_legend = "TABLE 1 ( " + " + ".join(keys_num) + " )"
    str_num_legend = "\n".join([
        str_num_legend[legend_char_width *
                       i:min(len(str_num_legend), legend_char_width * (i + 1))]
        for i in range(int(len(str_num_legend) / legend_char_width) + 1)
    ])
    str_den_legend = "TABLE 2 ( " + " + ".join(keys_den) + " )"
    str_den_legend = "\n".join([
        str_den_legend[legend_char_width *
                       i:min(len(str_den_legend), legend_char_width * (i + 1))]
        for i in range(int(len(str_den_legend) / legend_char_width) + 1)
    ])
    ax1.errorbar(x_n,
                 y_n,
                 yerr=[ey_lo_n, ey_hi_n],
                 xerr=[ex_lo_n, ex_hi_n],
                 c='k',
                 linewidth=2,
                 linestyle='None',
                 alpha=0.8,
                 label=str_num_legend)
    ax1.errorbar(x_d,
                 y_d,
                 yerr=[ey_lo_d, ey_hi_d],
                 xerr=[ex_lo_d, ex_hi_d],
                 c='r',
                 linewidth=4,
                 linestyle='None',
                 alpha=0.4,
                 label=str_den_legend)
    ax1.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plotter.plt.subplots_adjust(left=0.1, right=0.5, top=0.95, bottom=0.4)
    plotter.plt.ylabel("Values")
    plotter.plt.ylabel("Values")
    xlim = kwargs.get(
        "xlim", [x_d[0] - np.fabs(ex_lo_n[0]), x_d[-1] + np.fabs(ex_hi_n[-1])])
    ymin, ymax = ax1.get_ylim()
    ax1.axis(xmin=xlim[0], xmax=xlim[1])
    try:
        plotter.plt.text(
            xlim[0], 1.19 * ymax - 0.19 * ymin,
            ("$\\bf{TABLE}$ $\\bf{1:}$  " + table_num_._dep_var._name).replace(
                "\\\\", "\\").replace(r"\text{", r"{\rm "))
        plotter.plt.text(
            xlim[0], 1.08 * ymax - 0.08 * ymin,
            ("$\\bf{TABLE}$ $\\bf{2:}$  " + table_den_._dep_var._name).replace(
                "\\\\", "\\").replace(r"\text{", r"{\rm "))
    except:
        msg.warning(
            "plot_ratio_1D",
            "could not render observable name - no title given to plot")
    plotter.plt.grid()
    ax2 = fig.add_subplot(212)
    ax2.errorbar(x_n,
                 y_n / y_d,
                 yerr=[ey_lo_n / y_d, ey_hi_n / y_d],
                 xerr=[ex_lo_n, ex_hi_n],
                 c='k',
                 linewidth=2,
                 linestyle='None',
                 alpha=0.8)
    ax2.errorbar(x_d,
                 y_d / y_d,
                 yerr=[ey_lo_d / y_d, ey_hi_d / y_d],
                 xerr=[ex_lo_d, ex_hi_d],
                 c='r',
                 linewidth=4,
                 linestyle='None',
                 alpha=0.4)
    box = ax2.get_position()
    ax2.set_position([box.x0, box.y0, box.width * 0.4, box.height])
    ax2.axis(xmin=xlim[0], xmax=xlim[1])
    plotter.plt.ylabel("Ratio  $\\bf{vs.}$  TABLE 2")
    try:
        plotter.plt.xlabel(
            kwargs.get(
                "xlabel", table_den_._indep_vars[0].name().replace(
                    "\\\\", "\\").replace(r"\text{", r"{\rm ")))
    except:
        plotter.plt.xlabel("<error reading xlabel")
    plotter.plt.subplots_adjust(left=0.1, right=0.5, top=0.92, bottom=0.4)
    plotter.plt.grid()
    if kwargs.get("show", False):
        plotter.plt.show()
    if kwargs.get("save", False):
        fig.savefig(plotter.document, format='pdf')
    plotter.plt.close(fig)
    return chi2, y_n / y_d