<commit_before>import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np

DEFAULT_WIDTH = 1680
DEFAULT_HEIGHT = 800
DPI = 100


def seconds_for_analysis(duration, measurement_type, selector):
    if measurement_type in duration:
        data = duration[measurement_type]
        if selector in data:
            return data[selector].duration_seconds
    return 0


def add_legend(ax, columns, component_selectors, component_colors,
               package_selectors, package_colors):
    legend_labels = []
    legend_labels.append("component: overall")

    for label in component_selectors:
        legend_labels.append(label)

    legend_labels.append("package: overall")

    for label in package_selectors:
        legend_labels.append(label)

    legend_keys = []
    legend_keys.append(columns[0][0])

    for c in columns[1]:
        legend_keys.append(c[0])

    legend_keys.append(columns[2][0])

    for c in columns[3]:
        legend_keys.append(c[0])

    ax.legend(legend_keys, legend_labels)


def create_component_analysis_timing_graph(durations, width=DEFAULT_WIDTH,
                                           height=DEFAULT_HEIGHT, dpi=DPI):
    N = len(durations)

    component_selectors = ["security_issues", "source_licenses",
                           "metadata", "keywords_tagging", "dependency_snapshot",
                           "digests", "code_metrics"]

    package_selectors = ["github_details", "keywords_tagging", "libraries_io"]

    component_colors = ["#cc4040", "#cccc40", "#40cc40", "#40cccc",
                        "#cccccc", "#804040", "#808040", "#408040"]

    package_colors = ["#0040cc", "#ff4040", "#40ff40"]

    fig, ax = plt.subplots(figsize=(1.0 * width / dpi, 1.0 * height / dpi), dpi=dpi)

    column1data = np.array([duration["core-data"]["overall"].duration_seconds
                           for duration in durations.values()])

    column2data = [np.array([seconds_for_analysis(duration, "core-data", selector)
                            for duration in durations.values()])
                   for selector in component_selectors]

    column3data = np.array([duration["core-package-data"]["overall"].duration_seconds
                           for duration in durations.values()])

    column4data = [np.array([seconds_for_analysis(duration, "core-package-data", selector)
                            for duration in durations.values()])
                   for selector in package_selectors]

    ind = np.arange(N)  # the x locations for the groups
    width = 0.30        # the width of the bars

    column1 = ax.bar(ind, column1data, width, color='orange')

    bottom = np.zeros(N)
    column2 = []
    for elem, color in zip(column2data, component_colors):
        column2.append(ax.bar(ind + width / 3, elem, width, bottom=bottom, color=color))
        bottom += elem

    column3 = ax.bar(ind + width + 0.2, column3data, width, color='yellow')

    bottom = np.zeros(N)
    column4 = []
    for elem, color in zip(column4data, package_colors):
        column4.append(ax.bar(ind + width + 0.2 + width / 3, elem, width, bottom=bottom,
                       color=color))
        bottom += elem

    plt.ylabel('duration (seconds)')
    plt.title('component analysis')
    plt.xticks(ind, durations.keys())

    add_legend(ax, columns, component_selectors, component_colors, package_selectors,
               package_colors)

    return fig


def create_graph(title, y_axis_label, labels, values):
    N = len(values)
    indexes = np.arange(N)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
<commit_msg>Refactoring: code to create stacked column<commit_after>import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np

DEFAULT_WIDTH = 1680
DEFAULT_HEIGHT = 800
DPI = 100


def seconds_for_analysis(duration, measurement_type, selector):
    if measurement_type in duration:
        data = duration[measurement_type]
        if selector in data:
            return data[selector].duration_seconds
    return 0


def add_legend(ax, columns, component_selectors, component_colors,
               package_selectors, package_colors):
    legend_labels = []
    legend_labels.append("component: overall")

    for label in component_selectors:
        legend_labels.append(label)

    legend_labels.append("package: overall")

    for label in package_selectors:
        legend_labels.append(label)

    legend_keys = []
    legend_keys.append(columns[0][0])

    for c in columns[1]:
        legend_keys.append(c[0])

    legend_keys.append(columns[2][0])

    for c in columns[3]:
        legend_keys.append(c[0])

    ax.legend(legend_keys, legend_labels)


def stacked_column(measurements, ind, ax, columndata, colors, width, offset):
    bottom = np.zeros(measurements)
    column = []
    for elem, color in zip(columndata, colors):
        column.append(ax.bar(ind + offset, elem, width, bottom=bottom, color=color))
        bottom += elem
    return column


def create_component_analysis_timing_graph(durations, width=DEFAULT_WIDTH,
                                           height=DEFAULT_HEIGHT, dpi=DPI):
    N = len(durations)

    component_selectors = ["security_issues", "source_licenses",
                           "metadata", "keywords_tagging", "dependency_snapshot",
                           "digests", "code_metrics"]

    package_selectors = ["github_details", "keywords_tagging", "libraries_io"]

    component_colors = ["#cc4040", "#cccc40", "#40cc40", "#40cccc",
                        "#cccccc", "#804040", "#808040", "#408040"]

    package_colors = ["#0040cc", "#ff4040", "#40ff40"]

    fig, ax = plt.subplots(figsize=(1.0 * width / dpi, 1.0 * height / dpi), dpi=dpi)

    columndata = []
    columndata.append(np.array([duration["core-data"]["overall"].duration_seconds
                      for duration in durations.values()]))

    columndata.append([np.array([seconds_for_analysis(duration, "core-data", selector)
                                for duration in durations.values()])
                      for selector in component_selectors])

    columndata.append(np.array([duration["core-package-data"]["overall"].duration_seconds
                      for duration in durations.values()]))

    columndata.append([np.array([seconds_for_analysis(duration, "core-package-data", selector)
                                for duration in durations.values()])
                      for selector in package_selectors])

    ind = np.arange(N)   # the x locations for the groups
    width = 0.30         # the width of the bars
    offset = width / 3   # offset for the second column(s)

    pitch = width + 0.2  # pitch between component and package column tuples

    columns = []
    columns.append(ax.bar(ind, columndata[0], width, color='orange'))
    columns.append(stacked_column(N, ind, ax, columndata[1], component_colors, width, offset))
    columns.append(ax.bar(ind + pitch, columndata[2], width, color='yellow'))
    columns.append(stacked_column(N, ind, ax, columndata[3], package_colors, width, offset + pitch))

    plt.ylabel('duration (seconds)')
    plt.title('component analysis')
    plt.xticks(ind, durations.keys())

    add_legend(ax, columns, component_selectors, component_colors, package_selectors,
               package_colors)

    return fig


def create_graph(title, y_axis_label, labels, values):
    N = len(values)
    indexes = np.arange(N)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

{'fraction_non_alphanumeric': 0.11609639195846093, 'fraction_numerical': 0.022500332845160433, 'mean_word_length': 2.8582434514637907, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7213229', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2200, 'n_words': 508}