def gather_all_data(opt):
    """ Main function for separate plots for ampdet terms """
    LOG.info("Starting main evaluation loop.")
    opt = tripcor.check_opt(opt)
    # main loop
    for beam, xing, error_types, error_loc in main_loop(opt):
        _gather_data(opt.cwd, opt.machine, beam, xing, error_types, error_loc,
                     opt.optic_types, opt.seeds, opt.unused_stages)
def make_histogram_plots(opt):
    alpha_mean = .2
    alpha_hist = .6
    title = ""
    ps.set_style('standard')
    opt = tripcor.check_opt(opt)
    cwd = get_data_output_folder(opt.cwd)
    output_folder = get_plot_output_folder(opt.cwd)
    for beam, xing, error_types, error_loc, optic_type in hist_loop(opt):
        fig, axs = plt.subplots(len(AMPDET_NAMES), 1)
        for idx_data, output_id in _ordered_output_ids(opt.machine, beam,
                                                       opt.unused_stages):
            title, filename = get_seed_data_title_and_filename(
                beam, xing, error_types, error_loc, optic_type, output_id)
            seed_df = tfs.read_tfs(os.path.join(cwd, filename), index="SEED")
            y_max = len(seed_df.index)
            for idx_ax, term in enumerate(AMPDET_NAMES):
                ax = axs[idx_ax]
                data = seed_df[term]
                x_pos = data.mean()

                # plot mean
                stem_cont = ax.stem([x_pos], [y_max],
                                    markerfmt="",
                                    basefmt="",
                                    label="_nolegend_")
                plt.setp(stem_cont[1],
                         color=ps.get_mpl_color(idx_data),
                         alpha=alpha_mean)

                # plot std
                # error = data.std()
                # ebar_cont = ax.errorbar(x_pos, y_max, xerr=error, color=ps.get_mpl_color(idx_data),
                #                     label="_nolegend_", marker="")
                # ps.change_ebar_alpha_for_line(ebar_cont, alpha_mean)

                # plot histogram
                data.hist(ax=ax,
                          alpha=alpha_hist,
                          color=ps.get_mpl_color(idx_data),
                          label=output_id)

        for idx_ax, term in enumerate(AMPDET_NAMES):
            axs[idx_ax].set_xlabel(term)
            axs[idx_ax].set_ylabel("Seeds")

        legend = axs[0].legend()
        _reorder_legend(
            legend, _get_all_output_ids(opt.machine, beam, opt.unused_stages))
        ps.sync2d(axs)
        title = " ".join(title.split(" ")[:-1])
        figfilename = "{:s}.{:s}".format(title.replace(' ', '.'), "histogram")
        fig.canvas.set_window_title(title)
        make_bottom_text(axs[-1], title)
        fig.savefig(os.path.join(output_folder, figfilename + ".pdf"))
        fig.savefig(os.path.join(output_folder, figfilename + ".png"))
def main_body(opt):
    """ Same stuff for both mains """
    LOG.info("Starting main evaluation loop.")
    opt = tripcor.check_opt(opt)
    output_folder = get_plot_output_folder(opt.cwd)
    # main loop
    for beam, xing, error_types, error_loc in main_loop(opt):
        current_params = dict(beam=beam,
                              xing=xing,
                              error_types=error_types,
                              error_loc=error_loc)
        data = {}
        for otype in opt.optic_types:
            data[otype] = _load_and_average_data(opt.cwd, opt.machine, beam,
                                                 otype, xing, error_types,
                                                 error_loc, opt.unused_stages)
        yield output_folder, data, current_params
def plot_crossing_oneplot(opt):
    """ Main function to put all crossing into one plot. """
    LOG.info("Starting crossing evaluation loop.")
    opt = tripcor.check_opt(opt)
    output_folder = get_plot_output_folder(opt.cwd)

    # for beam in opt.beams:
    #     for otype in opt.optic_types:
    #         for error_types in opt.error_types:
    #             for error_loc in opt.error_locations:
    #                 for xing in opt.xing:
    #                     current_params = dict(beam=beam, optic_type=otype, error_types=error_types,
    #                                           error_loc=error_loc, xing=xing)
    #                     _update_min_max(_load_and_average_data(opt.cwd, opt.machine,
    #                                                       beam, otype, xing,
    #                                                       error_types, error_loc,
    #                                                       opt.unused_stages), current_params)

    for beam in opt.beams:
        for otype in opt.optic_types:
            for error_types in opt.error_types:
                for error_loc in opt.error_locations:
                    current_params = dict(beam=beam,
                                          optic_type=otype,
                                          error_types=error_types,
                                          error_loc=error_loc)
                    data = {}
                    ordered = []
                    for xing in opt.xing:
                        id = tripcor.get_nameparts_from_parameters(
                            xing=xing)[0]
                        ordered.append(id)
                        data[id] = _load_and_average_data(
                            opt.cwd, opt.machine, beam, otype, xing,
                            error_types, error_loc, opt.unused_stages)
                    _plot_separate_ampdet(output_folder,
                                          data,
                                          current_params,
                                          ordered,
                                          id="all_xing")
def make_cta_histogram(opt):
    alpha_mean = 1
    alpha_hist = .4
    mod_one = lambda x: np.mod(x, 1)
    ps.set_style('standard', MANUAL_STYLE_CTA)
    opt = tripcor.check_opt(opt)
    cwd = get_data_output_folder(opt.cwd)
    output_folder = get_cta_plot_output_folder(opt.cwd)
    for beam, error_types, error_loc, optic_type in cta_loop(opt):
        fig, ax = plt.subplots(1, 1)
        color_cycle = get_colors()
        for xing in opt.xing:
            color = color_cycle.next()
            _, filename = get_cta_seed_data_title_and_filename(
                beam, xing, error_types, error_loc, optic_type)

            xing_label = tripcor.get_nameparts_from_parameters(xing=xing)[0]
            try:
                xing_label = legend_map[xing_label]
            except KeyError:
                pass

            seed_df = tfs.read_tfs(os.path.join(cwd, filename),
                                   index="SEED").apply(mod_one)
            diff = np.abs(seed_df.QX - seed_df.QY) * 1e4

            y_max = len(seed_df.index) / 3

            # plot mean
            x_pos = diff.mean()
            stem_cont = ax.stem([x_pos], [y_max],
                                markerfmt="",
                                basefmt="",
                                label="_nolegend_")
            plt.setp(stem_cont[1], color=color, alpha=alpha_mean, ls="--")

            # plot std
            # error = data.std()
            # ebar_cont = ax.errorbar(x_pos, y_max, xerr=error, color=ps.get_mpl_color(idx_data),
            #                     label="_nolegend_", marker="")
            # ps.change_ebar_alpha_for_line(ebar_cont, alpha_mean)

            # plot histogram
            if diff.std() > 0:
                diff.hist(ax=ax,
                          alpha=alpha_hist,
                          color=color,
                          label="_nolegend_")
                diff.hist(ax=ax,
                          histtype="step",
                          color=color,
                          label=xing_label)

        ax.set_xlabel("|C$^{-}$| [$10^{-4}$]")
        ax.set_ylabel("Count")
        legend = ps.make_top_legend(ax, 2)
        legend.remove()

        figtitle, figfilename = get_cta_plot_title_and_filename(
            beam, error_types, error_loc, optic_type)
        fig.canvas.set_window_title(figtitle)
        fig.tight_layout()
        fig.savefig(os.path.join(output_folder, figfilename + ".hist.pdf"))
        fig.savefig(os.path.join(output_folder, figfilename + ".hist.png"))