<commit_before>import matplotlib.pyplot as plt
import numpy as np
import seaborn as sb
import tpms as t

NO_FILTER_LABEL = "no filter"


class _NewPlot:
    def __init__(self, *file_name_elements):
        self.file_name = "_".join([str(el) for el in file_name_elements])
        self.file_name = self.file_name.replace(' ', '_')

    def __enter__(self):
        plt.figure()

    def __exit__(self, type, value, traceback):
        plt.savefig(self.file_name, format="pdf")
        plt.close()


def _capitalized(text):
    return text[:1].upper() + text[1:]


def _get_group_param_values(stats_df, group_param):
    group_param_vals = stats_df[group_param.name].value_counts().index.tolist()
    group_param_vals.sort()
    return group_param_vals


def _get_fixed_param_info(fixed_param_values):
    return [k.get_value_name(v) for k, v in fixed_param_values.items()]


def _get_stats_plot_name_elements(
        base_name, plotted, group_param, fixed_param_info,
        versus=None, ascending=None):
    name_elements = [base_name, plotted]
    if versus:
        name_elements += ["vs", versus]
    name_elements += ["per", group_param.title.lower()]
    if ascending is not None:
        name_elements += ("asc" if ascending else "desc")
    name_elements += fixed_param_info
    if ascending is not None:
        name_elements.append("distribution")
    return name_elements


def _get_grouped_stats_plot_title(
        plotted, group_param, fixed_param_info, versus=None):

    title_elements = [plotted]
    if versus:
        title_elements += ["vs", versus.lower()]
    title_elements += ["per", group_param.title.lower()]

    return " ".join(title_elements) + ", ".join(fixed_param_info)


def _plot_statistic_for_grouped_param_values(
        stats_df, ycol, group_param, xcol):

    group_param_vals = _get_group_param_values(stats_df, group_param)

    for group_param_value in group_param_vals:
        group_stats = stats_df[stats_df[group_param.name] == group_param_value]
        group_stats.sort(columns=xcol, axis=0, inplace=True)
        xvals = group_stats[xcol]
        yvals = group_stats[ycol]
        plt.plot(xvals, yvals, '-o',
                 label=group_param.get_value_name(group_param_value))


def _plot_statistic(
        stats_df, statistic, group_param, xcol, xlabel, fixed_param_info):

    _plot_statistic_for_grouped_param_values(
        stats_df, statistic.name, group_param, xcol)

    plt.xlabel(xlabel)
    plt.ylabel(statistic.title)
    plt.legend(title=group_param.title, loc=4)

    stat_range = statistic.stat_range
    if stat_range is not None:
        min_val = stat_range[0]
        max_val = stat_range[1]
        if min_val is not None:
            plt.ylim(ymin=min_val)
        if max_val is not None:
            plt.ylim(ymax=max_val)

    title = _get_grouped_stats_plot_title(
        statistic.title, group_param, fixed_param_info, versus=xlabel)
    plt.suptitle(title)


def _set_distribution_plot_bounds(xmin, xmax):
    xmargin = (xmax - xmin) / 40.0
    plt.xlim(xmin=xmin-xmargin, xmax=xmax+xmargin)
    plt.ylim(ymin=-2.5, ymax=102.5)


def _set_distribution_plot_labels(clsfr_col, ascending):
    plt.xlabel(_capitalized(clsfr_col))
    plt.ylabel("Percentage of isoforms " +
               ("less" if ascending else "greater") + " than threshold")


def log_tpm_scatter_plot(tpms, base_name, quant_method, tpm_label):
    with _NewPlot(base_name, tpm_label, "log10 scatter"):
        plt.scatter(tpms[t.LOG10_REAL_TPM].values,
                    tpms[t.LOG10_CALCULATED_TPM].values,
                    c="lightblue", alpha=0.4)

        plt.suptitle("Scatter plot of log calculated vs real TPMs: " +
                     quant_method + ", " + tpm_label)
        plt.xlabel("Log10 real TPM")
        plt.ylabel("Log10 calculated TPM")

        min_val = np.log10(t.NOT_PRESENT_CUTOFF) - 0.2
        plt.xlim(xmin=min_val)
        plt.ylim(ymin=min_val)


def log_ratio_boxplot(tpms, base_name, quant_method, tpm_label, classifier,
                      filter=None, save_to_file=True):

    grouping_column = classifier.get_column_name()
    name_elements = [tpm_label]

    if filter:
        grouped_tpms = tpms.groupby(grouping_column)
        tpms = grouped_tpms.filter(filter)
    else:
        name_elements.append(NO_FILTER_LABEL)

    title_elements = [base_name] + name_elements + ["boxplot"]
    with _NewPlot(*title_elements):
        sb.boxplot(tpms[t.LOG10_RATIO], groupby=tpms[grouping_column],
                   sym='', color='lightblue')

        plt.suptitle("Log ratios of calculated to real TPMs: " +
                     ", ".join([quant_method] + name_elements))

        plt.xlabel(_capitalized(grouping_column))
        plt.ylabel("Log ratio (calculated/real TPM)")

        locs, labels = plt.xticks()
        plt.xticks(locs, classifier.get_value_labels(len(labels)))


def plot_statistic(stats, base_name, statistic,
                   group_param, varying_param, fixed_param_values):

    fixed_param_info = _get_fixed_param_info(fixed_param_values)
<commit_msg>Fix a few bugs recently introduced in plotting results of a quantification run.<commit_after>import matplotlib.pyplot as plt
import numpy as np
import seaborn as sb
import tpms as t

NO_FILTER_LABEL = "no filter"


class _NewPlot:
    def __init__(self, *file_name_elements):
        self.file_name = "_".join([str(el) for el in file_name_elements])
        self.file_name = self.file_name.replace(' ', '_')

    def __enter__(self):
        plt.figure()

    def __exit__(self, type, value, traceback):
        plt.savefig(self.file_name + ".pdf", format="pdf")
        plt.close()


def _capitalized(text):
    return text[:1].upper() + text[1:]


def _get_group_param_values(stats_df, group_param):
    group_param_vals = stats_df[group_param.name].value_counts().index.tolist()
    group_param_vals.sort()
    return group_param_vals


def _get_fixed_param_info(fixed_param_values):
    return [k.get_value_name(v) for k, v in fixed_param_values.items()]


def _get_stats_plot_name_elements(
        base_name, plotted, group_param, fixed_param_info,
        versus=None, ascending=None):
    name_elements = [base_name, plotted]
    if versus:
        name_elements += ["vs", versus]
    name_elements += ["per", group_param.title.lower()]
    if ascending is not None:
        name_elements += ("asc" if ascending else "desc")
    name_elements += fixed_param_info
    if ascending is not None:
        name_elements.append("distribution")
    return name_elements


def _get_grouped_stats_plot_title(
        plotted, group_param, fixed_param_info, versus=None):

    title_elements = [plotted]
    if versus:
        title_elements += ["vs", versus.lower()]
    title_elements += ["per", group_param.title.lower()]

    return " ".join(title_elements) + ", ".join(fixed_param_info)


def _plot_statistic_for_grouped_param_values(
        stats_df, ycol, group_param, xcol):

    group_param_vals = _get_group_param_values(stats_df, group_param)

    for group_param_value in group_param_vals:
        group_stats = stats_df[stats_df[group_param.name] == group_param_value]
        group_stats.sort(columns=xcol, axis=0, inplace=True)
        xvals = group_stats[xcol]
        yvals = group_stats[ycol]
        plt.plot(xvals, yvals, '-o',
                 label=group_param.get_value_name(group_param_value))


def _plot_statistic(
        stats_df, statistic, group_param, xcol, xlabel, fixed_param_info):

    _plot_statistic_for_grouped_param_values(
        stats_df, statistic.name, group_param, xcol)

    plt.xlabel(xlabel)
    plt.ylabel(statistic.title)
    plt.legend(title=group_param.title, loc=4)

    stat_range = statistic.stat_range
    if stat_range is not None:
        min_val = stat_range[0]
        max_val = stat_range[1]
        if min_val is not None:
            plt.ylim(ymin=min_val)
        if max_val is not None:
            plt.ylim(ymax=max_val)

    title = _get_grouped_stats_plot_title(
        statistic.title, group_param, fixed_param_info, versus=xlabel)
    plt.suptitle(title)


def _set_distribution_plot_bounds(xmin, xmax):
    xmargin = (xmax - xmin) / 40.0
    plt.xlim(xmin=xmin-xmargin, xmax=xmax+xmargin)
    plt.ylim(ymin=-2.5, ymax=102.5)


def _set_distribution_plot_labels(clsfr_col, ascending):
    plt.xlabel(_capitalized(clsfr_col))
    plt.ylabel("Percentage of isoforms " +
               ("less" if ascending else "greater") + " than threshold")


def log_tpm_scatter_plot(tpms, base_name, quant_method, tpm_label):
    with _NewPlot(base_name, tpm_label, "log10 scatter"):
        plt.scatter(tpms[t.LOG10_REAL_TPM].values,
                    tpms[t.LOG10_CALCULATED_TPM].values,
                    c="lightblue", alpha=0.4)

        plt.suptitle("Scatter plot of log calculated vs real TPMs: " +
                     quant_method + ", " + tpm_label)
        plt.xlabel("Log10 real TPM")
        plt.ylabel("Log10 calculated TPM")

        min_val = np.log10(t.NOT_PRESENT_CUTOFF) - 0.2
        plt.xlim(xmin=min_val)
        plt.ylim(ymin=min_val)


def log_ratio_boxplot(tpms, base_name, quant_method, tpm_label, classifier,
                      filter=None, save_to_file=True):

    grouping_column = classifier.get_column_name()
    name_elements = [tpm_label]

    if filter:
        grouped_tpms = tpms.groupby(grouping_column)
        tpms = grouped_tpms.filter(filter)
    else:
        name_elements.append(NO_FILTER_LABEL)

    title_elements = [base_name, grouping_column] + name_elements + ["boxplot"]
    with _NewPlot(*title_elements):
        sb.boxplot(tpms[t.LOG10_RATIO], groupby=tpms[grouping_column],
                   sym='', color='lightblue')

        plt.suptitle("Log ratios of calculated to real TPMs: " +
                     ", ".join([quant_method] + name_elements))

        plt.xlabel(_capitalized(grouping_column))
        plt.ylabel("Log ratio (calculated/real TPM)")

        locs, labels = plt.xticks()
        plt.xticks(locs, classifier.get_value_labels(len(labels)))


def plot_statistic(stats, base_name, statistic,
                   group_param, varying_param, fixed_param_values):

    fixed_param_info = _get_fixed_param_info(fixed_param_values)

{'fraction_non_alphanumeric': 0.10522198318012908, 'fraction_numerical': 0.006454136514766282, 'mean_word_length': 3.2541597337770383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6115231', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3431, 'n_words': 646}